Reputation:
// SPDX-License-Identifier: UNLICENSED
pragma solidity 0.8.12;
contract MyContract {
address payable public myaddress;
struct TUser {
uint totalInvested;
uint totalDeposits;
}
mapping( address => TUser ) public USERS;
constructor(address payable _walletProject) {
myaddress = _walletProject;
}
receive() payable external {
}
function newDeposit(uint _amount) public returns(uint,uint){
USERS[msg.sender].totalDeposits += 1;
USERS[msg.sender].totalInvested += _amount;
return (USERS[msg.sender].totalDeposits, USERS[msg.sender].totalInvested);
}
function getUserinfo (address _sender) public view returns (uint, uint) {
return (USERS[_sender].totalDeposits, USERS[_sender].totalInvested);
}
}
Hello everyone, its been hours i'm stuck on this issues and i cannot figure it out. Basically, i'm just trying to store some info for every user that does a deposit. At the moment, i'm just trying to see if my mapping/struct saves the info of just 1 user, but from multiple deposits, so it should return the total number of deposits and the sum of the amounts of each deposit, but it always returns either 0, in the GetUserInfo function, or undefined if i try to return directly from newDeposit function.
Here is the web3.js code to call MyContract functions.
async function sendEth(_plan, _amount) {
const accounts = await ethereum.request({ method: 'eth_requestAccounts' });
const contract = await new web3.eth.Contract(abi, address);
const weiValue = Web3.utils.toWei(_amount, 'ether');
const receipt = await ethereum
.request({
method: 'eth_sendTransaction',
params: [
{
from: accounts[0],
to: address,
value: web3.utils.toHex(weiValue),
},
],
}).then((txHash) => console.log(txHash)).catch((error) => console.error);
console.log('first');
let result =[];
result = await contract.methods.newDeposit(weiValue);
console.log(result);
}
I have edited my code as following, but it still doesnt save my user's info
The functions used to save data, i've tried with emiting events too, but still nothing.
function newDeposit(uint _amount) public {
USERS[msg.sender].totalDeposits += 1;
USERS[msg.sender].totalInvested += _amount;
}
Here is the function i use to read data
function getUserinfo () public view returns (uint, uint) {
return (USERS[msg.sender].totalDeposits, USERS[msg.sender].totalInvested);
}
And here is the web3.js part to start the transaction an send the data to the smart contract
async function sendEth(_amount) {
const weiValue = Web3.utils.toWei(_amount, 'ether');
const amount = Web3.utils.toHex(weiValue);
const receipt = await ethereum
.request({
method: 'eth_sendTransaction',
params: [
{
from: accounts[0],
to: address,
value: amount,
},
],
}).then((txHash) => txWait(txHash, amount)).catch((error) => console.error);
}
async function txWait(_txHash, _amount){
console.log("cazzo");
let transactionReceipt = null;
while (transactionReceipt == null) {
transactionReceipt = await web3.eth.getTransactionReceipt(_txHash);
await sleep(expectedBlockTime);
}
console.log(transactionReceipt);
console.log(accounts[0]);
contract.methods.newDeposit(_amount).send({from: accounts[0]});
console.log('ciao2');
}
I'm trying to test it directly from the FrontEnd application, i have deployed my contract on BscTestnet but there is no way it wants to work.
I'm using all those no sense console.logs, just to test and see if the code gets stuck somewhere, but it doesn't, i get all the console.logs back, the transaction works, everything seems to be fine but my contract doesn't really store the data.
Upvotes: 0
Views: 327
Reputation: 2642
The problem is not exactly with your mapping or struct, but how you test it.
As long as the newDeposit
call doesn't revert during execution, it should work as expected.
Since you are changing the state in your non-view function newDeposit
, you won't be able to retrieve the returned values immediately vbia web3js.
Options I can think of are:
emitting events in your deposit function and reading events in your tests
Following the call to newDeposit
function, the below expects should be satisfied correctly, since getUserInfo
is a view function.
const [invested, deposits] = await contract.methods.getUserInfo(accounts[0]);
// invested and deposits should be fine
Upvotes: 0