I am developing a crowd sale contract in solidity. The fallback function is payable so that investors can send ether to the contract. If incoming transaction is accepted then information about the investor is logged and the ether transferred to a storage wallet/address.

function() public payable {
    buyTokens(msg.sender, msg.value);

function buyTokens(address buyer, uint256 weiAmount) internal {
    require(checkPreSaleOn() || checkGeneralSaleOn());
    require(buyer != address(0));
    require(weiAmount > 0);
    require(weiAmount % 1 ether == 0); // only integer numbers
    uint256 numTokens;

    if (state == State.PreSale) {
        require(weiAmount <= 20000000000000000000); // max 20 ETHER
        numTokens = weiAmount.div(preSaleExchangeRateinWei);
    if (state == State.GeneralSale) {
        require(weiAmount <= 20000000000000000000); // max 20 ETHER
        numTokens = weiAmount.div(generalSaleExchangeRateinWei);

    investedAmount[buyer] = investedAmount[buyer].add(weiAmount);
    fundsRaisedInWei = fundsRaisedInWei.add(weiAmount);

    allocatedTokens[buyer] = allocatedTokens[buyer].add(numTokens);
    tokensIssued = tokensIssued.add(numTokens);
    TokensPurchase(buyer, weiAmount, numTokens);


Problem is that the javascript test (see below) is not awaiting for the (send ether) transaction to go through and so both the initial and final balances will be the same. Thus the assert will fail (because amount = 0). After a few seconds I check the wallet balance and I see the amount has been correctly transferred.

const MyContract = artifacts.require("./contracts/MyCrowdSaleV2.sol");

var Web3 = require('web3');
var pwd = "123";
contract('MyContract', async function(accounts) {
        it("Token purchases go through after pre-sale open", async function() {

            MyContract.new('0x0ca1afa472b258e8811a0c1406a66c7b9cf6acea', preSaleDuration, generalSaleDuration);

            await instance.openPreSale({from: accounts[0]});
            let initialBalance = await web3.eth.fromWei(instance.getBalance(instance.address),'ether').toNumber();
            var tx = {from: accounts[2], to: instance.address, value: web3.toWei(10, "ether"), gas: 200000 };
            let txHash = await web3.eth.sendTransaction(tx, pwd);
            let finalBalance = await web3.fromWei(web3.eth.getBalance(instance.address),'ether').toNumber();
            let amount = (finalBalance - initialBalance);

            await assert.equal(amount,10, "Could not purchase 10 ether of tokens during pre-sale");

Any ideas what I am doing wrong?

Thank you in advance

  • I originally thought that the block hadn't been mined yet, but as you say, the promise for web3.eth.sendTransaction supposedly returns the transaction receipt which should have the block number etc. inside it. Not sure what the problem is, sorry. – norganna Mar 19 '18 at 11:52
  • The problem is that the promise for "await web3.eth.sendTransaction" is not working and I don't understand why that is. For example the previous promise ("await instance.openPreSale({from: accounts[0]});") is working fine – rodneymo Mar 21 '18 at 15:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.