generated from PolymeshAssociation/typescript-boilerplate
/
TokenHolders.ts
52 lines (47 loc) · 1.44 KB
/
TokenHolders.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
import BigNumber from 'bignumber.js';
import { IdentityId } from 'polymesh-types/types';
import { IdentityBalance } from '~/api/entities/types';
import { Identity, Namespace, SecurityToken } from '~/internal';
import { PaginationOptions, ResultSet } from '~/types';
import { balanceToBigNumber, identityIdToString, stringToTicker } from '~/utils/conversion';
import { requestPaginated } from '~/utils/internal';
/**
* Handles all Security Token Holders related functionality
*/
export class TokenHolders extends Namespace<SecurityToken> {
/**
* Retrieve all the token holders with balance
*
* @note supports pagination
*/
public async get(paginationOpts?: PaginationOptions): Promise<ResultSet<IdentityBalance>> {
const {
context: {
polymeshApi: { query },
},
context,
parent: { ticker },
} = this;
const rawTicker = stringToTicker(ticker, context);
const { entries, lastKey: next } = await requestPaginated(query.asset.balanceOf, {
arg: rawTicker,
paginationOpts,
});
const data: { identity: Identity; balance: BigNumber }[] = entries.map(
([storageKey, balance]) => {
const entry = {
identity: new Identity(
{ did: identityIdToString(storageKey.args[1] as IdentityId) },
context
),
balance: balanceToBigNumber(balance),
};
return entry;
}
);
return {
data,
next,
};
}
}