i'm trying to transfer but failed with
_fakeToken.transfer(msg.sender, amountToBuy);
truffle-contract.min.js:1 Uncaught (in promise) Error: Returned error: VM Exception while processing transaction: revert ERC20: transfer amount exceeds balance -- Reason given: ERC20: transfer amount exceeds balance.
// SPDX-License-Identifier: MIT pragma solidity 0.8.9; import "@openzeppelin/contracts/token/ERC20/ERC20.sol"; import "@openzeppelin/contracts/access/Ownable.sol"; contract FakeToken is ERC20 { constructor() ERC20("Fake Token", "FT") { _mint(msg.sender, 10000 * (10 ** uint256(decimals()))); } }
// SPDX-License-Identifier: MIT pragma solidity 0.8.9; import { Lib } from './Lib.sol'; import './FakeToken.sol'; contract App { FakeToken private _fakeToken; constructor(address fakeTokenAddr) { _fakeToken = FakeToken(fakeTokenAddr); } function retFalse() public pure returns (bool) { return Lib.retFalse(); } function getTokenDetail() public view returns (string memory name, string memory symbol, uint totalSupply) { return (_fakeToken.name(), _fakeToken.symbol(), _fakeToken.totalSupply()); } function buyFakeToken() public payable { uint amountToBuy = msg.value; require(amountToBuy > 0, "You need to send some ether"); // uint vendorBalance = _fakeToken.balanceOf(address(this)); // require(vendorBalance >= amountToBuy, "Vendor contract has not enough tokens in its balance"); _fakeToken.transfer(msg.sender, amountToBuy); } function getBalance() external view returns (uint totalSupply) { return _fakeToken.totalSupply(); } function getBalance1() external view returns (uint totalSupply) { return _fakeToken.balanceOf(msg.sender); } }
Yes you can. but fist you need to grab the token contract into the vendor constructor upon deployment the contract. Had tried it on remix.
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
i'm trying to transfer but failed with
Yes you can. but fist you need to grab the token contract into the vendor constructor upon deployment the contract. Had tried it on remix.