diff --git a/hooks/useStakingData.ts b/hooks/useStakingData.ts index 660e1f8e59..6e7fa073df 100644 --- a/hooks/useStakingData.ts +++ b/hooks/useStakingData.ts @@ -114,6 +114,7 @@ const useStakingData = () => { const [kwentaAllowance, setKwentaAllowance] = useState(zeroBN); const [veKwentaBalance, setVEKwentaBalance] = useState(zeroBN); const [veKwentaAllowance, setVEKwentaAllowance] = useState(zeroBN); + const [totalVestable, setTotalVestable] = useState(0); const { refetch: resetStakingState } = useContractReads({ contracts: [ @@ -255,13 +256,14 @@ const useStakingData = () => { escrowRows[index].vestable = Number(d?.quantity / 1e18) ?? 0; escrowRows[index].fee = Number(d?.fee / 1e18) ?? 0; }); + setTotalVestable( + Object.values(escrowRows) + .map((d) => d.vestable) + .reduce((acc, curr) => acc + curr, 0) + ); }, }); - const totalVestable = useMemo(() => { - return escrowRows.reduce((acc, row) => wei(acc).add(wei(row.vestable ?? 0)) ?? zeroBN, zeroBN); - }, [escrowRows]); - const resetTime = useMemo(() => { const { epochEnd } = getEpochDetails(network?.id, epochPeriod); return epochEnd; diff --git a/package-lock.json b/package-lock.json index 8d999d59df..8ac8b5e3d4 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "kwenta", - "version": "5.1.1", + "version": "5.1.2", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "kwenta", - "version": "5.1.1", + "version": "5.1.2", "hasInstallScript": true, "dependencies": { "@artsy/fresnel": "1.7.0", diff --git a/package.json b/package.json index 7069632bc4..5fd26626e5 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "kwenta", - "version": "5.1.1", + "version": "5.1.2", "scripts": { "dev": "next", "build": "next build", diff --git a/queries/futures/useQueryCrossMarginAccount.ts b/queries/futures/useQueryCrossMarginAccount.ts index b0e70cd79b..d428dc4a26 100644 --- a/queries/futures/useQueryCrossMarginAccount.ts +++ b/queries/futures/useQueryCrossMarginAccount.ts @@ -1,6 +1,4 @@ -import { NetworkId } from '@synthetixio/contracts-interface'; -import request, { gql } from 'graphql-request'; -import { useEffect } from 'react'; +import { useEffect, useState } from 'react'; import { useRecoilState } from 'recoil'; import { CROSS_MARGIN_ACCOUNT_FACTORY } from 'constants/address'; @@ -11,21 +9,32 @@ import logError from 'utils/logError'; import useCrossMarginAccountContracts from '../../hooks/useCrossMarginContracts'; import { FuturesAccountState } from './types'; -import { getFuturesEndpoint } from './utils'; const SUPPORTED_NETWORKS = Object.keys(CROSS_MARGIN_ACCOUNT_FACTORY); export default function useQueryCrossMarginAccount() { const { crossMarginContractFactory } = useCrossMarginAccountContracts(); - const { network, walletAddress } = Connector.useContainer(); + const { network, walletAddress, signer } = Connector.useContainer(); const [futuresAccount, setFuturesAccount] = useRecoilState(futuresAccountState); const [storedCrossMarginAccounts, setStoredCrossMarginAccount] = usePersistedRecoilState( crossMarginAccountsState ); - const futuresEndpoint = getFuturesEndpoint(network?.id as NetworkId); + const [retryCount, setRetryCount] = useState(0); const handleAccountQuery = async () => { + const queryAccountFromLogs = async (address: string): Promise => { + if (!signer || !crossMarginContractFactory) return null; + const accountFilter = crossMarginContractFactory.filters.NewAccount(address); + if (accountFilter) { + const logs = await crossMarginContractFactory.queryFilter(accountFilter); + if (logs.length) { + return logs[0].args?.[1] || null; + } + } + return null; + }; + if (!SUPPORTED_NETWORKS.includes(String(network.id))) { const accountState: FuturesAccountState = { crossMarginAvailable: false, @@ -71,20 +80,7 @@ export default function useQueryCrossMarginAccount() { walletAddress, }); - const response = await request( - futuresEndpoint, - gql` - query crossMarginAccounts($owner: String!) { - crossMarginAccounts(where: { owner: $owner }) { - id - owner - } - } - `, - { owner: walletAddress } - ); - - const crossMarginAccount = response?.crossMarginAccounts[0]?.id || null; + const crossMarginAccount = await queryAccountFromLogs(walletAddress); const existingAccounts = crossMarginContractFactory ? storedCrossMarginAccounts[crossMarginContractFactory.address] @@ -114,12 +110,22 @@ export default function useQueryCrossMarginAccount() { try { return await handleAccountQuery(); } catch (err) { - logError(err); - setFuturesAccount({ - ...futuresAccount, - status: 'error', - }); - return null; + // This is a hacky workaround to deal with the delayed Metamask error + // which causes the logs query to fail on network switching + // https://github.com/MetaMask/metamask-extension/issues/13375#issuecomment-1046125113 + if (err.message.includes('underlying network changed') && retryCount < 5) { + setTimeout(() => { + setRetryCount(retryCount + 1); + }, 500); + } else { + setRetryCount(0); + logError(err); + setFuturesAccount({ + ...futuresAccount, + status: 'error', + }); + return null; + } } }; @@ -128,5 +134,12 @@ export default function useQueryCrossMarginAccount() { // eslint-disable-next-line }, [walletAddress, crossMarginContractFactory?.address]); + useEffect(() => { + if (retryCount) { + queryAccount(); + } + // eslint-disable-next-line + }, [retryCount]); + return queryAccount; } diff --git a/sections/dashboard/Stake/EscrowTable.tsx b/sections/dashboard/Stake/EscrowTable.tsx index e3d2a12bfe..d35ca2d698 100644 --- a/sections/dashboard/Stake/EscrowTable.tsx +++ b/sections/dashboard/Stake/EscrowTable.tsx @@ -11,7 +11,6 @@ import { TableCellHead } from 'components/Table/Table'; import { monitorTransaction } from 'contexts/RelayerContext'; import { useStakingContext } from 'contexts/StakingContext'; import { EscrowRow } from 'hooks/useStakingData'; -import { STAKING_LOW_GAS_LIMIT } from 'queries/staking/utils'; import { truncateNumbers } from 'utils/formatters/number'; import { StakingCard } from './common'; @@ -75,9 +74,6 @@ const EscrowTable = () => { const { config } = usePrepareContractWrite({ ...rewardEscrowContract, functionName: 'vest', - overrides: { - gasLimit: STAKING_LOW_GAS_LIMIT, - }, args: [escrowRows.filter((d, index) => !!checkedState[index]).map((d) => d.id)], enabled: escrowRows.filter((d, index) => !!checkedState[index]).map((d) => d.id).length > 0, }); diff --git a/translations/en.json b/translations/en.json index 5fde6ba0e0..61597be139 100644 --- a/translations/en.json +++ b/translations/en.json @@ -64,7 +64,7 @@ }, "hero": { "title": "TRADE SYNTHETIC PERPETUALS", - "copy": "Gain exposure to a variety of asset with up to <0>25x leverage and <0>deep liquidity." + "copy": "Gain exposure to a variety of assets with up to <0>25x leverage and <0>deep liquidity." }, "assets": { "title": "UNIQUE MARKETS",