0

I'm trying to test a simple payment contract according to the truffle tutorial, but not able to get it working. Would appreciate your guidance on how to get account balances (that seems to be the broken step here)?

Pay.sol

pragma solidity ^0.4.17;

/// @title Pay - Facilitates payments.
contract Pay {
    event Payment(
        address _from,
        address _to,
        uint amount
    );

    /// @dev Makes a payment.
    /// @param _to Address to pay to.
    function pay(address _to) public payable {
        require(msg.value > 0);
        // Does this transfer the right amount of ether (msg.value measured in wei)?
        _to.transfer(msg.value);
        Payment(msg.sender, _to, msg.value);
    }
}

2_pay_migration.js

var Pay = artifacts.require("Pay");

module.exports = function(deployer) {
    deployer.deploy(Pay);
}

pay.js

var Pay = artifacts.require("./Pay.sol");

contract('Pay', function(accounts) {
    it("should put 10000 wei in the first account", function() {
        return Pay.deployed().then(function(instance) {
            pay = instance;
            //console.log(pay.estimateGas());
            return pay.getBalance.call(accounts[0]);
        }).then(function(balance){
            startingBalance = balance;
            return pay.pay(accounts[1], 2500, {from: accounts[0]});
        }).then(function() {
            return pay.getBalance.call(accounts[0]);
        }).then(function(balance) {
            assert.equal(startingBalance, balance);
        })
    });
});

When running truffle test, the code breaks with the following error:

  1) Contract: Pay should put 10000 wei in the first account:
     TypeError: Cannot read property 'call' of undefined
      at test/pay.js:8:35
      at <anonymous>
      at process._tickCallback (internal/process/next_tick.js:118:7)
1

getBalance is not a function contained in your contract, but a web3 function. To get the balance you can use web3.eth.getBalance(contractAddress) where contractAddress in your case is pay.address. web3 is already available in the truffle's test files so you don't need to include.

https://github.com/ethereum/wiki/wiki/JavaScript-API#web3ethgetbalance

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.