Skip to content

Commit

Permalink
Merge pull request #12 from subsquid/develop
Browse files Browse the repository at this point in the history
release
  • Loading branch information
belopash authored May 16, 2024
2 parents 1ed1e61 + 1897c05 commit 105f363
Show file tree
Hide file tree
Showing 28 changed files with 902 additions and 65 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
"use-element-position": "^1.0.13",
"use-local-storage-state": "^19.2.0",
"viem": "^1.21.1",
"wagmi": "^1.4.12",
"wagmi": "^1.4.13",
"yup": "^1.4.0"
},
"devDependencies": {
Expand Down
21 changes: 17 additions & 4 deletions src/AppRoutes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ import React from 'react';

import { Navigate, Route, Routes } from 'react-router-dom';

import { demoFeaturesEnabled } from '@hooks/demoFeaturesEnabled.ts';
import { NetworkLayout } from '@layouts/NetworkLayout';
import { NetworkName, useSubsquidNetwork } from '@network/useSubsquidNetwork.ts';
import { AssetsPage } from '@pages/AssetsPage/AssetsPage.tsx';
import { Vesting } from '@pages/AssetsPage/Vesting.tsx';
import { DashboardPage } from '@pages/DashboardPage/DashboardPage.tsx';
import { DelegationsPage } from '@pages/DelegationsPage/DelegationsPage.tsx';
import { AddNewGateway } from '@pages/GatewaysPage/AddNewGateway.tsx';
Expand All @@ -18,11 +22,20 @@ import { hideLoader } from './index.tsx';
export const AppRoutes = () => {
hideLoader(0);

const { network } = useSubsquidNetwork();

return (
<Routes>
<Route element={<NetworkLayout />} path="/dashboard">
<Route element={<DashboardPage />} index />
<Route element={<Worker backPath="/dashboard" />} path="workers/:peerId" />
{demoFeaturesEnabled() || network === NetworkName.Testnet ? (
<Route element={<NetworkLayout />} path="/dashboard">
<Route element={<DashboardPage />} index />
<Route element={<Worker backPath="/dashboard" />} path="workers/:peerId" />
</Route>
) : null}

<Route element={<NetworkLayout />} path="/assets">
<Route element={<AssetsPage />} index />
<Route element={<Vesting backPath="/assets" />} path="vestings/:address" />
</Route>
<Route element={<NetworkLayout />} path="/workers">
<Route element={<WorkersPage />} index />
Expand All @@ -39,7 +52,7 @@ export const AppRoutes = () => {
<Route element={<AddNewGateway />} path="add" />
<Route element={<Gateway backPath="/gateways" />} path=":peerId" />
</Route>
<Route element={<Navigate to="/dashboard" replace={true} />} path="*" />
<Route element={<Navigate to="/assets" replace={true} />} path="*" />
</Routes>
);
};
166 changes: 166 additions & 0 deletions src/api/contracts/vesting.abi.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,43 @@
export const VESTING_CONTRACT_ABI = [
{
type: 'function',
name: 'depositedIntoProtocol',
inputs: [],
outputs: [
{
name: '',
type: 'uint256',
internalType: 'uint256',
},
],
stateMutability: 'view',
},
{
type: 'function',
name: 'duration',
inputs: [],
outputs: [
{
name: '',
type: 'uint256',
internalType: 'uint256',
},
],
stateMutability: 'view',
},
{
type: 'function',
name: 'end',
inputs: [],
outputs: [
{
name: '',
type: 'uint256',
internalType: 'uint256',
},
],
stateMutability: 'view',
},
{
type: 'function',
name: 'execute',
Expand Down Expand Up @@ -46,4 +85,131 @@ export const VESTING_CONTRACT_ABI = [
],
stateMutability: 'nonpayable',
},
{
type: 'function',
name: 'expectedTotalAmount',
inputs: [],
outputs: [
{
name: '',
type: 'uint256',
internalType: 'uint256',
},
],
stateMutability: 'view',
},
{
type: 'function',
name: 'immediateReleaseBIP',
inputs: [],
outputs: [
{
name: '',
type: 'uint256',
internalType: 'uint256',
},
],
stateMutability: 'view',
},
{
type: 'function',
name: 'releasable',
inputs: [
{
name: 'token',
type: 'address',
internalType: 'address',
},
],
outputs: [
{
name: '',
type: 'uint256',
internalType: 'uint256',
},
],
stateMutability: 'view',
},
{
type: 'function',
name: 'releasable',
inputs: [],
outputs: [
{
name: '',
type: 'uint256',
internalType: 'uint256',
},
],
stateMutability: 'view',
},
{
type: 'function',
name: 'release',
inputs: [
{
name: 'token',
type: 'address',
internalType: 'address',
},
],
outputs: [],
stateMutability: 'nonpayable',
},
{
type: 'function',
name: 'released',
inputs: [
{
name: 'token',
type: 'address',
internalType: 'address',
},
],
outputs: [
{
name: '',
type: 'uint256',
internalType: 'uint256',
},
],
stateMutability: 'view',
},
{
type: 'function',
name: 'start',
inputs: [],
outputs: [
{
name: '',
type: 'uint256',
internalType: 'uint256',
},
],
stateMutability: 'view',
},
{
type: 'function',
name: 'vestedAmount',
inputs: [
{
name: 'token',
type: 'address',
internalType: 'address',
},
{
name: 'timestamp',
type: 'uint64',
internalType: 'uint64',
},
],
outputs: [
{
name: '',
type: 'uint256',
internalType: 'uint256',
},
],
stateMutability: 'view',
},
] as const;
132 changes: 132 additions & 0 deletions src/api/contracts/vesting.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
import { useState } from 'react';

import { chunk } from 'lodash-es';
import { MulticallResult } from 'viem';
import { erc20ABI, useContractReads, useContractWrite, usePublicClient } from 'wagmi';

import { useSquidNetworkHeightHooks } 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: isHeightLoading } = useSquidNetworkHeightHooks();

const { data, isLoading } = useContractReads({
contracts: addresses?.flatMap(address => {
const vestingContract = { abi: VESTING_CONTRACT_ABI, address } as const;
return [
{
...vestingContract,
functionName: 'start',
},
{
...vestingContract,
functionName: 'end',
},
{
...vestingContract,
functionName: 'depositedIntoProtocol',
},
{
...vestingContract,
functionName: 'releasable',
args: [contracts.SQD],
},
{
...vestingContract,
functionName: 'released',
args: [contracts.SQD],
},
{
abi: erc20ABI,
address: contracts.SQD,
functionName: 'balanceOf',
args: [address],
},
{
...vestingContract,
functionName: 'immediateReleaseBIP',
},
{
...vestingContract,
functionName: 'expectedTotalAmount',
},
] as const;
}),
allowFailure: true,
enabled: !!addresses && !isHeightLoading,
blockNumber: currentHeight ? BigInt(currentHeight) : undefined,
});

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,
};
}

export function useVestingContract({ address }: { address?: `0x${string}` }) {
const { data, isLoading } = useVestingContracts({ addresses: address ? [address] : undefined });

return {
data: data?.[0],
isLoading,
};
}

function unwrapResult<T>(result?: MulticallResult<T>): T | undefined {
return result?.status === 'success' ? (result.result as T) : undefined;
}

export function useVestingContractRelease({ address }: { address?: `0x${string}` }) {
const client = usePublicClient();
const { setWaitHeight } = useSquidNetworkHeightHooks();
const [isLoading, setLoading] = useState(false);
const [error, setError] = useState<string | null>(null);
const { SQD } = useContracts();

const { writeAsync } = useContractWrite({
abi: VESTING_CONTRACT_ABI,
functionName: 'release',
args: [SQD],
address,
});

const release = async (): Promise<WriteContractRes> => {
setLoading(true);

try {
const tx = await writeAsync();

const receipt = await client.waitForTransactionReceipt(tx);
setWaitHeight(receipt.blockNumber, []);

return { success: true };
} catch (e) {
const failedReason = errorMessage(e);
setError(failedReason);
return { success: false, failedReason };
} finally {
setLoading(false);
}
};

return {
release,
isLoading,
error,
};
}
Loading

0 comments on commit 105f363

Please sign in to comment.