Skip to content

Commit

Permalink
feat(getPoolInfoFromChain): add config param cachePoolInfoChainSecs
Browse files Browse the repository at this point in the history
  • Loading branch information
AntonKozAllB committed Jul 8, 2024
1 parent 691f69f commit 28ad5d2
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 10 deletions.
1 change: 1 addition & 0 deletions src/configs/mainnet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export const mainnet: AllbridgeCoreSdkOptions = {
cctpTokenMessengerMinter: "CCTPiPYPc6AsJuwueEnWgSgucamXDZwBd53dQ11YiKX3",
cctpDomains: { ETH: 0, AVA: 1, OPT: 2, ARB: 3, SOL: 5, BAS: 6, POL: 7 },
},
cachePoolInfoChainSecs: 20,
};

/**
Expand Down
1 change: 1 addition & 0 deletions src/configs/testnet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export const testnet: AllbridgeCoreSdkOptions = {
cctpTokenMessengerMinter: "CCTPiPYPc6AsJuwueEnWgSgucamXDZwBd53dQ11YiKX3",
cctpDomains: { SPL: 0, ARB: 3, AMO: 7 },
},
cachePoolInfoChainSecs: 20,
};

/**
Expand Down
6 changes: 6 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ export interface AllbridgeCoreSdkOptions {
*/
tronJsonRpc?: string;
cctpParams: CctpParams;
/**
* The number of seconds that pool information taken from the chain will be cached.
*
* @type {number}
*/
cachePoolInfoChainSecs: number;
}

/**
Expand Down
31 changes: 21 additions & 10 deletions src/services/liquidity-pool/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Big } from "big.js";
import Cache from "timed-cache";
// @ts-expect-error import tron
import TronWeb from "tronweb";
import Web3 from "web3";
Expand All @@ -7,7 +8,7 @@ import { chainProperties, ChainSymbol, ChainType } from "../../chains";
import { AllbridgeCoreClient } from "../../client/core-api";
import { AllbridgeCoreClientPoolInfoCaching } from "../../client/core-api/core-client-pool-info-caching";
import { AllbridgeCoreSdkOptions } from "../../index";
import { PoolInfo, TokenWithChainDetails } from "../../tokens-info";
import { PoolInfo, PoolKeyObject, TokenWithChainDetails } from "../../tokens-info";
import { convertIntAmountToFloat, fromSystemPrecision } from "../../utils/calculation";
import { SYSTEM_PRECISION } from "../../utils/calculation/constants";
import { validateAmountDecimals, validateAmountGtZero } from "../../utils/utils";
Expand Down Expand Up @@ -113,6 +114,7 @@ export interface LiquidityPoolService {

export class DefaultLiquidityPoolService implements LiquidityPoolService {
public rawTxBuilder: RawPoolTransactionBuilder;
private cache: Cache<PoolInfo>;

constructor(
private api: AllbridgeCoreClientPoolInfoCaching,
Expand All @@ -121,6 +123,8 @@ export class DefaultLiquidityPoolService implements LiquidityPoolService {
private tokenService: TokenService
) {
this.rawTxBuilder = new DefaultRawPoolTransactionBuilder(api, nodeRpcUrlsConfig, this.params, tokenService);
const ttl = params.cachePoolInfoChainSecs > 0 ? params.cachePoolInfoChainSecs * 1000 : 20 * 1000;
this.cache = new Cache<PoolInfo>({ defaultTtl: ttl });
}

async getAllowance(a: Provider | GetAllowanceParams, b?: GetAllowanceParams): Promise<string> {
Expand Down Expand Up @@ -190,15 +194,22 @@ export class DefaultLiquidityPoolService implements LiquidityPoolService {
}

async getPoolInfoFromChain(token: TokenWithChainDetails, provider?: Provider): Promise<PoolInfo> {
const poolInfo = await getChainPoolService(
token.chainSymbol,
this.api,
this.nodeRpcUrlsConfig,
this.params,
provider
).getPoolInfoFromChain(token);
this.api.cachePut({ chainSymbol: token.chainSymbol, poolAddress: token.poolAddress }, poolInfo);
return poolInfo;
const poolKey: PoolKeyObject = { chainSymbol: token.chainSymbol, poolAddress: token.poolAddress };
const fromCache = this.cache.get(poolKey);
if (fromCache) {
return fromCache;
} else {
const poolInfo = await getChainPoolService(
token.chainSymbol,
this.api,
this.nodeRpcUrlsConfig,
this.params,
provider
).getPoolInfoFromChain(token);
this.cache.put(poolKey, poolInfo);
this.api.cachePut({ chainSymbol: token.chainSymbol, poolAddress: token.poolAddress }, poolInfo);
return poolInfo;
}
}
}

Expand Down

0 comments on commit 28ad5d2

Please sign in to comment.