[C#] 透過 ethplorer.io 取得錢包資產簡單易上手

2022-02-25

最近要查 ERC20 鏈上面 USDT 餘額,於是我問一下同事有沒有好的地方可以查,非常懶得自己架節點

於是 好友11 就推薦這個 https://ethplorer.io/zh/

基本上很簡單,去註冊後,驗證電話就可以拿到 token 他的配額也還夠用

一天有 756,000 次查詢的配額,小專案應該蠻夠用的


而且 API 也完整 https://github.com/EverexIO/Ethplorer/wiki/Ethplorer-API

今天大概就來測試一下如何取得 ETH , USDT , USDC 現在的餘額

基本上使用到的 API 就是 https://github.com/EverexIO/Ethplorer/wiki/Ethplorer-API#get-address-info

透過這就可以知道該地址現在的狀況,不過在這之前,因為 合約是誰都可以發的,所以你必須要知道正版的

USDT , USDC 合約,在正式鏈上面

USDT合約是 0xdac17f958d2ee523a2206206994597c13d831ec7

USDC合約是 0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48

這時候就可以來開始寫 code

回應的模型:

// Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(myJsonResponse); public class Price { public double rate { get; set; } public double diff { get; set; } public double diff7d { get; set; } public int ts { get; set; } public double marketCapUsd { get; set; } public double availableSupply { get; set; } public double volume24h { get; set; } public double diff30d { get; set; } public double volDiff1 { get; set; } public double volDiff7 { get; set; } public double volDiff30 { get; set; } } public class ETH { public Price price { get; set; } public double balance { get; set; } public string rawBalance { get; set; } public double totalIn { get; set; } public int totalOut { get; set; } } public class TokenInfo { public string address { get; set; } public string decimals { get; set; } public string name { get; set; } public string owner { get; set; } public string symbol { get; set; } public string totalSupply { get; set; } public int lastUpdated { get; set; } public int issuancesCount { get; set; } public int holdersCount { get; set; } public int ethTransfersCount { get; set; } public object price { get; set; } public int? slot { get; set; } public string storageTotalSupply { get; set; } public string image { get; set; } public string website { get; set; } public string links { get; set; } public string coingecko { get; set; } public List<string> publicTags { get; set; } public string twitter { get; set; } public string facebook { get; set; } } public class Token { public TokenInfo tokenInfo { get; set; } public double balance { get; set; } public int totalIn { get; set; } public int totalOut { get; set; } public string rawBalance { get; set; } } public class GetAddressInfoResponseModel { public string address { get; set; } public ETH ETH { get; set; } public int countTxs { get; set; } public List<Token> tokens { get; set; } }


主程式 這邊我測試錢包為 0x630eb51fdf14837d782c7bcc13b5ef29ffa47021

我有用到 RestSharp

var client = new RestSharp.RestClient("https://api.ethplorer.io/"); var request = new RestSharp.RestRequest("getAddressInfo/" + "0x630eb51fdf14837d782c7bcc13b5ef29ffa47021", RestSharp.Method.Get); request.AddParameter(RestSharp.Parameter.CreateParameter("apiKey", "freekey", RestSharp.ParameterType.QueryString)); request.AddParameter(RestSharp.Parameter.CreateParameter("showETHTotals", "true", RestSharp.ParameterType.QueryString)); var queryResult = client.ExecuteAsync(request).Result.Content; var resultData = JsonConvert.DeserializeObject<GetAddressInfoResponseModel>(queryResult); Console.WriteLine("ETH 餘額:"); Console.WriteLine(resultData.ETH.balance); //USDT Contract : 0xdac17f958d2ee523a2206206994597c13d831ec7 var usdtInfo = resultData.tokens.SingleOrDefault(x => x.tokenInfo.address == "0xdac17f958d2ee523a2206206994597c13d831ec7"); if (usdtInfo != null) { Console.WriteLine("USDT (0xdac17f958d2ee523a2206206994597c13d831ec7)餘額:"); Console.WriteLine(usdtInfo.balance / Math.Pow(10, int.Parse(usdtInfo.tokenInfo.decimals))); } //USDC Contract : 0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48 var usdcInfo = resultData.tokens.SingleOrDefault(x => x.tokenInfo.address == "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48"); if (usdcInfo != null) { // Console.WriteLine(usdcInfo.balance ); Console.WriteLine("USDC (0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48)餘額:"); Console.WriteLine(usdcInfo.balance/Math.Pow(10, int.Parse(usdcInfo.tokenInfo.decimals))); }


結果:

比對一下 etherscan 結果也正確


不過這是現在的,你可能看到的時候應該不是這數字

而且我不知道這是誰的錢包,我只是網路上找一個來測試的

這裡面有一個小地方就是每一個合約的 decimal 不一樣,所以你必須要 除上 10^n 才會拿到正確得數字


當麻許的碎念筆記 2014 | Donma Hsu Design.