1

I have a problem by executing the following smart contract. It's a simple escrow-smart-contract and I want to execute it in web3JS version 1.0. The contract should be valid but there is not effect when I execute the method payoutToSeller(). The seller should get 5 ether back, but that's not the case. Can someone help me?

const Web3 = require("web3");
const solc = require("solc");
let web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));

let getBalance = (acc) => {
  return new Promise((res, rej) => {
    let balance = web3.eth.getBalance(acc).then((data, error) => {
      if (!error) {
        let ethers = web3.utils.fromWei(data, "ether");
        res(ethers);
      } else {
        rej("Requesting balance failed.");
      }
    });
  }).catch((e) => {
    console.error(e)
  })
}

let source = `pragma solidity ^0.4.20;
contract Escrow {
  address public buyer;
  address public seller;
  address public arbiter;

  function Escrow(address _seller, address _arbiter) public payable { 
    buyer = msg.sender;
    seller = _seller;
    arbiter = _arbiter;
  }

  function payoutToSeller() public {
    if (msg.sender == arbiter || msg.sender == buyer) {
      seller.send(this.balance); // this = contract
    }
  }

  function refundBuyer() public {
    if (msg.sender == arbiter || msg.sender == seller) {
      buyer.send(this.balance);
    }
  }

  function getBalance() constant returns (uint) {
    return this.balance;
  }
}
`

let main = async () => {
  let accounts = await web3.eth.getAccounts();
  let buyer = accounts[0];
  let seller = accounts[1];
  let arbiter = accounts[2];

  // compile the solidity code
  let compiled = solc.compile(source);

  // save public interface of contract
  let abi = JSON.parse(compiled.contracts[":Escrow"].interface)

  // create var with contract
  let Escrow = new web3.eth.Contract(abi);

  console.log("Before:");
  console.log("\tBuyer: " + await getBalance(buyer));
  console.log("\tSeller: " + await getBalance(seller));
  console.log("\tArbiter: " + await getBalance(arbiter));
  console.log("\tContract: 0\n");

  // deploy contract
  let deployContractTx = Escrow.deploy({
    data: compiled.contracts[':Escrow'].bytecode,
    arguments: [seller, arbiter]
  });

  let calculatedGas = await deployContractTx.estimateGas();

  let contractInstance = await deployContractTx.send({
    from: buyer,
    gas: calculatedGas,
    value: web3.utils.toWei('5', "ether")
  });

  console.log("After sending the contract:");
  console.log("\tBuyer: " + await getBalance(buyer));
  console.log("\tSeller: " + await getBalance(seller));
  console.log("\tArbiter: " + await getBalance(arbiter));
  console.log("\tContract: " + await getBalance(contractInstance.options.address) + "\n");

  //
  //everything works fine until here.
  //

  await contractInstance.methods.payoutToSeller().call({from: arbiter});

  console.log("After the arbiter allowed the payout:");
  console.log("\tBuyer: " + await getBalance(buyer));
  console.log("\tSeller: " + await getBalance(seller));
  console.log("\tArbiter: " + await getBalance(arbiter));
  console.log("\tContract: " + await getBalance(contractInstance.options.address) + "\n");

}

main();
2

You're calling the function instead of sending a transaction, which means the function invocation has no side effects. This line:

await contractInstance.methods.payoutToSeller().call({from: arbiter});

should be something like this:

await contractInstance.methods.payoutToSeller().send({from: arbiter});
1
  • OMG, that's it? I thank you so much. I tried to find the problem for 2 hours.
    – ndsvw
    Mar 4 '18 at 11:48

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.