Skip to content

Commit

Permalink
feat: implement squid height context
Browse files Browse the repository at this point in the history
  • Loading branch information
belopash committed Jun 11, 2024
1 parent 2fc262b commit f7735c3
Show file tree
Hide file tree
Showing 15 changed files with 114 additions and 75 deletions.
11 changes: 7 additions & 4 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { WagmiProvider } from 'wagmi';

import { queryClient } from '@api/client';
import { Alert } from '@components/Alert';
import { SquidHeightProvider } from '@hooks/useSquidNetworkHeightHooks';
import { wagmiConfig } from '@network/config';

import { AppRoutes } from './AppRoutes';
Expand All @@ -33,10 +34,12 @@ function App() {
}}
anchorOrigin={{ vertical: 'bottom', horizontal: 'right' }}
>
<CssBaseline />
<BrowserRouter>
<AppRoutes />
</BrowserRouter>
<SquidHeightProvider>
<CssBaseline />
<BrowserRouter>
<AppRoutes />
</BrowserRouter>
</SquidHeightProvider>
</SnackbarProvider>
</RainbowKitProvider>
</ThemeProvider>
Expand Down
4 changes: 2 additions & 2 deletions src/api/contracts/claim.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { useWriteContract, useClient } from 'wagmi';
import { REWARD_TREASURY_CONTRACT_ABI } from '@api/contracts/reaward-treasury.abi';
import { VESTING_CONTRACT_ABI } from '@api/contracts/vesting.abi';
import { AccountType, SourceWallet } from '@api/subsquid-network-squid';
import { useSquidNetworkHeightHooks } from '@hooks/useSquidNetworkHeightHooks.ts';
import { useSquidNetworkHeight } from '@hooks/useSquidNetworkHeightHooks';
import { useAccount } from '@network/useAccount.ts';
import { useContracts } from '@network/useContracts.ts';

Expand Down Expand Up @@ -67,7 +67,7 @@ function useClaimFromVestingContract() {

export function useClaim() {
const client = useClient();
const { setWaitHeight } = useSquidNetworkHeightHooks();
const { setWaitHeight } = useSquidNetworkHeight();
const [isLoading, setLoading] = useState(false);
const [error, setError] = useState<string | null>(null);

Expand Down
4 changes: 2 additions & 2 deletions src/api/contracts/gateway-registration/useRegisterGateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { encodeFunctionData } from 'viem';
import { usePublicClient, useWriteContract } from 'wagmi';

import { AccountType } from '@api/subsquid-network-squid';
import { useSquidNetworkHeightHooks } from '@hooks/useSquidNetworkHeightHooks.ts';
import { useSquidNetworkHeight } from '@hooks/useSquidNetworkHeightHooks';
import { useAccount } from '@network/useAccount';
import { useContracts } from '@network/useContracts.ts';

Expand Down Expand Up @@ -82,7 +82,7 @@ export function useRegisterGateway() {
const [error, setError] = useState<string | null>(null);
const [isLoading, setLoading] = useState(false);

const { setWaitHeight } = useSquidNetworkHeightHooks();
const { setWaitHeight } = useSquidNetworkHeight();
const registerGatewayFromWallet = useRegisterGatewayFromWallet();
const registerGatewayFromVestingContract = useRegisterGatewayFromVestingContract();

Expand Down
4 changes: 2 additions & 2 deletions src/api/contracts/gateway-registration/useStakeGateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { encodeFunctionData } from 'viem';
import { usePublicClient, useWriteContract } from 'wagmi';

import { AccountType, SourceWallet } from '@api/subsquid-network-squid';
import { useSquidNetworkHeightHooks } from '@hooks/useSquidNetworkHeightHooks.ts';
import { useSquidNetworkHeight } from '@hooks/useSquidNetworkHeightHooks';
import { useAccount } from '@network/useAccount';
import { useContracts } from '@network/useContracts.ts';

Expand Down Expand Up @@ -105,7 +105,7 @@ function useStakeFromVestingContract() {

export function useStakeGateway() {
const client = usePublicClient();
const { setWaitHeight } = useSquidNetworkHeightHooks();
const { setWaitHeight } = useSquidNetworkHeight();
const [isLoading, setLoading] = useState(false);
const [error, setError] = useState<string | null>(null);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { useWriteContract, usePublicClient } from 'wagmi';

import { AccountType } from '@api/subsquid-network-squid';
import { BlockchainGateway } from '@api/subsquid-network-squid/gateways-graphql';
import { useSquidNetworkHeightHooks } from '@hooks/useSquidNetworkHeightHooks.ts';
import { useSquidNetworkHeight } from '@hooks/useSquidNetworkHeightHooks';
import { useAccount } from '@network/useAccount';
import { useContracts } from '@network/useContracts.ts';

Expand Down Expand Up @@ -78,7 +78,7 @@ export function useUnregisterGateway() {
const [error, setError] = useState<string | null>(null);
const [isLoading, setLoading] = useState(false);

const { setWaitHeight } = useSquidNetworkHeightHooks();
const { setWaitHeight } = useSquidNetworkHeight();
const unregisterGatewayFromWallet = useUnregisterGatewayFromWallet();
const unregisterGatewayFromVestingContract = useUnregisterGatewayFromVestingContract();

Expand Down
4 changes: 2 additions & 2 deletions src/api/contracts/gateway-registration/useUnstakeGateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { encodeFunctionData } from 'viem';
import { useWriteContract, usePublicClient } from 'wagmi';

import { AccountType, GatewayStakeFragmentFragment } from '@api/subsquid-network-squid';
import { useSquidNetworkHeightHooks } from '@hooks/useSquidNetworkHeightHooks.ts';
import { useSquidNetworkHeight } from '@hooks/useSquidNetworkHeightHooks';
import { useAccount } from '@network/useAccount';
import { useContracts } from '@network/useContracts.ts';

Expand Down Expand Up @@ -65,7 +65,7 @@ function useUnstakeFromVestingContract() {

export function useUnstakeGateway() {
const client = usePublicClient();
const { setWaitHeight } = useSquidNetworkHeightHooks();
const { setWaitHeight } = useSquidNetworkHeight();
const [isLoading, setLoading] = useState(false);
const [error, setError] = useState<string | null>(null);

Expand Down
8 changes: 4 additions & 4 deletions src/api/contracts/staking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
import { useApproveSqd } from '@api/contracts/sqd';
import { VESTING_CONTRACT_ABI } from '@api/contracts/vesting.abi';
import { AccountType, Worker, SourceWallet } from '@api/subsquid-network-squid';
import { useSquidNetworkHeightHooks } from '@hooks/useSquidNetworkHeightHooks.ts';
import { useSquidNetworkHeight } from '@hooks/useSquidNetworkHeightHooks';
import { useAccount } from '@network/useAccount';
import { useContracts } from '@network/useContracts.ts';

Expand Down Expand Up @@ -104,7 +104,7 @@ function useDepositFromVestingContract() {

export function useWorkerDelegate() {
const client = useClient();
const { setWaitHeight } = useSquidNetworkHeightHooks();
const { setWaitHeight } = useSquidNetworkHeight();
const [isLoading, setLoading] = useState(false);
const [error, setError] = useState<string | null>(null);

Expand Down Expand Up @@ -195,7 +195,7 @@ function useUndelegateFromVestingContract() {

export function useWorkerUndelegate() {
const client = usePublicClient();
const { setWaitHeight } = useSquidNetworkHeightHooks();
const { setWaitHeight } = useSquidNetworkHeight();
const [isLoading, setLoading] = useState(false);
const [error, setError] = useState<string | null>(null);

Expand Down Expand Up @@ -238,7 +238,7 @@ export function useWorkerUndelegate() {

export function useCapedStake({ workerId }: { workerId?: string }) {
const contracts = useContracts();
const { currentHeight, isLoading: isHeightLoading } = useSquidNetworkHeightHooks();
const { currentHeight, isLoading: isHeightLoading } = useSquidNetworkHeight();

const { data, isLoading } = useReadContract({
address: contracts.SOFT_CAP,
Expand Down
6 changes: 3 additions & 3 deletions src/api/contracts/vesting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ import { erc20Abi, MulticallResponse } from 'viem';
import { waitForTransactionReceipt } from 'viem/actions';
import { useReadContracts, useWriteContract, useClient } from 'wagmi';

import { useSquidNetworkHeightHooks } from '@hooks/useSquidNetworkHeightHooks';
import { useSquidNetworkHeight } from '@hooks/useSquidNetworkHeightHooks';
import { useContracts } from '@network/useContracts';

import { errorMessage, WriteContractRes } from './utils';
import { VESTING_CONTRACT_ABI } from './vesting.abi';

export function useVestingContracts({ addresses }: { addresses: `0x${string}`[] }) {
const contracts = useContracts();
const { currentHeight, isLoading: isSquidHeightLoading } = useSquidNetworkHeightHooks();
const { currentHeight, isLoading: isSquidHeightLoading } = useSquidNetworkHeight();

const { data, isLoading } = useReadContracts({
contracts: addresses.flatMap(address => {
Expand Down Expand Up @@ -119,7 +119,7 @@ function unwrapResult<T>(result?: MulticallResponse<T>): T | undefined {

export function useVestingContractRelease() {
const client = useClient();
const { setWaitHeight } = useSquidNetworkHeightHooks();
const { setWaitHeight } = useSquidNetworkHeight();
const [isLoading, setLoading] = useState(false);
const [error, setError] = useState<string | null>(null);
const { SQD } = useContracts();
Expand Down
4 changes: 2 additions & 2 deletions src/api/contracts/worker-registration/useRegisterWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { useWriteContract, usePublicClient, useClient } from 'wagmi';
import { useApproveSqd } from '@api/contracts/sqd';
import { VESTING_CONTRACT_ABI } from '@api/contracts/vesting.abi';
import { AccountType, SourceWallet } from '@api/subsquid-network-squid';
import { useSquidNetworkHeightHooks } from '@hooks/useSquidNetworkHeightHooks.ts';
import { useSquidNetworkHeight } from '@hooks/useSquidNetworkHeightHooks';
import { useAccount } from '@network/useAccount';
import { useContracts } from '@network/useContracts.ts';

Expand Down Expand Up @@ -120,7 +120,7 @@ export function useRegisterWorker() {
const [error, setError] = useState<string | null>(null);
const [isLoading, setLoading] = useState(false);

const { setWaitHeight } = useSquidNetworkHeightHooks();
const { setWaitHeight } = useSquidNetworkHeight();
const registerWorkerContract = useRegisterFromWallet();
const registerVestingContract = useRegisterWorkerFromVestingContract();

Expand Down
4 changes: 2 additions & 2 deletions src/api/contracts/worker-registration/useUnregisterWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { useWriteContract, useClient } from 'wagmi';

import { VESTING_CONTRACT_ABI } from '@api/contracts/vesting.abi';
import { AccountType } from '@api/subsquid-network-squid';
import { useSquidNetworkHeightHooks } from '@hooks/useSquidNetworkHeightHooks.ts';
import { useSquidNetworkHeight } from '@hooks/useSquidNetworkHeightHooks';
import { useAccount } from '@network/useAccount';
import { useContracts } from '@network/useContracts.ts';

Expand Down Expand Up @@ -76,7 +76,7 @@ export function useUnregisterWorker() {
const client = useClient();
const { address } = useAccount();
const [isLoading, setLoading] = useState(false);
const { setWaitHeight } = useSquidNetworkHeightHooks();
const { setWaitHeight } = useSquidNetworkHeight();
const [error, setError] = useState<string | null>(null);

const unregisterWorkerFromWallet = useUnregisterWorkerFromWallet();
Expand Down
4 changes: 2 additions & 2 deletions src/api/contracts/worker-registration/useUpdateWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { waitForTransactionReceipt } from 'viem/actions';
import { useWriteContract, useClient } from 'wagmi';

import { AccountType } from '@api/subsquid-network-squid';
import { useSquidNetworkHeightHooks } from '@hooks/useSquidNetworkHeightHooks.ts';
import { useSquidNetworkHeight } from '@hooks/useSquidNetworkHeightHooks';
import { useAccount } from '@network/useAccount';
import { useContracts } from '@network/useContracts.ts';

Expand Down Expand Up @@ -79,7 +79,7 @@ export function useUpdateWorker() {
const [error, setError] = useState<string | null>(null);
const [isLoading, setLoading] = useState(false);

const { setWaitHeight } = useSquidNetworkHeightHooks();
const { setWaitHeight } = useSquidNetworkHeight();
const updateWorkerFromWallet = useUpdateWorkerFromWallet();
const updateWorkerFromVestingContract = useUpdateWorkerFromVestingContract();

Expand Down
4 changes: 2 additions & 2 deletions src/api/contracts/worker-registration/useWithdrawWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { useWriteContract, useClient } from 'wagmi';
import { VESTING_CONTRACT_ABI } from '@api/contracts/vesting.abi';
import { UnregisterWorkerRequest } from '@api/contracts/worker-registration/useUnregisterWorker';
import { AccountType } from '@api/subsquid-network-squid';
import { useSquidNetworkHeightHooks } from '@hooks/useSquidNetworkHeightHooks.ts';
import { useSquidNetworkHeight } from '@hooks/useSquidNetworkHeightHooks';
import { useAccount } from '@network/useAccount';
import { useContracts } from '@network/useContracts.ts';

Expand Down Expand Up @@ -69,7 +69,7 @@ export function useWithdrawWorker() {
const client = useClient();
const { address } = useAccount();
const [isLoading, setLoading] = useState(false);
const { setWaitHeight } = useSquidNetworkHeightHooks();
const { setWaitHeight } = useSquidNetworkHeight();
const [error, setError] = useState<string | null>(null);

const withdrawWorkerFromWallet = useWithdrawWorkerFromWallet();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,49 @@
import { useEffect, useMemo } from 'react';
import {
createContext,
PropsWithChildren,
useCallback,
useContext,
useEffect,
useMemo,
} from 'react';

import { logger } from '@logger';
import { useQueryClient } from '@tanstack/react-query';
import { max, partition } from 'lodash-es';
import { useBlockNumber } from 'wagmi';

import { useSquidDataSource, useSquidNetworkHeightQuery } from '@api/subsquid-network-squid';
import { localStorageStringSerializer, useLocalStorageState } from '@hooks/useLocalStorageState';

type HeightHook = { height: number; invalidateQueries: unknown[] };

export function useSquidNetworkHeightHooks() {
const SquidHeightContext = createContext<{
currentHeight: number;
waitHeight: number;
heightHooks: HeightHook[];
isLoading: boolean;
setWaitHeight: (height: bigint | string, invalidateQueries?: unknown[]) => void;
}>({
currentHeight: 0,
waitHeight: 0,
isLoading: false,
heightHooks: [],
setWaitHeight: () => {},
});

export function useSquidNetworkHeight() {
const { isLoading, currentHeight, waitHeight, setWaitHeight } = useContext(SquidHeightContext);

return {
isLoading,
isWaiting: currentHeight < waitHeight,
currentHeight,
waitHeight,
setWaitHeight,
};
}

export function SquidHeightProvider({ children }: PropsWithChildren) {
const queryClient = useQueryClient();
const dataSource = useSquidDataSource();
const [heightHooksRaw, setHeightHooksRaw] = useLocalStorageState<string>('squid_height_hooks', {
Expand All @@ -30,28 +64,14 @@ export function useSquidNetworkHeightHooks() {
const heightHooks: HeightHook[] = useMemo(() => {
try {
return JSON.parse(heightHooksRaw);
} catch (e: any) {
logger.error(`Cant parse json form squid_height_hooks: ${e.message}`);
} catch (e: unknown) {
logger.error(
`Cant parse json form squid_height_hooks: ${e instanceof Error ? e.message : e}`,
);
return [];
}
}, [heightHooksRaw]);

const maxWaitedHook = useMemo(() => {
return max(heightHooks.map(h => h.height)) || 0;
}, [heightHooks]);

const setWaitHeight = useMemo(() => {
return (height: bigint | string, invalidateQueries: unknown[] = []) => {
heightHooks.push({
height: Number(height),
invalidateQueries: invalidateQueries,
});
heightHooks.splice(0, heightHooks.length - 10);

setHeightHooksRaw(JSON.stringify(heightHooks));
};
}, [heightHooks, setHeightHooksRaw]);

useEffect(() => {
const [notReady, ready] = partition(heightHooks, hook => hook.height > currentHeight);
if (!ready.length) return;
Expand All @@ -69,11 +89,43 @@ export function useSquidNetworkHeightHooks() {
});
}, [currentHeight, heightHooks, queryClient, setHeightHooksRaw]);

return {
isLoading,
isWaiting: heightHooks.length > 0,
waitHeight: maxWaitedHook,
currentHeight: currentHeight ? String(currentHeight) : '0',
setWaitHeight,
};
const setWaitHeight = useCallback(
(height: bigint | string, invalidateQueries: unknown[] = []) => {
heightHooks.push({
height: Number(height),
invalidateQueries: invalidateQueries,
});
heightHooks.splice(0, heightHooks.length - 10);

setHeightHooksRaw(JSON.stringify(heightHooks));
},
[heightHooks, setHeightHooksRaw],
);

const maxWaitHeight = useMemo(() => {
return max(heightHooks.map(h => h.height)) || 0;
}, [heightHooks]);

const { data: chainHeight } = useBlockNumber();
useEffect(() => {
if (isLoading) return;

if (chainHeight && BigInt(currentHeight) < chainHeight && maxWaitHeight < chainHeight) {
setWaitHeight(chainHeight);
}
}, [chainHeight, currentHeight, heightHooks, isLoading, maxWaitHeight, setWaitHeight]);

return (
<SquidHeightContext.Provider
value={{
isLoading,
heightHooks,
currentHeight,
waitHeight: maxWaitHeight,
setWaitHeight,
}}
>
{children}
</SquidHeightContext.Provider>
);
}
Loading

0 comments on commit f7735c3

Please sign in to comment.