Rene Winter
Rene Winter

Reputation: 41

Receive Token Transfers via Etherscan API

I want to receive all token transfers for a specific token. For example if you are open that url, it will show 85 transfers in your browser: https://etherscan.io/token/0x2d8b6fc9ae0fa508caa14453ce13acb8d906c184

I already found out, that I can get the transactions of the contract, which are 51 in that example: https://etherscan.io/address/0x2d8b6fc9ae0fa508caa14453ce13acb8d906c184

I can get this information with the following api call - which is not what I want, because this is giving me the transactions of the contract: https://api.etherscan.io/api?module=account&action=txlist&address=0x2d8b6fc9ae0fa508caa14453ce13acb8d906c184&apikey=...

Upvotes: 1

Views: 70

Answers (1)

Rene Winter
Rene Winter

Reputation: 41

I found a solution via moralis API (free version with up to 40k CU/day) - C# code:

const string CURSOR_SUFFIX = @"&cursor=";
const string GET_TRANSACTIONS_P = @"https://deep-index.moralis.io/api/v2.2/erc20/";
const string GET_TRANSACTIONS_S = @"/transfers?chain=eth&order=ASC&limit=100";

private async Task<JsonResult> getTransactionPageForToken(string pContractAddress, string pCursor)
{
    JsonResult jrTokenTransactions;
    string sUrl = GET_TRANSACTIONS_P + pContractAddress + GET_TRANSACTIONS_S;

    if (pCursor != string.Empty)
    {
        sUrl += CURSOR_SUFFIX + pCursor;
    }

    using (var client = new HttpClient())
    {
        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
        client.DefaultRequestHeaders.Add("X-API-Key", API_KEY_TOKEN);

        var response = await client.GetAsync(sUrl);
        response.EnsureSuccessStatusCode();
        string sContent = await response.Content.ReadAsStringAsync();

        jrTokenTransactions = JsonConvert.DeserializeObject<JsonResult>(sContent);
        Console.WriteLine(sContent);
    }

    return jrTokenTransactions;
}

Upvotes: 0

Related Questions