简体   繁体   中英

Openzepplin crowdsale contract got: VM Exception while processing transaction: revert error

I am developing smart contract based on openzeppelin-solidity and I want to write an easy Crowdsale contract, only I did is inherit Contract.sol:

// FloatFlowerTokenCrowdsale.sol
pragma solidity 0.4.23;

import "openzeppelin-solidity/contracts/crowdsale/Crowdsale.sol";

contract FloatFlowerTokenCrowdsale is Crowdsale{
  constructor(ERC20 _token) public Crowdsale(1000, msg.sender, _token) 
  {

  }
}

Here is my FloatFlowerToken.sol

// FloatFlowerToken.sol
pragma solidity 0.4.23;

import "openzeppelin-solidity/contracts/token/ERC20/StandardToken.sol";

contract FloatFlowerToken is StandardToken {
  string public name = "FLOATFLOWER TOKEN";
  string public symbol = "FFT";
  uint8 public decimals = 18;

  constructor() public {
    totalSupply_ = 36000000;
    balances[msg.sender] = totalSupply_;
  }
}

And this is my 2_deploy_contract.js

const FloatFlowerToken = artifacts.require('./FloatFlowerToken.sol');
const FloatFlowerTokenCrowdsale =
    artifacts.require('./FloatFlowerTokenCrowdsale.sol');

module.exports = function(deployer, network, accounts) {
    return deployer
        .then(() => {
            return deployer.deploy(FloatFlowerToken);
        })
        .then(() => {
            return deployer.deploy(FloatFlowerTokenCrowdsale, FloatFlowerToken.address);
        })
};

After I execute the truffle test and I got the error Error: VM Exception while processing transaction: revert

And here is my test code:

it('one ETH should buy 1000 FLOATFLOWER TOKEN in Crowdsale', function(done) {
    FloatFlowerTokenCrowdsale.deployed().then(async function(instance) {
        const data = await instance.sendTransaction({from: accounts[7], value: web3.toWei(1, "ether")}, function(error, txhash) {
            console.log(error);
        });
        const tokenAddress = await instance.token.call();
        const FloatFlowerToken = FloatFlowerToken.at(tokenAddress);
        const tokenAmount = await FloatFlowerToken.balanceOf(accounts[7]);
        assert.equal(tokenAmount.toNumber(), 1000000000000000000000, 'The sender didn\'t receive the tokens as crowdsale rate.');
    })
})

I don't know how to check the error log and to know which line cause this problem.

You have 2 issues:

First, the units you're working with aren't correct. You've initialized your crowdsale to sell 1000 tokens for every Wei sent. From the documentation in the Zeppelin contract:

@param _rate Number of token units a buyer gets per wei

@param _wallet Address where collected funds will be forwarded to

@param _token Address of the token being sold

You're passing in 1 ether in your transaction, which means you're attempting to buy 1000 * (10^18) token units, but you've only allocated 36000000 total supply. You need to increase your total supply and/or lower your rate.

Second, only token owners can do a transfer unless an approve has been done first. When you deploy the token contract, all of the tokens are owned by msg.sender . However, when someone makes a purchase through your crowdsale contract, the request to do the transfer is coming from the address of the crowdsale contract, not the token owner when your token contract was deployed. The simplest way around this is after deploying your contracts, transfer enough tokens for the crowdsale from the address you used to create the token contract over to the address of the crowdsale contract.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM