generated from PolymeshAssociation/typescript-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathremoveSecondaryAccounts.ts
64 lines (53 loc) · 1.67 KB
/
removeSecondaryAccounts.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import BigNumber from 'bignumber.js';
import { find } from 'lodash';
import { assertSecondaryAccounts } from '~/api/procedures/utils';
import { PolymeshError, Procedure } from '~/internal';
import { Account, ErrorCode, TxTags } from '~/types';
import { stringToAccountId } from '~/utils/conversion';
export interface RemoveSecondaryAccountsParams {
accounts: Account[];
}
/**
* @hidden
*/
export async function prepareRemoveSecondaryAccounts(
this: Procedure<RemoveSecondaryAccountsParams>,
args: RemoveSecondaryAccountsParams
): Promise<void> {
const {
context: {
polymeshApi: { tx },
},
context,
} = this;
const { accounts } = args;
const identity = await context.getSigningIdentity();
const [{ account: primaryAccount }, secondaryAccounts] = await Promise.all([
identity.getPrimaryAccount(),
identity.getSecondaryAccounts(),
]);
const isPrimaryAccountPresent = find(accounts, account => account.isEqual(primaryAccount));
if (isPrimaryAccountPresent) {
throw new PolymeshError({
code: ErrorCode.UnmetPrerequisite,
message: 'You cannot remove the primary Account',
});
}
assertSecondaryAccounts(accounts, secondaryAccounts);
this.addTransaction({
transaction: tx.identity.removeSecondaryKeys,
feeMultiplier: new BigNumber(accounts.length),
args: [accounts.map(({ address }) => stringToAccountId(address, context))],
});
}
/**
* @hidden
*/
export const removeSecondaryAccounts = (): Procedure<RemoveSecondaryAccountsParams> =>
new Procedure(prepareRemoveSecondaryAccounts, {
permissions: {
transactions: [TxTags.identity.RemoveSecondaryKeys],
assets: [],
portfolios: [],
},
});