In this blog post I will cover the process of creating and deploying a smart contract with Nethereum.
Nethereum is an opensource .Net integration library for Ethereum. This library simplifies the access and smart contract interaction with Ethereum nodes.
This article assumes prior understanding of Ethereum. I suggest you have a look at the follow youtube video by Kevin Healy which covers the basics of smart contracts.
The following are the basic setup of creating, deploying and performing a transaction on a contract.
Create a contact
The first task is to create the contract.Ethereum contracts have to created with a language like Solidity, Viper or Serpent. Luckily there are now plugins for both Visual Studio and Visual Studio code to allow use one IDE to manage to process from start to end.
The following is a very simple contract exposing a simple function that will perform a multiplication calculation.
[code]
contract SampleContract {
uint _value;
function calculate(uint val) returns (uint ret) {
return val * _value;
}
}
[/code]
Once compiled in the bin folder we will have two files.
- .bin file. Solidity binary file containing the hex-encoded binary to provide with the transaction request.
- .abi file, which details all of the publicly accessible contract methods and their associated parameters. These details along with the contract address are crucial for interacting with smart contracts
Setup of a private chain.
Now that we have the contract we have setup an environment where we can deploy and mine our contract without spending real ether.
Netherium provides a local test chain with geth which provides a very convenient infrastructure to test our smart contracts.
startget.bat will launch our local test chain. You will need to but the geth binary in the same folder of the script.
Deploy and Mine The Contract
Its now time to write the .net code that will deploy our smart contract on our local test blockchain.
Adding the following nuget package will pull all the dependencies of Nethereum.
nuget install Netherium.Web3.
First step involves setting up the test account parameters. These attributes can be retrieved from accountinfo.txt in the test harness provided by Nethereum.
[code]
var senderAddress = "0x12890d2cce102216644c59daE5baed380d84830c";
var password = "password";
[/code]
and the smart contract abi and bytecode which we have generated in the first step.
[code]
var abi = @"[{""constant"":false,"inputs"":[{""name"":""val"",""type"":""uint256""}],""name"":""calculate"",""outputs"":[{""name"":""ret"",""type"":""uint256""}],""type"":""function""}]";
var byteCode = "0x60606040526040516020806052833950608060405251600081905550602b8060276000396000f3606060405260e060020a60003504631df4f1448114601a575b005b600054600435026060908152602090f3";
[/code]
The first step is to unlock the account for x amount of seconds to allow us to deploy the contract and perform additional operations.
[code]
var unlockAccountRet =
await = web3.Personal.UnlockAccount.SendRequestAsync(senderAddress,
password,
new HexBigInteger(60));
[/code]
Once the account is unlocked we create the transaction to deploy the smart contract. We will need the
abi and the byteCode representation of the contract that was generated in the first step.
[code]
var transHash = await web3.Eth.DeplyContract.SendRequestAsync(abi,
byteCode,
senderAddress);
[/code]
The result of the deployment is the transaction hash which will be used in the future to receive the transaction receipt. The transaction receipt will hold the outcome of our transaction.
In order for the receipt to be generated, the transaction has to be mined. Given we are using a private chain we will need to perform the mining process.
[code]
await web3.Miner.Start.SendRequestAsync(6);
[/code]
Once the contract has been mined we can retrieve the transaction receipt. Important to note that the mining process might take some time so multiple calls to GetTransactionReceipt will be required.
[code]
var receipt = await web3.Eth.Transactions.GetTransactionReceipt.
SendRequestAync(transHash);
[/code]
Call a function within a Transaction
If we wish to validate a function call through the block chain consensus. The function call has to be performed within a transaction, which will than be mined.
In the following code snippet we will create an instance of the contract object from the contract address. Following that we can get access to the function inside the contract and execute it in the context of a Transaction which will then be mined.In the SendTransactionAync we will include the senderAddress which will be charged the gas associated with the operation. (Note that for simplicity I have excluded gas consumption which can be added as a parameter).
[code]
var contractAddress = receipt.ContractAddress;
var contract = web3.Eth.GetContract(abi, contractAddress);
var testFunction = contract.GetFunction("calculate");
transactionHash = await multiplyFunction.
SendTransactionAsync(senderAddress, 7);
[/code]
Follow this the Transaction will have to be mined.
[code]
var receipt = await web3.Eth.Transactions.GetTransactionReceipt
.SendRequestAsync(transactionHash);
[/code]
Important to note that it will take time for the mining of the transaction to complete. Multiple calls to GetTransactionReceipt will be required until the transaction is mined.
And we can done.
In this post we covered the basic concepts of using Smart Contract in .NET with the help of the Netherium library.
Hope this tutorial was helpful. If you need assistance or consultancy feel free to contact me on [email protected].