This is a continuation of Part 1 where I described how Safe Remote Purchase works. In this blog post, I will document the process of executing the Safe Remote Purchase Smart Contract using Remix Solidity IDE and MetaMask, running on a Google Chrome browser.
MetatMask, in their own words is "a bridge that allows you to visit the distributed web of tomorrow in your browser today". What this really means is that anyone, with a Metamask Ethereum wallet with some ethers in them, can execute smart contracts on a browser, without first synchronizing an Ethereum wallet to the main blockchain network. This is truly god-send, as I have said in a previous post. Click to read how MetaMask works. To run this set of instructions, you will need to create 2 MetaMask accounts on the Ropsten Test Network. You will need at least 3 ETH in each account. Visit one of these Faucets to pick up the ETH that you need:
These are the ETH that my buyer and seller accounts currently have:
Visit Remix. Copy and paste purchase.sol into the code window. I have made minor changes to the original Safe Remote Purchase source code, mostly so that it compiles in Remix without complaining. Compile your codes.
Make sure that MetaMask is running and your seller's account is currently active. You will know if it is because the account that is currently active will display in the Account field. Seller wishes to stake 2 ETH, which is 2x the amount of ETH that widget he wishes to sell cost. Click [Deploy] to deploy your Smart Contract.
The MetaMask notification pops up. Click [Confirm] to execute the contract.
A link comes up in Remix's message window. Click it to monitor your contract creation process on the Etherscan.io.
You will see a similar Transaction Information if the Contract is successfully executed. Click on the Contract address to see it.
Notice that the Contract's balance field says 2 Ether. This is because the seller created this contract by staking 2 ETH.
We are now ready to assume the role of the buyer. Switch to the buyer's account on MetaMask.
Open a new Remix window. Note that the current active account in the account window is the buyer's instead of the seller's. Paste the address of the Contract created by the seller in the "At Address" field (do not deploy a new contract). Enter 2 ether as the value to stake. Remember, the buyer needs to stake 2x the value that he wishes to pay for the seller's widget. Now click "confirmPurchase".
The Metamask Notification windows pops up. Press [Confirm] to confirm this transaction.
Visit the Contract on Etherscan.io. Notice that the Contract now holds 4 ethers - 2 from the seller and 2 from the buyer.
Assuming that the seller is monitoring his contract on Etherscan and notices that the buyer has deposited 2 ETH in the contract. The seller delivers the widget to the buyer. The buyer receives the widget and presses "confirmReceived".
The contract executes and transfers 1 ETH to the buyer and 3 ETH to the seller.
Note that the seller has received 3 ETH from the contract, 2 of which belongs to him and another 1 ETH from the buyer who bought his widget. The buyer received 1 ETH. The other ETH that he staked when he confirmed his purchase has now gone to the seller.
In "Escrow Service as a Smart Contract", we examined the business logic behind Safe Remote Purchase. We also walked through the steps to execute Smart Contracts in the Remix browser using Ethers from a MetaMask wallet. This process isn't so straightforward mostly because it requires buyers and sellers to copy and paste codes. In a more user friendly environment, there would have been a slick user interface in the form of a Distributed Application (or DApp) on a web browser. I have previously written a post on how this is done.