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 7 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
47 changes: 39 additions & 8 deletions src/clients/TokenClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,17 @@ import {
runTransaction,
toBN,
winston,
getRedisCache,
} from "../utils";

type TokenDataType = { [chainId: number]: { [token: string]: { balance: BigNumber; allowance: BigNumber } } };
type TokenShortfallType = {
[chainId: number]: { [token: string]: { deposits: number[]; totalRequirement: BigNumber } };
};
type FetchTokenDataReturnType = {
tokenData: Record<string, { balance: BigNumber; allowance: BigNumber }>;
chainId: number;
};

export class TokenClient {
tokenData: TokenDataType = {};
Expand Down Expand Up @@ -183,7 +188,14 @@ export class TokenClient {

const [balanceInfo, bondToken] = await Promise.all([
Promise.all(Object.values(this.spokePoolClients).map((spokePoolClient) => this.fetchTokenData(spokePoolClient))),
this.hubPoolClient.hubPool.bondToken(),
// Make eth_call using nearest 100_000th block to take advantage of eth_call caching when blockTag is
// specified. This is unexpected to change so we are ok to rarely increase this block tag.
this.hubPoolClient.hubPool.bondToken({
blockTag: Math.min(
nicholaspai marked this conversation as resolved.
Show resolved Hide resolved
this.hubPoolClient.deploymentBlock,
Math.round(this.hubPoolClient.latestBlockSearched / 100_000) * 100_000
),
}),
]);

this.bondToken = new Contract(bondToken, ERC20.abi, this.hubPoolClient.hubPool.signer);
Expand All @@ -210,10 +222,16 @@ export class TokenClient {
this.logger.debug({ at: "TokenBalanceClient", message: "TokenBalance client updated!", balanceData });
}

async fetchTokenData(spokePoolClient: SpokePoolClient): Promise<{
tokenData: Record<string, { balance: BigNumber; allowance: BigNumber }>;
chainId: number;
}> {
async getAllowanceCacheKey(spokePoolClient: SpokePoolClient, originToken: string): Promise<string> {
return `l2TokenAllowance_${
spokePoolClient.chainId
}_${originToken}_${await spokePoolClient.spokePool.signer.getAddress()}_targetContract:${
spokePoolClient.spokePool.address
}`;
}

async fetchTokenData(spokePoolClient: SpokePoolClient): Promise<FetchTokenDataReturnType> {
nicholaspai marked this conversation as resolved.
Show resolved Hide resolved
const redis = await getRedisCache(this.logger);
const tokens = spokePoolClient
.getAllOriginTokens()
.map((address) => new Contract(address, ERC20.abi, spokePoolClient.spokePool.signer));
Expand All @@ -223,9 +241,22 @@ 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,
});
let allowance: BigNumber;
if (redis) {
const result = await redis.get<string>(await this.getAllowanceCacheKey(spokePoolClient, token.address));
if (result !== null) {
allowance = toBN(result);
}
}
if (!allowance) {
allowance = await token.allowance(this.relayerAddress, spokePoolClient.spokePool.address, {
blockTag,
});
if (redis) {
// Save allowance in cache with no TTL as these should never decrement.
await redis.set(await this.getAllowanceCacheKey(spokePoolClient, token.address), MAX_SAFE_ALLOWANCE);
}
}

return [token.address, { balance, allowance }];
})
Expand Down
Loading