Skip to main content

Deploy a smart contract using Nethereum

In this tutorial, you'll use Nethereum to deploy a smart contract on Quorum Blockchain Service (QBS).

Prerequisites

Steps

  1. Navigate to the Nethereum Playground.

  2. In the Select sample drop-down menu in the upper right corner, choose Smart Contracts: Smart Contracts Deployment, Querying, Transactions, Nonces, Estimating Gas, Gas Price.

  3. In the code sample, on lines 160 and 161, set url to the connection string for your QBS instance and privateKey to the private key of the account to use on QBS. You can acquire the private key by exporting it from MetaMask.

    // ### Instantiating Web3 and the Account
    // To create an instance of web3 we first provide the url of our testchain and the private key of our account.
    // Here we are using http://testchain.nethereum.com:8545 which is our simple single node Nethereum testchain.
    // When providing an Account instantiated with a private key, all our transactions will be signed by Nethereum.

    var url = // the connection string of your QBS instance
    var privateKey = // the private key of your account to use on QBS
    var account = new Account(privateKey);
    var web3 = new Web3(account, url);
    Security warning

    Keep your private keys secret.

    Private keys must be kept secret and not committed to any code respositories. Improper handling of private keys can lead to loss of funds and identity fraud.

    For example, see MyCrypto's Protecting Yourself and Your Funds guide.

  4. On lines 283 and 285, set var estimate and transfer.Gas to 0.

    // ### Estimating Gas

    // Nethereum does an automatic estimation of the total gas necessary to make the function transaction by calling the "EthEstimateGas" internally with the "CallInput".

    // If needed, this can be done manually, using the TransactionHandler and the "transfer" transaction FunctionMessage.

    var estimate = 0; // await transferHandler.EstimateGasAsync(contractAddress, transfer);

    transfer.Gas = 0; // estimate.Value;
    info

    QBS is a zero-gas network.

  5. On lines 310 and 312, set transfer.Gas and transfer.GasPrice to 0.

    // Nethereum internally calls the Ethereum client to set the GasPrice, Nonce and estimate the Gas,
    // so if we want to sign the transaction for the contract completely offline we will need to set those values before hand.
   transfer.Nonce = 2;

transfer.Gas = 0;

transfer.GasPrice = 0; // Nethereum.Web3.Web3.Convert.ToWei(25, UnitConversion.EthUnit.Gwei);

1. Select **Compile and Execute**. View the result in the **Output** box.

```text title="Example deployment output"
Smart contract deployed at address:0xa492a4dabd592e21c2ec9316fe853c878984510
Balance of deployment owner address: 100000
Transaction hash transfer is: 0xfdbdcaeef46f24f66ac6d1151bff31c1b7ee903e6ae02b89a950c749af983972
Balance of deployment owner address after transfer: 99900
Balance of deployment owner address from previous Block Number: 73712 is: 100000
...