Skip to content

Commit

Permalink
Add retries for only the failed requests
Browse files Browse the repository at this point in the history
  • Loading branch information
serjonya-trili committed Sep 12, 2023
1 parent 2d38d02 commit 5e71b3c
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 23 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
"@types/node": "18.15.0",
"@types/papaparse": "^5.3.7",
"@types/pluralize": "^0.0.30",
"@types/promise-retry": "^1.1.3",
"@types/prop-types": "^15.7.5",
"@types/react": "18.2.0",
"@types/react-dom": "18.2.0",
Expand Down Expand Up @@ -117,6 +118,7 @@
"pluralize": "^8.0.0",
"prettier": "^2.8.8",
"process": "^0.11.10",
"promise-retry": "^2.0.1",
"prop-types": "^15.8.1",
"react": "^18.2.0",
"react-app-rewired": "^2.2.1",
Expand Down
1 change: 1 addition & 0 deletions src/utils/multisig/fetch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { getAllMultiSigContracts } from "./fetch";
import { GHOSTNET } from "../../types/Network";

jest.mock("axios");
jest.unmock("../tezos");

const mockedAxios = axios as jest.Mocked<typeof axios>;

Expand Down
39 changes: 20 additions & 19 deletions src/utils/multisig/fetch.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,32 @@
import axios from "axios";
import { Network } from "../../types/Network";
import { RawTzktGetBigMapKeys, RawTzktGetSameMultisigs } from "../tzkt/types";
import { withRateLimit } from "../tezos";
const MULTISIG_FETCH_LIMIT = 10000;
const TYPE_HASH = 1963879877;
const CODE_HASH = -1890025422;

export const getAllMultiSigContracts = async (
network: Network
): Promise<RawTzktGetSameMultisigs> => {
try {
const url = `${network.tzktApiUrl}/v1/contracts?typeHash=${TYPE_HASH}&codeHash=${CODE_HASH}&includeStorage=true&limit=${MULTISIG_FETCH_LIMIT}`;
const { data } = await axios.get<RawTzktGetSameMultisigs>(url);
export const getAllMultiSigContracts = (network: Network): Promise<RawTzktGetSameMultisigs> =>
withRateLimit(async () => {
try {
const url = `${network.tzktApiUrl}/v1/contracts?typeHash=${TYPE_HASH}&codeHash=${CODE_HASH}&includeStorage=true&limit=${MULTISIG_FETCH_LIMIT}`;
const { data } = await axios.get<RawTzktGetSameMultisigs>(url);

return data;
} catch (error: any) {
throw new Error(`Error fetching same contracts from tzkt: ${error.message}`);
}
};
return data;
} catch (error: any) {
throw new Error(`Error fetching same contracts from tzkt: ${error.message}`);
}
});

// get all pending operations for a multisig contract address
export const getPendingOperations = async (
export const getPendingOperations = (
bigMaps: number[],
network: Network
): Promise<RawTzktGetBigMapKeys> => {
const url = `${network.tzktApiUrl}/v1/bigmaps/keys?active=true&bigmap.in=${bigMaps.join(
","
)}&limit=${MULTISIG_FETCH_LIMIT}`;
const { data } = await axios.get<RawTzktGetBigMapKeys>(url);
return data;
};
): Promise<RawTzktGetBigMapKeys> =>
withRateLimit(async () => {
const url = `${network.tzktApiUrl}/v1/bigmaps/keys?active=true&bigmap.in=${bigMaps.join(
","
)}&limit=${MULTISIG_FETCH_LIMIT}`;
const { data } = await axios.get<RawTzktGetBigMapKeys>(url);
return data;
});
3 changes: 2 additions & 1 deletion src/utils/tezos/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { TezTransfer } from "../../types/Transfer";
import { RawTokenBalance } from "../../types/TokenBalance";
import { Network } from "../../types/Network";
import Semaphore from "@chriscdn/promise-semaphore";
import promiseRetry from "promise-retry";

// TzKT defines type Account = {type: string};
// whilst accountsGet returns all the info about accounts
Expand All @@ -26,7 +27,7 @@ const tzktRateLimiter = new Semaphore(10);
export const withRateLimit = <T>(fn: () => Promise<T>) =>
tzktRateLimiter
.acquire()
.then(fn)
.then(() => promiseRetry(fn, { retries: 3, minTimeout: 100 }))
.finally(() => tzktRateLimiter.release());

export const getAccounts = async (pkhs: string[], network: Network): Promise<TzktAccount[]> =>
Expand Down
1 change: 1 addition & 0 deletions src/utils/useAssetsPolling.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ jest.unmock("./tezos");
jest.mock("@tzkt/sdk-api", () => {
return {
delegatesGet: jest.fn(),
blocksGetCount: jest.fn(),
};
});

Expand Down
11 changes: 8 additions & 3 deletions src/utils/useAssetsPolling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import { useSelectedNetwork } from "./hooks/networkHooks";
import { AppDispatch } from "./redux/store";
import { RawPkh } from "../types/Address";
import { useToast } from "@chakra-ui/react";
import { Multisig } from "./multisig/types";

const getTezTransfersPayload = async (
pkh: string,
Expand All @@ -45,6 +46,7 @@ const getTokensTransfersPayload = async (
network: Network
): Promise<TokenTransfersPayload> => {
const transfers = await getTokenTransfers(pkh, network);

// there are no token transfers without a token & amount assigned
return { pkh, transfers: transfers as TokenTransfer[] };
};
Expand All @@ -71,8 +73,8 @@ const MAX_BIGMAP_PER_REQUEST = 300;

const updatePendingOperations = async (
dispatch: AppDispatch,
multisigs: any[],
network: Network
network: Network,
multisigs: Multisig[]
) => {
const multisigChunks = chunk(multisigs, MAX_BIGMAP_PER_REQUEST);
const pendingOperations = await Promise.all(
Expand Down Expand Up @@ -140,7 +142,7 @@ const updateAccountAssets = async (
// all these requests should happen only after we fetched the multisigs
// otherwise, we might miss multisig operations until after the next fetch happens
await Promise.all([
updatePendingOperations(dispatch, multisigs, network),
updatePendingOperations(dispatch, network, multisigs),
updateTezBalances(dispatch, network, allAccountAddresses),
updateTezTransfers(dispatch, network, allAccountAddresses),
updateDelegations(dispatch, network, allAccountAddresses),
Expand Down Expand Up @@ -193,6 +195,7 @@ export const useAssetsPolling = () => {
isClosable: true,
});
},
retry: false, // retries are handled by the underlying functions
refetchInterval: BLOCK_TIME,
refetchIntervalInBackground: true,
refetchOnWindowFocus: false,
Expand All @@ -207,13 +210,15 @@ export const useAssetsPolling = () => {

const blockNumberQuery = useQuery("blockNumber", {
queryFn: () => updateBlockLevel(dispatch, network),
retry: false, // retries are handled by the underlying functions
refetchInterval: BLOCK_TIME,
refetchIntervalInBackground: true,
refetchOnWindowFocus: false,
});

const bakersQuery = useQuery("bakers", {
queryFn: () => updateBakers(dispatch, network),
retry: false, // retries are handled by the underlying functions
refetchInterval: BAKERS_REFRESH_RATE,
refetchIntervalInBackground: true,
refetchOnWindowFocus: false,
Expand Down
18 changes: 18 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -8447,6 +8447,15 @@ __metadata:
languageName: node
linkType: hard

"@types/promise-retry@npm:^1.1.3":
version: 1.1.3
resolution: "@types/promise-retry@npm:1.1.3"
dependencies:
"@types/retry": "*"
checksum: c78619e5e035f97f9b851c39c54413f24057647dacba894cf7d2410f1b4565f4c67aa2e59276c1562490feef55d4fb46c40e27893bb91a027e3a7863289da7f5
languageName: node
linkType: hard

"@types/prop-types@npm:*, @types/prop-types@npm:^15.7.5":
version: 15.7.5
resolution: "@types/prop-types@npm:15.7.5"
Expand Down Expand Up @@ -8562,6 +8571,13 @@ __metadata:
languageName: node
linkType: hard

"@types/retry@npm:*":
version: 0.12.2
resolution: "@types/retry@npm:0.12.2"
checksum: e5675035717b39ce4f42f339657cae9637cf0c0051cf54314a6a2c44d38d91f6544be9ddc0280587789b6afd056be5d99dbe3e9f4df68c286c36321579b1bf4a
languageName: node
linkType: hard

"@types/retry@npm:0.12.0":
version: 0.12.0
resolution: "@types/retry@npm:0.12.0"
Expand Down Expand Up @@ -23727,6 +23743,7 @@ __metadata:
"@types/node": 18.15.0
"@types/papaparse": ^5.3.7
"@types/pluralize": ^0.0.30
"@types/promise-retry": ^1.1.3
"@types/prop-types": ^15.7.5
"@types/react": 18.2.0
"@types/react-dom": 18.2.0
Expand Down Expand Up @@ -23764,6 +23781,7 @@ __metadata:
pluralize: ^8.0.0
prettier: ^2.8.8
process: ^0.11.10
promise-retry: ^2.0.1
prop-types: ^15.8.1
react: ^18.2.0
react-app-rewired: ^2.2.1
Expand Down

0 comments on commit 5e71b3c

Please sign in to comment.