前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >如何在.NET应用中访问以太坊智能合约【Nethereum】

如何在.NET应用中访问以太坊智能合约【Nethereum】

作者头像
用户1408045
发布2019-03-12 09:56:49
1.6K0
发布2019-03-12 09:56:49
举报
文章被收录于专栏:汇智网教程

Nethereum基本上是目前唯一可用的.NET平台下的web3.js移植包。在这个教程中,我们将首先编写并部署一个简单的智能合约,然后创建一个简单的.NET应用,并使用Nethereum来访问以太坊上的智能合约。Nethereum是通过以太坊节点旳标准RPC接口访问智能合约,因此使用Nethereum可以对接所有的以太坊节点实现,例如geth或parity。

智能合约开发与部署

首先安装开发用以太坊节点软件Ganache:

代码语言:javascript
复制
~$ npm install -g ganache-cli

然后安装以太坊开发框架Truffle:

代码语言:javascript
复制
~$ npm install -g truffle

现在创建一个项目目录,进入该目录,并执行truffle init进行初始化:

代码语言:javascript
复制
~$ mkdir demo && cd hubwiz
~/hubwiz$ truffle init

truffle会创建一些新的文件夹:contract、test、migration等。在contract文件夹中,创建一个新的合约文件Vote.sol:

代码语言:javascript
复制
~/hubwiz/contracts$ touch Vote.sol

按如下内容编辑Vote.sol,这个合约只是简单地跟踪两个候选人的得票数,它使用交易发起账户作为投票人,并且每个账户只能投一票:

代码语言:javascript
复制
pragma solidity ^0.4.16;

 contract Vote {

     uint public candidate1;
     uint public candidate2;
     mapping (address => bool) public voted;

     function castVote(uint candidate) public  {
         require(!voted[msg.sender] && (candidate == 1 || candidate == 2));
         if(candidate == 1){
             candidate1++;
         }else{
             candidate2++;            
         }
         voted[msg.sender] = true;
     }
 }

接下来在migration文件夹创建一个新的js文件2_vote.js,内容如下:

代码语言:javascript
复制
var vote = artifacts.require("Vote");

 module.exports = function(deployer) {
   // deployment steps
   deployer.deploy(vote);
 };

然后打开项目文件夹下的truffle.js,用以下内容替换:

代码语言:javascript
复制
module.exports = {
   networks: {
     ganache: {
       host: "127.0.0.1",
       port: 7545,
       network_id: "*" // Match any network id
     }
   }
 };

现在打开一个终端,启动ganache:

代码语言:javascript
复制
~$ ganache-cli

然后打开另一个终端,用truffle部署合约:

代码语言:javascript
复制
~/hubwiz$ truffle deploy --reset --network ganache

你会看到终端输出类似下面的合约地址,拷贝下来,后面还要用到:

代码语言:javascript
复制
Vote: 0xe4e47451aad6c89a6d9e4ad104a7b77ffe1d3b36

.Net应用开发与智能合约访问

创建一个新的控制台项目,添加对如下开发包的依赖:

  • Nethereum.Web3
  • Nethereum.Contracts

然后按如下内容修改program.cs:

代码语言:javascript
复制
using System;
 using System.Numerics;
 using System.Threading.Tasks;
 using Nethereum.Contracts;
 using Nethereum.Hex.HexTypes;
 using Nethereum.Web3;

 namespace console
 {
     class Program
     {
         static void Main(string[] args)
         {
             //The URL endpoint for the blockchain network.
             string url = "HTTP://localhost:7545";

             //The contract address:合约部署的地址
             string address = "0x345cA3e014Aaf5dcA488057592ee47305D9B3e10";

             //The ABI for the contract.
             string ABI = @"[{'constant':true,'inputs':[],'name':'candidate1','outputs':[{'name':'','type':'uint256'}],'payable':false,'stateMutability':'view','type':'function'},{'constant':false,'inputs':[{'name':'candidate','type':'uint256'}],'name':'castVote','outputs':[],'payable':false,'stateMutability':'nonpayable','type':'function'},{'constant':true,'inputs':[],'name':'candidate2','outputs':[{'name':'','type':'uint256'}],'payable':false,'stateMutability':'view','type':'function'},{'constant':true,'inputs':[{'name':'','type':'address'}],'name':'voted','outputs':[{'name':'','type':'bool'}],'payable':false,'stateMutability':'view','type':'function'}]";

             //Creates the connecto to the network and gets an instance of the contract.
             Web3 web3 = new Web3(url);
             Contract voteContract = web3.Eth.GetContract(ABI, address);

             //Reads the vote count for Candidate 1 and Candidate 2
             Task<BigInteger> candidate1Function = voteContract.GetFunction("candidate1").CallAsync<BigInteger>();
             candidate1Function.Wait();
             int candidate1 = (int)candidate1Function.Result;
             Task<BigInteger> candidate2Function = voteContract.GetFunction("candidate2").CallAsync<BigInteger>();
             candidate2Function.Wait();
             int candidate2 = (int)candidate2Function.Result;            
             Console.WriteLine("Candidate 1 votes: {0}", candidate1);
             Console.WriteLine("Candidate 2 votes: {0}", candidate2);

             //Prompts for the account address.
             Console.Write("Enter the address of your account: ");
             string accountAddress = Console.ReadLine();

             //Prompts for the users vote.
             int vote = 0;
             Console.Write("Press 1 to vote for candidate 1, Press 2 to vote for candidate 2: ");
             Int32.TryParse(Convert.ToChar(Console.Read()).ToString(), out vote);
             Console.WriteLine("You pressed {0}", vote);

             //Executes the vote on the contract.
             try{
                 HexBigInteger gas = new HexBigInteger(new BigInteger(400000));
                 HexBigInteger value = new HexBigInteger(new BigInteger(0));                 
                 Task<string> castVoteFunction = voteContract.GetFunction("castVote").SendTransactionAsync(accountAddress, gas, value, vote);
                 castVoteFunction.Wait();
                 Console.WriteLine("Vote Cast!");
             }catch(Exception e){
                 Console.WriteLine("Error: {0}", e.Message);
             }               
         }
     }
 }

别忘了用你自己部署的合约地址修改上面代码中的合约地址。现在运行应用,就可以投票了!

用Nethereum很容易就可以为.Net应用添加访问以太坊智能合约的能力,由于Nethereum基于.NET平台,因此它可以用于.NET Core应用、.NET Standard应用、Xamarin以及各种windows应用中。

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 智能合约开发与部署
  • .Net应用开发与智能合约访问
相关产品与服务
区块链
云链聚未来,协同无边界。腾讯云区块链作为中国领先的区块链服务平台和技术提供商,致力于构建技术、数据、价值、产业互联互通的区块链基础设施,引领区块链底层技术及行业应用创新,助力传统产业转型升级,推动实体经济与数字经济深度融合。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档