2

I have a simple Escrow contract that I've deployed using testrpc. I'm not sure how to interact with the methods once I've created an initialized a new one.

Here's the contract:

pragma solidity ^0.4.4;

contract Escrow {

address public buyer;
address public seller;
address public arbiter;

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

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

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

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

Console returns the following when I create a new instance of my escrow contract:

Escrow.new([acct2, acct3]).then(function(instance) { console.log(instance.address);})
0x7982e782f1f3937d7b626691b11216155b6b47d0
undefined 

So it seems to have worked. My question is how do I interact with that contract to send it some value? Similar to how I would with a deployed MetaCoin contract using something like MetaCoin.deployed().then(function(instance) { return instance.sendCoin(acct1, 10, {from: acct2});})?

Also, who is the msg.sender in this case?

Thank you for any help.

UPDATE - Setting address = Escrow.new([acct2, acct3]).then(function(instance) { return instance.address;})

Trying to add contract address to Escrow.at(address) and receiving the following error Error: Invalid address passed to Escrow.at(): [object Promise]. Also have tried Escrow.at(address).get() and getting the same error. Thank you.

2

First in order for your contract to accept eth you need to implement a fallback function:

function() payable {}

Then you can send some eth to your contract:

web3.eth.sendTransaction({from:accounts[0], to:"YOUR_CONTRACT_ADDRESS", value: web3.toWei(1, "ether")});

And call contract methods:

Escrow.at("YOUR_CONTRACT_ADDRESS").payoutToSeller({from:acct3});

This call follows your example using acc3 which was defined as arbiter in contract constructor as the account who publish the transaction.

  • Thank you Jakub. Having an issue still. address = Escrow.new([acct2, acct3]).then(function(instance) {return instance.address;}) returns '0xcc63a4e6d0c487476719972fb4b6b70b0cdd646d' When I web3.eth.sendTransaction({from:acct1, to:address, value:web3.toWei(1, "ether")}); I'm getting Error: invalid address. I've added the fallback function and recompiled everything. – Michael O'Rourke Jul 5 '17 at 14:15
  • 1
    I see. You need to assign the address inside the body of promise: Escrow.new([acct2, acct3]).then(function(instance) { address = instance.address;}) – Jakub Wojciechowski Jul 5 '17 at 14:17
  • That worked. Thank you! One more question if you don't mind. Who is msg.sender when I call the new() method? – Michael O'Rourke Jul 5 '17 at 14:19
  • 1
    If you don't put an object that specifies the sender {from: SENDER} in the constructor it's going to be the default account: web.eth.accounts[0]. – Jakub Wojciechowski Jul 5 '17 at 14:21

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.