0

MyCode Like this:

// SPDX-License-Identifier: GPL-3.0

pragma solidity >=0.7.0 <0.9.0;

import "./INToken.sol";
import "./Console.sol";

contract TransferCoin is Console {

    // 这个状态变量存储在区块链网络上
    address public owner;
    INToken public coin;

    constructor(address _coinAddress) {
        // 合约拥有者为合约创建者
        owner = msg.sender;
        // 设置映币地址
        coin = INToken(_coinAddress);
    }

    function trans(address _to, uint256 _amount) public {
        log("_to", _to);
        log("_amount", _amount);
        coin.transfer(_to, _amount);
    }
}

when i try to use trans function, the error: transact to TransferCoin.trans errored: Returned error: VM Exception while processing transaction: revert ERC20: transfer amount exceeds balance.
but my account's balance is 99999596.
Can anyone help me tell me how to solve this problem, thanks.

3
  • Could you please share INToken.sol source code ? Mar 14, 2022 at 8:04
  • solidity// SPDX-License-Identifier: GPL-3.0` `pragma solidity >=0.7.0 <0.9.0;` import "openzeppelin-solidity/contracts/token/ERC20/ERC20.sol"; contract INToken is ERC20 { uint constant INITIAL_SUPPLY = 10000000000 * 10; constructor() ERC20("InkeCoin", "INT") { _mint(msg.sender, INITIAL_SUPPLY); emit Transfer(address(0), msg.sender, INITIAL_SUPPLY); } function decimals() public view virtual override returns (uint8) { return 1; } }
    – tsywkGo
    Mar 14, 2022 at 9:10
  • ``` // SPDX-License-Identifier: GPL-3.0 pragma solidity >=0.7.0 <0.9.0; import "openzeppelin-solidity/contracts/token/ERC20/ERC20.sol"; contract INToken is ERC20 { uint constant INITIAL_SUPPLY = 10000000000 * 10; // 1000亿 constructor() ERC20("InkeCoin", "INT") { _mint(msg.sender, INITIAL_SUPPLY); emit Transfer(address(0), msg.sender, INITIAL_SUPPLY); } function decimals() public view virtual override returns (uint8) { return 1; } } ```
    – tsywkGo
    Mar 14, 2022 at 9:17

2 Answers 2

0

In TransferCoin::trans function, you are calling coin.transfer(_to, _amount), since your INtoken contract inherit from Openzeppelin ERC20, transfer implementation use msg.sender It means that it's a transfer from your TransferCoin contract balance.

Please see differences between tx.origin and msg.sender

Maybe you should take a look at delegatecall

0

Make sure your contract has enough INToken balance. Try this, https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC20/ERC20.sol#L158

coin.transferFrom(address(this), _to, _amount);

1
  • thanks, but there will be another error: errored: Returned error: VM Exception while processing transaction: revert ERC20: insufficient allowance
    – tsywkGo
    Mar 14, 2022 at 8:24

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.