generated from PolymeshAssociation/typescript-boilerplate
/
TokenHolders.ts
97 lines (76 loc) · 2.76 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
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
import { Balance } from '@polkadot/types/interfaces';
import BigNumber from 'bignumber.js';
import sinon from 'sinon';
import { Identity } from '~/api/entities/Identity';
import { Namespace } from '~/base';
import { IdentityId, Ticker } from '~/polkadot';
import { dsMockUtils, entityMockUtils } from '~/testUtils/mocks';
import { IdentityBalance } from '~/types';
import { tuple } from '~/types/utils';
import * as utilsModule from '~/utils';
import { TokenHolders } from '../TokenHolders';
describe('TokenHolders class', () => {
beforeAll(() => {
entityMockUtils.initMocks();
dsMockUtils.initMocks();
});
afterEach(() => {
entityMockUtils.reset();
dsMockUtils.reset();
});
afterAll(() => {
entityMockUtils.cleanup();
dsMockUtils.cleanup();
});
test('should extend namespace', () => {
expect(TokenHolders.prototype instanceof Namespace).toBe(true);
});
describe('method: get', () => {
afterAll(() => {
sinon.restore();
});
test('should retrieve all the token holders with balance', async () => {
const ticker = 'TEST';
const mockContext = dsMockUtils.getContextInstance();
const rawTicker = dsMockUtils.createMockTicker(ticker);
const fakeData = [
{
identity: 'someIdentity',
value: 1000,
},
{
identity: 'otherIdentity',
value: 2000,
},
];
const expectedHolders: IdentityBalance[] = [];
sinon
.stub(utilsModule, 'stringToTicker')
.withArgs(ticker, mockContext)
.returns(rawTicker);
const identityIdToStringStub = sinon.stub(utilsModule, 'identityIdToString');
const balanceToBigNumberStub = sinon.stub(utilsModule, 'balanceToBigNumber');
const balanceOfEntries: [[Ticker, IdentityId], Balance][] = [];
const context = dsMockUtils.getContextInstance();
fakeData.forEach(({ identity, value }) => {
const identityId = dsMockUtils.createMockIdentityId(identity);
const fakeBalance = dsMockUtils.createMockBalance(value);
const balance = new BigNumber(value);
identityIdToStringStub.withArgs(identityId).returns(identity);
balanceToBigNumberStub.withArgs(fakeBalance).returns(balance);
balanceOfEntries.push(tuple([rawTicker, identityId], fakeBalance));
expectedHolders.push({
identity: new Identity({ did: identity }, context),
balance,
});
});
dsMockUtils.createQueryStub('asset', 'balanceOf', {
entries: balanceOfEntries,
});
const token = entityMockUtils.getSecurityTokenInstance();
const tokenHolders = new TokenHolders(token, context);
const result = await tokenHolders.get();
expect(result).toEqual(expectedHolders);
});
});
});