Hello! Would appreciate for help!
When I try to run:
await wallet.balances(accounts[0], web3.utils.fromUtf8("LINK"))
I get this error message:
(Uncaught TypeError: wallet.balances is not a function)
3_token_migration.js file:
const Link = artifacts.require("Link");
const Wallet = artifacts.require("Wallet");
module.exports = async function (deployer, network, accounts) {
await deployer.deploy(Link);
let wallet = await Wallet.deployed();
let link = await Link.deployed();
await link.approve(wallet.address, 500);
wallet.addToken(web3.utils.fromUtf8("LINK"), link.address);
await wallet.deposit(100, web3.utils.fromUtf8("LINK"));
let balanceOfLink = await wallet.balances(accounts[0], web3.utils.fromUtf8("LINK"));
console.log(balanceOfLink);
}
wallet.sol file:
// SPDX-License-Identifier: MIT
pragma solidity >=0.4.22 <0.9.0;
import "../node_modules/@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "../node_modules/@openzeppelin/contracts/utils/math/SafeMath.sol";
import "../node_modules/@openzeppelin/contracts/access/Ownable.sol";
contract Wallet is Ownable{
using SafeMath for uint256;
/*
event depositDone(uint amount, address indexed depositedTo);
*/
struct Token{
bytes32 ticker;
address tokenAddress;
// we need tokenAddress in order to be able to call the token contract.
}
// struct Token stores information about the tokens that this contract and wallet support.
mapping(bytes32 => Token) public tokenMapping;
// a mapping that points from bytes32 (token ticker) to the struct object (struct Token).
bytes32[] public tokenList;
// here we will have all of the tickers, like id's for ERC20 tokens.
mapping(address => mapping(bytes32 => uint256)) public balances;
// mapping of addresses that hold multiple ERC20 tokens, bytes32 represents a ticker(tokenId)
// of a ERC20 token.
modifier tokenExist(bytes32 ticker){
require(tokenMapping[ticker].tokenAddress != address(0), "Token does not exist");
_;
}
function addToken(bytes32 ticker, address tokenAddress) onlyOwner external {
tokenMapping[ticker] = Token(ticker, tokenAddress);
// we save a new token that we save to our tokenMapping.
tokenList.push(ticker);
// we only add a ticker of a new token to the tokenList, in order to have a list of all token id's.
}
function deposit(uint amount, bytes32 ticker) tokenExist(ticker) external {
balances[msg.sender][ticker] = balances[msg.sender][ticker].add(amount);
IERC20(tokenMapping[ticker].tokenAddress).transferFrom(msg.sender, address(this), amount);
}
function withdraw(uint amount, bytes32 ticker) tokenExist(ticker) external {
/*
require(tokenMapping[ticker].tokenAddress != address(0));
// we check so that token exists, so that tokenAddress is not a 0 address.
// we add a modifier tokenExist instead of the require statement.
*/
require(balances[msg.sender][ticker] >= amount, "Balance not sufficient");
// check that you have the actual balance before withdrawal.
balances[msg.sender][ticker] = balances[msg.sender][ticker].sub(amount);
// to adjust the balances, balance of msg.sender is substracted the amount. Add SafeMath for .sub().
IERC20(tokenMapping[ticker].tokenAddress).transfer(msg.sender, amount);
// we input tokenAddress and we do transfer call from this contract to msg.sender.
}
}
Best regards