【问题标题】:How to send tokens to contract address如何将代币发送到合约地址
【发布时间】:2019-05-07 04:34:38
【问题描述】:

我想将erc20代币发送到能够交易代币的合约地址。

但是,我没有通过测试并且错误提示 Error: VM Exception while processing transaction: revert

我的功能是这个

TokenSale.sol

  function startSale(address _tokenSaleContractAddress) public {
    require(msg.sender == admin);
    require(tokenContract.transfer(_tokenSaleContractAddress, 750000)); 
  }

MyToken.sol

  function transfer(address _to, uint256 _value) public returns (bool success) {
    require(balanceOf[msg.sender] >= _value);

    balanceOf[msg.sender] -= _value;
    balanceOf[_to] += _value;

    emit Transfer(msg.sender, _to, _value);

    return true;
  }

我的测试是这样的

  it('facilitates start sale', function() {
    return MyToken.deployed().then(function(instance) {
      tokenInstance = instance;
      return TokenSale.deployed()
    }).then(function(instance) {
      tokenSaleInstance = instance;
      return tokenSaleInstance.startSale(tokenSaleInstance.address, {from: admin} )
    }).then(function(receipt) {

      return tokenInstance.balanceOf(tokenSaleInstance.address)
    }).then(function(balance) {
      assert.equal(balance.toNumber, 750000);
    });
  });

你能告诉我为什么我没有通过测试吗?

【问题讨论】:

  • 很难说没有看到你的其余代码,但看起来你没有将任何代币转移到TokenSale合约,所以当它尝试转移代币时,它有余额不足。此外,您似乎正试图让TokenSale 合约将代币转移给自己?您正在调用startSale 并将TokenSale 自己的地址作为参数传递。
  • 管理员有 1000000 个代币,我想发送 750000 个代币到TokenSale 合约。
  • 我在 github 上发布了我的代码。 github.com/Yosuke-Aramaki/erc20_proto_react.git

标签: ethereum solidity truffle


【解决方案1】:

管理员有 1000000 个代币,我想将 750000 个代币发送到TokenSale 合约。

为此,管理员需要在代币合约上调用transfer。 IE。你的测试代码应该有这个:

tokenInstance.transfer(tokenSaleInstance.address, 750000, { from: admin });

之后,调用 startSale 应该会成功,但没有理由调用它,因为它只是将 750,000 个令牌转移给自己。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-26
    • 2022-09-27
    • 2022-01-02
    • 2021-07-18
    • 2021-05-17
    • 2021-09-23
    相关资源
    最近更新 更多