Truffle - Mappings - Building DEX

Hello everyone
I can t access the balances mapping of the Wallet contract, from Truffle

in Truffle, all compiled and deployed, I instanciated :
let wallet = await Wallet.deployed()

when I try to check the balances I get this :

truffle(develop)> wallet.balances(accounts[0],web3.utils.fromUtf8(“Link”))
evalmachine.:0
wallet.balances(accounts[0],web3.utils.fromUtf8(“Link”))
^

Uncaught TypeError: wallet.balances is not a function

Can someone please give some insights ?

pragma solidity >=0.4.22 <0.9.0;

import '../node_modules/@openzeppelin/contracts/token/ERC20/IERC20.sol';
import '../node_modules/@openzeppelin/contracts/access/Ownable.sol';

contract Wallet is Ownable {

    struct Token {
        bytes32 ticker;
        address tokenAddress;
    }
    mapping (bytes32 => Token) public tokenMapping ; //Ticker => Token.
    bytes32[] public tokenList ; //All Tickers.

    modifier TokenExist(bytes32 _ticker){
        require (tokenMapping[_ticker].tokenAddress != address(0), "Token not listed yet") ;
        _;
    }

    mapping (address => mapping(bytes32 => uint256)) balances ; //Address => Ticker=>Amount.

    function addToken (bytes32 _ticker, address _tokenAddress) external onlyOwner {
        tokenList.push(_ticker);
        tokenMapping[_ticker]= Token(_ticker,_tokenAddress);
    }

    function deposit (uint256 _amount, bytes32 _ticker) external TokenExist(_ticker) {
        //IERC20(tokenMapping[_ticker].tokenAddress).transferFrom(sender, recipient, amount);
        IERC20(tokenMapping[_ticker].tokenAddress).transferFrom(msg.sender, address(this), _amount);
        balances[msg.sender][_ticker] += _amount;
    }

     function withDraw (uint256 _amount, bytes32 _ticker) external TokenExist(_ticker) {  
        require (balances[msg.sender][_ticker] >= _amount, "Not enough Funds");
        balances[msg.sender][_ticker] -= _amount;
        IERC20(tokenMapping[_ticker].tokenAddress).transfer(msg.sender, _amount);
        //IERC20(tokenMapping[_ticker].tokenAddress).transfer(recipient, amount);
    }

}
2 Likes

Hey @Sieg, hope you are well.

Although you can not access to the mapping by calling it that way, you could create a get function to achieve the same result, something like:

function getMapping (address _account, bytes32 _ticker) public view returns(uint){
 return balances[_account][_ticker];
} 

Then you can invoke the function and should give you the proper result based on the input.

Carlos Z

Hi @thecil
Many Thanks for your answer
Sieg

1 Like