2

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

I need something like this.

function func() public returns((address=>someStruct) myMapping)
Léo Natan
  • 56,823
  • 9
  • 150
  • 195

2 Answers2

5

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": []
}
ilyar
  • 1,331
  • 12
  • 21
1

This solved my problem:

function func() public returns(mapping (address=>someStruct))
Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77