国产xxxx99真实实拍_久久不雅视频_高清韩国a级特黄毛片_嗯老师别我我受不了了小说

資訊專欄INFORMATION COLUMN

.net平臺下開發以太坊的實戰示例(C#)

marser / 3503人閱讀

摘要:我以后會在這篇文章中添加更多內容,但就目前而言,正在使用它發布的系列示例,介紹如何使用庫執行針對以太坊區塊鏈平臺的最常見任務。

這是一篇文章的早期版本,旨在幫助那些對以太坊區塊鏈平臺有基本了解的開發人員,如果想學習如何使用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 pattern
 static 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:	" + 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:	" + 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:	" + ex.ToString());
 if (ex.InnerException != null) Console.WriteLine("ScanTxExample.Tx:	" + 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:	" + accountAddress.ToString());
 Console.WriteLine("balanceEther:	" + 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:	" + account + " balanceEther:	" + 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:	" + fromAddress.ToString());
 Console.WriteLine("toAddress:	" + toAddress.ToString());
 Console.WriteLine("amountWei:	" + amountWei.ToString());
 Console.WriteLine("sendTxHash:	" + 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:	" + 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:	" + txHash1.ToString());
 var txHash2 = await setMessageFunction.SendTransactionAsync(fromAddress, new HexBigInteger(900000), null, 2, nowTimestamp);
 Console.WriteLine("txHash2:	" + 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:	" + timeoutCount.ToString());

var txReceipt3 = await setMessageFunction.SendTransactionAndWaitForReceiptAsync(fromAddress, new HexBigInteger(900000), null, null, 2, nowTimestamp + " Wait");
 Console.WriteLine("txReceipt3:	" + txReceipt3.TransactionHash.ToString());
 Console.WriteLine("txReceipt3:	" + txReceipt3.CumulativeGasUsed.Value.ToString());

var getResult1 = await getMessageFunction.CallAsync(1);
 Console.WriteLine("getResult1:	" + getResult1.ToString());
 var getResult2 = await getMessageFunction.CallAsync(2);
 Console.WriteLine("getResult2:	" + 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:	" + 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:	" + txHash1.ToString());
 var txHash2 = await setMessageFunction.SendTransactionAsync(fromAddress, new HexBigInteger(900000), null, 2, nowTimestamp);
 Console.WriteLine("txHash2:	" + 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:	" + timeoutCount.ToString());

var txReceipt3 = await setMessageFunction.SendTransactionAndWaitForReceiptAsync(fromAddress, new HexBigInteger(900000), null, null, 2, nowTimestamp + " Wait");
 Console.WriteLine("txReceipt3:	" + txReceipt3.TransactionHash.ToString());
 Console.WriteLine("txReceipt3:	" + txReceipt3.CumulativeGasUsed.Value.ToString());

var getResult1 = await getMessageFunction.CallAsync(1);
 Console.WriteLine("getResult1:	" + getResult1.ToString());
 var getResult2 = await getMessageFunction.CallAsync(2);
 Console.WriteLine("getResult2:	" + getResult2.ToString());

var logMultipliedEvents = await multipliedEvent.GetFilterChanges(filterAllMultipliedEvent);
 foreach (var mea in logMultipliedEvents)
 {
 Console.WriteLine("multipliedEvent:	" +
 mea.Event.sender + " " + mea.Event.oldProduct.ToString() + " " + mea.Event.value.ToString() + " " + mea.Event.newProduct.ToString());
 }

var logNewMessageEvents = await newMessageEvent.GetFilterChanges(filterAllNewMessageEvent);
 foreach (var mea in logNewMessageEvents)
 {
 Console.WriteLine("newMessageEvent:	" +
 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(filterAllNewMessageEvent);
 foreach (var mea in logNewMessageEvents)
 {
 Console.WriteLine("newMessageEvent:	" +
 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:	" + 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:	" + 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 epoch
 System.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 epoch
 System.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];
}
}

我覺得這是發布代碼示例的一種相當簡單粗暴的方式。有任何更好建議歡迎留言。

如果你想馬上使用c#開發以太坊dapp,可以學習下面的教程:

C#以太坊,主要講解如何使用C#開發基于.Net的以太坊應用,包括賬戶管理、狀態與交易、智能合約開發與交互、過濾器和事件等。

這里是原文

文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。

轉載請注明本文地址:http://specialneedsforspecialkids.com/yun/24284.html

相關文章

  • 2018以太坊智能合約編程語言solidity的最佳IDEs

    摘要:使用基于以太坊的智能合約的集成開發環境。以太坊教程,主要介紹智能合約與應用開發,適合入門。以太坊,主要是介紹使用進行智能合約開發交互,進行賬號創建交易轉賬代幣開發以及過濾器和事件等內容。 Solidity是一種以智能合約為導向的編程語言。這是一種只有四年的年輕語言,旨在幫助開發基于以太坊數字貨幣的智能合約。 理解它官方文檔應該是學習Solidity的最佳來源:solidity.read...

    darkerXi 評論0 收藏0
  • 區塊鏈開發中使用的最流行的編程語言

    摘要:我們目前正處于一個新興的區塊鏈開發行業中。,一種在以太坊開發人員中流行的新的簡單編程語言,因為它是用于開發以太坊智能合約的語言。它是全球至少萬開發人員使用的世界上最流行的編程語言之一。以太坊,主要是針對工程師使用進行區塊鏈以太坊開發的詳解。 我們目前正處于一個新興的區塊鏈開發行業中。區塊鏈技術處于初期階段,然而這種顛覆性技術已經成功地風靡全球,并且最近經歷了一場與眾不同的繁榮。由于許多...

    2shou 評論0 收藏0
  • 以太坊將成為新互聯網的支柱

    摘要:以太坊將成為新互聯網的支柱,我為什么這么說正在以太坊上構建我們的第層,而不是其他區塊鏈平臺這就是原因。以太坊不會犧牲去中心化的原則而下沉權力在區塊鏈方面,有一項稱為可擴展性三難的基本法則。 以太坊將成為新互聯網的支柱,我為什么這么說?Loom Network正在以太坊上構建我們的第2層,而不是其他區塊鏈平臺——這就是原因。 每個月都有其他的公司發布白皮書,聲稱已經解決了以太坊所面臨的可...

    liuhh 評論0 收藏0
  • PHP和樹莓派開發一個比特幣/以太坊交易機器人

    摘要:注目前我已經擴展了機器人以便能夠交易以太坊我希望它能夠在不必太多關注的情況下做事,而我想到的第一件事就是為什么不創建一個可以自動進行比特幣和以太坊的簡單交易機器人。在下一個泡沫破裂的時候自動購買比特幣。選擇比特幣以太坊和歐元美元錢包。 我最近得到了Raspberry Pi Zero Wifi,我告訴你這個東西是改變游戲規則的。我之前使用過RasPis,但由于該設備的占地面積小得多,耗電...

    ShowerSun 評論0 收藏0

發表評論

0條評論

marser

|高級講師

TA的文章

閱讀更多
最新活動
閱讀需要支付1元查看
<