Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TW-737 Use TZKT for getting a delegate if possible #1014

12 changes: 8 additions & 4 deletions src/lib/apis/tzkt/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,17 @@ import {
allInt32ParameterKeys,
TzktGetRewardsParams,
TzktGetRewardsResponse,
TzktRelatedContract
TzktRelatedContract,
TzktAccount
} from './types';

const TZKT_API_BASE_URLS = {
[TempleChainId.Mainnet]: 'https://api.tzkt.io/v1',
[TempleChainId.Jakartanet]: 'https://api.jakartanet.tzkt.io/v1',
[TempleChainId.Limanet]: 'https://api.limanet.tzkt.io/v1',
[TempleChainId.Mumbai]: 'https://api.mumbainet.tzkt.io/v1',
[TempleChainId.Nairobi]: 'https://api.nairobinet.tzkt.io/v1',
[TempleChainId.Ghostnet]: 'https://api.ghostnet.tzkt.io/v1',
[TempleChainId.Dcp]: 'https://explorer-api.tlnt.net/v1',
[TempleChainId.DcpTest]: 'https://explorer.tlnt.net:8009/v1'
[TempleChainId.DcpTest]: 'https://explorer-api.test.tlnt.net/v1'
};

export type TzktApiChainId = keyof typeof TZKT_API_BASE_URLS;
Expand Down Expand Up @@ -189,3 +190,6 @@ export const fetchAllTokensBalancesFromTzkt = async (selectedRpcUrl: string, acc

return balances;
};

export const getAccountStatsFromTzkt = async (account: string, chainId: string) =>
isKnownChainId(chainId) ? await fetchGet<TzktAccount>(chainId, `/accounts/${account}`) : null;
keshan3262 marked this conversation as resolved.
Show resolved Hide resolved
3 changes: 3 additions & 0 deletions src/lib/apis/tzkt/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@ export type {
TzktAccountToken
} from './types';

export { TzktAccountType } from './types';

export type { TzktApiChainId } from './api';
export {
isKnownChainId,
getAccountStatsFromTzkt,
getDelegatorRewards,
getOneUserContracts,
fetchTzktTokens,
Expand Down
268 changes: 268 additions & 0 deletions src/lib/apis/tzkt/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,3 +221,271 @@ export interface TzktTokenTransfer {
};
transactionId: number;
}

export enum TzktAccountType {
User = 'user',
Delegate = 'delegate',
Contract = 'contract',
Ghost = 'ghost',
Rollup = 'rollup',
SmartRollup = 'smart_rollup',
Empty = 'empty'
}

interface TzktAccountBase {
type: TzktAccountType;
address: string;
alias: string | nullish;
}

interface TzktUserAccount extends TzktAccountBase {
type: TzktAccountType.User;
id: number;
publicKey: string;
revealed: boolean;
balance: number;
rollupBonds: number;
smartRollupBonds: number;
counter: number;
delegate: TzktAlias | nullish;
delegationLevel: number;
delegationTime: string | nullish;
numContracts: number;
rollupsCount: number;
smartRollupsCount: number;
activeTokensCount: number;
tokenBalancesCount: number;
tokenTransfersCount: number;
numActivations: number;
numDelegations: number;
numOriginations: number;
numTransactions: number;
numReveals: number;
numRegisterConstants: number;
numSetDepositsLimits: number;
numMigrations: number;
txRollupOriginationCount: number;
txRollupSubmitBatchCount: number;
txRollupCommitCount: number;
txRollupReturnBondCount: number;
txRollupFinalizeCommitmentCount: number;
txRollupRemoveCommitmentCount: number;
txRollupRejectionCount: number;
txRollupDispatchTicketsCount: number;
transferTicketCount: number;
increasePaidStorageCount: number;
drainDelegateCount: number;
smartRollupAddMessagesCount: number;
smartRollupCementCount: number;
smartRollupExecuteCount: number;
smartRollupOriginateCount: number;
smartRollupPublishCount: number;
smartRollupRecoverBondCount: number;
smartRollupRefuteCount: number;
refutationGamesCount: number;
activeRefutationGamesCount: number;
firstActivity: number | nullish;
firstActivityTime: string | nullish;
lastActivity: number | nullish;
lastActivityTime: string | nullish;
}

interface TzktDelegateAccount extends TzktAccountBase {
type: TzktAccountType.Delegate;
id: number;
active: boolean;
publicKey: string | nullish;
revealed: boolean;
balance: number;
rollupBonds: number;
smartRollupBonds: number;
frozenDeposit: number;
frozenDepositLimit: number | nullish;
counter: number;
activationLevel: number;
activationTime: string;
deactivationLevel: number | nullish;
deactivationTime: string | nullish;
stakingBalance: number;
delegatedBalance: number;
numContracts: number;
rollupsCount: number;
smartRollupsCount: number;
activeTokensCount: number;
tokenBalancesCount: number;
tokenTransfersCount: number;
numDelegators: number;
numBlocks: number;
numEndorsements: number;
numPreendorsements: number;
numBallots: number;
numProposals: number;
numActivations: number;
numDoubleBaking: number;
numDoubleEndorsing: number;
numDoublePreendorsing: number;
numNonceRevelations: number;
vdfRevelationsCount: number;
numRevelationPenalties: number;
numEndorsingRewards: number;
numDelegations: number;
numOriginations: number;
numTransactions: number;
numReveals: number;
numRegisterConstants: number;
numSetDepositsLimits: number;
numMigrations: number;
txRollupOriginationCount: number;
txRollupSubmitBatchCount: number;
txRollupCommitCount: number;
txRollupReturnBondCount: number;
txRollupFinalizeCommitmentCount: number;
txRollupRemoveCommitmentCount: number;
txRollupRejectionCount: number;
txRollupDispatchTicketsCount: number;
transferTicketCount: number;
increasePaidStorageCount: number;
updateConsensusKeyCount: number;
drainDelegateCount: number;
smartRollupAddMessagesCount: number;
smartRollupCementCount: number;
smartRollupExecuteCount: number;
smartRollupOriginateCount: number;
smartRollupPublishCount: number;
smartRollupRecoverBondCount: number;
smartRollupRefuteCount: number;
refutationGamesCount: number;
activeRefutationGamesCount: number;
firstActivity: number;
firstActivityTime: string | nullish;
lastActivity: number;
lastActivityTime: string | nullish;
extras: unknown;
software: { date: string; version: string | nullish };
}

interface TzktContractAccount extends TzktAccountBase {
type: TzktAccountType.Contract;
id: number;
kind: 'delegator_contract' | 'smart_contract' | nullish;
tzips: string[] | nullish;
balance: number;
creator: TzktAlias | nullish;
manager: TzktAlias | nullish;
delegate: TzktAlias | nullish;
delegationLevel: number | nullish;
delegationTime: string | nullish;
numContracts: number;
activeTokensCount: number;
tokensCount: number;
tokenBalancesCount: number;
tokenTransfersCount: number;
numDelegations: number;
numOriginations: number;
numTransactions: number;
numReveals: number;
numMigrations: number;
transferTicketCount: number;
increasePaidStorageCount: number;
eventsCount: number;
firstActivity: number;
firstActivityTime: string;
lastActivity: number;
lastActivityTime: string;
typeHash: number;
codeHash: number;
/** TZIP-16 metadata (with ?legacy=true this field will contain tzkt profile info). */
metadata: unknown;
extras: unknown;
/** Contract storage value. Omitted by default. Use ?includeStorage=true to include it into response. */
storage: unknown;
}

interface TzktGhostAccount extends TzktAccountBase {
type: TzktAccountType.Ghost;
id: number;
activeTokensCount: number;
tokenBalancesCount: number;
tokenTransfersCount: number;
firstActivity: number;
firstActivityTime: string;
lastActivity: number;
lastActivityTime: string;
extras: unknown;
}

interface TzktRollupAccount extends TzktAccountBase {
type: TzktAccountType.Rollup;
id: number;
creator: TzktAlias | nullish;
rollupBonds: number;
activeTokensCount: number;
tokenBalancesCount: number;
tokenTransfersCount: number;
numTransactions: number;
txRollupOriginationCount: number;
txRollupSubmitBatchCount: number;
txRollupCommitCount: number;
txRollupReturnBondCount: number;
txRollupFinalizeCommitmentCount: number;
txRollupRemoveCommitmentCount: number;
txRollupRejectionCount: number;
txRollupDispatchTicketsCount: number;
transferTicketCount: number;
firstActivity: number;
firstActivityTime: string;
lastActivity: number;
lastActivityTime: string;
extras: unknown;
}

interface TzktSmartRollupAccount extends TzktAccountBase {
type: TzktAccountType.SmartRollup;
id: number;
creator: TzktAlias | nullish;
pvmKind: 'arith' | 'wasm' | nullish;
genesisCommitment: string | nullish;
lastCommitment: string | nullish;
inboxLevel: number;
totalStakers: number;
activeStakers: number;
executedCommitments: number;
cementedCommitments: number;
pendingCommitments: number;
refutedCommitments: number;
orphanCommitments: number;
smartRollupBonds: number;
activeTokensCount: number;
tokenBalancesCount: number;
tokenTransfersCount: number;
numTransactions: number;
transferTicketCount: number;
smartRollupCementCount: number;
smartRollupExecuteCount: number;
smartRollupOriginateCount: number;
smartRollupPublishCount: number;
smartRollupRecoverBondCount: number;
smartRollupRefuteCount: number;
refutationGamesCount: number;
activeRefutationGamesCount: number;
firstActivity: number;
firstActivityTime: string;
lastActivity: number;
lastActivityTime: string;
extras: unknown;
}

interface TzktEmptyAccount extends TzktAccountBase {
type: TzktAccountType.Empty;
alias: undefined;
counter: number;
}

export type TzktAccount =
| TzktUserAccount
| TzktDelegateAccount
| TzktContractAccount
| TzktGhostAccount
| TzktRollupAccount
| TzktSmartRollupAccount
| TzktEmptyAccount;
29 changes: 21 additions & 8 deletions src/lib/temple/front/baking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,36 @@ import {
bakingBadGetBaker,
getAllBakersBakingBad
} from 'lib/apis/baking-bad';
import type { TzktRewardsEntry } from 'lib/apis/tzkt';
import { getAccountStatsFromTzkt, isKnownChainId, TzktRewardsEntry, TzktAccountType } from 'lib/apis/tzkt';
import { useRetryableSWR } from 'lib/swr';

import { useNetwork, useTezos } from './ready';
import { useChainId, useNetwork, useTezos } from './ready';

export function useDelegate(address: string, suspense = true) {
const tezos = useTezos();
const chainId = useChainId(suspense);

const getDelegate = useCallback(async () => {
try {
return await tezos.rpc.getDelegate(address);
} catch {
return null;
if (chainId && isKnownChainId(chainId)) {
try {
const accountStats = await getAccountStatsFromTzkt(address, chainId);

switch (accountStats?.type) {
case TzktAccountType.Empty:
return null;
case TzktAccountType.User:
case TzktAccountType.Contract:
return accountStats.delegate?.address ?? null;
}
} catch (e) {
console.error(e);
}
}
}, [address, tezos]);

return useRetryableSWR(['delegate', tezos.checksum, address], getDelegate, {
return await tezos.rpc.getDelegate(address);
lendihop marked this conversation as resolved.
Show resolved Hide resolved
}, [address, tezos, chainId]);

return useRetryableSWR(['delegate', tezos.checksum, address, chainId], getDelegate, {
dedupingInterval: 20_000,
suspense
});
Expand Down
20 changes: 11 additions & 9 deletions src/lib/temple/front/blockexplorer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,19 @@ export const BLOCK_EXPLORERS: BlockExplorer[] = [
}
],
[
TempleChainId.Jakartanet,
TempleChainId.Mumbai,
{
account: 'https://jakartanet.tzkt.io',
transaction: 'https://jakartanet.tzkt.io'
account: 'https://mumbainet.tzkt.io',
transaction: 'https://mumbainet.tzkt.io',
api: 'https://api.mumbainet.tzkt.io'
}
],
[
TempleChainId.Limanet,
TempleChainId.Nairobi,
{
account: 'https://limanet.tzkt.io',
transaction: 'https://limanet.tzkt.io',
api: 'https://api.limanet.tzkt.io'
account: 'https://nairobinet.tzkt.io',
transaction: 'https://nairobinet.tzkt.io',
api: 'https://api.nairobinet.tzkt.io'
}
]
])
Expand All @@ -70,8 +71,9 @@ export const BLOCK_EXPLORERS: BlockExplorer[] = [
[
TempleChainId.DcpTest,
{
account: 'https://explorer.tlnt.net:444',
transaction: 'https://explorer.tlnt.net:444'
account: 'https://explorer.test.tlnt.net',
transaction: 'https://explorer.test.tlnt.net',
api: 'https://explorer-api.test.tlnt.net'
}
]
])
Expand Down
Loading
Loading