Blockchain Openzepplin crowdsale合同在处理事务时获得:VM异常:还原错误

Blockchain Openzepplin crowdsale合同在处理事务时获得:VM异常:还原错误,blockchain,ethereum,solidity,truffle,Blockchain,Ethereum,Solidity,Truffle,我正在开发基于openzeppelin solidity的智能合约,我想写一份简单的众售合约,唯一做的就是继承合约。sol: // FloatFlowerTokenCrowdsale.sol pragma solidity 0.4.23; import "openzeppelin-solidity/contracts/crowdsale/Crowdsale.sol"; contract FloatFlowerTokenCrowdsale is Crowdsale{ constructor

我正在开发基于openzeppelin solidity的智能合约,我想写一份简单的众售合约,唯一做的就是继承合约。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) 
  {

  }
}
这是我的代币

// 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_;
  }
}
这是我的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);
        })
};
在我执行
truffle测试之后,我得到了错误
error:VM异常,同时处理事务:revert

这是我的测试代码:

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.');
    })
})
我不知道如何检查错误日志,也不知道是哪一行导致此问题。

您有两个问题:

首先,您使用的单位不正确。您已经初始化了crowdsale,每发送一次,您就可以卖出1000个代币。齐柏林飞艇合同中的文件:

@param\u每名买家获得的代币单位数量

@param_钱包地址,将收取的资金转发至

@正在出售的令牌的参数令牌地址

您在交易中传递了1个以太,这意味着您试图购买1000*(10^18)个代币单位,但您只分配了36000000个总供应量。您需要增加总供应量和/或降低费率


第二,只有令牌所有者才能进行转移,除非先进行了批准。部署令牌合约时,所有令牌都归
msg.sender
所有。但是,当有人通过您的众筹合同进行购买时,进行转移的请求来自众筹合同的地址,而不是部署您的代币合同时的代币所有者。解决这一问题的最简单方法是,在部署合同后,将足够的代币从用于创建代币合同的地址转移到代币合同的地址。

谢谢,这很有效。很抱歉,我忽略了文档中所说的内容,我将
totalSupply_u
更改为
(36000000*(10**18))
,并将一些代币转移到众筹合同中,它就起作用了。