I tried to make a deposit to of “LINK” to my Wallet (as described in the video).
This is my Wallet.sol code
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.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;
struct Token {
bytes32 ticker;
address tokenAddress;
}
mapping(bytes32 => Token) public tokenMapping;
bytes32[] public tokenList;
mapping(address => mapping(bytes32 => uint256)) public balances;
modifier tokenExist(bytes32 ticker) {
require(tokenMapping[ticker].tokenAddress != address(0), "Token does not exist");
_;
}
function addToken(bytes32 ticker, address tokenAddress) external onlyOwner {
tokenMapping[ticker] = Token(ticker, tokenAddress);
tokenList.push(ticker);
}
function deposit(uint amount, bytes32 ticker) external tokenExist(ticker) {
IERC20(tokenMapping[ticker].tokenAddress).transferFrom(tokenMapping[ticker].tokenAddress, address(this), amount);
balances[msg.sender][ticker] = balances[msg.sender][ticker].add(amount);
}
function withdraw(uint amount, bytes32 ticker) external tokenExist(ticker) {
require(balances[msg.sender][ticker] >= amount, "Balance not sufficient");
balances[msg.sender][ticker] = balances[msg.sender][ticker].sub(amount);
IERC20(tokenMapping[ticker].tokenAddress).transfer(msg.sender, amount);
}
}
And this is my “LINK” Token.sol code
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.0;
import "../node_modules/@openzeppelin/contracts/token/ERC20/ERC20.sol";
contract Link is ERC20 {
constructor() ERC20("ChainLink", "LINK") public {
_mint(msg.sender, 1000);
}
}
All previous steps were successful but when I tried to call deposit function, it throws some error:
truffle(develop)> wallet.deposit(100, web3.utils.fromUtf8("LINK"))
Uncaught:
Error: Returned error: VM Exception while processing transaction: revert ERC20: transfer amount exceeds balance -- Reason given: ERC20: transfer amount exceeds balance.
at evalmachine.<anonymous>:0:8
at sigintHandlersWrap (vm.js:273:12)
at Script.runInContext (vm.js:140:14)
at runScript (/home/kresna/.nvm/versions/node/v14.15.5/lib/node_modules/truffle/build/webpack:/packages/core/lib/console.js:270:1)
at Console.interpret (/home/kresna/.nvm/versions/node/v14.15.5/lib/node_modules/truffle/build/webpack:/packages/core/lib/console.js:285:1)
at bound (domain.js:413:15)
at REPLServer.runBound [as eval] (domain.js:424:12)
at REPLServer.onLine (repl.js:817:10)
at REPLServer.emit (events.js:315:20)
at REPLServer.EventEmitter.emit (domain.js:467:12)
at REPLServer.Interface._onLine (readline.js:337:10)
at REPLServer.Interface._line (readline.js:666:8)
at REPLServer.Interface._ttyWrite (readline.js:1010:14)
at REPLServer.self._ttyWrite (repl.js:907:9)
at ReadStream.onkeypress (readline.js:213:10)
at ReadStream.emit (events.js:315:20)
at ReadStream.EventEmitter.emit (domain.js:467:12)
at emitKeys (internal/readline/utils.js:345:14)
at emitKeys.next (<anonymous>)
at ReadStream.onData (readline.js:1144:36) {
data: {
'0x129c458a669afe0c56350c6daadb34bea0b097c44a5e82f582e6612425b7cb73': {
error: 'revert',
program_counter: 2340,
return: '0x08c379a00000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000002645524332303a207472616e7366657220616d6f756e7420657863656564732062616c616e63650000000000000000000000000000000000000000000000000000',
reason: 'ERC20: transfer amount exceeds balance'
},
stack: 'RuntimeError: VM Exception while processing transaction: revert ERC20: transfer amount exceeds balance\n' +
' at Function.RuntimeError.fromResults (/home/kresna/.nvm/versions/node/v14.15.5/lib/node_modules/truffle/build/webpack:/node_modules/ganache-core/lib/utils/runtimeerror.js:94:1)\n' +
' at BlockchainDouble.processBlock (/home/kresna/.nvm/versions/node/v14.15.5/lib/node_modules/truffle/build/webpack:/node_modules/ganache-core/lib/blockchain_double.js:627:1)\n' +
' at runMicrotasks (<anonymous>)\n' +
' at processTicksAndRejections (internal/process/task_queues.js:93:5)',
name: 'RuntimeError'
},
reason: 'ERC20: transfer amount exceeds balance',
hijackedStack: 'Error: Returned error: VM Exception while processing transaction: revert ERC20: transfer amount exceeds balance -- Reason given: ERC20: transfer amount exceeds balance.\n'
PS: I’ve checked Link balances and it was 1000. I also call approval function from Link to make allowance spending 500 token.
What might goes wrong?
Thank you for the help