Skip to content

Commit

Permalink
Add query retries
Browse files Browse the repository at this point in the history
  • Loading branch information
JaniAnttonen committed Mar 22, 2022
1 parent de8a1d7 commit 210512c
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 25 deletions.
10 changes: 9 additions & 1 deletion components/AvailableStakes/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { ethers } from "ethers";
import Image from "next/image";
import React, { useCallback, useEffect, useMemo, useState } from "react";
import { Column, Row } from "react-table";
import { retry } from "ts-retry";
import { useSnapshot } from "valtio";
import { CaretDown } from "phosphor-react";
import { useGetAssetPairsQuery } from "../../generated/graphql";
Expand Down Expand Up @@ -37,10 +38,17 @@ export const AvailableStakes = () => {
useEffect(() => {
const id = setTimeout(() => {
if (!fetching) {
executeQuery({ requestPolicy: "network-only" });
retry(() => {
console.log(_data)
return executeQuery(
{ requestPolicy: "network-only" }
)
},
{ until: () => _data !== undefined })
}
}, 3000);
return () => clearTimeout(id);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [fetching, executeQuery]);

const columns: Column<ColumnType>[] = useMemo(
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"react-toastify": "^8.2.0",
"sharp": "^0.29.3",
"subscriptions-transport-ws": "^0.11.0",
"ts-retry": "^2.3.9",
"urql": "^2.0.5",
"valtio": "^1.2.6",
"web3modal": "^1.9.4"
Expand Down
15 changes: 2 additions & 13 deletions pages/stake.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,10 @@ import { MyStakes } from "../components/MyStakes";
import Stack from "../components/Stack";
import Text from "../components/Text";
import { GetAssetPairsDocument } from "../generated/graphql";
import { state, VanillaEvents } from "../state";
import { state } from "../state";
import { fetchStakes } from "../state/actions/stakes";
import { connectWallet } from "../state/actions/wallet";
import client, { ssrCache } from "../urql";
import { emitEvent } from "../utils/helpers";


const StakingIntro = () => (
Expand Down Expand Up @@ -111,7 +110,7 @@ const Stake = () => {
const { walletAddress } = snapshot(state);
// Check that the event was created by the logged in user
if (user.toLowerCase() === walletAddress?.toLowerCase()) {
emitEvent(VanillaEvents.stakesChanged);
fetchStakes();
}
};

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

useEffect(() => {
const onStakesChange = () => {
fetchStakes();
};
window.addEventListener(VanillaEvents.stakesChanged, onStakesChange);
return () => {
window.removeEventListener(VanillaEvents.stakesChanged, onStakesChange);
};
}, []);

return (
<>
<Flex
Expand Down
14 changes: 10 additions & 4 deletions state/actions/stakes.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { isAddress, Token } from "@vanilladefi/core-sdk";
import { getAllStakes } from "@vanilladefi/stake-sdk";
import { retryAsync } from 'ts-retry';
import { snapshot } from "valtio";
import { Sentiment, Stake, state } from "..";
import tokens from "../../tokens";
Expand Down Expand Up @@ -29,10 +30,15 @@ export const fetchStakes = async () => {
process.env.NEXT_PUBLIC_VANILLA_ROUTER_ADDRESS || ""
);

const res = await getAllStakes(walletAddress, _tokens, {
signerOrProvider: signer || (polygonProvider as any),
optionalAddress: contractAddress || "",
});
const res = await retryAsync(
async () => {
console.log(_tokens)
return await getAllStakes(walletAddress, _tokens, {
signerOrProvider: signer || (polygonProvider as any),
optionalAddress: contractAddress || "",
})
}
);

let _stakes: Stake[] = [];

Expand Down
19 changes: 12 additions & 7 deletions state/actions/wallet.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
import { isAddress, vnlDecimals } from "@vanilladefi/core-sdk";
import {
getBasicWalletDetails,
getJuiceStakingContract,
getJuiceStakingContract
} from "@vanilladefi/stake-sdk";
import { BigNumber, providers } from "ethers";
import { snapshot } from "valtio";
import { parseUnits } from "ethers/lib/utils";
import { toast } from "react-toastify";
import { retryAsync } from 'ts-retry';
import { snapshot } from "valtio";
import { ref, state, subscribeKey, VanillaEvents } from "..";
import { correctNetwork, getHexaDecimalChainId } from "../../lib/config";
import {
correctNetwork,
getHexaDecimalChainId
} from "../../lib/config";
import { emitEvent, formatJuice, parseJuice } from "../../utils/helpers";
import { showDialog } from "./dialog";
import { parseUnits } from "ethers/lib/utils";

let lockedWalletToast: any;

Expand Down Expand Up @@ -220,12 +224,13 @@ export const updateBalances = async () => {
const contractAddress = isAddress(
process.env.NEXT_PUBLIC_VANILLA_ROUTER_ADDRESS || ""
);
let { vnlBalance, ethBalance, maticBalance, juiceBalance } =
await getBasicWalletDetails(walletAddress, {
let { vnlBalance, ethBalance, maticBalance, juiceBalance } = await retryAsync(
async () => await getBasicWalletDetails(walletAddress, {
polygonProvider: polygonProvider || undefined,
ethereumProvider: ethereumProvider || undefined,
optionalAddress: contractAddress || undefined,
});
})
);

state.balances.vnl = Number(vnlBalance).toLocaleString();
state.balances.eth = Number(ethBalance).toLocaleString();
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -8208,6 +8208,11 @@ ts-node@^9:
source-map-support "^0.5.17"
yn "3.1.1"

ts-retry@^2.3.9:
version "2.3.9"
resolved "https://registry.yarnpkg.com/ts-retry/-/ts-retry-2.3.9.tgz#ee7e976c8beb93fb069ef7366c5afabc8f3083e9"
integrity sha512-/O/iMy5a56uQID22RU/9OyMLAzr/9V+KkqpM4o0V6vKANbvb4Xmn3enC+LkKLs2ubqqyUOZUc70A+l3wd5SHjg==

tsconfig-paths@^3.11.0, tsconfig-paths@^3.9.0:
version "3.12.0"
resolved "https://registry.yarnpkg.com/tsconfig-paths/-/tsconfig-paths-3.12.0.tgz#19769aca6ee8f6a1a341e38c8fa45dd9fb18899b"
Expand Down

0 comments on commit 210512c

Please sign in to comment.