Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve(TokenClient): Cache allowance and bondToken eth_call data #1322

Merged
merged 18 commits into from
Mar 22, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 54 additions & 5 deletions src/clients/TokenClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
runTransaction,
toBN,
winston,
getRedisCache,
} from "../utils";

type TokenDataType = { [chainId: number]: { [token: string]: { balance: BigNumber; allowance: BigNumber } } };
Expand Down Expand Up @@ -183,7 +184,7 @@ export class TokenClient {

const [balanceInfo, bondToken] = await Promise.all([
Promise.all(Object.values(this.spokePoolClients).map((spokePoolClient) => this.fetchTokenData(spokePoolClient))),
this.hubPoolClient.hubPool.bondToken(),
this._getBondToken(),
]);

this.bondToken = new Contract(bondToken, ERC20.abi, this.hubPoolClient.hubPool.signer);
Expand Down Expand Up @@ -223,10 +224,7 @@ export class TokenClient {
await Promise.all(
tokens.map(async (token) => {
const balance: BigNumber = await token.balanceOf(this.relayerAddress, { blockTag });
const allowance: BigNumber = await token.allowance(this.relayerAddress, spokePoolClient.spokePool.address, {
blockTag,
});

const allowance = await this._getAllowance(spokePoolClient, token, blockTag);
return [token.address, { balance, allowance }];
})
)
Expand All @@ -235,6 +233,57 @@ export class TokenClient {
return { tokenData, chainId: spokePoolClient.chainId };
}

private async _getAllowanceCacheKey(spokePoolClient: SpokePoolClient, originToken: string): Promise<string> {
return `l2TokenAllowance_${
spokePoolClient.chainId
}_${originToken}_${await spokePoolClient.spokePool.signer.getAddress()}_targetContract:${
spokePoolClient.spokePool.address
}`;
}

private async _getAllowance(
spokePoolClient: SpokePoolClient,
token: Contract,
blockTag: number | "latest"
): Promise<BigNumber> {
const redis = await getRedisCache(this.logger);
if (redis) {
const result = await redis.get<string>(await this._getAllowanceCacheKey(spokePoolClient, token.address));
if (result !== null) {
return toBN(result);
}
}
const allowance: BigNumber = await token.allowance(this.relayerAddress, spokePoolClient.spokePool.address, {
blockTag,
});
if (allowance.gte(MAX_SAFE_ALLOWANCE) && redis) {
// Save allowance in cache with no TTL as these should be exhausted.
await redis.set(await this._getAllowanceCacheKey(spokePoolClient, token.address), MAX_SAFE_ALLOWANCE);
}
return allowance;
}

_getBondTokenCacheKey(): string {
return `bondToken_${this.hubPoolClient.hubPool.address}`;
}

private async _getBondToken(): Promise<string> {
const redis = await getRedisCache(this.logger);
if (redis) {
const cachedBondToken = await redis.get<string>(this._getBondTokenCacheKey());
if (cachedBondToken !== null) {
return cachedBondToken;
}
}
const bondToken: string = await this.hubPoolClient.hubPool.bondToken();
if (redis) {
// The bond token should not change, and using the wrong bond token will be immediately obvious, so cache with
// infinite TTL.
await redis.set(this._getBondTokenCacheKey(), bondToken);
nicholaspai marked this conversation as resolved.
Show resolved Hide resolved
}
return bondToken;
}

private _hasTokenPairData(chainId: number, token: string) {
const hasData = !!this.tokenData?.[chainId]?.[token];
if (!hasData) {
Expand Down
Loading