Skip to content

Commit

Permalink
feat: display delegation capacity
Browse files Browse the repository at this point in the history
  • Loading branch information
belopash committed Jun 5, 2024
1 parent c154820 commit 11a975c
Show file tree
Hide file tree
Showing 18 changed files with 391 additions and 183 deletions.
26 changes: 26 additions & 0 deletions src/api/contracts/soft-cap.abi.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
export const SOFT_CAP_ABI = [
{
type: 'function',
name: 'cap',
inputs: [{ name: 'x', type: 'uint256', internalType: 'UD60x18' }],
outputs: [{ name: '', type: 'uint256', internalType: 'UD60x18' }],
stateMutability: 'pure',
},
{
type: 'function',
name: 'capedStake',
inputs: [{ name: 'workerId', type: 'uint256', internalType: 'uint256' }],
outputs: [{ name: '', type: 'uint256', internalType: 'uint256' }],
stateMutability: 'view',
},
{
type: 'function',
name: 'capedStakeAfterDelegation',
inputs: [
{ name: 'workerId', type: 'uint256', internalType: 'uint256' },
{ name: 'delegationAmount', type: 'int256', internalType: 'int256' },
],
outputs: [{ name: '', type: 'uint256', internalType: 'uint256' }],
stateMutability: 'view',
},
] as const;
31 changes: 29 additions & 2 deletions src/api/contracts/staking.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { useState } from 'react';
import { useEffect, useRef, useState } from 'react';

import { logger } from '@logger';
import Decimal from 'decimal.js';
import { encodeFunctionData } from 'viem';
import { waitForTransactionReceipt } from 'viem/actions';
import { useWriteContract, usePublicClient, useClient } from 'wagmi';
import { useWriteContract, usePublicClient, useClient, useReadContract } from 'wagmi';

import { useApproveSqd } from '@api/contracts/sqd';
import {
Expand All @@ -19,6 +19,7 @@ import { useSquidNetworkHeightHooks } from '@hooks/useSquidNetworkHeightHooks.ts
import { useAccount } from '@network/useAccount';
import { useContracts } from '@network/useContracts.ts';

import { SOFT_CAP_ABI } from './soft-cap.abi';
import { STAKING_CONTRACT_ABI } from './staking.abi';

type WorkerDepositRequest = {
Expand Down Expand Up @@ -232,3 +233,29 @@ export function useWorkerUndelegate() {
error,
};
}

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

const { data, isLoading } = useReadContract({
address: contracts.SOFT_CAP,
abi: SOFT_CAP_ABI,
functionName: 'capedStake',
args: [BigInt(workerId || -1)],
blockNumber: BigInt(currentHeight),
query: {
enabled: !!workerId && !isHeightLoading,
},
});

const res = useRef<string | undefined>(undefined);
useEffect(() => {
if (!isLoading) res.current = data?.toString();
}, [data, isLoading]);

return {
data: res.current,
isLoading: !res.current,
};
}
66 changes: 33 additions & 33 deletions src/api/contracts/vesting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ import { VESTING_CONTRACT_ABI } from './vesting.abi';

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

const { data: res } = useReadContracts({
const { data, isLoading } = useReadContracts({
contracts: addresses.flatMap(address => {
const vestingContract = { abi: VESTING_CONTRACT_ABI, address } as const;
return [
Expand Down Expand Up @@ -60,48 +60,48 @@ export function useVestingContracts({ addresses }: { addresses: `0x${string}`[]
allowFailure: true,
blockNumber: BigInt(currentHeight),
query: {
enabled: !isHeightLoading && !!addresses.length,
enabled: !isSquidHeightLoading && !!addresses.length,
select: r => {
if (r?.some(r => r.status === 'success')) {
return chunk(r, 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(),
}));
} else {
return res.current;
}
},
},
});

const data = useRef<
const res = useRef<
| {
start?: number;
end?: number;
deposited?: string | undefined;
releasable?: string | undefined;
released?: string | undefined;
balance?: string | undefined;
deposited?: string;
releasable?: string;
released?: string;
balance?: string;
initialRelease?: number;
expectedTotal?: string | undefined;
expectedTotal?: string;
}[]
| 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 addresses.length
? {
data: data.current,
isLoading: !data.current,
}
: {
data: [],
isLoading: false,
};
if (!addresses.length) res.current = [];
if (!isLoading) res.current = data;
}, [addresses, data, isLoading]);

return {
data: res.current,
isLoading: !res.current,
};
}

export function useVestingContract({ address }: { address?: `0x${string}` }) {
Expand Down
8 changes: 5 additions & 3 deletions src/api/subsquid-network-squid/api.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,16 @@ query account($address: String!) {
fragment WorkerFragment on Worker {
id
name
email
peerId
website
status
createdAt
description
bond
claimableReward
claimedReward
uptime24Hours
uptime90Days
totalDelegation
capedDelegation
delegationCount
apr
stakerApr
Expand All @@ -71,6 +69,10 @@ fragment WorkerFragment on Worker {
}

fragment WorkerFullFragment on Worker {
totalDelegationRewards
website
email
description
queries24Hours
queries90Days
scannedData24Hours
Expand Down
Loading

0 comments on commit 11a975c

Please sign in to comment.