ERC20 wallet in .NET part 1 – basic

in #erc206 years ago (edited)

Hi, In the next few weeks I would like to present a tutorial helping you to create ERC20 wallet, based on Ethereum using the .NET Framework.
I will post a tutorial on how to develop your own ERC20 wallet. It is a quite time-consuming process and requires explanation of many issues, that's why I decided to split it into few articles:

  1. Basic, configuration and communication in Ethereum network
  2. Creating an account and transferring tokens.
  3. Securing your wallet, adding security features.
  4. … (your further requests)
    In the first part I will try to show the easiest way to create your own wallet for tokens in Ethereum network. There will be minimum security. If you want to do this in a professional manner, you need to focus much more on security aspects.
    What is a token in Ethereum network? A token is a crypto coin in Ethereum network, which is described and managed by a contract. Contract is a computer program run in Ethereum network on EVM. EVM is Ethereum Virtual Machine, which is a virtual machine run on machines in network.

What is ERC20? It is a token standard in Ethereum network. This standard describes that contract should have method below and few events

function totalSupply() public view returns (uint256); /// - function return total amount of token in network.
function balanceOf(address who) public view returns (uint256); /// - function return value of token on address send as parameters.
function transfer(address to, uint256 value) public returns (bool); /// - method to use to send some tokens to other wallet.
function allowance(address owner, address spender) public view returns (uint256); /// - Function to check the amount of tokens that an owner allowed to a spender.
function transferFrom(address from, address to, uint256 value) public returns (bool); /// - Transfer tokens from one address to another
function approve(address spender, uint256 value) public returns (bool); /// - Approve the passed address to spend the specified amount of tokens on behalf of msg.sender.



Before we start we should have access to Ethereum network. I will describe Geth server in this post. Geth is a network server giving us access to Ethereum network.
You can download Geth from the link below:

https://geth.ethereum.org/downloads/

Install it anywhere on your PC. Installation adds path to system environment automatically.

Furthermore, it can be configured in the following way that in Ethereum there in main network and other test network. More information about configuration can be found here: https://github.com/ethereum/go-ethereum/wiki/Command-Line-Options

When you run this in CMD it is going to start a server:

geth.exe --fast --cache=8192 --rpc --rpccorsdomain "http://localhost:8545" --rpcapi "web3,eth,personal,net,miner,admin,debug" console



Description:
--fast Enable fast syncing through state downloads
--cache value Megabytes of memory allocated to internal caching (min 16MB / database forced) (default: 128)
--rpc Enable the HTTP-RPC server
--rpccorsdomain value Comma separated list of domains from which to accept cross origin requests (browser enforced)
--rpcapi value API's offered over the HTTP-RPC interface

When we start the console, it should look like this.

Then start new CMD console and type:

Geth attach http://localhost:8548






When there will be some data, you can type:

web3.net.peerCount



The information about current connected peers will be shown.
Then we should wait until blockchain will synchronize. We can check like this:

web3.eth.getBlock("latest").number



Then we will have last block number, it should be the same as: https://etherscan.io
Synchronization can take some time. It depends on your network connection and your computer's performance.

Let’s start coding- full code is available on GitHub [https://github.com/SebastianDabkowski/Blockchain/tree/master/SD.WalletERC20]

In the project, I use nuget package Nethereum

PM > Install-Package Nethereum.Web3



Application will have 2 inputs textbox and 2 outputs

To get token balance we need token contract address and wallet to address. In result we will have token balance and eth balance.
This is solution:

MainWindows has 1 button refresh current balance of token and eth.

In Ethereum network class we have some method to communicate to Ethereum network:

First we use it is GetTokenBalance in parameter we send wallet address

There is set up connection in base class

In constructor class Web3 we will set URL connection. In this case I use constructor without parameter in default connection to [http://localhost:8545]

Next method execute “balanceOf” from contract, to connect with contract we need 2 things contract address and abi. Abi is contract description in json. As it was on beginning there we have some standard method in ERC20 contract, so in this case I use standard ERC20 ABI

In ABI file we have description on many methods and events. We will use method “balanceOf”
Decryption of JSON we have here[https://github.com/ethereum/wiki/wiki/Ethereum-Contract-ABI#json]
If we have abi file loaded and contract address we can execute method from Ethereum network. In result we will have balance token in wei.
In application I add some extension method to easy display some values.

In result we will have token values

Next method is check ETH balance

To check balance, we have method to do this in Nethereum network and also it returns value in wei. Next, I use converter to see balance in Ethereum. Then on screen we can se balance of token and Ethereum for this wallet.

Every example in this tutorial was executed in ROBSTEN (test Ethereum network).

Sort:  

This post made me realize I need to work a little more on my own posts. Good Job!

thanks, there will be another post soon.

@sebek584, congratulations on making your first post! I gave you an upvote!

Please give me a follow and take a moment to read this post regarding commenting and spam.
(tl;dr - if you spam, you will be flagged!)

Coin Marketplace

STEEM 0.39
TRX 0.12
JST 0.040
BTC 70118.22
ETH 3546.28
USDT 1.00
SBD 4.89