So testen Sie einen einfachen Zahlungsvertrag

Ich versuche, einen einfachen Zahlungsvertrag gemäß dem Trüffel-Tutorial zu testen, kann es aber nicht zum Laufen bringen. Würden Sie sich über Ihre Anleitung zum Abrufen von Kontoständen freuen (das scheint hier der kaputte Schritt zu sein)?

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

Beim Ausführen des Trüffeltests bricht der Code mit dem folgenden Fehler ab:

  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)

Antworten (1)

getBalanceist keine in Ihrem Vertrag enthaltene Funktion, sondern eine web3-Funktion. Um das Guthaben zu erhalten, können Sie verwenden, web3.eth.getBalance(contractAddress)wo contractAddress in Ihrem Fall ist pay.address. web3 ist bereits in den Testdateien von Truffle verfügbar, sodass Sie es nicht einschließen müssen.

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