generated from PolymeshAssociation/typescript-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Account.ts
206 lines (180 loc) · 5.35 KB
/
Account.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import BigNumber from 'bignumber.js';
import { TxTag } from 'polymesh-types/types';
import { Entity, Identity } from '~/api/entities';
import { Authorizations } from '~/api/entities/common/namespaces/Authorizations';
import { Context } from '~/base';
import { transactions } from '~/middleware/queries';
import { Query, TransactionOrderByInput } from '~/middleware/types';
import {
AccountBalance,
Ensured,
ExtrinsicData,
ResultSet,
SubCallback,
UnsubCallback,
} from '~/types';
import {
addressToKey,
extrinsicIdentifierToTxTag,
identityIdToString,
stringToAccountId,
txTagToExtrinsicIdentifier,
} from '~/utils/conversion';
import { calculateNextKey } from '~/utils/internal';
export interface UniqueIdentifiers {
address: string;
}
/**
* Represents an account in the Polymesh blockchain. Accounts can hold POLYX, control Identities and vote on proposals (among other things)
*/
export class Account extends Entity<UniqueIdentifiers> {
/**
* @hidden
* Check if a value is of type [[UniqueIdentifiers]]
*/
public static isUniqueIdentifiers(identifier: unknown): identifier is UniqueIdentifiers {
const { address } = identifier as UniqueIdentifiers;
return typeof address === 'string';
}
/**
* Polymesh-specific address of the account. Serves as an identifier
*/
public address: string;
/**
* public key of the account. This is a hex representation of the address that is transversal to any Substrate chain
*/
public key: string;
// Namespaces
public authorizations: Authorizations<Account>;
/**
* @hidden
*/
public constructor(identifiers: UniqueIdentifiers, context: Context) {
super(identifiers, context);
const { address } = identifiers;
this.address = address;
this.key = addressToKey(address);
this.authorizations = new Authorizations(this, context);
}
/**
* Get the free/locked POLYX balance of the account
*
* @note can be subscribed to
*/
public getBalance(): Promise<AccountBalance>;
public getBalance(callback: SubCallback<AccountBalance>): Promise<UnsubCallback>;
// eslint-disable-next-line require-jsdoc
public getBalance(
callback?: SubCallback<AccountBalance>
): Promise<AccountBalance | UnsubCallback> {
const { context, address } = this;
if (callback) {
return context.accountBalance(address, callback);
}
return context.accountBalance(address);
}
/**
* Retrieve the Identity associated to this Account (null if there is none)
*/
public async getIdentity(): Promise<Identity | null> {
const {
context: {
polymeshApi: {
query: { identity },
},
},
context,
address,
} = this;
try {
const identityIdWrapper = await identity.keyToIdentityIds(
stringToAccountId(address, context)
);
const did = identityIdToString(identityIdWrapper.unwrap().asUnique);
return new Identity({ did }, context);
} catch (err) {
return null;
}
}
/**
* Retrieve a list of transactions signed by this account. Can be filtered using parameters
*
* @param filters.tag - tag associated with the transaction
* @param filters.success - whether the transaction was successful or not
* @param filters.size - page size
* @param filters.start - page offset
*/
public async getTransactionHistory(
filters: {
blockNumber?: BigNumber;
tag?: TxTag;
success?: boolean;
size?: number;
start?: number;
orderBy?: TransactionOrderByInput;
} = {}
): Promise<ResultSet<ExtrinsicData>> {
const { context, address } = this;
const { blockNumber, tag, success, size, start, orderBy } = filters;
let moduleId;
let callId;
if (tag) {
({ moduleId, callId } = txTagToExtrinsicIdentifier(tag));
}
/* eslint-disable @typescript-eslint/camelcase */
const result = await context.queryMiddleware<Ensured<Query, 'transactions'>>(
transactions({
block_id: blockNumber ? blockNumber.toNumber() : undefined,
address: addressToKey(address),
module_id: moduleId,
call_id: callId,
success,
count: size,
skip: start,
orderBy,
})
);
const {
data: {
transactions: { items: transactionList, totalCount: count },
},
} = result;
const data: ExtrinsicData[] = [];
transactionList.forEach(
({
block_id,
extrinsic_idx,
address: rawAddress,
nonce,
module_id,
call_id,
params,
success: txSuccess,
spec_version_id,
extrinsic_hash,
}) => {
// TODO remove null check once types fixed
/* eslint-disable @typescript-eslint/no-non-null-assertion */
data.push({
blockNumber: new BigNumber(block_id),
extrinsicIdx: extrinsic_idx,
address: rawAddress ?? null,
nonce: nonce!,
txTag: extrinsicIdentifierToTxTag({ moduleId: module_id, callId: call_id }),
params,
success: !!txSuccess,
specVersionId: spec_version_id,
extrinsicHash: extrinsic_hash!,
});
/* eslint-enabled @typescript-eslint/no-non-null-assertion */
}
);
/* eslint-enable @typescript-eslint/camelcase */
const next = calculateNextKey(count, size, start);
return {
data,
next,
count,
};
}
}