Search code examples
blockchainethereumsmartcontractstruffle

Open Zeppelin + Truffle Ethereum Crowdsale buyTokens() function ridiculous upfront cost


I am attempting to write an Ethereum Crowdsale contract that I am attempting to bind to an existing token I had migrated to the Ropsten testnet prior, but I am running into a problem I just cannot figure out. I can migrate both contracts no problem, but I cannot buy tokens as the gas price is ridiculously high. Below is my code, and the error I get when I try to call the buyTokens() function:

Error in truffle console:

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;
}

I have made sure that I am calling the function after startTime and before endTime, and I update the startTime in the 2_deploy_contracts.js every time before I migrate the contract. I do this to rely on as little as possible to debug this code. I am also making sure that I am sending less Wei than the cap. "c" is the deployed contract that I got using the deployed() function. Any help is appreciated, cheers!


Solution

  • You're not sending in your value correctly. You need to use web3.utils.toWei(0.00000000000001, 'ether')

    Simplified reproduction of the issue:

    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: [] }