Skip to content

Commit 210512c

Browse files
committed
Add query retries
1 parent de8a1d7 commit 210512c

6 files changed

Lines changed: 39 additions & 25 deletions

File tree

components/AvailableStakes/index.tsx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { ethers } from "ethers";
22
import Image from "next/image";
33
import React, { useCallback, useEffect, useMemo, useState } from "react";
44
import { Column, Row } from "react-table";
5+
import { retry } from "ts-retry";
56
import { useSnapshot } from "valtio";
67
import { CaretDown } from "phosphor-react";
78
import { useGetAssetPairsQuery } from "../../generated/graphql";
@@ -37,10 +38,17 @@ export const AvailableStakes = () => {
3738
useEffect(() => {
3839
const id = setTimeout(() => {
3940
if (!fetching) {
40-
executeQuery({ requestPolicy: "network-only" });
41+
retry(() => {
42+
console.log(_data)
43+
return executeQuery(
44+
{ requestPolicy: "network-only" }
45+
)
46+
},
47+
{ until: () => _data !== undefined })
4148
}
4249
}, 3000);
4350
return () => clearTimeout(id);
51+
// eslint-disable-next-line react-hooks/exhaustive-deps
4452
}, [fetching, executeQuery]);
4553

4654
const columns: Column<ColumnType>[] = useMemo(

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
"react-toastify": "^8.2.0",
3636
"sharp": "^0.29.3",
3737
"subscriptions-transport-ws": "^0.11.0",
38+
"ts-retry": "^2.3.9",
3839
"urql": "^2.0.5",
3940
"valtio": "^1.2.6",
4041
"web3modal": "^1.9.4"

pages/stake.tsx

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,10 @@ import { MyStakes } from "../components/MyStakes";
1414
import Stack from "../components/Stack";
1515
import Text from "../components/Text";
1616
import { GetAssetPairsDocument } from "../generated/graphql";
17-
import { state, VanillaEvents } from "../state";
17+
import { state } from "../state";
1818
import { fetchStakes } from "../state/actions/stakes";
1919
import { connectWallet } from "../state/actions/wallet";
2020
import client, { ssrCache } from "../urql";
21-
import { emitEvent } from "../utils/helpers";
2221

2322

2423
const StakingIntro = () => (
@@ -111,7 +110,7 @@ const Stake = () => {
111110
const { walletAddress } = snapshot(state);
112111
// Check that the event was created by the logged in user
113112
if (user.toLowerCase() === walletAddress?.toLowerCase()) {
114-
emitEvent(VanillaEvents.stakesChanged);
113+
fetchStakes();
115114
}
116115
};
117116

@@ -124,16 +123,6 @@ const Stake = () => {
124123
};
125124
}, [polygonProvider, signer, walletAddress]);
126125

127-
useEffect(() => {
128-
const onStakesChange = () => {
129-
fetchStakes();
130-
};
131-
window.addEventListener(VanillaEvents.stakesChanged, onStakesChange);
132-
return () => {
133-
window.removeEventListener(VanillaEvents.stakesChanged, onStakesChange);
134-
};
135-
}, []);
136-
137126
return (
138127
<>
139128
<Flex

state/actions/stakes.tsx

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { isAddress, Token } from "@vanilladefi/core-sdk";
22
import { getAllStakes } from "@vanilladefi/stake-sdk";
3+
import { retryAsync } from 'ts-retry';
34
import { snapshot } from "valtio";
45
import { Sentiment, Stake, state } from "..";
56
import tokens from "../../tokens";
@@ -29,10 +30,15 @@ export const fetchStakes = async () => {
2930
process.env.NEXT_PUBLIC_VANILLA_ROUTER_ADDRESS || ""
3031
);
3132

32-
const res = await getAllStakes(walletAddress, _tokens, {
33-
signerOrProvider: signer || (polygonProvider as any),
34-
optionalAddress: contractAddress || "",
35-
});
33+
const res = await retryAsync(
34+
async () => {
35+
console.log(_tokens)
36+
return await getAllStakes(walletAddress, _tokens, {
37+
signerOrProvider: signer || (polygonProvider as any),
38+
optionalAddress: contractAddress || "",
39+
})
40+
}
41+
);
3642

3743
let _stakes: Stake[] = [];
3844

state/actions/wallet.tsx

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
11
import { isAddress, vnlDecimals } from "@vanilladefi/core-sdk";
22
import {
33
getBasicWalletDetails,
4-
getJuiceStakingContract,
4+
getJuiceStakingContract
55
} from "@vanilladefi/stake-sdk";
66
import { BigNumber, providers } from "ethers";
7-
import { snapshot } from "valtio";
7+
import { parseUnits } from "ethers/lib/utils";
88
import { toast } from "react-toastify";
9+
import { retryAsync } from 'ts-retry';
10+
import { snapshot } from "valtio";
911
import { ref, state, subscribeKey, VanillaEvents } from "..";
10-
import { correctNetwork, getHexaDecimalChainId } from "../../lib/config";
12+
import {
13+
correctNetwork,
14+
getHexaDecimalChainId
15+
} from "../../lib/config";
1116
import { emitEvent, formatJuice, parseJuice } from "../../utils/helpers";
1217
import { showDialog } from "./dialog";
13-
import { parseUnits } from "ethers/lib/utils";
1418

1519
let lockedWalletToast: any;
1620

@@ -220,12 +224,13 @@ export const updateBalances = async () => {
220224
const contractAddress = isAddress(
221225
process.env.NEXT_PUBLIC_VANILLA_ROUTER_ADDRESS || ""
222226
);
223-
let { vnlBalance, ethBalance, maticBalance, juiceBalance } =
224-
await getBasicWalletDetails(walletAddress, {
227+
let { vnlBalance, ethBalance, maticBalance, juiceBalance } = await retryAsync(
228+
async () => await getBasicWalletDetails(walletAddress, {
225229
polygonProvider: polygonProvider || undefined,
226230
ethereumProvider: ethereumProvider || undefined,
227231
optionalAddress: contractAddress || undefined,
228-
});
232+
})
233+
);
229234

230235
state.balances.vnl = Number(vnlBalance).toLocaleString();
231236
state.balances.eth = Number(ethBalance).toLocaleString();

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8208,6 +8208,11 @@ ts-node@^9:
82088208
source-map-support "^0.5.17"
82098209
yn "3.1.1"
82108210

8211+
ts-retry@^2.3.9:
8212+
version "2.3.9"
8213+
resolved "https://registry.yarnpkg.com/ts-retry/-/ts-retry-2.3.9.tgz#ee7e976c8beb93fb069ef7366c5afabc8f3083e9"
8214+
integrity sha512-/O/iMy5a56uQID22RU/9OyMLAzr/9V+KkqpM4o0V6vKANbvb4Xmn3enC+LkKLs2ubqqyUOZUc70A+l3wd5SHjg==
8215+
82118216
tsconfig-paths@^3.11.0, tsconfig-paths@^3.9.0:
82128217
version "3.12.0"
82138218
resolved "https://registry.yarnpkg.com/tsconfig-paths/-/tsconfig-paths-3.12.0.tgz#19769aca6ee8f6a1a341e38c8fa45dd9fb18899b"

0 commit comments

Comments
 (0)