Skip to content

Commit

Permalink
added getGasBalance
Browse files Browse the repository at this point in the history
  • Loading branch information
faramozzayw committed Dec 1, 2023
1 parent 2d8453c commit 0690dbc
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 7 deletions.
20 changes: 17 additions & 3 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 {
GasBalanceResponse,
PendingInfoResponse,
ReceiveTransactionCostRequest,
ReceiveTransactionCostResponse,
Expand All @@ -15,12 +16,14 @@ const _55_SECONDS_TTL = 55 * 1000;
export class ApiClientCaching implements ApiClient {
private tokenInfoCache: Cache<Promise<TokenInfo>>;
private pendingInfoCache: Cache<Promise<PendingInfoResponse>>;
private gasBalanceCache: Cache<Promise<GasBalanceResponse>>;
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 });
this.tokenInfoCache = new Cache({ defaultTtl: _55_SECONDS_TTL });
this.receivedTransactionCache = new Cache({ defaultTtl: _20_SECONDS_TTL });
this.pendingInfoCache = new Cache({ defaultTtl: _20_SECONDS_TTL });
this.gasBalanceCache = new Cache({ defaultTtl: _20_SECONDS_TTL });
}

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

async getGasBalance(chainSymbol: ChainSymbol, address: string): Promise<GasBalanceResponse> {
const GAS_BALANCE_CACHE_KEY = `GAS_BALANCE_${chainSymbol}_${address}`;
const gasBalance = this.gasBalanceCache.get(GAS_BALANCE_CACHE_KEY);
if (gasBalance) {
return gasBalance;
}
const gasBalancePromise = this.apiClient.getGasBalance(chainSymbol, address);
this.gasBalanceCache.put(GAS_BALANCE_CACHE_KEY, gasBalancePromise);
return gasBalancePromise;
}

async getPendingInfo(): Promise<PendingInfoResponse> {
const PENDING_INFO_CACHE_KEY = "PENDING_INFO_CACHE_KEY";
const pendingInfo = this.pendingInfoCache.get(PENDING_INFO_CACHE_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,
GasBalanceResponse,
PendingInfoResponse,
PoolInfoResponse,
ReceiveTransactionCostRequest,
Expand All @@ -25,6 +26,7 @@ export interface TokenInfo {
export interface ApiClient {
getTokenInfo(): Promise<TokenInfo>;
getPendingInfo(): Promise<PendingInfoResponse>;
getGasBalance(chainSymbol: ChainSymbol, address: string): Promise<GasBalanceResponse>;
getTransferStatus(chainSymbol: ChainSymbol, txId: string): Promise<TransferStatusResponse>;
getReceiveTransactionCost(args: ReceiveTransactionCostRequest): Promise<ReceiveTransactionCostResponse>;
getPoolInfoMap(pools: PoolKeyObject[] | PoolKeyObject): Promise<PoolInfoMap>;
Expand Down Expand Up @@ -58,6 +60,11 @@ export class ApiClientImpl implements ApiClient {
return data;
}

async getGasBalance(chainSymbol: ChainSymbol, address: string): Promise<GasBalanceResponse> {
const { data } = await this.api.get<GasBalanceResponse>(`/chain/${chainSymbol}/${address}`);
return data;
}

async getTransferStatus(chainSymbol: ChainSymbol, txId: string): Promise<TransferStatusResponse> {
const { data } = await this.api.get<TransferStatusResponse>(`/chain/${chainSymbol}/${txId}`);
return data;
Expand Down
4 changes: 4 additions & 0 deletions src/client/core-api/core-api.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ export interface ReceiveTransactionCostResponse {
sourceNativeTokenPrice: string;
}

export interface GasBalanceResponse {
gasBalance: string;
}

export interface TransferStatusResponse {
txId: string;

Expand Down
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 {
GasBalanceResponse,
PendingInfoResponse,
ReceiveTransactionCostRequest,
ReceiveTransactionCostResponse,
Expand Down Expand Up @@ -39,6 +40,10 @@ export class AllbridgeCoreClientPoolInfoCaching implements AllbridgeCoreClient {
return this.client.getPendingInfo();
}

getGasBalance(chainSymbol: ChainSymbol, address: string): Promise<GasBalanceResponse> {
return this.client.getGasBalance(chainSymbol, address);
}

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 {
GasBalanceResponse,
PendingInfoResponse,
ReceiveTransactionCostRequest,
ReceiveTransactionCostResponse,
Expand All @@ -23,6 +24,8 @@ export interface AllbridgeCoreClient {
getTransferStatus(chainSymbol: ChainSymbol, txId: string): Promise<TransferStatusResponse>;

getReceiveTransactionCost(args: ReceiveTransactionCostRequest): Promise<ReceiveTransactionCostResponse>;

getGasBalance(chainSymbol: ChainSymbol, address: string): Promise<GasBalanceResponse>;
}

export class AllbridgeCoreClientImpl implements AllbridgeCoreClient {
Expand All @@ -41,6 +44,10 @@ export class AllbridgeCoreClientImpl implements AllbridgeCoreClient {
return this.apiClient.getPendingInfo();
}

async getGasBalance(chainSymbol: ChainSymbol, address: string): Promise<GasBalanceResponse> {
return this.apiClient.getGasBalance(chainSymbol, address);
}

async getChainDetailsMapAndPoolInfoMap(): Promise<{
chainDetailsMap: ChainDetailsMap;
poolInfoMap: PoolInfoMap;
Expand Down
10 changes: 10 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
SwapAndBridgeCalculationData,
TokenWithChainDetails,
TransferStatusResponse,
GasBalanceResponse,
} from "./models";
import { AllbridgeCoreSdkService, NodeRpcUrlsConfig } from "./services";
import { DefaultUtils, Utils } from "./utils";
Expand Down Expand Up @@ -125,6 +126,15 @@ export class AllbridgeCoreSdk {
return this.service.getTransferStatus(chainSymbol, txId);
}

/**
* Get gas balance
* @param chainSymbol
* @param address
*/
async getGasBalance(chainSymbol: ChainSymbol, address: string): Promise<GasBalanceResponse> {
return this.service.getGasBalance(chainSymbol, address);
}

/**
* Returns information about pending transactions for the same destination chain and the amount of tokens can be received as a result of transfer considering pending transactions.
* @param amount the amount of tokens that will be sent
Expand Down
7 changes: 6 additions & 1 deletion src/models/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@ export {
export { BridgeService } from "../services/bridge/index";
export { LiquidityPoolService } from "../services/liquidity-pool/index";
export { TransactionResponse } from "../services/models/index";
export { Messenger, TransferStatusResponse, BridgeTransaction } from "../client/core-api/core-api.model";
export {
Messenger,
TransferStatusResponse,
BridgeTransaction,
GasBalanceResponse,
} from "../client/core-api/core-api.model";
export { ChainSymbol, ChainType } from "../chains/index";
export { RawBridgeTransactionBuilder } from "../services/bridge/raw-bridge-transaction-builder";
export { RawPoolTransactionBuilder } from "../services/liquidity-pool/raw-pool-transaction-builder";
Expand Down
15 changes: 12 additions & 3 deletions src/services/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ import { ChainSymbol } from "../chains";
import { AllbridgeCoreClientImpl } from "../client/core-api";
import { ApiClientImpl } from "../client/core-api/api-client";
import { ApiClientCaching } from "../client/core-api/api-client-caching";
import { Messenger, PendingInfoDTO, TransferStatusResponse } from "../client/core-api/core-api.model";
import {
GasBalanceResponse,
Messenger,
PendingInfoDTO,
TransferStatusResponse,
} from "../client/core-api/core-api.model";
import { AllbridgeCoreClientPoolInfoCaching } from "../client/core-api/core-client-pool-info-caching";
import { mainnet } from "../configs";
import { AllbridgeCoreSdkOptions, NodeRpcUrls } from "../index";
Expand Down Expand Up @@ -75,8 +80,8 @@ export class AllbridgeCoreSdkService {

constructor(nodeRpcUrlsConfig: NodeRpcUrlsConfig, params: AllbridgeCoreSdkOptions = mainnet) {
const apiClient = new ApiClientImpl(params);
const apiClientTokenInfoCaching = new ApiClientCaching(apiClient);
const coreClient = new AllbridgeCoreClientImpl(apiClientTokenInfoCaching);
const apiClientCaching = new ApiClientCaching(apiClient);
const coreClient = new AllbridgeCoreClientImpl(apiClientCaching);
this.api = new AllbridgeCoreClientPoolInfoCaching(coreClient);

const solBridgeParams: SolanaBridgeParams = {
Expand Down Expand Up @@ -106,6 +111,10 @@ export class AllbridgeCoreSdkService {
return this.api.getTransferStatus(chainSymbol, txId);
}

async getGasBalance(chainSymbol: ChainSymbol, address: string): Promise<GasBalanceResponse> {
return this.api.getGasBalance(chainSymbol, address);
}

async getPendingStatusInfo(
amount: string,
amountFormat: AmountFormat,
Expand Down

0 comments on commit 0690dbc

Please sign in to comment.