generated from PolymeshAssociation/typescript-boilerplate
/
index.ts
215 lines (174 loc) · 6.96 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
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
207
208
209
210
211
212
213
214
215
import { Balance } from '@polkadot/types/interfaces';
import sinon from 'sinon';
import { modifyToken, transferTokenOwnership } from '~/api/procedures';
import { Entity, TransactionQueue } from '~/base';
import { dsMockUtils } from '~/testUtils/mocks';
import { TokenIdentifierType } from '~/types';
import { tuple } from '~/types/utils';
import * as utilsModule from '~/utils';
import { SecurityToken } from '../';
describe('SecurityToken class', () => {
beforeAll(() => {
dsMockUtils.initMocks();
});
afterEach(() => {
dsMockUtils.reset();
});
afterAll(() => {
dsMockUtils.cleanup();
});
test('should extend entity', () => {
expect(SecurityToken.prototype instanceof Entity).toBe(true);
});
describe('constructor', () => {
test('should assign ticker and did to instance', () => {
const ticker = 'test';
const context = dsMockUtils.getContextInstance();
const securityToken = new SecurityToken({ ticker }, context);
expect(securityToken.ticker).toBe(ticker);
expect(securityToken.did).toBe(utilsModule.tickerToDid(ticker));
});
});
describe('method: isUniqueIdentifiers', () => {
test('should return true if the object conforms to the interface', () => {
expect(SecurityToken.isUniqueIdentifiers({ ticker: 'someTicker' })).toBe(true);
expect(SecurityToken.isUniqueIdentifiers({})).toBe(false);
expect(SecurityToken.isUniqueIdentifiers({ ticker: 3 })).toBe(false);
});
});
describe('method: details', () => {
test('should return details for a security token', async () => {
const ticker = 'test';
const totalSupply = 1000;
const isDivisible = true;
const owner = '0x0wn3r';
const assetType = 'EquityCommon';
const context = dsMockUtils.getContextInstance();
const securityToken = new SecurityToken({ ticker }, context);
dsMockUtils.createQueryStub('asset', 'tokens', {
returnValue: dsMockUtils.createMockSecurityToken({
/* eslint-disable @typescript-eslint/camelcase */
owner_did: dsMockUtils.createMockIdentityId(owner),
name: dsMockUtils.createMockAssetName(ticker),
asset_type: dsMockUtils.createMockAssetType(assetType),
divisible: dsMockUtils.createMockBool(isDivisible),
link_id: dsMockUtils.createMockU64(3),
total_supply: dsMockUtils.createMockBalance(totalSupply),
/* eslint-enable @typescript-eslint/camelcase */
}),
});
const details = await securityToken.details();
expect(details.name).toBe(ticker);
expect(details.totalSupply).toEqual(
utilsModule.balanceToBigNumber((totalSupply as unknown) as Balance)
);
expect(details.isDivisible).toBe(isDivisible);
expect(details.owner.did).toBe(owner);
expect(details.assetType).toBe(assetType);
});
});
describe('method: transferOwnership', () => {
test('should prepare the procedure with the correct arguments and context, and return the resulting transaction queue', async () => {
const ticker = 'TEST';
const context = dsMockUtils.getContextInstance();
const securityToken = new SecurityToken({ ticker }, context);
const did = 'someOtherDid';
const expiry = new Date('10/14/3040');
const args = {
did,
expiry,
};
const expectedQueue = ('someQueue' as unknown) as TransactionQueue<SecurityToken>;
sinon
.stub(transferTokenOwnership, 'prepare')
.withArgs({ ticker, ...args }, context)
.resolves(expectedQueue);
const queue = await securityToken.transferOwnership(args);
expect(queue).toBe(expectedQueue);
});
});
describe('method: modify', () => {
test('should prepare the procedure with the correct arguments and context, and return the resulting transaction queue', async () => {
const ticker = 'TEST';
const context = dsMockUtils.getContextInstance();
const securityToken = new SecurityToken({ ticker }, context);
const makeDivisible: true = true;
const args = {
makeDivisible,
};
const expectedQueue = ('someQueue' as unknown) as TransactionQueue<SecurityToken>;
sinon
.stub(modifyToken, 'prepare')
.withArgs({ ticker, ...args }, context)
.resolves(expectedQueue);
const queue = await securityToken.modify(args);
expect(queue).toBe(expectedQueue);
});
});
describe('method: currentFundingRound', () => {
test('should return the funding round for a security token', async () => {
const ticker = 'test';
const fundingRound = 'Series A';
const context = dsMockUtils.getContextInstance();
const securityToken = new SecurityToken({ ticker }, context);
dsMockUtils.createQueryStub('asset', 'fundingRound', {
returnValue: dsMockUtils.createMockFundingRoundName(fundingRound),
});
const result = await securityToken.currentFundingRound();
expect(result).toBe(fundingRound);
});
});
describe('method: getIdentifiers', () => {
test('should return the list of token identifiers for a security token', async () => {
const ticker = 'TEST';
const isinValue = 'FAKE ISIN';
const cusipValue = 'FAKE CUSIP';
const cinsValue = 'FAKE CINS';
const isinMock = dsMockUtils.createMockAssetIdentifier(isinValue);
const cusipMock = dsMockUtils.createMockAssetIdentifier(cusipValue);
const cinsMock = dsMockUtils.createMockAssetIdentifier(cinsValue);
const tokenIdentifiers = [
{
type: TokenIdentifierType.Isin,
value: isinValue,
},
{
type: TokenIdentifierType.Cusip,
value: cusipValue,
},
{
type: TokenIdentifierType.Cins,
value: cinsValue,
},
];
const rawIdentifiers = tokenIdentifiers.map(({ type, value }) =>
tuple(
dsMockUtils.createMockIdentifierType(type),
dsMockUtils.createMockAssetIdentifier(value)
)
);
const context = dsMockUtils.getContextInstance();
const tokenIdentifierTypeToIdentifierTypeStub = sinon.stub(
utilsModule,
'tokenIdentifierTypeToIdentifierType'
);
tokenIdentifierTypeToIdentifierTypeStub
.withArgs(tokenIdentifiers[0].type, context)
.returns(rawIdentifiers[0][0]);
tokenIdentifierTypeToIdentifierTypeStub
.withArgs(tokenIdentifiers[1].type, context)
.returns(rawIdentifiers[1][0]);
tokenIdentifierTypeToIdentifierTypeStub
.withArgs(tokenIdentifiers[2].type, context)
.returns(rawIdentifiers[2][0]);
dsMockUtils.createQueryStub('asset', 'identifiers', {
multi: [isinMock, cusipMock, cinsMock],
});
const securityToken = new SecurityToken({ ticker }, context);
const result = await securityToken.getIdentifiers();
expect(result[0].value).toBe(isinValue);
expect(result[1].value).toBe(cusipValue);
expect(result[2].value).toBe(cinsValue);
});
});
});