-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: explorer api pagination (#2002)
* refactor: query parameters on base api add: make paginated request on explorer api Co-authored-by: Tuditi <[email protected]> * fix: paginated request logic * refactor: move evm explorer to blockscout module * fix: imports * feat: add blockscout transaction interface * fix: correct queryParameters --------- Co-authored-by: Tuditi <[email protected]> Co-authored-by: Tuditi <[email protected]> Co-authored-by: Tuditi <[email protected]>
- Loading branch information
1 parent
5ec84d6
commit 70a7506
Showing
19 changed files
with
237 additions
and
88 deletions.
There are no files selected for viewing
84 changes: 84 additions & 0 deletions
84
packages/shared/src/lib/auxiliary/blockscout/api/blockscout.api.ts
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import { NftStandard } from '@core/nfts/enums' | ||
import { TokenStandard } from '@core/token/enums' | ||
import { QueryParameters } from '@core/utils' | ||
import { BaseApi } from '@core/utils/api' | ||
import { DEFAULT_EXPLORER_URLS } from '@core/network/constants' | ||
import { SupportedNetworkId } from '@core/network/enums' | ||
import { IBlockscoutApi, IBlockscoutAsset, IBlockscoutAssetMetadata, IBlockscoutTransaction } from '../interfaces' | ||
import { NetworkId } from '@core/network/types' | ||
|
||
interface INextPageParams { | ||
block_number: number | ||
index: number | ||
items_count: number | ||
} | ||
|
||
interface IPaginationResponse<T> { | ||
items: T[] | ||
next_page_params: INextPageParams | null | ||
} | ||
|
||
export class BlockscoutApi extends BaseApi implements IBlockscoutApi { | ||
constructor(networkId: NetworkId) { | ||
const explorerUrl = DEFAULT_EXPLORER_URLS[networkId as SupportedNetworkId] | ||
super(`${explorerUrl}/api/v2`) | ||
} | ||
|
||
private async makePaginatedGetRequest<T>( | ||
path: string, | ||
queryParameters?: QueryParameters, | ||
items: T[] = [], | ||
nextPageParameters?: INextPageParams | null | ||
): Promise<T[]> { | ||
if (nextPageParameters === null) { | ||
return Promise.resolve(items) | ||
} | ||
return this.get<IPaginationResponse<T>>(path, { ...queryParameters, ...nextPageParameters }).then( | ||
(response) => { | ||
if (!response) { | ||
return Promise.resolve(items) | ||
} | ||
return this.makePaginatedGetRequest( | ||
path, | ||
queryParameters, | ||
items.concat(response.items), | ||
response.next_page_params | ||
) | ||
} | ||
) | ||
} | ||
|
||
async getAssetMetadata(assetAddress: string): Promise<IBlockscoutAssetMetadata | undefined> { | ||
const response = await this.get<IBlockscoutAssetMetadata>(`tokens/${assetAddress}`) | ||
if (response) { | ||
response.type = response.type.replace('-', '') as TokenStandard.Erc20 | NftStandard.Erc721 | ||
return response | ||
} | ||
} | ||
|
||
async getAssetsForAddress( | ||
address: string, | ||
standard: TokenStandard.Erc20 | NftStandard.Erc721 = TokenStandard.Erc20 | ||
): Promise<IBlockscoutAsset[]> { | ||
const tokenType = standard.replace('ERC', 'ERC-') | ||
const path = `addresses/${address}/tokens` | ||
const response = await this.get<IPaginationResponse<IBlockscoutAsset>>(path, { type: tokenType }) | ||
if (response) { | ||
return (response?.items ?? []).map((asset) => ({ | ||
...asset, | ||
token: { | ||
...asset.token, | ||
type: asset.token.type.replace('-', ''), | ||
}, | ||
})) | ||
} else { | ||
return [] | ||
} | ||
} | ||
|
||
async getTransactionsForAddress(address: string): Promise<IBlockscoutTransaction[]> { | ||
const path = `addresses/${address}/transactions` | ||
const items = await this.makePaginatedGetRequest<IBlockscoutTransaction>(path) | ||
return items | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './blockscout.api' |
14 changes: 14 additions & 0 deletions
14
packages/shared/src/lib/auxiliary/blockscout/interfaces/blockscout-api.interface.ts
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { NftStandard } from '@core/nfts/enums' | ||
import { TokenStandard } from '@core/token/enums' | ||
import { IBlockscoutAsset } from './blockscout-asset.interface' | ||
import { IBlockscoutAssetMetadata } from './blockscout-asset-metadata.interface' | ||
import { IBlockscoutTransaction } from './blockscout-transaction.interface' | ||
|
||
export interface IBlockscoutApi { | ||
getAssetMetadata(assetAddress: string): Promise<IBlockscoutAssetMetadata | undefined> | ||
getAssetsForAddress( | ||
address: string, | ||
tokenStandard?: TokenStandard.Erc20 | NftStandard.Erc721 | ||
): Promise<IBlockscoutAsset[]> | ||
getTransactionsForAddress(address: string): Promise<IBlockscoutTransaction[]> | ||
} |
2 changes: 1 addition & 1 deletion
2
...aces/explorer-asset-metadata.interface.ts → ...es/blockscout-asset-metadata.interface.ts
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
9 changes: 9 additions & 0 deletions
9
packages/shared/src/lib/auxiliary/blockscout/interfaces/blockscout-asset.interface.ts
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { IBlockscoutAssetMetadata } from './blockscout-asset-metadata.interface' | ||
|
||
// snake_case returned by the API | ||
export interface IBlockscoutAsset { | ||
token: IBlockscoutAssetMetadata | ||
token_id: string | ||
token_instance: unknown | ||
value: string | ||
} |
81 changes: 81 additions & 0 deletions
81
packages/shared/src/lib/auxiliary/blockscout/interfaces/blockscout-transaction.interface.ts
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import { IBlockscoutAssetMetadata } from './blockscout-asset-metadata.interface' | ||
|
||
interface IFee { | ||
type: string | ||
value: string | ||
} | ||
|
||
interface IAddressTag { | ||
address_hash: string | ||
display_name: string | ||
label: string | ||
} | ||
|
||
interface IWatchlistName { | ||
display_name: string | ||
label: string | ||
} | ||
|
||
interface IAddressParam { | ||
hash: string | ||
implementation_name: string | ||
name: string | ||
is_contract: boolean | ||
private_tags: IAddressTag[] | ||
watchlist_names: IWatchlistName[] | ||
public_tags: IAddressTag[] | ||
is_verified: boolean | ||
} | ||
|
||
interface IDecodedInput { | ||
method_call: string | ||
method_id: string | ||
parameters: Record<string, string> // IDecodedInputParameters | ||
} | ||
|
||
interface ITokenTransfer { | ||
block_hash: string | ||
from: IAddressParam | ||
log_index: string | ||
method: string | ||
timestamp: string | ||
to: IAddressParam | ||
token: IBlockscoutAssetMetadata | ||
} | ||
|
||
export interface IBlockscoutTransaction { | ||
timestamp: string | ||
fee: IFee | ||
gas_limit: number | ||
block: number | ||
status: string // e.g ok | error | ||
method: string // e.g transferFrom | ||
confirmations: number | ||
type: number | ||
exchange_rate: string | ||
to: IAddressParam | ||
tx_burnt_fee: string | ||
max_fee_per_gas: string | ||
result: string | ||
hash: string | ||
gas_price: string | ||
priority_fee: string | ||
base_fee_per_gas: string | ||
from: IAddressParam | ||
token_transfers: ITokenTransfer[] | ||
tx_types: string[] | ||
gas_used: string | ||
created_contract: IAddressParam | ||
position: number | ||
nonce: number | ||
has_error_in_internal_txs: boolean | ||
actions: unknown // TransactionAction | ||
decoded_input: IDecodedInput | ||
token_transfers_overflow: boolean | ||
raw_input: string | ||
value: string | ||
max_priority_fee_per_gas: string | ||
revert_reason: string | ||
confirmation_duration: string | ||
tx_tag: string | ||
} |
4 changes: 4 additions & 0 deletions
4
packages/shared/src/lib/auxiliary/blockscout/interfaces/index.ts
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export * from './blockscout-api.interface' | ||
export * from './blockscout-asset.interface' | ||
export * from './blockscout-asset-metadata.interface' | ||
export * from './blockscout-transaction.interface' |
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
43 changes: 0 additions & 43 deletions
43
packages/shared/src/lib/core/network/classes/evm-explorer-api.class.ts
This file was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,2 @@ | ||
export * from './evm-explorer-api.class' | ||
export * from './iscp-chain.class' | ||
export * from './stardust-network.class' |
12 changes: 0 additions & 12 deletions
12
packages/shared/src/lib/core/network/interfaces/explorer-api.interface.ts
This file was deleted.
Oops, something went wrong.
9 changes: 0 additions & 9 deletions
9
packages/shared/src/lib/core/network/interfaces/explorer-asset.interface.ts
This file was deleted.
Oops, something went wrong.
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
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
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
Oops, something went wrong.