mirror of
https://github.com/pezkuwichain/pezkuwi-apps.git
synced 2026-06-19 00:41:07 +00:00
d21bfb1320
Rebranded terminology: - Polkadot → Pezkuwi - Kusama → Dicle - Westend → Zagros - Rococo → PezkuwiChain - Substrate → Bizinikiwi - parachain → teyrchain Custom logos with Kurdistan brand colors (#e6007a → #86e62a): - bizinikiwi-hexagon.svg - sora-bizinikiwi.svg - hezscanner.svg - heztreasury.svg - pezkuwiscan.svg - pezkuwistats.svg - pezkuwiassembly.svg - pezkuwiholic.svg
62 lines
2.0 KiB
TypeScript
62 lines
2.0 KiB
TypeScript
// Copyright 2017-2025 @pezkuwi/app-teyrchains authors & contributors
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
import type { DeriveContributions, DeriveOwnContributions } from '@pezkuwi/api-derive/types';
|
|
import type { Balance, ParaId } from '@pezkuwi/types/interfaces';
|
|
|
|
import { useEffect, useState } from 'react';
|
|
|
|
import { createNamedHook, useAccounts, useApi, useCall } from '@pezkuwi/react-hooks';
|
|
import { encodeAddress } from '@pezkuwi/util-crypto';
|
|
|
|
interface Result extends DeriveContributions {
|
|
hasLoaded: boolean;
|
|
myAccounts: string[];
|
|
myAccountsHex: string[];
|
|
myContributions: Record<string, Balance>;
|
|
}
|
|
|
|
const NO_CONTRIB: Result = {
|
|
blockHash: '-',
|
|
contributorsHex: [],
|
|
hasLoaded: false,
|
|
myAccounts: [],
|
|
myAccountsHex: [],
|
|
myContributions: {}
|
|
};
|
|
|
|
function useContributionsImpl (paraId: ParaId): Result {
|
|
const { api } = useApi();
|
|
const { allAccountsHex } = useAccounts();
|
|
const [state, setState] = useState<Result>(() => NO_CONTRIB);
|
|
const derive = useCall<DeriveContributions>(api.derive.crowdloan.contributions, [paraId]);
|
|
const myContributions = useCall<DeriveOwnContributions>(api.derive.crowdloan.ownContributions, [paraId, state.myAccountsHex]);
|
|
|
|
useEffect((): void => {
|
|
derive && setState((prev): Result => {
|
|
let myAccountsHex = derive.contributorsHex.filter((h) => allAccountsHex.includes(h));
|
|
let myAccounts: string[];
|
|
|
|
if (myAccountsHex.length === prev.myAccountsHex.length) {
|
|
myAccountsHex = prev.myAccountsHex;
|
|
myAccounts = prev.myAccounts;
|
|
} else {
|
|
myAccounts = myAccountsHex.map((a) => encodeAddress(a, api.registry.chainSS58));
|
|
}
|
|
|
|
return { ...prev, ...derive, hasLoaded: true, myAccounts, myAccountsHex };
|
|
});
|
|
}, [api, allAccountsHex, derive]);
|
|
|
|
useEffect((): void => {
|
|
myContributions && setState((prev) => ({
|
|
...prev,
|
|
myContributions
|
|
}));
|
|
}, [myContributions]);
|
|
|
|
return state;
|
|
}
|
|
|
|
export default createNamedHook('useContributions', useContributionsImpl);
|