Truffle Interactions – Questions About Interacting with Contracts

soliditytruffle

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.

Best Answer

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.

Related Topic