Skip to content

Commit

Permalink
fix: sync squid and contract states
Browse files Browse the repository at this point in the history
  • Loading branch information
belopash committed May 19, 2024
1 parent a2d4a6e commit b27efb9
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 21 deletions.
50 changes: 34 additions & 16 deletions src/api/contracts/vesting.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useState } from 'react';
import { useEffect, useRef, useState } from 'react';

import { chunk } from 'lodash-es';
import { erc20Abi, MulticallResponse } from 'viem';
Expand All @@ -15,7 +15,7 @@ export function useVestingContracts({ addresses }: { addresses?: `0x${string}`[]
const contracts = useContracts();
const { currentHeight, isLoading: isHeightLoading } = useSquidNetworkHeightHooks();

const { data, isRefetching, isLoading } = useReadContracts({
const { data: res } = useReadContracts({
contracts: addresses?.flatMap(address => {
const vestingContract = { abi: VESTING_CONTRACT_ABI, address } as const;
return [
Expand Down Expand Up @@ -58,26 +58,44 @@ export function useVestingContracts({ addresses }: { addresses?: `0x${string}`[]
] as const;
}),
allowFailure: true,
// blockNumber: BigInt(currentHeight || 0), // FIXME: uncomment to keep read in sync with squid, now always `isLoading` on height change
blockNumber: BigInt(currentHeight),
query: {
enabled: !!addresses && !isHeightLoading,
},
});

const data = useRef<
| {
start?: number;
end?: number;
deposited?: string | undefined;
releasable?: string | undefined;
released?: string | undefined;
balance?: string | undefined;
initialRelease?: number;
expectedTotal?: string | undefined;
}[]
| undefined
>(undefined);

useEffect(() => {
if (res?.some(r => r.status === 'success')) {
data.current = chunk(res, 8).map(ch => ({
start: Number(unwrapResult(ch[0])) * 1000,
end: Number(unwrapResult(ch[1])) * 1000,
deposited: unwrapResult(ch[2])?.toString(),
releasable: unwrapResult(ch[3])?.toString(),
released: unwrapResult(ch[4])?.toString(),
balance: unwrapResult(ch[5])?.toString(),
initialRelease: Number(unwrapResult(ch[6]) || 0) / 100,
expectedTotal: unwrapResult(ch[7])?.toString(),
}));
}
}, [res]);

return {
data: data
? chunk(data, 8).map(ch => ({
start: Number(unwrapResult(ch[0])) * 1000,
end: Number(unwrapResult(ch[1])) * 1000,
deposited: unwrapResult(ch[2])?.toString(),
releasable: unwrapResult(ch[3])?.toString(),
released: unwrapResult(ch[4])?.toString(),
balance: unwrapResult(ch[5])?.toString(),
initialRelease: Number(unwrapResult(ch[6]) || 0) / 100,
expectedTotal: unwrapResult(ch[7])?.toString(),
}))
: undefined,
isLoading: isLoading && !isRefetching,
data: data.current,
isLoading: !data.current,
};
}

Expand Down
2 changes: 1 addition & 1 deletion src/hooks/useSquidNetworkHeightHooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export function useSquidNetworkHeightHooks() {
isLoading,
isWaiting: heightHooks.length > 0,
waitHeight: maxWaitedHook,
currentHeight: currentHeight ? String(currentHeight) : undefined,
currentHeight: currentHeight ? String(currentHeight) : '0',
setWaitHeight,
};
}
21 changes: 20 additions & 1 deletion src/network/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,24 @@ import { arbitrumSepolia, arbitrum } from 'wagmi/chains';
export const wagmiConfig = getDefaultConfig({
appName: 'Subsquid Network',
projectId: process.env.WALLET_CONNECT_PROJECT_ID || '',
chains: [arbitrumSepolia, arbitrum],
chains: [
{
...arbitrumSepolia,
rpcUrls: {
default: {
http: ['https://arbitrum-sepolia.public.blastapi.io'],
webSocket: ['wss://arbitrum-sepolia.public.blastapi.io'],
},
},
},
{
...arbitrum,
rpcUrls: {
default: {
http: ['https://arbitrum-one.public.blastapi.io'],
webSocket: ['wss://arbitrum-one.public.blastapi.io'],
},
},
},
],
});
6 changes: 3 additions & 3 deletions src/pages/AssetsPage/Vestings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ export function MyVestings() {
});
const { SQD_TOKEN } = useContracts();

if (isLoading || isVestingsLoading) return <Loader />;

return (
<Box>
<NetworkPageTitle title="My Vesting" />
{assets.vestings.length ? (
{isLoading || isVestingsLoading ? (
<Loader />
) : assets.vestings.length ? (
<BorderedTable>
<TableHead>
<TableRow>
Expand Down

0 comments on commit b27efb9

Please sign in to comment.