1- import { ChainID , CovalentClient } from '@covalenthq/client-sdk' ;
1+ import {
2+ GoldRushClient ,
3+ ChainID ,
4+ GoldRushResponse ,
5+ GetTransactionsForAddressV3QueryParamOpts
6+ } from '@covalenthq/client-sdk' ;
27import retry from 'async-retry' ;
38
49import { EnvVars } from '../../config' ;
510import { CodedError } from '../../utils/errors' ;
611
7- const client = new CovalentClient ( EnvVars . COVALENT_API_KEY , { enableRetry : false , threadCount : 10 } ) ;
12+ const client = new GoldRushClient ( EnvVars . COVALENT_API_KEY , { enableRetry : false , threadCount : 10 } ) ;
813
9- const RETRY_OPTIONS = { maxRetryTime : 30_000 } ;
14+ const RETRY_OPTIONS : retry . Options = { maxRetryTime : 30_000 } ;
1015
11- export const getEvmBalances = async ( walletAddress : string , chainId : string ) =>
12- await retry (
13- async ( ) =>
16+ /** For v2 only for now. No support in v3. */
17+ const ACTIVITIES_PER_PAGE = 30 ;
18+
19+ export const getEvmBalances = ( walletAddress : string , chainId : string ) =>
20+ retry (
21+ ( ) =>
1422 client . BalanceService . getTokenBalancesForWalletAddress ( Number ( chainId ) as ChainID , walletAddress , {
1523 nft : true ,
1624 noNftAssetMetadata : true ,
1725 quoteCurrency : 'USD' ,
1826 noSpam : false
19- } ) . then ( ( { data, error, error_message, error_code } ) => {
20- if ( error ) {
21- throw new CodedError ( Number ( error_code ) || 500 , error_message ) ;
22- }
23-
24- return data ;
25- } ) ,
27+ } ) . then ( processGoldRushResponse ) ,
2628 RETRY_OPTIONS
2729 ) ;
2830
29- export const getEvmTokensMetadata = async ( walletAddress : string , chainId : string ) =>
30- await retry (
31- async ( ) =>
31+ export const getEvmTokensMetadata = ( walletAddress : string , chainId : string ) =>
32+ retry (
33+ ( ) =>
3234 client . BalanceService . getTokenBalancesForWalletAddress ( Number ( chainId ) as ChainID , walletAddress , {
3335 nft : false ,
3436 quoteCurrency : 'USD' ,
3537 noSpam : false
36- } ) . then ( ( { data, error, error_message, error_code } ) => {
37- if ( error ) {
38- throw new CodedError ( Number ( error_code ) || 500 , error_message ) ;
39- }
40-
41- return data ;
42- } ) ,
38+ } ) . then ( processGoldRushResponse ) ,
4339 RETRY_OPTIONS
4440 ) ;
4541
@@ -49,20 +45,62 @@ export const getEvmCollectiblesMetadata = async (walletAddress: string, chainId:
4945 const withUncached = CHAIN_IDS_WITHOUT_CACHE_SUPPORT . includes ( Number ( chainId ) ) ;
5046
5147 return await retry (
52- async ( ) =>
48+ ( ) =>
5349 client . NftService . getNftsForAddress ( Number ( chainId ) as ChainID , walletAddress , {
5450 withUncached,
5551 noSpam : false
56- } ) . then ( ( { data, error, error_message, error_code } ) => {
57- if ( error ) {
58- throw new CodedError ( Number ( error_code ) || 500 , error_message ) ;
59- }
60-
61- return data ;
62- } ) ,
52+ } ) . then ( processGoldRushResponse ) ,
6353 RETRY_OPTIONS
6454 ) ;
6555} ;
6656
67- export const getStringifiedResponse = ( response : any ) =>
68- JSON . stringify ( response , ( _ , value ) => ( typeof value === 'bigint' ? value . toString ( ) : value ) ) ;
57+ export const getEvmAccountTransactions = ( walletAddress : string , chainId : string , page ?: number ) =>
58+ retry ( async ( ) => {
59+ const options : GetTransactionsForAddressV3QueryParamOpts = {
60+ // blockSignedAtAsc: true,
61+ noLogs : false ,
62+ quoteCurrency : 'USD' ,
63+ withSafe : false
64+ } ;
65+
66+ const res = await ( typeof page === 'number'
67+ ? client . TransactionService . getTransactionsForAddressV3 ( Number ( chainId ) as ChainID , walletAddress , page , options )
68+ : client . TransactionService . getAllTransactionsForAddressByPage (
69+ Number ( chainId ) as ChainID ,
70+ walletAddress ,
71+ options
72+ ) ) ;
73+
74+ return processGoldRushResponse ( res ) ;
75+ } , RETRY_OPTIONS ) ;
76+
77+ export const getEvmAccountERC20Transfers = (
78+ walletAddress : string ,
79+ chainId : string ,
80+ contractAddress : string ,
81+ page ?: number
82+ ) =>
83+ retry ( async ( ) => {
84+ const res = await client . BalanceService . getErc20TransfersForWalletAddressByPage (
85+ Number ( chainId ) as ChainID ,
86+ walletAddress ,
87+ {
88+ contractAddress,
89+ quoteCurrency : 'USD' ,
90+ pageNumber : page ,
91+ pageSize : ACTIVITIES_PER_PAGE
92+ }
93+ ) ;
94+
95+ return processGoldRushResponse ( res ) ;
96+ } , RETRY_OPTIONS ) ;
97+
98+ function processGoldRushResponse < T > ( { data, error, error_message, error_code } : GoldRushResponse < T > ) {
99+ if ( error ) {
100+ const code = error_code && Number . isSafeInteger ( Number ( error_code ) ) ? Number ( error_code ) : 500 ;
101+
102+ throw new CodedError ( code , error_message ?? 'Unknown error' ) ;
103+ }
104+
105+ return JSON . stringify ( data , ( _ , value ) => ( typeof value === 'bigint' ? value . toString ( ) : value ) ) ;
106+ }
0 commit comments