这是一篇文章的早期版本,旨在帮助那些对以太坊区块链平台有基本了解的开发人员,如果想学习如何使用Nethereum .NET库对Ethereum平台进行编程。

我以后会在这篇文章中添加更多内容,但就目前而言,正在使用它发布.NET 10+的系列C#示例,介绍如何使用Nethereum库执行针对以太坊区块链平台的最常见任务。

最常见任务列表包括以下内容:

  • 1.获取协议版本
  • 2.获取最大块数
  • 3.获取帐户余额
  • 4.发送以太
  • 5.等待交易收据
  • 6.在区块链中扫描块
  • 7.列出个人账户(及其余额)
  • 8.在区块链中扫描交易
  • 9.与现有(已部署)合同互动
  • 10.用事件和现有(已部署)合约交互
  • 11.获取所有活动更改
  • 12.获取合约金额历史

这里是c#的主文件:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;using Nethereum.Hex.HexTypes;
using Nethereum.Web3;namespace MWH.MyNethereum.QuickRef
{static public class TaskExamples{const int UNLOCK_TIMEOUT = 2 * 60; // 2 minutes (arbitrary)const int SLEEP_TIME = 5 * 1000; // 5 seconds (arbitrary)const int MAX_TIMEOUT = 2 * 60 * 1000; // 2 minutes (arbirtrary)// These static public variables do not represent a recommended patternstatic public string LastProtocolVersion = "";static public string LastTxHash = "";static public Nethereum.RPC.Eth.DTOs.TransactionReceipt LastTxReceipt = null;static public HexBigInteger LastMaxBlockNumber = new HexBigInteger(0);static public async Task GetProtocolVersionExample(Web3 web3){Console.WriteLine("GetProtocolVersionExample:");var protocolVersion = await web3.Eth.ProtocolVersion.SendRequestAsync();Console.WriteLine("protocolVersion:\t" + protocolVersion.ToString());LastProtocolVersion = protocolVersion;}static public async Task GetMaxBlockExample(Web3 web3){Console.WriteLine("GetMaxBlockExample:");var maxBlockNumber = await web3.Eth.Blocks.GetBlockNumber.SendRequestAsync();Console.WriteLine("maxBlockNumber:\t" + maxBlockNumber.Value.ToString());LastMaxBlockNumber = maxBlockNumber;}static public async Task ScanBlocksExample(Web3 web3, ulong startBlockNumber, ulong endBlockNumber){Console.WriteLine("ScanBlocksExample:");long txTotalCount = 0;for (ulong blockNumber = startBlockNumber; blockNumber <= endBlockNumber; blockNumber++){var blockParameter = new Nethereum.RPC.Eth.DTOs.BlockParameter(blockNumber);var block = await web3.Eth.Blocks.GetBlockWithTransactionsByNumber.SendRequestAsync(blockParameter);var trans = block.Transactions;int txCount = trans.Length;txTotalCount += txCount;if (blockNumber % 1000 == 0) Console.Write(".");if (blockNumber % 10000 == 0){DateTime blockDateTime = Helpers.UnixTimeStampToDateTime((double)block.Timestamp.Value);Console.WriteLine(blockNumber.ToString() + " " + txTotalCount.ToString() + " " + blockDateTime.ToString());}}Console.WriteLine();}static public async Task ScanTxExample(Web3 web3, ulong startBlockNumber, ulong endBlockNumber){Console.WriteLine("ScanTxExample:");long txTotalCount = 0;for (ulong blockNumber = startBlockNumber; blockNumber <= endBlockNumber; blockNumber++){var blockParameter = new Nethereum.RPC.Eth.DTOs.BlockParameter(blockNumber);var block = await web3.Eth.Blocks.GetBlockWithTransactionsByNumber.SendRequestAsync(blockParameter);var trans = block.Transactions;int txCount = trans.Length;txTotalCount += txCount;foreach (var tx in trans){try{var bn = tx.BlockNumber.Value;var th = tx.TransactionHash;var ti = tx.TransactionIndex.Value;var rpt = await web3.Eth.Transactions.GetTransactionReceipt.SendRequestAsync(th);var status = rpt.Status.Value;var nc = tx.Nonce.Value;var from = tx.From;Console.WriteLine(th.ToString() + " " + ti.ToString() + " " + from.ToString() + " " + status.ToString());var to = tx.To;if (to == null) to = "to:NULL";var v = tx.Value.Value;var g = tx.Gas.Value;var gp = tx.GasPrice.Value;Console.WriteLine(th.ToString() + " " + ti.ToString() + " " + nc.ToString() + " " + from.ToString() + " " + to.ToString() + " " + v.ToString() + " " + g.ToString() + " " + gp.ToString());}catch (Exception ex){Console.WriteLine("ScanTxExample.Tx:\t" + ex.ToString());if (ex.InnerException != null) Console.WriteLine("ScanTxExample.Tx:\t" + ex.InnerException.ToString());}}Console.WriteLine();}}static public async Task GetAccountBalanceExample(Web3 web3, string accountAddress){Console.WriteLine("GetAccountBalanceExample:");var balanceWei = await web3.Eth.GetBalance.SendRequestAsync(accountAddress);var balanceEther = Web3.Convert.FromWei(balanceWei.Value);Console.WriteLine("accountAddress:\t" + accountAddress.ToString());Console.WriteLine("balanceEther:\t" + balanceEther.ToString());}static public async Task ListPersonalAccountsExample(Web3 web3){Console.WriteLine("ListPersonalAccountsExample:");var accounts = await web3.Personal.ListAccounts.SendRequestAsync();foreach (var account in accounts){var balanceWei = await web3.Eth.GetBalance.SendRequestAsync(account);var balanceEther = Web3.Convert.FromWei(balanceWei.Value);Console.WriteLine("account:\t" + account + " balanceEther:\t" + balanceEther.ToString());}}static public async Task SendEtherExample(Web3 web3, string fromAddress, string fromPassword, string toAddress, long amountWei){Console.WriteLine("SendEtherExample:");var unlockResult = await web3.Personal.UnlockAccount.SendRequestAsync(fromAddress, fromPassword, UNLOCK_TIMEOUT);var sendTxHash = await web3.Eth.TransactionManager.SendTransactionAsync(fromAddress, toAddress, new HexBigInteger(amountWei));Console.WriteLine("fromAddress:\t" + fromAddress.ToString());Console.WriteLine("toAddress:\t" + toAddress.ToString());Console.WriteLine("amountWei:\t" + amountWei.ToString());Console.WriteLine("sendTxHash:\t" + sendTxHash.ToString());LastTxHash = sendTxHash;}static public async Task WaitForTxReceiptExample(Web3 web3, string txHash){Console.WriteLine("WaitForTxReceiptExample:");int timeoutCount = 0;var txReceipt = await web3.Eth.Transactions.GetTransactionReceipt.SendRequestAsync(txHash);while (txReceipt == null && timeoutCount < MAX_TIMEOUT){Console.WriteLine("Sleeping...");Thread.Sleep(SLEEP_TIME);txReceipt = await web3.Eth.Transactions.GetTransactionReceipt.SendRequestAsync(txHash);timeoutCount += SLEEP_TIME;}Console.WriteLine("timeoutCount " + timeoutCount.ToString());LastTxReceipt = txReceipt;}static public async Task InteractWithExistingContractExample(Web3 web3, string fromAddress, string fromPassword, string contractAddress, string contractAbi){Console.WriteLine("InteractWithExistingContractExample:");var contract = web3.Eth.GetContract(contractAbi, contractAddress);var setMessageFunction = contract.GetFunction("setMsg");var getMessageFunction = contract.GetFunction("getMsg");string nowTimestamp = DateTime.UtcNow.ToString() + " UTC";Console.WriteLine("now:\t" + nowTimestamp);var unlockResult = await web3.Personal.UnlockAccount.SendRequestAsync(fromAddress, fromPassword, UNLOCK_TIMEOUT);var txHash1 = await setMessageFunction.SendTransactionAsync(fromAddress, new HexBigInteger(900000), null, 1, "Hello World");Console.WriteLine("txHash1:\t" + txHash1.ToString());var txHash2 = await setMessageFunction.SendTransactionAsync(fromAddress, new HexBigInteger(900000), null, 2, nowTimestamp);Console.WriteLine("txHash2:\t" + txHash2.ToString());var txReceipt2 = await web3.Eth.Transactions.GetTransactionReceipt.SendRequestAsync(txHash2);int timeoutCount = 0;while (txReceipt2 == null && timeoutCount < MAX_TIMEOUT){Console.WriteLine("Sleeping...");Thread.Sleep(SLEEP_TIME);txReceipt2 = await web3.Eth.Transactions.GetTransactionReceipt.SendRequestAsync(txHash2);timeoutCount += SLEEP_TIME;}Console.WriteLine("timeoutCount:\t" + timeoutCount.ToString());var txReceipt3 = await setMessageFunction.SendTransactionAndWaitForReceiptAsync(fromAddress, new HexBigInteger(900000), null, null, 2, nowTimestamp + " Wait");Console.WriteLine("txReceipt3:\t" + txReceipt3.TransactionHash.ToString());Console.WriteLine("txReceipt3:\t" + txReceipt3.CumulativeGasUsed.Value.ToString());var getResult1 = await getMessageFunction.CallAsync<string>(1);Console.WriteLine("getResult1:\t" + getResult1.ToString());var getResult2 = await getMessageFunction.CallAsync<string>(2);Console.WriteLine("getResult2:\t" + getResult2.ToString());}static public async Task InteractWithExistingContractWithEventsExample(Web3 web3, string fromAddress, string fromPassword, string contractAddress, string contractAbi){Console.WriteLine("InteractWithExistingContractWithEventsExample:");var contract = web3.Eth.GetContract(contractAbi, contractAddress);var setMessageFunction = contract.GetFunction("setMsg");var getMessageFunction = contract.GetFunction("getMsg");var multipliedEvent = contract.GetEvent("MultipliedEvent");var newMessageEvent = contract.GetEvent("NewMessageEvent");var filterAllMultipliedEvent = await multipliedEvent.CreateFilterAsync();var filterAllNewMessageEvent = await newMessageEvent.CreateFilterAsync();string nowTimestamp = DateTime.UtcNow.ToString() + " UTC";Console.WriteLine("now:\t" + nowTimestamp);var unlockResult = await web3.Personal.UnlockAccount.SendRequestAsync(fromAddress, fromPassword, UNLOCK_TIMEOUT);var txHash1 = await setMessageFunction.SendTransactionAsync(fromAddress, new HexBigInteger(900000), null, 1, "Hello World");Console.WriteLine("txHash1:\t" + txHash1.ToString());var txHash2 = await setMessageFunction.SendTransactionAsync(fromAddress, new HexBigInteger(900000), null, 2, nowTimestamp);Console.WriteLine("txHash2:\t" + txHash2.ToString());var txReceipt2 = await web3.Eth.Transactions.GetTransactionReceipt.SendRequestAsync(txHash2);int timeoutCount = 0;while (txReceipt2 == null && timeoutCount < MAX_TIMEOUT){Console.WriteLine("Sleeping...");Thread.Sleep(SLEEP_TIME);txReceipt2 = await web3.Eth.Transactions.GetTransactionReceipt.SendRequestAsync(txHash2);timeoutCount += SLEEP_TIME;}Console.WriteLine("timeoutCount:\t" + timeoutCount.ToString());var txReceipt3 = await setMessageFunction.SendTransactionAndWaitForReceiptAsync(fromAddress, new HexBigInteger(900000), null, null, 2, nowTimestamp + " Wait");Console.WriteLine("txReceipt3:\t" + txReceipt3.TransactionHash.ToString());Console.WriteLine("txReceipt3:\t" + txReceipt3.CumulativeGasUsed.Value.ToString());var getResult1 = await getMessageFunction.CallAsync<string>(1);Console.WriteLine("getResult1:\t" + getResult1.ToString());var getResult2 = await getMessageFunction.CallAsync<string>(2);Console.WriteLine("getResult2:\t" + getResult2.ToString());var logMultipliedEvents = await multipliedEvent.GetFilterChanges<FunctionOutputHelpers.MultipliedEventArgs>(filterAllMultipliedEvent);foreach (var mea in logMultipliedEvents){Console.WriteLine("multipliedEvent:\t" +mea.Event.sender + " " + mea.Event.oldProduct.ToString() + " " + mea.Event.value.ToString() + " " + mea.Event.newProduct.ToString());}var logNewMessageEvents = await newMessageEvent.GetFilterChanges<FunctionOutputHelpers.NewMessageEventArgs>(filterAllNewMessageEvent);foreach (var mea in logNewMessageEvents){Console.WriteLine("newMessageEvent:\t" +mea.Event.sender + " " + mea.Event.ind.ToString() + " " + mea.Event.msg.ToString());}}static public async Task GetAllChangesExample(Web3 web3, string fromAddress, string fromPassword, string contractAddress, string contractAbi){Console.WriteLine("GetAllChangesExample:");var contract = web3.Eth.GetContract(contractAbi, contractAddress);var newMessageEvent = contract.GetEvent("NewMessageEvent");var filterAllNewMessageEvent = await newMessageEvent.CreateFilterAsync(fromAddress);var logNewMessageEvents = await newMessageEvent.GetAllChanges<FunctionOutputHelpers.NewMessageEventArgs>(filterAllNewMessageEvent);foreach (var mea in logNewMessageEvents){Console.WriteLine("newMessageEvent:\t" +mea.Event.sender + " " + mea.Event.ind.ToString() + " " + mea.Event.msg.ToString());}}static public async Task GetContractValuesHistoryUniqueOffsetValueExample(Web3 web3, string contractAddress, HexBigInteger recentBlockNumber, ulong numberBlocks, int offset){Console.WriteLine("GetContractValuesHistoryUniqueOffsetValueExample:");string previousValue = "";for (ulong blockNumber = (ulong)recentBlockNumber.Value; blockNumber > (ulong)recentBlockNumber.Value - numberBlocks; blockNumber--){var blockNumberParameter = new Nethereum.RPC.Eth.DTOs.BlockParameter(blockNumber);var valueAtOffset = await web3.Eth.GetStorageAt.SendRequestAsync(contractAddress, new HexBigInteger(offset), blockNumberParameter);if (valueAtOffset != previousValue){var block = await web3.Eth.Blocks.GetBlockWithTransactionsByNumber.SendRequestAsync(blockNumberParameter);DateTime blockDateTime = Helpers.UnixTimeStampToDateTime((double)block.Timestamp.Value);Console.WriteLine("blockDateTime:\t" + blockDateTime.ToString());for (int storageOffset = 0; storageOffset < offset+2; storageOffset++){var valueAt = await web3.Eth.GetStorageAt.SendRequestAsync(contractAddress, new HexBigInteger(storageOffset), blockNumberParameter);Console.WriteLine("value:\t" + blockNumber.ToString() + " " + storageOffset.ToString() + " " + valueAt + " " + Helpers.ConvertHex(valueAt.Substring(2)));}previousValue = valueAtOffset;}}}}
}

以下是一些支持文件:

FunctionOutputHelpers.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;using Nethereum.Web3;
using Nethereum.ABI.FunctionEncoding.Attributes;namespace MWH.MyNethereum.QuickRef
{static public class FunctionOutputHelpers{// event MultipliedEvent(// address indexed sender,// int oldProduct,// int value,// int newProduct// );[FunctionOutput]public class MultipliedEventArgs{[Parameter("address", "sender", 1, true)]public string sender { get; set; }[Parameter("int", "oldProduct", 2, false)]public int oldProduct { get; set; }[Parameter("int", "value", 3, false)]public int value { get; set; }[Parameter("int", "newProduct", 4, false)]public int newProduct { get; set; }}//event NewMessageEvent(// address indexed sender,// uint256 indexed ind,// string msg//);[FunctionOutput]public class NewMessageEventArgs{[Parameter("address", "sender", 1, true)]public string sender { get; set; }[Parameter("uint256", "ind", 2, true)]public int ind { get; set; }[Parameter("string", "msg", 3, false)]public string msg { get; set; }}}
}

Helpers.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace MWH.MyNethereum.QuickRef
{static public class Helpers{public static string ConvertHex(String hexString){try{string ascii = string.Empty;for (int i = 0; i < hexString.Length; i += 2){String hs = string.Empty;hs = hexString.Substring(i, 2);uint decval = System.Convert.ToUInt32(hs, 16);char character = System.Convert.ToChar(decval);ascii += character;}return ascii;}catch (Exception ex) { Console.WriteLine(ex.Message); }return string.Empty;}public static DateTime UnixTimeStampToDateTime(double unixTimeStamp){// Unix timestamp is seconds past epochSystem.DateTime dtDateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, System.DateTimeKind.Utc);dtDateTime = dtDateTime.AddSeconds(unixTimeStamp); // .ToLocalTime();return dtDateTime;}public static DateTime JavaTimeStampToDateTime(double javaTimeStamp){// Java timestamp is milliseconds past epochSystem.DateTime dtDateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, System.DateTimeKind.Utc);dtDateTime = dtDateTime.AddMilliseconds(javaTimeStamp); // .ToLocalTime();return dtDateTime;}}
}

以下是用于实施这些示例时使用的以太坊合约的Solidity代码:

Test3.sol

pragma solidity 0.4.19;contract Test3 {int public _product;
string[5] _msgs;event MultipliedEvent(
address indexed sender,
int oldProduct,
int value,
int newProduct
);event NewMessageEvent(
address indexed sender,
uint256 indexed ind,
string msg
);function Test3() public {
_product = 1;
}function multiply(int value) public returns(int product) {
int old = _product;
_product = value * _product;
MultipliedEvent( msg.sender, old, value, _product );
return _product;
}function getProduct() public constant returns(int product) {
return _product;
}function setMsg(uint256 i, string m) public returns(uint256 mi) {
_msgs[i] = m;
NewMessageEvent( msg.sender, i, m);
return -i;
}function getMsg(uint256 index) public constant returns(string m) {
return _msgs[index];
}
}

我觉得这是发布代码示例的一种相当简单粗暴的方式。有任何更好建议欢迎留言。

转载于:https://www.cnblogs.com/malakashi/p/9602657.html

c#使用类库nthereum在.net上开发以太坊的实战示例相关推荐

  1. java和以太坊交互_java类库web3j开发以太坊智能合约快速入门

    web3j简介 web3j是一个轻量级.高度模块化.响应式.类型安全的Java和Android类库提供丰富API,用于处理以太坊智能合约及与以太坊网络上的客户端(节点)进行集成. 可以通过它进行以太坊 ...

  2. python字符串类库_Python开发以太坊的类库Web3.py V4的新功能

    Web3.py第4版的第一个测试版于2017年11月中旬发布,此后又发布了5个版本,目前已经稳定发布到4.6.主要版本变化意味着一些向后兼容的更改,以及这些更改启用的一些全新功能.感兴趣的话请继续阅读 ...

  3. 在MacOS上构建以太坊开发环境

    以太坊是流行的区块链去中心化平台.作为本文的一部分,你将了解如何在MacOS平台上设置以太坊. 首先需要对区块链和以太坊有基本理解,即什么是以太坊之类的? 工具集 以太坊开发生态系统包括以下工具列表. ...

  4. JAVA使用web3j开发以太坊实战案例

    JAVA使用web3j开发以太坊实战案例 必读 1.前言 2.基础(必看) 3.web3j引入 4.创建账户(离线创建) 5.geth节点搭建.基本使用及一般问题 6.常量类 后面内容都要用到(必备! ...

  5. 以太坊去中心化_开发以太坊去中心化投票应用程序的指南

    以太坊去中心化 by Timothy Ko 蒂莫西·高(Timothy Ko) 开发以太坊去中心化投票应用程序的指南 (A guide to developing an Ethereum decent ...

  6. php开发以太坊无法连接到远程Geth,connect: permission denied.

    2019独角兽企业重金招聘Python工程师标准>>> 我在安装了Geth节点的ubuntu服务器X上创建了一个shell脚本,在测试链中插入智能合约数据.此外,我通过使用Shell ...

  7. 如何把一些不同类型的数据混合存入一片内存中_如何从技术上增强以太坊的隐私性?...

    以太坊的隐私性非常重要,混合器.计算数据层.零知识证明等只是一些用于增强以太坊区块链上隐私性的技术. 图片来源:pixabay 公共区块链网络的隐私是一个矛盾体,因为根据技术设计的性质,数据必须在网络 ...

  8. solidity开发以太坊代币智能合约

    智能合约开发是以太坊编程的核心之一,而代币是区块链应用的关键环节,下面我们来用solidity语言开发一个代币合约的实例,希望对大家有帮助. 以太坊的应用被称为去中心化应用(DApp),DApp的开发 ...

  9. 使用web3和infura开发以太坊ethereum区块链

    web3 Github: https://github.com/ethereum/web3.js/ web3.js是以太坊提供的一个Javascript库,它封装了以太坊的RPC通信API,提供了一系 ...

  10. 使用hardhat开发以太坊智能合约-测试合约

    Web3工具网站[点我访问] 现已上线,欢迎使用,如有好的意见和建议也欢迎反馈. 本系列课程: 第一节:使用hardhat开发以太坊智能合约-搭建环境 第二节:使用hardhat开发以太坊智能合约-测 ...

最新文章

  1. 导师发现我刷短视频,给我发了一条链接
  2. 【权游相关】龙的简史 | 混乱博物馆
  3. 解决WPF中重载Window.OnRender函数失效问题
  4. 手动封装js的call和apply和bind和typeof和new方法
  5. 为下半年圣诞节提前准备素材模板,可临摹psd分层模板!
  6. python代码示例-总算知道python入门代码示例
  7. HDU-3729 I'm Telling the Truth
  8. 信号与系统(关于流程框图的一个简单问题记录)
  9. 美团2021校招笔试-编程题题解
  10. ftp工具绿色版,四款好用的绿色版ftp工具
  11. cycJava基础以及集合总结
  12. 小米note3的开发者选项在哪里?怎么进入开发者模式?如何显示布局边界?
  13. java sorted 方法_多个.sorted(…)调用java 8流
  14. Comparing the Effects of DNS, DoT, and DoH
  15. C++递归算法之分成互质组
  16. Adapter的notifyDataSetChange无效的问题
  17. 一个二维表查询的实现
  18. html中复选文本框标签,JavaScript:HTML中的UI-标签
  19. MIPI CSI-2笔记(2) -- 物理层Physical Layer
  20. 利用gdal的RasterIO进行最近邻、双线性、三次卷积重采样的重采样

热门文章

  1. 多线程之线程通信条件Condition二
  2. Spring Security OAuth 个性化token 1
  3. session的生命周期是怎样的
  4. 去除A和B数组中的交集
  5. AMD已秒英特尔全家?真的吗?来看看真正公平公正的版本吧!
  6. Dialog的半透明背景的灰度
  7. MFC(6)点击按钮弹出新的对话框源码
  8. 艾伟:WinForm控件开发总结(三)------认识WinForm控件常用的Attribute
  9. 王乐园 php_见到海贼王主题乐园的雕像,感觉认知回不去了,索隆是认真的?...
  10. 同时安装python2和python3