1

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(&quot;_to&quot;, _to);
    log(&quot;_amount&quot;, _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.

tsywkGo
  • 23
  • 3
  • Could you please share INToken.sol source code ? – Patrice Tisserand Mar 14 '22 at 08:04
  • ```solidity// SPDX-License-Identifier: GPL-3.0pragma 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 '22 at 09: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 '22 at 09:17

2 Answers2

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);

Andrew
  • 11
  • 2
  • thanks, but there will be another error: errored: Returned error: VM Exception while processing transaction: revert ERC20: insufficient allowance – tsywkGo Mar 14 '22 at 08:24
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