OCD-related dedupe of pool accounts (#7539)

This commit is contained in:
Jaco 2022-05-06 10:02:37 +02:00 committed by GitHub
parent cce2229f73
commit bec332a3b5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 18 deletions

View File

@ -9,7 +9,7 @@ import { useMemo } from 'react';
import { createNamedHook, useAccounts, useApi, useCall } from '@polkadot/react-hooks';
import { createAccount } from './usePoolAccounts';
import { createAccounts } from './usePoolAccounts';
const OPT_MULTI = {
transform: ([[ids], opts]: [[string[]], Option<PalletNominationPoolsPoolMember>[]]): OwnPoolBase[] =>
@ -40,8 +40,7 @@ function useOwnPoolsImpl (): OwnPool[] | undefined {
return useMemo(
() => base && base.map((base) => ({
...base,
rewardId: createAccount(api, base.poolId, 1),
stashId: createAccount(api, base.poolId, 0)
...createAccounts(api, base.poolId)
})),
[api, base]
);

View File

@ -12,28 +12,32 @@ import { bnToU8a, stringToU8a, u8aConcat } from '@polkadot/util';
const EMPTY_H256 = new Uint8Array(32);
const MOD_PREFIX = stringToU8a('modl');
const U32_OPTS = { bitLength: 32, isLe: true };
export function createAccount (api: ApiPromise, poolId: BN, index: number): string {
return api.registry.createType(
'AccountId32',
u8aConcat(
MOD_PREFIX,
api.consts.nominationPools.palletId.toU8a(),
new Uint8Array([index]),
bnToU8a(poolId, { bitLength: 32 }),
EMPTY_H256
)
).toString();
function createAccount (api: ApiPromise, palletId: Uint8Array, poolId: BN, index: number): string {
return api.registry.createType('AccountId32', u8aConcat(
MOD_PREFIX,
palletId,
new Uint8Array([index]),
bnToU8a(poolId, U32_OPTS),
EMPTY_H256
)).toString();
}
export function createAccounts (api: ApiPromise, poolId: BN): PoolAccounts {
const palletId = api.consts.nominationPools.palletId.toU8a();
return {
rewardId: createAccount(api, palletId, poolId, 1),
stashId: createAccount(api, palletId, poolId, 0)
};
}
function usePoolAccountsImpl (poolId: BN): PoolAccounts {
const { api } = useApi();
return useMemo(
() => ({
rewardId: createAccount(api, poolId, 1),
stashId: createAccount(api, poolId, 0)
}),
() => createAccounts(api, poolId),
[api, poolId]
);
}