Openzepplin crowdsale contract got: VM Exception while processing transaction: revert 错误
Openzepplin crowdsale contract got: VM Exception while processing transaction: revert error
我正在开发基于 openzeppelin-solidity 的智能合约,我想写一个简单的 Crowdsale 合约,我只是继承 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)
{
}
}
这是我的 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_;
}
}
这是我的 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 test
后出现错误 Error: VM Exception while processing transaction: 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.');
})
})
我不知道如何查看错误日志并知道是哪一行导致了这个问题。
您有 2 个问题:
首先,您使用的单位不正确。您已将众筹初始化为每发送一个 Wei 就出售 1000 个代币。来自 Zeppelin 合约中的文档:
@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
您在交易中传递了 1 个以太币,这意味着您正试图购买 1000 * (10^18) 个代币单位,但您只分配了 36000000 个总供应量。您需要增加总供应量 and/or 降低利率。
其次,除非先获得批准,否则只有代币所有者才能进行转账。当您部署代币合约时,所有代币都归 msg.sender
所有。但是,当有人通过您的众筹合约进行购买时,转账请求来自众筹合约的地址,而不是部署您的代币合约时的代币所有者。解决此问题的最简单方法是在部署您的合同后,将用于众筹的足够代币从您用于创建代币合同的地址转移到众筹合同的地址。
我正在开发基于 openzeppelin-solidity 的智能合约,我想写一个简单的 Crowdsale 合约,我只是继承 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)
{
}
}
这是我的 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_;
}
}
这是我的 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 test
后出现错误 Error: VM Exception while processing transaction: 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.');
})
})
我不知道如何查看错误日志并知道是哪一行导致了这个问题。
您有 2 个问题:
首先,您使用的单位不正确。您已将众筹初始化为每发送一个 Wei 就出售 1000 个代币。来自 Zeppelin 合约中的文档:
@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
您在交易中传递了 1 个以太币,这意味着您正试图购买 1000 * (10^18) 个代币单位,但您只分配了 36000000 个总供应量。您需要增加总供应量 and/or 降低利率。
其次,除非先获得批准,否则只有代币所有者才能进行转账。当您部署代币合约时,所有代币都归 msg.sender
所有。但是,当有人通过您的众筹合约进行购买时,转账请求来自众筹合约的地址,而不是部署您的代币合约时的代币所有者。解决此问题的最简单方法是在部署您的合同后,将用于众筹的足够代币从您用于创建代币合同的地址转移到众筹合同的地址。