Skip to content

Commit

Permalink
Merge pull request #18 from subsquid/develop
Browse files Browse the repository at this point in the history
release
  • Loading branch information
belopash committed May 22, 2024
2 parents 509931d + fc18c7b commit 6a70613
Show file tree
Hide file tree
Showing 26 changed files with 512 additions and 261 deletions.
2 changes: 0 additions & 2 deletions src/api/contracts/gateway-registration/useStakeGateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { encodeFunctionData } from 'viem';
import { usePublicClient, useWriteContract } from 'wagmi';

import { AccountType, SourceWallet } from '@api/subsquid-network-squid';
import { BlockchainGateway } from '@api/subsquid-network-squid/gateways-graphql';
import { useSquidNetworkHeightHooks } from '@hooks/useSquidNetworkHeightHooks.ts';
import { useAccount } from '@network/useAccount';
import { useContracts } from '@network/useContracts.ts';
Expand All @@ -18,7 +17,6 @@ import { VESTING_CONTRACT_ABI } from '../vesting.abi';
import { GATEWAY_REGISTRATION_CONTRACT_ABI } from './GatewayRegistration.abi';

type StakeGatewayRequest = {
gateway: BlockchainGateway;
amount: number;
durationBlocks: number;
autoExtension: boolean;
Expand Down
11 changes: 5 additions & 6 deletions src/api/contracts/gateway-registration/useUnstakeGateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ import { useState } from 'react';
import { encodeFunctionData } from 'viem';
import { useWriteContract, usePublicClient } from 'wagmi';

import { AccountType } from '@api/subsquid-network-squid';
import { BlockchainGateway } from '@api/subsquid-network-squid/gateways-graphql';
import { AccountType, GatewayStakeFragmentFragment } from '@api/subsquid-network-squid';
import { useSquidNetworkHeightHooks } from '@hooks/useSquidNetworkHeightHooks.ts';
import { useAccount } from '@network/useAccount';
import { useContracts } from '@network/useContracts.ts';
Expand All @@ -15,7 +14,7 @@ import { VESTING_CONTRACT_ABI } from '../vesting.abi';
import { GATEWAY_REGISTRATION_CONTRACT_ABI } from './GatewayRegistration.abi';

type UnstakeGatewayRequest = {
gateway: BlockchainGateway;
operator: GatewayStakeFragmentFragment;
};

function useUnstakeFromWallet() {
Expand All @@ -42,7 +41,7 @@ function useUnstakeFromVestingContract() {
const { address: account } = useAccount();
const { writeContractAsync } = useWriteContract({});

return async ({ gateway }: UnstakeGatewayRequest): Promise<TxResult> => {
return async ({ operator }: UnstakeGatewayRequest): Promise<TxResult> => {
try {
const data = encodeFunctionData({
abi: GATEWAY_REGISTRATION_CONTRACT_ABI,
Expand All @@ -52,7 +51,7 @@ function useUnstakeFromVestingContract() {
return {
tx: await writeContractAsync({
account,
address: gateway.owner.id as `0x${string}`,
address: operator.account.id as `0x${string}`,
abi: VESTING_CONTRACT_ABI,
functionName: 'execute',
args: [contracts.GATEWAY_REGISTRATION, data],
Expand All @@ -77,7 +76,7 @@ export function useUnstakeGateway() {
setLoading(true);

const res =
req.gateway.owner.type === AccountType.User
req.operator.account.type === AccountType.User
? await unstakeFromWallet(req)
: await unstakeFromVestingContract(req);

Expand Down
23 changes: 13 additions & 10 deletions src/api/contracts/vesting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export function useVestingContracts({ addresses }: { addresses: `0x${string}`[]
allowFailure: true,
blockNumber: BigInt(currentHeight),
query: {
enabled: !isHeightLoading,
enabled: !isHeightLoading && !!addresses.length,
},
});

Expand All @@ -79,9 +79,7 @@ export function useVestingContracts({ addresses }: { addresses: `0x${string}`[]
>(undefined);

useEffect(() => {
if (addresses.length === 0) {
data.current = [];
} else if (res?.some(r => r.status === 'success')) {
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,
Expand All @@ -93,12 +91,17 @@ export function useVestingContracts({ addresses }: { addresses: `0x${string}`[]
expectedTotal: unwrapResult(ch[7])?.toString(),
}));
}
}, [addresses.length, res]);

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

return addresses.length
? {
data: data.current,
isLoading: !data.current,
}
: {
data: [],
isLoading: false,
};
}

export function useVestingContract({ address }: { address?: `0x${string}` }) {
Expand Down
2 changes: 1 addition & 1 deletion src/api/subsquid-network-squid/accounts-graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export function useMySources({ enabled }: { enabled?: boolean } = {}) {
: [wallet, ...wallet.owned].map(a => ({
type: a.type,
id: a.id,
balance: a.balance as string,
balance: a.balance,
}));
}, [address, wallet]);

Expand Down
44 changes: 28 additions & 16 deletions src/api/subsquid-network-squid/api.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ fragment WorkerFragment on Worker {
jailReason
owner {
id
type
}
realOwner {
id
Expand Down Expand Up @@ -204,21 +205,6 @@ fragment GatewayFragment on Gateway {
email
endpointUrl
website
operator {
autoExtension
stake {
amount
locked
lockStart
lockEnd
}
pendingStake {
amount
locked
lockStart
lockEnd
}
}
owner {
id
type
Expand All @@ -238,6 +224,32 @@ query gatewayByPeerId($peerId: String!) {
}
}

fragment GatewayStakeFragment on GatewayOperator {
account {
id
type
}
autoExtension
stake {
amount
locked
lockStart
lockEnd
}
pendingStake {
amount
locked
lockStart
lockEnd
}
}

query myGatewayStakes($address: String!) {
gatewayOperators(where: {account: {id_eq: $address, OR: {owner: {id_eq: $address}}}}) {
...GatewayStakeFragment
}
}

fragment VestingFragment on Account {
id
type
Expand All @@ -251,4 +263,4 @@ query vestingByAddress($address: String!) {
accountById(id: $address) {
...VestingFragment
}
}
}
35 changes: 30 additions & 5 deletions src/api/subsquid-network-squid/gateways-graphql.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import { useSquidDataSource } from '@api/subsquid-network-squid/datasource';
import { useAccount } from '@network/useAccount';

import { GatewayFragmentFragment, useGatewayByPeerIdQuery, useMyGatewaysQuery } from './graphql';
import {
GatewayFragmentFragment,
useGatewayByPeerIdQuery,
useMyGatewaysQuery,
useMyGatewayStakesQuery,
} from './graphql';

// inherit API interface for internal class
export interface BlockchainGateway extends GatewayFragmentFragment {
Expand All @@ -10,14 +15,10 @@ export interface BlockchainGateway extends GatewayFragmentFragment {

export class BlockchainGateway {
ownedByMe?: boolean;
totalStaked: string = '0';
pendingStaked: string = '0';

constructor({ gateway, address }: { gateway: GatewayFragmentFragment; address?: `0x${string}` }) {
Object.assign(this, {
...gateway,
totalStaked: String(gateway.operator?.stake?.amount || 0),
pendingStaked: String(gateway.operator?.pendingStake?.amount || 0),
createdAt: new Date(),
ownedByMe: gateway?.owner?.id === address,
});
Expand Down Expand Up @@ -82,3 +83,27 @@ export function useGatewayByPeerId(peerId?: string) {
isLoading: enabled ? isLoading : false,
};
}

export function useMyGatewayStakes() {
const datasource = useSquidDataSource();
const { address } = useAccount();

const enabled = !!address;
const { data, isLoading } = useMyGatewayStakesQuery(
datasource,
{
address: address || '',
},
{
select: res => {
return res.gatewayOperators[0];
},
enabled,
},
);

return {
data: data,
isLoading: enabled && isLoading,
};
}
Loading

0 comments on commit 6a70613

Please sign in to comment.