A .NET Developer’s Workflow for Creating and Calling Ethereum Smart Contracts

Kevin Small
Apr 3 · 5 min read

There are many great tools available to create Ethereum Smart Contracts. It can be hard to choose between them. In this article, you will learn a simple workflow for developing Solidity smart contracts and calling their functions from C#. This workflow is well suited to .NET developers because it minimises the amount of new tools you need to know about. By using the excellent Nethereum .NET library you can continue to use the Visual Studio set of tools you are already familiar with.

Imagine your goal is to take the contract called SimpleStorage.sol from the Solidity documentation and call its functions from a C# project. Your preference is to use Visual Studio where possible. This article is based on my own experiences working on the Nethereum project to integrate Ethereum with SAP Sales and Distribution business processes.

Workflow Overview

There are many possible workflows to achieve your goal, and as new versions of tools and plugins are released other options will appear. At the time of writing this workflow was found to be simple and quick:

flow
A .NET Developer’s Workflow: Write Solidity and Consume in C#

The diagram above shows these steps:

  1. Write Solidity smart contracts and compile them in Visual Studio Code. The output of the compilation process are some files representing the ABI and bytecode for the contracts.
  2. Use the Nethereum Autogen code generator to automatically build C# API classes that provide access to the smart contracts.
  3. Use Visual Studio to write C# to call methods in the generated C# API classes.

In this article the term function refers to a Solidity function and method refers to a C# method.

Initial Setup

Create Project

In a command prompt, you will create a new .NET core console project that you’ll use to hold all your files:

dotnet new sln --name DevWorkflowExample
dotnet new console --name SimpleStorage
dotnet sln add .\SimpleStorage\SimpleStorage.csproj
cd SimpleStorage
dotnet add package Nethereum.Web3
dotnet add package Nethereum.Autogen.ContractApi

Prepare Visual Studio Code

  1. Open Visual Studio Code.
  2. Open extensions and install the Solidity extension here.
  3. Open the SimpleStorage folder we just created. You should see something like this:
project

4. If at any time, VS Code asks “Required assets to build and debug are missing from ‘SimpleStorage’. Add them?” say yes.

5. Create a new file (ctrl+N).

6. Paste the following solidity code into the file:

pragma solidity >=0.4.0 <0.7.0;

contract SimpleStorage {
uint storedData;

function set(uint x) public {
storedData = x;
}

function get() public view returns (uint) {
return storedData;
}
}

7. Save the file as SimpleStorage.sol in the root of the SimpleStorage folder.

The contract is from the Solidity documentation and you can see it is a very simple contract, just set() and get() functions. Now you are ready to begin the main developer workflow. The steps below correspond to the numbers on the diagram above.

Main Developer Workflow

Step 1 — Compile Smart Contract in Visual Studio Code

In Visual Studio Code, press Shift-Ctrl-P and choose “Solidity: Compile Current Solidity Contract” or press F5. You should see some new files appearing in the SimpleStorage\bin folder, most importantly SimpleStorage.abi and SimpleStorage.bin.

Step 2 — Rebuild the C# Project in Visual Studio

Open the solution DevWorkflowExample.sln in Visual Studio (not Visual Studio Code).

Right-click on the SimpleStorage project and choose “Rebuild”. The act of rebuilding the project triggers the Nethereum.Autogen.ContractApi package to build the C# API classes to let you interact with the SimpleStorage.sol contract. You should see a collection of new files added to the project in a folder called SimpleStorage like this:

project

The generated SimpleStorageService class contains some useful methods:

Notice the C# method naming is different for the set() and get() function calls. This is because set() changes a value on the blockchain, so it costs Ether, so it has be called using an Ethereum transaction, and will return a receipt. The get() function doesn't change any values on the blockchain, so it is a simple call and is free (no transaction and no receipt).

Step 3 — Call Smart Contract functions from C# in Visual Studio

Now you can call functions from your smart contract in the .NET core console program, by making calls to the generated C# classes mentioned in the previous section. For example, paste the code below into Program.cs, replacing everything that is currently there.

Build the project.

The workflow is done! You can now make further edits to the smart contract in Visual Studio Code, compile it there, and simply rebuild the project in Visual Studio to be able to make C# calls to the amended Solidity functions.

Program Execution

Of course, you’d like to check that the program runs successfully. For the project to run, it needs to speak to a blockchain and here you do need a new tool. A good option during development is to run a local blockchain as described here: https://github.com/Nethereum/TestChains#geth.

With a local blockchain running, run the SimpleStorage console project from Visual Studio. You should get output like below:

Contract Deployment Tx Status: 1
Contract Address: 0x243e72b69141f6af525a9a5fd939668ee9f2b354

Sending a transaction to the function set()...
Finished storing an int: Tx Hash: 0xe4c8e72bf18c391c3dd0d18aa4c2ec4672591b974383f7d02120657d766d1bf3
Finished storing an int: Tx Status: 1

Calling the function get()...
Int value: 42 (expecting value 42)

Finished

Where to go from here

The next step in your development process would probably be to add some tests for your Solidity contract. Does this mean you absolutely have to go off and learn Truffle or some other tooling? The answer is no, you don’t. There is an example here of using XUnit test fixtures to launch a local chain before running tests to deploy contracts and call functions.

Note, you don’t have to use the generated SimpleStorageService class to call your smart contract's functions. At the very least, though, it is instructive to see how the calls work in the generated code.

The Autogen code generator is configurable and can also be called from the command line and VSCode.

Credits

Thanks to Vijay055 from the Nethereum Gitter who posted some similar project code as a demo. Thanks to Juan Blanco who founded the Nethereum project, Dave Whiffin for the Autogen code generator package and Gael Blanchemain for reviewing and improving the article content.

Coinmonks

Coinmonks is a technology-focused publication embracing decentralize technologies. We are Non-profit and education is our core value. Learn, Build and thrive. Our other project— https://coinmonks.com, https://cryptofi.co, https://coincodecap.com

Kevin Small

Written by

Visualizations of Blockchain Technologies

Coinmonks

Coinmonks

Coinmonks is a technology-focused publication embracing decentralize technologies. We are Non-profit and education is our core value. Learn, Build and thrive. Our other project— https://coinmonks.com, https://cryptofi.co, https://coincodecap.com