【问题标题】:How to return mapping in ton-solidity contracts?如何在 ton-solidity 合约中返回映射?
【发布时间】:2021-12-26 17:33:59
【问题描述】:

我可以在 ton-solidity 合约的函数中返回映射吗?

我需要这样的东西。

function func() public returns((address=>someStruct) myMapping)

【问题讨论】:

    标签: solidity ton


    【解决方案1】:

    这解决了我的问题:

    function func() public returns(mapping (address=>someStruct))
    

    【讨论】:

      【解决方案2】:

      Contract.sol

      pragma ton-solidity >= 0.51.0;
      pragma AbiHeader expire;
      
      struct someStruct {
          string foo;
          uint32 bar;
      }
      
      contract Contract {
      
          mapping (address => someStruct) _myMapping;
      
          constructor() public
          {
              tvm.accept();
              _myMapping[msg.sender] = someStruct("quz", now);
          }
      
          function func() external view returns(mapping (address=>someStruct))
          {
              return _myMapping;
          }
      }
      

      run.sh

      #!/usr/bin/env bash
      
      set -o errexit
      
      tondev se reset
      
      rm -fr *.abi.json *.tvc
      
      # Deploy Contract
      tondev sol compile Contract.sol
      tondev contract deploy Contract --value 1000000000
      
      # Run Contract
      tondev contract run-local Contract func
      

      运行

      bash run.sh
      

      结果

      Execution has finished with result: {
          "output": {
              "value0": {
                  "0:0000000000000000000000000000000000000000000000000000000000000000": {
                      "foo": "quz",
                      "bar": "1637140937"
                  }
              }
          },
          "out_messages": []
      }
      

      【讨论】:

        猜你喜欢
        • 2016-10-03
        • 2019-01-05
        • 2022-07-11
        • 2019-02-26
        • 1970-01-01
        • 2021-09-02
        • 2022-09-29
        • 2018-07-08
        • 2021-08-22
        相关资源
        最近更新 更多