Skip to content

Commit

Permalink
Merge pull request #65 from lidofinance/fix/v2-cache-collusion
Browse files Browse the repository at this point in the history
fix: erc20 cache collusion
  • Loading branch information
Jeday authored Nov 16, 2023
2 parents 78f8d63 + 4a85154 commit 155c55a
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 15 deletions.
15 changes: 2 additions & 13 deletions packages/sdk/src/erc20/erc20.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import {
PublicClient,
WalletClient,
encodeFunctionData,
getContract,
} from 'viem';
import { NOOP, PERMIT_MESSAGE_TYPES } from '../common/constants.js';
import { parseValue } from '../common/utils/parse-value.js';
Expand All @@ -45,19 +44,9 @@ export abstract class AbstractLidoSDKErc20 {

public abstract contractAddress(): Promise<Address>;

@Logger('Contracts:')
@Cache(30 * 60 * 1000, ['core.chain.id', 'contractAddressWstETH'])
public async getContract(): Promise<
public abstract getContract(): Promise<
GetContractReturnType<typeof erc20abi, PublicClient, WalletClient>
> {
const address = await this.contractAddress();
return getContract({
address,
abi: erc20abi,
publicClient: this.core.rpcProvider,
walletClient: this.core.web3Provider,
});
}
>;

// Balance

Expand Down
23 changes: 22 additions & 1 deletion packages/sdk/src/erc20/steth.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
import { Address } from 'viem';
import {
Address,
GetContractReturnType,
PublicClient,
WalletClient,
getContract as getContractViem,
} from 'viem';
import { AbstractLidoSDKErc20 } from './erc20.js';
import { Logger, Cache } from '../common/decorators/index.js';
import { LIDO_CONTRACT_NAMES } from '../common/constants.js';
import invariant from 'tiny-invariant';
import { erc20abi } from './abi/erc20abi.js';

export class LidoSDKstETH extends AbstractLidoSDKErc20 {
@Logger('Contracts:')
Expand All @@ -11,4 +18,18 @@ export class LidoSDKstETH extends AbstractLidoSDKErc20 {
invariant(this.core.chain, 'Chain is not defined');
return this.core.getContractAddress(LIDO_CONTRACT_NAMES.lido);
}

@Logger('Contracts:')
@Cache(30 * 60 * 1000, ['core.chain.id', 'contractAddressStETH'])
public override async getContract(): Promise<
GetContractReturnType<typeof erc20abi, PublicClient, WalletClient>
> {
const address = await this.contractAddress();
return getContractViem({
address,
abi: erc20abi,
publicClient: this.core.rpcProvider,
walletClient: this.core.web3Provider,
});
}
}
23 changes: 22 additions & 1 deletion packages/sdk/src/erc20/wsteth.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
import { Address } from 'viem';
import {
Address,
GetContractReturnType,
PublicClient,
WalletClient,
getContract as getContractViem,
} from 'viem';
import { AbstractLidoSDKErc20 } from './erc20.js';
import { Logger, Cache } from '../common/decorators/index.js';
import { LIDO_CONTRACT_NAMES } from '../common/constants.js';
import invariant from 'tiny-invariant';
import { erc20abi } from './abi/erc20abi.js';

export class LidoSDKwstETH extends AbstractLidoSDKErc20 {
@Logger('Contracts:')
Expand All @@ -25,4 +32,18 @@ export class LidoSDKwstETH extends AbstractLidoSDKErc20 {
verifyingContract: await this.contractAddress(),
};
}

@Logger('Contracts:')
@Cache(30 * 60 * 1000, ['core.chain.id', 'contractAddressWstETH'])
public override async getContract(): Promise<
GetContractReturnType<typeof erc20abi, PublicClient, WalletClient>
> {
const address = await this.contractAddress();
return getContractViem({
address,
abi: erc20abi,
publicClient: this.core.rpcProvider,
walletClient: this.core.web3Provider,
});
}
}

0 comments on commit 155c55a

Please sign in to comment.