generated from PolymeshAssociation/typescript-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 11
/
index.ts
148 lines (130 loc) · 4.22 KB
/
index.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import BigNumber from 'bignumber.js';
import { UniqueIdentifiers } from '~/api/entities/Account';
import { MultiSigProposal } from '~/api/entities/MultiSigProposal';
import { Account, Context, Identity, modifyMultiSig, PolymeshError } from '~/internal';
import { ErrorCode, ModifyMultiSigParams, MultiSigDetails, ProcedureMethod } from '~/types';
import {
addressToKey,
identityIdToString,
signatoryToSignerValue,
signerValueToSigner,
stringToAccountId,
u64ToBigNumber,
} from '~/utils/conversion';
import { createProcedureMethod } from '~/utils/internal';
/**
* Represents a MultiSig Account. A MultiSig Account is composed of one or more signing Accounts. In order to submit a transaction, a specific amount of those signing Accounts must approve it first
*/
export class MultiSig extends Account {
/**
* @hidden
*/
public constructor(identifiers: UniqueIdentifiers, context: Context) {
super(identifiers, context);
this.modify = createProcedureMethod(
{
getProcedureAndArgs: modifyArgs => [modifyMultiSig, { multiSig: this, ...modifyArgs }],
},
context
);
}
/**
* Return details about this MultiSig such as the signing Accounts and the required number of signatures to execute a MultiSigProposal
*/
public async details(): Promise<MultiSigDetails> {
const {
context: {
polymeshApi: {
query: { multiSig },
},
},
context,
address,
} = this;
const rawAddress = stringToAccountId(address, context);
const [rawSigners, rawSignersRequired] = await Promise.all([
multiSig.multiSigSigners.entries(rawAddress),
multiSig.multiSigSignsRequired(rawAddress),
]);
const signers = rawSigners.map(
([
{
args: [, signatory],
},
]) => {
return signerValueToSigner(signatoryToSignerValue(signatory), context);
}
);
const requiredSignatures = u64ToBigNumber(rawSignersRequired);
return { signers, requiredSignatures };
}
/**
* Given an ID, fetch a { @link api/entities/MultiSigProposal!MultiSigProposal } for this MultiSig
*
* @throws if the MultiSigProposal is not found
*/
public async getProposal(args: { id: BigNumber }): Promise<MultiSigProposal> {
const { id } = args;
const { address, context } = this;
const proposal = new MultiSigProposal({ multiSigAddress: address, id }, context);
const exists = await proposal.exists();
if (!exists) {
throw new PolymeshError({
code: ErrorCode.DataUnavailable,
message: `Proposal with ID "${id}" was not found`,
});
}
return proposal;
}
/**
* Return all { @link api/entities/MultiSigProposal!MultiSigProposal } for this MultiSig Account
*/
public async getProposals(): Promise<MultiSigProposal[]> {
const {
context: {
polymeshApi: {
query: { multiSig },
},
},
context,
address,
} = this;
const rawAddress = stringToAccountId(address, context);
const rawProposalEntries = await multiSig.proposals.entries(rawAddress);
return rawProposalEntries.map(
([
{
args: [, rawId],
},
]) => new MultiSigProposal({ multiSigAddress: address, id: u64ToBigNumber(rawId) }, context)
);
}
/**
* Returns the Identity of the MultiSig creator. This Identity can add or remove signers directly without creating a MultiSigProposal first.
*/
public async getCreator(): Promise<Identity> {
const {
context: {
polymeshApi: {
query: { multiSig },
},
},
context,
address,
} = this;
const rawAddress = addressToKey(address, context);
const rawCreatorDid = await multiSig.multiSigToIdentity(rawAddress);
if (rawCreatorDid.isEmpty) {
throw new PolymeshError({
code: ErrorCode.DataUnavailable,
message: 'No creator was found for this MultiSig address',
});
}
const did = identityIdToString(rawCreatorDid);
return new Identity({ did }, context);
}
/**
* Modify the signers for the MultiSig. The signing Account must belong to the Identity of the creator of the MultiSig
*/
public modify: ProcedureMethod<Pick<ModifyMultiSigParams, 'signers'>, void>;
}