Last active
December 14, 2022 01:59
-
-
Save mt-akar/e4ca59f0ef82a6cb7fd3ce30fe3b6e1e to your computer and use it in GitHub Desktop.
Comprehensive web3 SDK for C#
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
var web3 = new Web3("ProjectId", "ProjectSecret"); | |
// Get the chain height | |
var block1 = await web3.EthMainNet.Eth.GetBlockNumberAsync(); | |
var block2 = await web3.Optimism.Eth.GetBlockNumberAsync(); | |
// Query USDT balance | |
var balance1 = await web3.EthMainNet.Erc20.Usdt.Functions.BalanceOf.CallAsync("0x28c6c06298d514db089934071355e5743bf21d60"); | |
var balance2 = await web3.Optimism.Erc20.Usdt.Functions.BalanceOf.CallAsync("0x28c6c06298d514db089934071355e5743bf21d60"); | |
// Query NFT owner | |
var owner = await web3.EthMainNet.Erc721.BoredApesYachtClub.Functions.OwnerOf.CallAsync(2974); | |
// Polymorphism | |
var contracts = new List<Erc20StandardContract>() | |
{ | |
web3.EthMainNet.Erc20.Usdt.Standard, | |
web3.EthMainNet.Erc20.Usdc.Standard, | |
web3.EthMainNet.Erc20.Busd.Standard | |
}; | |
foreach (var contract in contracts) | |
{ | |
var balance = await contract.BalanceOfAsync("0x28c6c06298d514db089934071355e5743bf21d60"); | |
} | |
// Smart contract utilities such as multi-call | |
var results = await web3.EthMainNet.MultiCallAsync(new (Hex Address, bool AllowFailure, Hex CallData)[] | |
{ | |
(web3.EthMainNet.Erc20.ShibaInu.Address, false, "0x06fdde03"), | |
(web3.EthMainNet.Erc721.BoredApesYachtClub.Address, false, web3.EthMainNet.Erc721.BoredApesYachtClub.Functions.OwnerOf.Data("0x692F70593d162dcAa5EB8663AAa4aeB83F4D766F")), | |
("0xd514db089934071355e5743bf21d6028c6c06298", false, "0x06fdde03"), | |
}); | |
// Custom JSON RPC request | |
var result1 = await web3.EthMainNet.JsonRpcAsync("eth_getBalance", new[] {"0x743bf21d6028c6c06298d514db089934071355e5", "latest"}); | |
var result2 = await web3.EthMainNet.JsonRpcAsync(new JsonRpcRequestData | |
{ | |
JsonRpc = "2.0", | |
Method = "eth_getBalance", | |
Params = new[] {"0x743bf21d6028c6c06298d514db089934071355e5", "latest"}, | |
Id = 5, | |
}.ToJson()); | |
// On top of the pure RPC client, this library includes SDKs for other services | |
// Indexed data | |
var txList = await web3.EthMainNet.Index | |
.From("0x3a2206206994597c13d831ec7dac17f958d2ee52", "0x97c13d831ec7dac17f958d2ee523a22062069945") | |
.To("0x7f958d2ee523a2206206994597c13d831ec7dac1") | |
.MinBlock(14_000_000) | |
.MaxBlock(14_000_100) | |
.ToListAsync(); | |
// Real time events over websockets | |
using var newBlockSub = _web3.EthMainNet.Subscriptions.NewBlockHeader.Create(); | |
var evenBlocks = newBlockSub.DataReceived | |
.Where(block => block.Number % 2 == 0) | |
.ToAsyncEnumerable() | |
await foreach (var block in evenBlocks) | |
{ | |
Console.WriteLine($"Block number: {block.Number.HexString}"); | |
} | |
// Market data | |
var price1 = await web3.EthMainNet.PriceAsync(); | |
var price2 = await web3.EthMainNet.Erc20.Matic.Metadata.PriceAsync(); | |
// Static metadata | |
var symbol = web3.EthMainNet.Erc20.Matic.Metadata.Symbol; | |
var logoUrl = web3.EthMainNet.Erc20.Uni.Metadata.LogoUrl; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment