Reputation: 41
I try to get a loop run through all structs associated with the caller address, but I can't get my head around this warning. I think I understand the problem, but can't get my head around what should I do differently to achieve this result the other way.
The error I am getting:
TypeError: Integer constant expected.
--> minitest.sol:30:31:
|
30 | balance += Wallet[walletNumbers[msg.sender][i]].balance;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
My code simplified just for the error part:
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^ 0.8 .0;
contract MiniTest {
uint nextWalletNumber = 0;
struct Wallet {
address owner;
uint balance;
uint debt;
}
Wallet[] public walletInfo;
mapping(address => uint[]) public walletNumbers;
function createWallet() public {
Wallet memory newWallet = Wallet(msg.sender, 1000, 0);
walletInfo.push(newWallet);
walletNumbers[msg.sender].push(nextWalletNumber);
nextWalletNumber++;
}
function allWalletsBalance() public view returns(uint) {
uint balance;
for (uint i; i < walletNumbers[msg.sender].length; i++) {
balance += Wallet[walletNumbers[msg.sender][i]].balance;
}
return balance;
}
}
Is there another way to achieve this for loop and take out uint from all structs associated with that address?
Upvotes: 1
Views: 861
Reputation: 49303
although I could not figure out the relationship between your state variables, this line of code is
balance += Wallet[walletNumbers[msg.sender][i]].balance;
Wallet
is a struct. instead you should be using walletInfo
the name of the array.
balance += walletInfo[walletNumbers[msg.sender][i]].balance;
Upvotes: 2