![見出し画像](https://assets.st-note.com/production/uploads/images/82876223/rectangle_large_type_2_f954e54227f0f7a3a5880cea0663f7e3.png?width=1200)
Nethereum 入門 (3) - サンプルコードの確認
「Nethereum Playground」の以下の4つのサンプルコードを確認してみます。
・Chain information : アカウントの残高の照会
・Chain information : ブロック番号・ブロック・トランザクション・レシートの取得
・Ether : etherとwei間の単位変換
・Ether : etherの転送
前回
1. Chain information : アカウントの残高の照会
アカウントの残高を紹介するサンプルコードです。
・web3.Eth.GetBalance.SendRequestAsync(address) : 残高の取得。
using System;
using System.Text;
using Nethereum.Hex.HexConvertors.Extensions;
using System.Threading.Tasks;
using Nethereum.Web3;
public class Program
{
static async Task Main(string[] args)
{
// メインネットに接続
// <自身のプロジェクトID>はInfuraで生成した自身のプロジェクトIDと置き換える
var web3 = new Web3("https://mainnet.infura.io/v3/<自身のプロジェクトID>");
// イーサリアム財団のアカウントの残高の確認
var balance = await web3.Eth.GetBalance.SendRequestAsync("0xde0b295669a9fd93d5f28d9ec85e40f4cb697bae");
Console.WriteLine($"Balance in Wei: {balance.Value}");
// weiをetherに変換
var etherAmount = Web3.Convert.FromWei(balance.Value);
Console.WriteLine($"Balance in Ether: {etherAmount}");
}
}
Balance in Wei: 343270355903185816963191
Balance in Ether: 343270.355903185816963191
2. Chain information : ブロック番号・ブロック・トランザクション・レシートの取得
ブロック番号・ブロック・トランザクション・レシートを取得するサンプルコードです。
・web3.Eth.Blocks.GetBlockNumber.SendRequestAsync() : 現在のブロック番号の取得。
・web3.Eth.Blocks.GetBlockWithTransactionsByNumber.SendRequestAsync(blockNumber) : 現在のブロックの取得。
・web3.Eth.Transactions.GetTransactionByHash.SendRequestAsync(hash) : トランザクションの取得。
・web3.Eth.Transactions.GetTransactionReceipt.SendRequestAsync(hash) : レシートの取得。
using System;
using System.Text;
using Nethereum.Hex.HexConvertors.Extensions;
using System.Threading.Tasks;
using Nethereum.Web3;
using Nethereum.RPC.Eth.Blocks;
using Nethereum.Hex.HexTypes;
public class EthRpcCalls_BlockNumber_Block_Transaction_Receipt
{
private static async Task Main(string[] args)
{
// メインネットに接続
// <自身のプロジェクトID>はInfuraで生成した自身のプロジェクトIDと置き換える
var web3 = new Web3("https://mainnet.infura.io/v3/<自身のプロジェクトID>");
// 現在のブロック番号の取得
var blockNumber = await web3.Eth.Blocks.GetBlockNumber.SendRequestAsync();
Console.WriteLine("Current BlockNumber is: " + blockNumber.Value);
// 現在のブロックの取得
var block = await web3.Eth.Blocks.GetBlockWithTransactionsByNumber.SendRequestAsync(new HexBigInteger(8257129));
Console.WriteLine("Block number: " + block.Number.Value); // ブロック番号
Console.WriteLine("Block hash: " + block.BlockHash); // ブロックのHash
Console.WriteLine("Block no of transactions:" + block.Transactions.Length); // トランザクション数
Console.WriteLine("Block transaction 0 From:" + block.Transactions[0].From); // トランザクション0のFrom
Console.WriteLine("Block transaction 0 To:" + block.Transactions[0].To); // トランザクション0のTo
Console.WriteLine("Block transaction 0 Amount:" + block.Transactions[0].Value); // トランザクション0のAmount
Console.WriteLine("Block transaction 0 Hash:" + block.Transactions[0].TransactionHash); // トランザクション0のHash
// トランザクションの取得
var transaction =
await web3.Eth.Transactions.GetTransactionByHash.SendRequestAsync(
"0xb4729a0d8dd30e3070d0cb203090f2b792e029f6fa4629e96d2ebc1de13cb5c4");
Console.WriteLine("Transaction From:" + transaction.From); // トランザクションのFrom
Console.WriteLine("Transaction To:" + transaction.To); // トランザクションのTo
Console.WriteLine("Transaction Amount:" + transaction.Value); // トランザクションのAmount
Console.WriteLine("Transaction Hash:" + transaction.TransactionHash); // トランザクションのHash
// レシートの取得
var transactionReceipt =
await web3.Eth.Transactions.GetTransactionReceipt.SendRequestAsync(
"0xb4729a0d8dd30e3070d0cb203090f2b792e029f6fa4629e96d2ebc1de13cb5c4");
Console.WriteLine("Transaction Hash:" + transactionReceipt.TransactionHash); // トランザクションのHash
Console.WriteLine("TransactionReceipt Logs:" + transactionReceipt.Logs.Count); // トランザクションレシートのログ数
}
}
Current BlockNumber is: 15152506
Block number: 8257129
Block hash: 0xc06bfec6689df458fbc3a30d1632006af9f50ce3144f08f1798900c235ed5865
Block no of transactions:217
Block transaction 0 From:0xec34e830ffb7375ce12a1a0095a781c91bb48005
Block transaction 0 To:0xe7a55436e7abaeecb1c2c21993070197a83a9d15
Block transaction 0 Amount:2000000000000000000
Block transaction 0 Hash:0xb4729a0d8dd30e3070d0cb203090f2b792e029f6fa4629e96d2ebc1de13cb5c4
Transaction From:0xec34e830ffb7375ce12a1a0095a781c91bb48005
Transaction To:0xe7a55436e7abaeecb1c2c21993070197a83a9d15
Transaction Amount:2000000000000000000
Transaction Hash:0xb4729a0d8dd30e3070d0cb203090f2b792e029f6fa4629e96d2ebc1de13cb5c4
Transaction Hash:0xb4729a0d8dd30e3070d0cb203090f2b792e029f6fa4629e96d2ebc1de13cb5c4
TransactionReceipt Logs:0
3. Ether : etherとwei間の単位変換
etherとwei間の単位変換を行うサンプルコードです。
・Web3.Convert.FromWei(balanceInWei) : weiをetherに変換。
・Web3.Convert.ToWei(balanceInEther) : etherをweiに変換。
using System;
using System.Text;
using Nethereum.Hex.HexConvertors.Extensions;
using System.Threading.Tasks;
using Nethereum.Web3;
public class Program
{
private static async Task Main(string[] args)
{
// メインネットに接続
// <自身のプロジェクトID>はInfuraで生成した自身のプロジェクトIDと置き換える
var web3 = new Web3("https://mainnet.infura.io/v3/<自身のプロジェクトID>");
// イーサリアム財団のアカウントの残高の確認
var balance = await web3.Eth.GetBalance.SendRequestAsync("0xde0b295669a9fd93d5f28d9ec85e40f4cb697bae");
Console.WriteLine("Balance of Ethereum Foundation's account: " + balance.Value);
// weiをetherに変換
var balanceInEther = Web3.Convert.FromWei(balance.Value);
Console.WriteLine("Balance of Ethereum Foundation's account in Ether: " + balanceInEther);
// etherをweiに変換
var backToWei = Web3.Convert.ToWei(balanceInEther);
Console.WriteLine("Balance of Ethereum Foundation's account back in Wei: " + backToWei);
}
}
Balance of Ethereum Foundation's account: 343270355903185816963191
Balance of Ethereum Foundation's account in Ether: 343270.355903185816963191
Balance of Ethereum Foundation's account back in Wei: 343270355903185816963191
4. Ether : etherの転送
etherの転送を行うサンプルコードです。
・web3.Eth.GetEtherTransferService().TransferEtherAndWaitForReceiptAsync(account) : etherの転送。
using System;
using System.Text;
using Nethereum.Hex.HexConvertors.Extensions;
using System.Threading.Tasks;
using Nethereum.Web3;
using Nethereum.Web3.Accounts;
public class Program
{
private static async Task Main(string[] args)
{
// 送信元アカウントの準備
var privateKey = "<秘密鍵>"; // 秘密鍵
var chainId = 444444444500; // テストネットに既存のチェーンID
var account = new Account(privateKey, chainId);
Console.WriteLine("Our account: " + account.Address);
// Nethereumテストチェーンを指すアカウントを使用してWeb3インスタンスを生成
var web3 = new Web3(account, "http://testchain.nethereum.com:8545");
web3.TransactionManager.UseLegacyAsDefault = true; // 1559の代わりにレガシーオプションを使用
// 送信先アカウントの残高の確認
var balance = await web3.Eth.GetBalance.SendRequestAsync("0x13f022d72158410433cbd66f5dd8bf6d2d129924");
Console.WriteLine("Receiver account balance before sending Ether: " + balance.Value + " Wei");
Console.WriteLine("Receiver account balance before sending Ether: " + Web3.Convert.FromWei(balance.Value) + " Ether");
// etherを転送
var transaction = await web3.Eth.GetEtherTransferService()
.TransferEtherAndWaitForReceiptAsync("0x13f022d72158410433cbd66f5dd8bf6d2d129924", 1.11m);
// 送信先アカウントの残高の確認
balance = await web3.Eth.GetBalance.SendRequestAsync("0x13f022d72158410433cbd66f5dd8bf6d2d129924");
Console.WriteLine("Receiver account balance after sending Ether: " + balance.Value);
Console.WriteLine("Receiver account balance after sending Ether: " + Web3.Convert.FromWei(balance.Value) + " Ether");
}
}
Our account: 0x94618601FE6cb8912b274E5a00453949A57f8C1e
Receiver account balance before sending Ether: 0 Wei
Receiver account balance before sending Ether: 0 Ether
Receiver account balance after sending Ether: 1110000000000000000
Receiver account balance after sending Ether: 1.11 Ether