Skip to content

Commit

Permalink
fix: update caching logic (#854)
Browse files Browse the repository at this point in the history
  • Loading branch information
sharonsheah authored Sep 13, 2023
1 parent 790da99 commit e0c7e16
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
12 changes: 8 additions & 4 deletions packages/checkout/sdk/src/client/blockscout.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,10 @@ describe('Blockscout', () => {
const token = BlockscoutTokenType.ERC20;
const client = new Blockscout({ chainId: ChainId.IMTBL_ZKEVM_TESTNET });

await client.getTokensByWalletAddress({ walletAddress: '0x1234567890', tokenType: token });
await client.getTokensByWalletAddress({ walletAddress: '0x1234567890', tokenType: token });
const precache = await client.getTokensByWalletAddress({ walletAddress: '0x1234567890', tokenType: token });
const cached = await client.getTokensByWalletAddress({ walletAddress: '0x1234567890', tokenType: token });

expect(cached).toEqual(precache);

expect(mockedAxios.get).toHaveBeenNthCalledWith(
1,
Expand Down Expand Up @@ -278,8 +280,10 @@ describe('Blockscout', () => {

const client = new Blockscout({ chainId: ChainId.IMTBL_ZKEVM_TESTNET });

await client.getNativeTokenByWalletAddress({ walletAddress: '0x1234567890' });
await client.getNativeTokenByWalletAddress({ walletAddress: '0x1234567890' });
const precache = await client.getNativeTokenByWalletAddress({ walletAddress: '0x1234567890' });
const cached = await client.getNativeTokenByWalletAddress({ walletAddress: '0x1234567890' });

expect(cached).toEqual(precache);

expect(mockedAxios.get).toHaveBeenNthCalledWith(
1,
Expand Down
8 changes: 4 additions & 4 deletions packages/checkout/sdk/src/client/blockscout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ export class Blockscout {
if (params.nextPage) url += `&${new URLSearchParams(params.nextPage as Record<string, string>)}`;

// Cache response data to prevent unnecessary requests
const cached = this.getCache(url) as AxiosResponse;
if (cached && cached.status < 400) return Promise.resolve(cached.data);
const cached = this.getCache(url);
if (cached) return Promise.resolve(cached);

const response = await Blockscout.makeHttpRequest(url);
if (response.status >= 400) {
Expand Down Expand Up @@ -146,8 +146,8 @@ export class Blockscout {
const url = `${this.url}/api/v2/addresses/${params.walletAddress}`;

// Cache response data to prevent unnecessary requests
const cached = this.getCache(url) as AxiosResponse;
if (cached && cached.status < 400) return Promise.resolve(cached.data);
const cached = this.getCache(url);
if (cached) return Promise.resolve(cached);

const response = await Blockscout.makeHttpRequest(url);
if (response.status >= 400) {
Expand Down

0 comments on commit e0c7e16

Please sign in to comment.