Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Balance Dai
using System;
using System.Numerics;
using System.Threading.Tasks;
using Nethereum.ABI.FunctionEncoding.Attributes;
using Nethereum.Contracts.CQS;
using Nethereum.StandardTokenEIP20;
using Nethereum.Web3;
namespace TestBalance
{
class Program
{
static void Main(string[] args)
{
BalanceAsync().Wait();
BalanceAsync2().Wait();
Console.ReadLine();
}
public static async Task BalanceAsync2()
{
var senderAddress = "0x7345c51439d1C84949f56A6Ad0EcdF3F3f1D2bE2";
var contractAddress = "0x89d24a6b4ccb1b6faa2625fe562bdd9a23260359";
//no private key we are not signing anything
var web3 = new Web3("https://mainnet.infura.io");
var standardToken = new StandardTokenService(web3, contractAddress);
var balance = await standardToken.GetBalanceOfAsync<BigInteger>(senderAddress);
Console.WriteLine("Balance of token: " + Web3.Convert.FromWei(balance));
}
public static async Task BalanceAsync()
{
var senderAddress = "0x7345c51439d1C84949f56A6Ad0EcdF3F3f1D2bE2";
var contractAddress = "0x89d24a6b4ccb1b6faa2625fe562bdd9a23260359";
//no private key we are not signing anything
var web3 = new Web3("https://mainnet.infura.io");
var balanceOfFunctionMessage = new BalanceOfFunction()
{
Owner = senderAddress,
};
var contractHandler = web3.Eth.GetContractHandler(contractAddress);
var balance = await contractHandler.QueryAsync<BalanceOfFunction,BigInteger>(balanceOfFunctionMessage);
Console.WriteLine("Balance of token: " + Web3.Convert.FromWei(balance));
}
[Function("balanceOf", "uint256")]
public class BalanceOfFunction : ContractMessage
{
[Parameter("address", "_owner", 1)]
public string Owner { get; set; }
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.