Search code examples
solidityeverscale

How to return mapping in ton-solidity contracts?


Can I return a mapping in a function of ton-solidity contracts?

I need something like this.

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

Solution

  • 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
    

    Run

    bash run.sh
    

    Result

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