Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Request retries #85

Open
wants to merge 4 commits into
base: feat/delayed-loaders
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions components/AvailableStakes/index.tsx
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
import { ethers } from "ethers";
import Image from "next/image";
import { CaretDown } from "phosphor-react";
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";
import { state } from "../../state";
import { findToken } from "../../utils/helpers";
import valueUSD from "../../utils/valueUSD";
import Box from "../Box";
import Button from "../Button";
import Text from "../Text";
import Container from "../Container";
import Flex from "../Flex";
import Heading from "../Heading";
import StakeSubRow, { ColumnType } from "../StakeSubRow";
import Table from "../Table";
import TableFilter from "../TableFilter";
import Text from "../Text";

export const AvailableStakes = () => {
const { stakes } = useSnapshot(state);
Expand All @@ -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
7 changes: 4 additions & 3 deletions components/MyStakes/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import Link from "../Link";
import StakeSubRow, { ColumnType } from "../StakeSubRow";
import Table from "../Table";
import Text from "../Text";
import { formatUnits } from "ethers/lib/utils";

export const MyStakes = () => {
const { stakes, rawBalances, balances } = useSnapshot(state);
Expand Down Expand Up @@ -113,8 +112,10 @@ export const MyStakes = () => {
let v = BigNumber.from(juiceChange)
.mul(10 ** 4)
.div(rawJuiceStake);

percentage = Number(formatUnits(v, 2)).toFixed(2);
/*
v is a percentage multiplied by 10**2, having about 4 precise points and safe to convert to number
*/
percentage = (v.toNumber() / 100).toFixed(2);
} else {
percentage = "0";
}
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