'Solidity - Send array of tuple data with address and amount
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?
Solution 1:[1]
I did some tests and I believe you need to do some fixes in your code
1° 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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | NinjaMAN |