我正在尝试编写一个以太坊众筹合约,我试图将其绑定到我之前迁移到 Ropsten 测试网的现有代币,但我遇到了一个我无法弄清楚的问题。我可以迁移两个合约没有问题,但我不能购买代币,因为汽油价格高得离谱。下面是我的代码,以及我尝试调用 buyTokens(( 函数时遇到的错误:
松露控制台中的错误:
truffle(develop)> c.buyTokens("0x627306090abab3a6e1400e9345bc60c78a8bef57", {value: 0.00000000000001})
Error: Error: sender doesn't have enough funds to send tx. The upfront cost is: 1188468318377797839569896498995073345436650825553660099636 and the sender's account only has: 99249141200000000000
at runCall (/usr/local/lib/node_modules/truffle/build/chain.bundled.js:67228:10)
at /usr/local/lib/node_modules/truffle/build/chain.bundled.js:14393:24
at replenish (/usr/local/lib/node_modules/truffle/build/chain.bundled.js:11525:17)
at iterateeCallback (/usr/local/lib/node_modules/truffle/build/chain.bundled.js:11510:17)
at /usr/local/lib/node_modules/truffle/build/chain.bundled.js:11485:16
at /usr/local/lib/node_modules/truffle/build/chain.bundled.js:14398:13
at /usr/local/lib/node_modules/truffle/build/chain.bundled.js:63440:16
at replenish (/usr/local/lib/node_modules/truffle/build/chain.bundled.js:63387:25)
at /usr/local/lib/node_modules/truffle/build/chain.bundled.js:63396:9
at eachLimit (/usr/local/lib/node_modules/truffle/build/chain.bundled.js:63320:36)
at Object.InvalidResponse (/usr/local/lib/node_modules/truffle/build/cli.bundled.js:41484:16)
at /usr/local/lib/node_modules/truffle/build/cli.bundled.js:329530:36
at /usr/local/lib/node_modules/truffle/build/cli.bundled.js:325200:9
at XMLHttpRequest.request.onreadystatechange (/usr/local/lib/node_modules/truffle/build/cli.bundled.js:328229:7)
at XMLHttpRequestEventTarget.dispatchEvent (/usr/local/lib/node_modules/truffle/build/cli.bundled.js:176415:18)
at XMLHttpRequest._setReadyState (/usr/local/lib/node_modules/truffle/build/cli.bundled.js:176705:12)
at XMLHttpRequest._onHttpResponseEnd (/usr/local/lib/node_modules/truffle/build/cli.bundled.js:176860:12)
at IncomingMessage.<anonymous> (/usr/local/lib/node_modules/truffle/build/cli.bundled.js:176820:24)
at emitNone (events.js:110:20)
at IncomingMessage.emit (events.js:207:7)
2_deploy_contracts.js
const TestTokenCrowdsale = artifacts.require('./TestTokenCrowdsale.sol');
var walletAddress =
require('fs').readFileSync('../keystore/address').toString();
const duration = {
seconds: function(val) { return val},
minutes: function(val) { return val * this.seconds(60) },
hours: function(val) { return val * this.minutes(60) },
days: function(val) { return val * this.hours(24) },
weeks: function(val) { return val * this.days(7) },
years: function(val) { return val * this.days(365)}
};
module.exports = function(deployer) {
const startTime = 1515896330 + duration.minutes(5);
const endTime = startTime + duration.minutes(20);
const rate = new web3.BigNumber(2);
const cap = new web3.BigNumber(1000000000000000000);
const tokenAddress = '****';
deployer.deploy(TestTokenCrowdsale, startTime, endTime, rate, cap, walletAddress, tokenAddress);
}
TestTokenCrowdsale.sol
pragma solidity ^0.4.18;
import 'zeppelin-solidity/contracts/crowdsale/CappedCrowdsale.sol';
import './TestToken.sol';
contract TestTokenCrowdsale is CappedCrowdsale {
address public tokenAddress;
FrontierToken public testToken;
/**
* @dev Crowdsale initializer.
* @param _startTime -> Crowdsale start time in UNIX seconds.
* @param _endTime -> Crowdsale end time in UNIX seconds.
* @param _rate -> TST/ETH exchange rate.
* @param _cap -> Fund cap in Wei.
* @param _wallet -> Wallet to collect Ether.
* @param _tokenAddress -> Deployed TestToken address.
*/
function TestTokenCrowdsale(uint256 _startTime, uint256 _endTime, uint256 _rate, uint256 _cap, address _wallet, address _tokenAddress)
CappedCrowdsale(_cap)
Crowdsale(_startTime, _endTime, _rate, _wallet) public {
tokenAddress = _tokenAddress;
token = createTokenContract();
}
function createTokenContract() internal returns (MintableToken) {
testToken = TestToken(tokenAddress);
return TestToken(tokenAddress);
}
}
TestToken.sol
pragma solidity ^0.4.18;
import 'zeppelin-solidity/contracts/token/MintableToken.sol';
import 'zeppelin-solidity/contracts/token/BurnableToken.sol';
contract FrontierToken is MintableToken, BurnableToken {
string public name = "Test Token";
string public symbol = "TST";
uint8 public decimals = 18;
}
我已经确保我在 startTime 之后和 endTime 之前调用函数,并且每次在迁移合约之前都会更新2_deploy_contracts.js中的 startTime。我这样做是为了尽可能少地依赖调试此代码。我也在确保我送的魏少于上限。"c"是我使用 deploy(( 函数获得的已部署合约。任何帮助不胜感激,干杯!
您没有正确发送value
。您需要使用web3.utils.toWei(0.00000000000001, 'ether')
简化问题重现:
pragma solidity ^0.4.18;
contract Test {
uint256 public balance;
function somePayable() public payable {
balance = msg.value;
}
}
truffle(development)> var c undefined truffle(development)> Test.deployed().then(i => c = i); ...Output Truncated... truffle(development)> c.somePayable({from: web3.eth.accounts[0], value: 0.0001}) Error: Error: sender doesn't have enough funds to send tx. The upfront cost is: 1188468318377797841622495538693466475719926236618134981990 and the senders account only has: 299763510000000000000 truffle(development)> c.somePayable({from: web3.eth.accounts[0], value: web3.toWei(0.0001, 'ether')}); { tx: '0x4bd0e15e22a6a0af3a7cea6c57de18bad0d58cdec27e5a7721234d8ac91c09d4', receipt: { transactionHash: '0x4bd0e15e22a6a0af3a7cea6c57de18bad0d58cdec27e5a7721234d8ac91c09d4', transactionIndex: 0, blockHash: '0x76e5c94eced759feb22a511c14fe5ab27f8572b4efc6f0acb5c7e060b922e99f', blockNumber: 11, gasUsed: 41416, cumulativeGasUsed: 41416, contractAddress: null, logs: [] }, logs: [] }