1

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?

6
  • @Yilmaz please do not add the [web3] tag to questions. Add the specific library tag instead for a question about a web3-based library. "Web3" itself is a meta tag that does not add any value and means too many different things to be useful Commented Aug 31, 2022 at 19:33
  • @TylerH I thought web3 means decentralized web. so anything related to smartcontract or blockchain should be related to web3. Or what I am missing? Commented Aug 31, 2022 at 19:36
  • web3 is a buzzword term and a meta tag; it's not meaningful (you can't be an expert in web3 for example...), further, web3 itself is not a programming term, concept, entity, etc. There is lots of stuff (like smart contracts, solidity language, web3js or web3react libraries, etc) that are programming related and specific; those are better tags to use. See stackoverflow.com/help/tagging for more info on kinds of useful tags). Commented Aug 31, 2022 at 19:58
  • For example for almost 3 years people used the web3 tag to refer exclusively to the web3js library (and sometimes web3py or similar early library); only later did people start to use it as a catch all for what they really meant which is usually something like "solidity" and "smartcontract". I have been endeavoring to retag all these questions bit by bit (so as not to flood the front page) with the actual tags they need (e.g. web3 -> web3js or web3 -> web3dart or web3 -> web3py). And where they are just talking about some vague web3 tech, I just remove the tag. Commented Aug 31, 2022 at 20:01
  • @TylerH just for curiosity, how long does it take to remove a tag? you said you had removed the tag but still being used? Commented Sep 4, 2022 at 18:20

1 Answer 1

2

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;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.