Nethereum以太坊开发快速入门

更新时间:2023-06-25 23:58:19 阅读: 评论:0

Nethereum以太坊开发快速⼊门
Nethereum基本上是⽬前唯⼀可⽤的平台下的web3.js移植包。在这个教程中,我们将⾸先编写并部署⼀个简单的智能合约,然后创建⼀个简单的应⽤,并使⽤Nethereum来访问以太坊上的智能合约。Nethereum是通过以太坊节点旳标准RPC接⼝访问智能合约,因此使⽤Nethereum可以对接所有的以太坊节点实现,例如geth或parity。
如果你希望快速掌握Netherem的开发,可以访问汇智⽹的互动教程,技术问题可以直接咨询课程助教。
智能合约开发与部署
⾸先安装开发⽤以太坊节点软件Ganache:
鸿门宴 翻译
~$ npm install -g ganache-cli
然后安装以太坊开发框架Truffle:
~$ npm install -g truffle
现在创建⼀个项⽬⽬录,进⼊该⽬录,并执⾏truffle init进⾏初始化:
~
$ mkdir demo && cd hubwiz
~/hubwiz$ truffle init
truffle会创建⼀些新的⽂件夹:contract、test、migration等。在contract⽂件夹中,创建⼀个新的合约⽂件Vote.sol:
~/hubwiz/contracts$ touch Vote.sol
按如下内容编辑Vote.sol,这个合约只是简单地跟踪两个候选⼈的得票数,它使⽤交易发起账户作为投票⼈,并且每个账户只能投⼀票: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.nder] && (candidate == 1 || candidate == 2));
if(candidate == 1){
candidate1++;
}el{
candidate2++;
}
voted[msg.nder] = true;
}
}
接下来在migration⽂件夹创建⼀个新的js⽂件2_vote.js,内容如下:
var vote = quire("Vote");
葡萄用英语怎么说// deployment steps
deployer.deploy(vote);
bear的意思};
然后打开项⽬⽂件夹下的truffle.js,⽤以下内容替换:
networks: {
ganache: {
host: "127.0.0.1",
port: 7545,
network_id: "*" // Match any network id
}
}
};
现在打开⼀个终端,启动ganache:
~$ ganache-cli
然后打开另⼀个终端,⽤truffle部署合约:
~/hubwiz$ truffle deploy --ret --network ganache
你会看到终端输出类似下⾯的合约地址,拷贝下来,后⾯还要⽤到:Vote: 0xe4e47451aad6c89a6d9e4ad104a7b77ffe1d3b36
.Net应⽤开发与智能合约访问
boss lady创建⼀个新的控制台项⽬,添加对如下开发包的依赖:
Nethereum.Web3
Nethereum.Contracts
然后按如下内容修改program.cs:
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':fal,'stateMutability':'view','type':'functio n'},{'constant':fal,'inputs':[{'name':'candidate','type':'uint256'}],'name':'castVote','outputs':[],'payable':fal,'stateMutability':'nonpayable','type':'function'},{'c onstant':true,'inputs':[],'name':'candi
date2','outputs':[{'name':'','type':'uint256'}],'payable':fal,'stateMutability':'view','type':'function'},{'constant':true,'inputs':[ {'name':'','type':'address'}],'name':'voted','outputs':[{'name':'','type':'bool'}],'payable':fal,'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 urs vote.
int vote = 0;
Console.Write("Press 1 to vote for candidate 1, Press 2 to vote for candidate 2: ");
Int32.TryPar(Convert.ToChar(Console.Read()).ToString(), out vote);
despite的用法Console.WriteLine("You presd {0}", vote);
//Executes the vote on the contract.
六级新题型>at timestry{
zf是什么意思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基于平台, Core应⽤、 Standard应⽤、Xamarin以及各种windows应⽤中。
原⽂链接:

本文发布于:2023-06-25 23:58:19,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/90/157711.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:合约   开发   智能
相关文章
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图