Ethereum Asked by SkyTemple on December 18, 2021
I would like an array of mappings, is this possible?
mapping (address >= value) sentValue;
sentValue[] public sentValues;
I want to record amounts sent by address, and I want each send event to be unique with the ability for one address to send multiple different amounts and have each tracked in the array individually? The end result would be the ability to iterate through this array and show in order amounts sent by user.
How do I define a new mapping each time a user sends some amount, and what is the code to push that new mapping on the end of the array?
What you are searching for is a mapping of mapping. I created the following smart contract that I think achieve what you described.
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.5.0 <0.8.0;
contract myContract {
mapping (address => mapping (uint => uint)) public payments;
mapping (address => uint) public lastPayment;
function sendMoney() public payable {
require(msg.value > 0);
payments[msg.sender][lastPayment[msg.sender]] = msg.value;
lastPayment[msg.sender]++;
}
}
Please note that using public
for payments
and lastPayment
variables tells the compiler to create two smart contract functions to read those values.
Here the gist, and here the link to try by yourself with remix.
Answered by Giuseppe Bertone on December 18, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP