How to test a smart contract using Remix ide – Pradeep

How to test a smart contract using Remix ide
In this article we’ll look at a “Fund proposal” (for demo only) smart contract which can be used to raise funds for proposals.
User roles
The 3 roles played are
- Organiser — An account used to create the fund raising smart contract
- Proposer- An account used to create a proposal
- Donor- An account used to donate ether for a proposal
There can be multiple proposals and multiple donors for each proposal.
For simplicity we’ll demo 1 proposal and 1 donor.
Smart contract
The FundProposal smart contract has the below functions
- registerProposal — To register a proposal
- registerDonor —Donor registers interest in a proposal
- receive- Donor sends ether via this smart contract to the proposer.
Smart code code : https://github.com/gogit/remix-smart-contract/blob/master/FundProposal.sol
The Remix ide will be used for compiling, deploying and testing the smart contract.
Remix ide
Remix ide is a concise and seemingly complete environment to test your smart contract. It provides code completion, compilation, deployment, test function calls etc via plugins.
Installing and Running Remix
git clone https://github.com/ethereum/remix-ide.git
cd remix-ide
npm install
npm start
open http://127.0.0.1:8080 in your browser.
Steps
- Create the smart contract
- Compile it
- Deploy it
- Create a proposal
- Look at the transactions so far
- Register interest for donating funds
- Donate funds
- Check funds in accounts
Create the smart contract
Click on the + sign and create a new file called FundProposal.sol. Copy the code from https://raw.githubusercontent.com/gogit/remix-smart-contract/master/FundProposal.sol and paste
Compile it
Click on compile to compile the smart contract
Deploy the smart contract
Deploy the smart contract using the first account in the list. After the deployment you’ll see a small decrement in the ether allocated to the first account.
Create a proposal
Use the second account (from the dropdown) to create a proposal using the function “registerProposal”. Once again the account loses a small amount of ether after the transaction.
Look at the transactions so far
Click on transactions recorded and save transactions to see the transaction records. You can also see the accounts involved in the transactions.
Copy the value associated with account[1] (the Proposer 0x147…). You’ll need the proposers address to register as a donor (for this demo).
Register interest for donating funds
Select the 3rd account from the dropdown and use the Proposer address to register as a donor. The account again loses a small amount of ether for this transaction.
Donate funds
FundProposal smart contract has been created using account 1. A proposal has been created using account 2 and a donor registered for this proposal using account 3.
The donor can now send ether to the smart contract that is transferred to the proposal/proposer
- Send 30 ether
- Send 40 ether
Select account 3, enter the value and scroll all the way down and click on transact
Check funds in accounts
Account 3 (Donor) has transferred 70 ether to account 2 (Proposer)
We have looked at the process involved in creating a smart contract and testing it.
There are loads of gotchas and magic keywords that drive behavior. The links below should be useful
https://github.com/ethereum/
Published at Thu, 06 Feb 2020 14:22:49 +0000
{flickr|100|campaign}
