Skip to content

Commit

Permalink
feat(sdk): add getPendingStatusInfo method
Browse files Browse the repository at this point in the history
  • Loading branch information
AntonKozAllB committed Nov 22, 2023
1 parent bc9af4f commit 1d4a438
Show file tree
Hide file tree
Showing 15 changed files with 669 additions and 296 deletions.
14 changes: 14 additions & 0 deletions src/client/core-api/api-client-caching.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { ChainSymbol } from "../../chains";
import { PoolInfoMap, PoolKeyObject } from "../../tokens-info";
import { ApiClient, TokenInfo } from "./api-client";
import {
PendingInfoResponse,
ReceiveTransactionCostRequest,
ReceiveTransactionCostResponse,
TransferStatusResponse,
Expand All @@ -13,11 +14,13 @@ const _55_SECONDS_TTL = 55 * 1000;

export class ApiClientCaching implements ApiClient {
private tokenInfoCache: Cache<Promise<TokenInfo>>;
private pendingInfoCache: Cache<Promise<PendingInfoResponse>>;
private receivedTransactionCache: Cache<ReceiveTransactionCostResponse>;

constructor(private apiClient: ApiClient) {
this.tokenInfoCache = new Cache<Promise<TokenInfo>>({ defaultTtl: _55_SECONDS_TTL });
this.receivedTransactionCache = new Cache<ReceiveTransactionCostResponse>({ defaultTtl: _20_SECONDS_TTL });
this.pendingInfoCache = new Cache<Promise<PendingInfoResponse>>({ defaultTtl: _20_SECONDS_TTL });
}

getTokenInfo(): Promise<TokenInfo> {
Expand All @@ -31,6 +34,17 @@ export class ApiClientCaching implements ApiClient {
return tokenInfoPromise;
}

async getPendingInfo(): Promise<PendingInfoResponse> {
const PENDING_INFO_CACHE_KEY = "PENDING_INFO_CACHE_KEY";
const pendingInfo = this.pendingInfoCache.get(PENDING_INFO_CACHE_KEY);
if (pendingInfo) {
return pendingInfo;
}
const pendingInfoPromise = this.apiClient.getPendingInfo();
this.pendingInfoCache.put(PENDING_INFO_CACHE_KEY, pendingInfoPromise);
return pendingInfoPromise;
}

async getReceiveTransactionCost(args: ReceiveTransactionCostRequest): Promise<ReceiveTransactionCostResponse> {
const RECEIVE_TX_COST_KEY = `RECEIVE_TX_COST_${args.sourceChainId}_${args.destinationChainId}_${args.messenger}`;
const transactionCost = this.receivedTransactionCache.get(RECEIVE_TX_COST_KEY);
Expand Down
7 changes: 7 additions & 0 deletions src/client/core-api/api-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
} from "./core-api-mapper";
import {
ChainDetailsResponse,
PendingInfoResponse,
PoolInfoResponse,
ReceiveTransactionCostRequest,
ReceiveTransactionCostResponse,
Expand All @@ -23,6 +24,7 @@ export interface TokenInfo {

export interface ApiClient {
getTokenInfo(): Promise<TokenInfo>;
getPendingInfo(): Promise<PendingInfoResponse>;
getTransferStatus(chainSymbol: ChainSymbol, txId: string): Promise<TransferStatusResponse>;
getReceiveTransactionCost(args: ReceiveTransactionCostRequest): Promise<ReceiveTransactionCostResponse>;
getPoolInfoMap(pools: PoolKeyObject[] | PoolKeyObject): Promise<PoolInfoMap>;
Expand Down Expand Up @@ -51,6 +53,11 @@ export class ApiClientImpl implements ApiClient {
};
}

async getPendingInfo(): Promise<PendingInfoResponse> {
const { data } = await this.api.get<PendingInfoResponse>("/pending-info");
return data;
}

async getTransferStatus(chainSymbol: ChainSymbol, txId: string): Promise<TransferStatusResponse> {
const { data } = await this.api.get<TransferStatusResponse>(`/chain/${chainSymbol}/${txId}`);
return data;
Expand Down
8 changes: 8 additions & 0 deletions src/client/core-api/core-api.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,3 +136,11 @@ export type PoolInfoResponse = {
string: PoolInfo;
};
};

export type PendingInfoResponse = Record<ChainSymbol, TokenPendingInfoDTO>;
export type TokenPendingInfoDTO = Record<string, PendingInfoDTO>;

export interface PendingInfoDTO {
pendingTxs: number;
totalSentAmount: string;
}
5 changes: 5 additions & 0 deletions src/client/core-api/core-client-pool-info-caching.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { ChainSymbol } from "../../chains";
import { ChainDetailsMap, PoolInfo, PoolInfoMap, PoolKeyObject, TokenWithChainDetails } from "../../tokens-info";
import { mapChainDetailsMapToPoolKeyObjects, mapPoolKeyObjectToPoolKey } from "./core-api-mapper";
import {
PendingInfoResponse,
ReceiveTransactionCostRequest,
ReceiveTransactionCostResponse,
TransferStatusResponse,
Expand Down Expand Up @@ -34,6 +35,10 @@ export class AllbridgeCoreClientPoolInfoCaching implements AllbridgeCoreClient {
return this.client.getReceiveTransactionCost(args);
}

getPendingInfo(): Promise<PendingInfoResponse> {
return this.client.getPendingInfo();
}

async getPoolInfoByKey(poolKeyObject: PoolKeyObject): Promise<PoolInfo> {
this.poolInfoCache.putAllIfNotExists((await this.client.getChainDetailsMapAndPoolInfoMap()).poolInfoMap);
const poolInfo = this.poolInfoCache.get(poolKeyObject);
Expand Down
7 changes: 7 additions & 0 deletions src/client/core-api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { ChainSymbol } from "../../chains";
import { ChainDetailsMap, PoolInfoMap, PoolKeyObject, TokenWithChainDetails } from "../../tokens-info";
import { ApiClient } from "./api-client";
import {
PendingInfoResponse,
ReceiveTransactionCostRequest,
ReceiveTransactionCostResponse,
TransferStatusResponse,
Expand All @@ -17,6 +18,8 @@ export interface AllbridgeCoreClient {
getChainDetailsMap(): Promise<ChainDetailsMap>;
tokens(): Promise<TokenWithChainDetails[]>;

getPendingInfo(): Promise<PendingInfoResponse>;

getTransferStatus(chainSymbol: ChainSymbol, txId: string): Promise<TransferStatusResponse>;

getReceiveTransactionCost(args: ReceiveTransactionCostRequest): Promise<ReceiveTransactionCostResponse>;
Expand All @@ -34,6 +37,10 @@ export class AllbridgeCoreClientImpl implements AllbridgeCoreClient {
return Object.values(map).flatMap((chainDetails) => chainDetails.tokens);
}

async getPendingInfo(): Promise<PendingInfoResponse> {
return this.apiClient.getPendingInfo();
}

async getChainDetailsMapAndPoolInfoMap(): Promise<{
chainDetailsMap: ChainDetailsMap;
poolInfoMap: PoolInfoMap;
Expand Down
Loading

0 comments on commit 1d4a438

Please sign in to comment.