1

My question is related to the question that is available in this post. However, the answer I'm looking for is not available in that post.

Assume that the seller (owner) deploys the following contract.

contract Purchase {
    uint public value;
    address public seller;
    address public buyer;
    enum State { Created, Locked, Inactive }
    State public state;

constructor() public payable { seller = msg.sender; value = msg.value / 2; require((2 * value) == msg.value, "Value has to be even."); }

modifier condition(bool _condition) { require(_condition); _; }

modifier onlyBuyer() { require( msg.sender == buyer, "Only buyer can call this." ); _; }

modifier onlySeller() { require( msg.sender == seller, "Only seller can call this." ); _; }

modifier inState(State _state) { require( state == _state, "Invalid state." ); _; }

event Aborted(); event PurchaseConfirmed(); event ItemReceived();

function abort() public onlySeller inState(State.Created) { emit Aborted(); state = State.Inactive; seller.transfer(address(this).balance); }

function confirmPurchase() public inState(State.Created) condition(msg.value == (2 * value)) payable { emit PurchaseConfirmed(); buyer = msg.sender; state = State.Locked; }

function confirmReceived() public onlyBuyer inState(State.Locked) { emit ItemReceived();

state = State.Inactive;

buyer.transfer(value);
seller.transfer(address(this).balance);

}

}

Once the seller deploys the contract how can a buyer (address) can interact with this already deployed contract (making sure the new msg.sender is the buyer) in web3.py. This can be done in Remix using the "At address" option.

Note: I'm using Ganache as the local test network.

At Address option in remix

cuser
  • 113
  • 3

1 Answers1

2

You can interact with any existing contract with web3.py.

  1. You create a new Contract class with web3.eth.contract by passing contract ABI to it

  2. You can create new instances of this new contract class by giving the address of the deployed contract in the constructor

Some example code:

# Load abi from JSON file 
abi = "..."
Purchase = w3.eth.contract(abi=abi)
my_contract = Purchase(contract_address)
my_contract.confirmPurchase().transact({'from': "0x ...my_configured_web3_accoint", value, to_wei("1", "ether")})

You also need to learn how to use web3.py accounts.

More information is in web3.py documentation.

Mikko Ohtamaa
  • 22,269
  • 6
  • 62
  • 127