This simple sample demonstrates how to simple transfer Ether and the Deployment of a StandardTokenContract, Transfer of a token, Query the balance of a token and finally retrieve Events from Ethereum.
This sample uses the latest version of Nethereum which you can download from the releases.
To transfer Ether Nethereum provides a specific Unity Request, the EthTransferUnityRequest
.
The EthTransferUnityRequest it is instantiated with the "url" of our Ethereum client, the private key to be able to sign transactions and our account address (the same of the private key).
var url = "http://localhost:8545";
var privateKey = "0xb5b1870957d373ef0eeffecc6e4812c0fd08f554b37b233526acc331bf1544f7";
var account = "0x12890d2cce102216644c59daE5baed380d84830c";
var ethTransfer = new EthTransferUnityRequest(url, privateKey, account);
Once our unity request is instantiated it we can initiate the transfer as follows:
var receivingAddress = "0xde0B295669a9FD93d5F28D9Ec85E40f4cb697BAe";
yield return ethTransfer.TransferEther(receivingAddress, 1.1m, 2);
Here we have specified the receivingAddress, the amount to send and the optional gas price in Gwei. The request will automatically convert the gas price to Wei.
We can validate afterwards if we have had any exception as following:
if (ethTransfer.Exception != null)
{
Debug.Log(ethTransfer.Exception.Message);
yield break;
}
If no errors have occurred we can retrieve the transaction hash from the Request and Poll every 2 seconds to wait for the transaction to be mined.
var transactionHash = ethTransfer.Result;
//create a poll to get the receipt when mined
var transactionReceiptPolling = new TransactionReceiptPollingRequest(url);
//checking every 2 seconds for the receipt
yield return transactionReceiptPolling.PollForReceipt(transactionHash, 2);
Finally we can check the balance of our recieving account, using EthGetBalanceUnityRequest
. Note that we specify we want the balance for the latest Block when doing the request.
var balanceRequest = new EthGetBalanceUnityRequest(url);
yield return balanceRequest.SendRequest(receivingAddress, BlockParameter.CreateLatest());
We can convert the result in Wei to Eth using the default Wei UnitConvertor.
Debug.Log("Balance of account:" + UnitConversion.Convert.FromWei(balanceRequest.Result.Value));
public IEnumerator TransferEther()
{
var url = "http://localhost:8545";
var privateKey = "0xb5b1870957d373ef0eeffecc6e4812c0fd08f554b37b233526acc331bf1544f7";
var account = "0x12890d2cce102216644c59daE5baed380d84830c";
//initialising the transaction request sender
var ethTransfer = new EthTransferUnityRequest(url, privateKey, account);
var receivingAddress = "0xde0B295669a9FD93d5F28D9Ec85E40f4cb697BAe";
yield return ethTransfer.TransferEther("0xde0B295669a9FD93d5F28D9Ec85E40f4cb697BAe", 1.1m, 2);
if (ethTransfer.Exception != null)
{
Debug.Log(ethTransfer.Exception.Message);
yield break;
}
var transactionHash = ethTransfer.Result;
Debug.Log("Transfer transaction hash:" + transactionHash);
//create a poll to get the receipt when mined
var transactionReceiptPolling = new TransactionReceiptPollingRequest(url);
//checking every 2 seconds for the receipt
yield return transactionReceiptPolling.PollForReceipt(transactionHash, 2);
Debug.Log("Transaction mined");
var balanceRequest = new EthGetBalanceUnityRequest(url);
yield return balanceRequest.SendRequest(receivingAddress, BlockParameter.CreateLatest());
Debug.Log("Balance of account:" + UnitConversion.Convert.FromWei(balanceRequest.Result.Value));
}