Once the basic architecture or the big picture of the blockchain ecosystem -- as explained in Part I of this article -- is understood, we can now move into the nuts-and-bolts of actually building a software application. To do so, we need the following four pieces :
- An Ether wallet - to store Ether and pay it out when necessary
- A Solidity development environment or IDE to build contracts
- A connection service provider that allows client software to connect to the Ethereum network
- A platform or IDE to build a client software that can execute transactions against Solidity contracts residing on the blockchain
1. The Ether Wallet - MetaMask
2. The Remix Ethereum IDE
- We have an employee database consisting of EmpID, EmpName, EmpSalary that is stored in a simple array called database9 that is indexed by the EmpID. The EmpID is the primary key or index and is stored separately in another index array called db9index.
- There are six functions that are defined, namely,
- insertEmp - create new record in the database
- updateEmpSalary - update the salary field
- deleteEmp - delete a record
- getEmpCount - get total number of records
- getEmpData - get record of employee specified by EmpID
- isEmp - to check whether an employee ID exists or not
- The coding tab where you write or edit your Solidity program
- The compile tab where you choose your compiler and compile your code. On successful compilation, go to the bottom and look for a field called ABI. Copy the contents of the field and store it separately. We will need it later for the client application. You can of course come back and copy it later as well.
- The deployment tab that we will study in some detail
- The Gas limit and the wei value can be ignored or left on default.
- Assuming that the compile is successful, you can now press the Deploy button. Now a couple of things will happen
- Since Deployment costs you Ether, MetaMask will seek your confirmation to debit the required number of Ether ( do not panic, it is a miniscule fraction of an Ether) and send it to the network.
- Once you confirm, the deployment transaction will be be submitted and may take a few seconds to complete. You will get a confirmation from MetaMask that you transaction (for deployment, or whatever) has been confirmed.
- If you scroll down you will see details of the contract ( in this case PMCRUDCON) that has been deployed. Copy the contract address - that is more important than the name.
- There is a button to execute each of the functions along with a way to send arguments to the same. Go ahead and insert an employee with name, salary, emp id as 'Narendra Modi', 10000, 100 or something like that.
- Note that the Blue boxes represent Read Only functions that do not require Ether payment. The Brown boxes represent state-changing transactions and for each MetaMask will request you to confirm a payment and only then will the transaction be submitted to the network.
3. Connection Service - infura.io
4. The Python Client
- Install the web3 client with pip. Note that you need to restart the VM after the installation
- Define the contract address, the wallet address and the wallet private key
- Unhide the cell containing ABI variable and place your own ABI variable from Remix there
- Define the contract in terms of its address, and ABI value
- Execute the read_only functions and see the results. Data that you have entered in the Remix client should be visible here.The only way you can get an error is if you have not transferred the following variables correctly
- Endpoint URL from infura.io
- Contract Address & ABI value from Remix
- Wallet Address and Wallet Private Key from MetaMask
This application has demonstrated how to write a basic application that allows us to create, read, update and delete data records into the blockchain. This is primarily what any commercial application does and we have shown how to this with a