Skip to content

Commit

Permalink
fix: correctly filter empty delegations
Browse files Browse the repository at this point in the history
  • Loading branch information
belopash committed Jun 11, 2024
1 parent ec14fb4 commit 483e890
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 65 deletions.
26 changes: 14 additions & 12 deletions src/api/subsquid-network-squid/api.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ query workerOwner($workerId: String!) {

query myWorkerDelegations($address: String!, $workerId: String!) {
workerById(id: $workerId) {
delegations(where: { realOwner: { id_eq: $address }, deposit_gt: 0 }) {
delegations(where: { realOwner: { id_eq: $address }, deposit_gt: 0 }) {
claimableReward
claimedReward
deposit
Expand Down Expand Up @@ -209,17 +209,19 @@ query myAssets($address: String!) {
}

query myDelegations($address: String!) {
workers(where: { delegations_some: { realOwner: { id_eq: $address } } }) {
...WorkerFragment
delegations(where: { realOwner: { id_eq: $address } }) {
claimableReward
claimedReward
deposit
locked
owner {
id
type
}
delegations(where: { realOwner: { id_eq: $address }, deposit_gt: 0 }) {
claimableReward
claimedReward
deposit
locked
owner {
id
type
}
worker {
...WorkerFragment
totalDelegation
capedDelegation
}
}
}
Expand Down
74 changes: 38 additions & 36 deletions src/api/subsquid-network-squid/graphql.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5743,31 +5743,31 @@ export type MyDelegationsQueryVariables = Exact<{

export type MyDelegationsQuery = {
__typename?: 'Query';
workers: Array<{
__typename?: 'Worker';
version?: string;
createdAt: string;
uptime90Days?: number;
apr?: number;
stakerApr?: number;
totalDelegation: string;
capedDelegation: string;
id: string;
name?: string;
peerId: string;
status: WorkerStatus;
online?: boolean;
jailed?: boolean;
dialOk?: boolean;
jailReason?: string;
delegations: Array<{
__typename?: 'Delegation';
claimableReward: string;
claimedReward: string;
deposit: string;
locked?: boolean;
owner: { __typename?: 'Account'; id: string; type: AccountType };
}>;
delegations: Array<{
__typename?: 'Delegation';
claimableReward: string;
claimedReward: string;
deposit: string;
locked?: boolean;
owner: { __typename?: 'Account'; id: string; type: AccountType };
worker: {
__typename?: 'Worker';
totalDelegation: string;
capedDelegation: string;
version?: string;
createdAt: string;
uptime90Days?: number;
apr?: number;
stakerApr?: number;
id: string;
name?: string;
peerId: string;
status: WorkerStatus;
online?: boolean;
jailed?: boolean;
dialOk?: boolean;
jailReason?: string;
};
}>;
};

Expand Down Expand Up @@ -6441,17 +6441,19 @@ export const useMyAssetsQuery = <TData = MyAssetsQuery, TError = unknown>(

export const MyDelegationsDocument = `
query myDelegations($address: String!) {
workers(where: {delegations_some: {realOwner: {id_eq: $address}}}) {
...WorkerFragment
delegations(where: {realOwner: {id_eq: $address}}) {
claimableReward
claimedReward
deposit
locked
owner {
id
type
}
delegations(where: {realOwner: {id_eq: $address}, deposit_gt: 0}) {
claimableReward
claimedReward
deposit
locked
owner {
id
type
}
worker {
...WorkerFragment
totalDelegation
capedDelegation
}
}
}
Expand Down
44 changes: 27 additions & 17 deletions src/api/subsquid-network-squid/workers-graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { useAccount } from '@network/useAccount.ts';
import { useSquidDataSource } from './datasource';
import {
ClaimType,
MyDelegationsQuery,
useAllWorkersQuery,
useMyClaimsQuery,
useMyDelegationsQuery,
Expand All @@ -19,6 +20,7 @@ import {
useWorkerDelegationInfoQuery,
useWorkerOwnerQuery,
Worker,
WorkerFragmentFragment,
} from './graphql';
import { useNetworkSettings } from './settings-graphql';

Expand Down Expand Up @@ -332,15 +334,6 @@ export function useMyClaimsAvailable({ source }: { source?: string } = {}) {
};
}

// type ArrayElement<ArrayType extends readonly unknown[]> =
// ArrayType extends readonly (infer ElementType)[] ? ElementType : never;

// type Delegation = {
// worker: BlockchainApiWorker;
// totalReward: any;
// delegations: Omit<ArrayElement<MyDelegationsQuery['delegations']>, 'worker'>[];
// };

export function useMyDelegations() {
const { address } = useAccount();
const { isPending: isSettingsLoading } = useNetworkSettings();
Expand All @@ -351,17 +344,34 @@ export function useMyDelegations() {
});

const workers = useMemo(() => {
return (
data?.workers.map(w => {
return {
...w,
type W = WorkerFragmentFragment &
Pick<WorkerExtended, 'delegationCapacity'> & {
delegations: Omit<MyDelegationsQuery['delegations'][number], 'worker'>[];
};

const workers: Map<string, W> = new Map();
data?.delegations.map(d => {
let worker = workers.get(d.worker.id);
if (!worker) {
worker = {
...d.worker,
delegations: [],
delegationCapacity: calculateDelegationCapacity({
totalDelegation: w.totalDelegation,
capedDelegation: w.capedDelegation,
totalDelegation: d.worker.totalDelegation,
capedDelegation: d.worker.capedDelegation,
}),
};
}) || []
);
workers.set(worker.id, worker);
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const delegation = {
...d,
worker: undefined,
};
delete delegation['worker'];
worker.delegations.push(delegation);
});
return [...workers.values()];
}, [data]);

return {
Expand Down

0 comments on commit 483e890

Please sign in to comment.