1

I'm working on a Solidity Smart Contract for Tron Network. I want the owner can set an array of address => uint. This is the code:

pragma solidity ^0.8.0;

struct singleUser {
    address user;
    uint amount;
}

mapping(address=>uint) private _users;

function setUsers(singleUser[] memory _data) {
    for(uint i = 0; i<_data.length; i++){
        _users[_data[i].address] = _data[i].amount;
    }
}

Now, I'm testing this contract direclty on Shasta Network and on TronIDE, but I cannot defined the _data variable. I've tried as follow:

[{address:0x54426759596b4a6655397744444d6e656b72516e534a676346576677584872465364,amount:60000000},{address:0x5442363852436b4e666b736e383476655938516946337262347777587278524e3454,amount:80000000}]
[{address:'TBgYYkJfU9wDDMnekrQnSJgcFWfwXHrFSd',amount:60000000},{address:'TB68RCkNfksn84veY8QiF3rb4wwXrxRN4T',amount:80000000}]

No one of these works. How can I set this tuple?

Klode
  • 353
  • 3
  • 18

1 Answers1

1

I did some tests and I believe you need to do some fixes in your code

contract keyword is missing

2° In setUsers function amount attribute does not exist, should use user because it is declared in singleUser struct

3° I created a simple public function to retrieve a user amount


pragma solidity ^0.8.0;

contract StackOverflow {

    struct singleUser {
        address user;
        uint256 amount;
    }

    mapping(address => uint256) private _users;
    
    function setUsers(singleUser[] memory _data) public {
        for(uint i = 0; i<_data.length; i++){
            _users[_data[i].user] = _data[i].amount;
        }
    }
    
    function getUserAmount(address user) public view returns(uint256) {
        return _users[user];
    }
}

You can test tuple using this way:

#setUsers
[
  ["0x0000000000000000000000000000000000000001",1234], 
  ["0x0000000000000000000000000000000000000002",5678]
]

IDE setUsers decoded input:

{
    "tuple[] _data": [
        [
            "0x0000000000000000000000000000000000000001",
            {
                "_hex": "0x04d2",
                "_isBigNumber": true
            }
        ],
        [
            "0x0000000000000000000000000000000000000002",
            {
                "_hex": "0x162e",
                "_isBigNumber": true
            }
        ]
    ]
}
#getUserAmount

"0x0000000000000000000000000000000000000001"

IDE getUserAmount decoded input:

{
    "address user": "0x0000000000000000000000000000000000000001"
}

IDE getUserAmount decoded output:

{
    "0": "uint256: 1234"
}

also, I recommend you to use IDE remix

NinjaMAN
  • 101
  • 7
  • Thanks for your supply. I actually forgot to enter the "contract" syntax. However, I've tried to insert this tuple data `[["0x4112c97f271c8f3ab178af228fc0127b38dcc4f2f6", 120000000],["0x410c4736129a339805dca20c0d158ff4fe21dab4c9", 60000000],["0x41ce48724a1178bc4186408b6c64552f7f7de07c73", 46000000]]`, but I've received this from Contract: `{"reason":"types/value length mismatch","code":"INVALID_ARGUMENT","argument":"tuple","value":["0x4112c97f271c8f3ab178af228fc0127b38dcc4f2f6",120000000]}`. Is my tuple data correct? – Klode Oct 06 '21 at 05:10
  • Hi, I've understand the Address must be represented in his Base58 format. So the tuple data must be like this one: `[['TBgYYkJfU9wDDMnekrQnSJgcFWfwXHrFSd',60000000],['TB68RCkNfksn84veY8QiF3rb4wwXrxRN4T', 100000000]]`. But the response from contract is `{}` in Shasta TronScan access. – Klode Oct 08 '21 at 13:49
  • Finally! It works in this format, or guess I don't get any error, and the transaction gone okay. `[{"address":"TBgYYkJfU9wDDMnekrQnSJgcFWfwXHrFSd","amount":60000000}]`. In this way I got correct the transaction. But when I require the user Amount with getUserAmount I got always 0. – Klode Oct 08 '21 at 14:52