-
Notifications
You must be signed in to change notification settings - Fork 5
/
LaunchSimpleSto.ts
271 lines (237 loc) · 10.5 KB
/
LaunchSimpleSto.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/* eslint-disable import/no-duplicates */
import { ImportMock, MockManager } from 'ts-mock-imports';
import { restore, spy } from 'sinon';
import * as contractWrappersModule from '@polymathnetwork/contract-wrappers';
import {
BigNumber,
SecurityTokenEvents,
TransactionReceiptWithDecodedLogs,
} from '@polymathnetwork/contract-wrappers';
import { LaunchSimpleSto } from '../LaunchSimpleSto';
import { Procedure } from '../Procedure';
import { PolymathError } from '../../PolymathError';
import {
ErrorCode,
LaunchSimpleStoProcedureArgs,
PolyTransactionTag,
ProcedureType,
StoType,
Currency,
} from '../../types';
import * as simpleStoFactoryModule from '../../entities/factories/SimpleStoFactory';
import * as utilsModule from '../../utils';
import * as contextModule from '../../Context';
import * as wrappersModule from '../../PolymathBase';
import * as tokenFactoryModule from '../../testUtils/MockedTokenFactoryModule';
import * as moduleWrapperFactoryModule from '../../testUtils/MockedModuleWrapperFactoryModule';
import { Wallet } from '../../Wallet';
import { TransferErc20 } from '..';
import { mockFactories } from '../../testUtils/mockFactories';
import { SimpleSto, SecurityToken } from '../../entities';
const params: LaunchSimpleStoProcedureArgs = {
symbol: 'TEST1',
startDate: new Date(2030, 1),
endDate: new Date(2031, 1),
tokensOnSale: new BigNumber(1000),
rate: new BigNumber(10),
currency: Currency.ETH,
raisedFundsWallet: '0x6666666666666666666666666666666666666666',
unsoldTokensWallet: '0x7777777777777777777777777777777777777777',
};
const currentWallet = '0x8888888888888888888888888888888888888888';
const securityTokenAddress = '0x9999999999999999999999999999999999999999';
const polyTokenAddress = '0x5555555555555555555555555555555555555555';
const moduleFactoryAddress = '0x4444444444444444444444444444444444444444';
const costInPoly = new BigNumber(5);
const costInUsd = new BigNumber(6);
describe('LaunchSimpleSto', () => {
let target: LaunchSimpleSto;
let contextMock: MockManager<contextModule.Context>;
let wrappersMock: MockManager<wrappersModule.PolymathBase>;
let tokenFactoryMock: MockManager<tokenFactoryModule.MockedTokenFactoryModule>;
let moduleWrapperFactoryMock: MockManager<
moduleWrapperFactoryModule.MockedModuleWrapperFactoryModule
>;
let polyTokenMock: MockManager<contractWrappersModule.PolyToken>;
// Mock factories
let simpleStoFactoryMock: MockManager<simpleStoFactoryModule.SimpleStoFactory>;
let securityTokenMock: MockManager<contractWrappersModule.SecurityToken_3_0_0>;
let moduleFactoryMock: MockManager<contractWrappersModule.ModuleFactory_3_0_0>;
beforeEach(() => {
// Mock the context, wrappers, and tokenFactory to test LaunchCappedSto
contextMock = ImportMock.mockClass(contextModule, 'Context');
wrappersMock = ImportMock.mockClass(wrappersModule, 'PolymathBase');
tokenFactoryMock = ImportMock.mockClass(tokenFactoryModule, 'MockedTokenFactoryModule');
moduleWrapperFactoryMock = ImportMock.mockClass(
moduleWrapperFactoryModule,
'MockedModuleWrapperFactoryModule'
);
contextMock.set('contractWrappers', wrappersMock.getMockInstance());
wrappersMock.set('tokenFactory', tokenFactoryMock.getMockInstance());
wrappersMock.set('moduleFactory', moduleWrapperFactoryMock.getMockInstance());
securityTokenMock = ImportMock.mockClass(contractWrappersModule, 'SecurityToken_3_0_0');
securityTokenMock.mock('address', Promise.resolve(securityTokenAddress));
securityTokenMock.mock('balanceOf', Promise.resolve(new BigNumber(10)));
moduleFactoryMock = ImportMock.mockClass(contractWrappersModule, 'ModuleFactory_3_0_0');
moduleFactoryMock.mock('setupCostInPoly', Promise.resolve(costInPoly));
moduleFactoryMock.mock('isCostInPoly', Promise.resolve(false));
moduleFactoryMock.mock('setupCost', Promise.resolve(costInUsd));
tokenFactoryMock.mock(
'getSecurityTokenInstanceFromTicker',
securityTokenMock.getMockInstance()
);
moduleWrapperFactoryMock.mock('getModuleFactory', moduleFactoryMock.getMockInstance());
simpleStoFactoryMock = ImportMock.mockClass(simpleStoFactoryModule, 'SimpleStoFactory');
const factoryMockSetup = mockFactories();
factoryMockSetup.simpleStoFactory = simpleStoFactoryMock.getMockInstance();
contextMock.set('factories', factoryMockSetup);
contextMock.set('currentWallet', new Wallet({ address: () => Promise.resolve(currentWallet) }));
polyTokenMock = ImportMock.mockClass(contractWrappersModule, 'PolyToken');
polyTokenMock.mock('balanceOf', Promise.resolve(new BigNumber(20)));
polyTokenMock.mock('address', Promise.resolve(polyTokenAddress));
polyTokenMock.mock('allowance', Promise.resolve(new BigNumber(0)));
wrappersMock.set('polyToken', polyTokenMock.getMockInstance());
wrappersMock.mock('isTestnet', Promise.resolve(false));
wrappersMock.mock('getModuleFactoryAddress', moduleFactoryAddress);
// Instantiate LaunchSimpleSto
target = new LaunchSimpleSto(params, contextMock.getMockInstance());
});
afterEach(() => {
restore();
});
describe('Types', () => {
test('should extend procedure and have LaunchSimpleSto type', async () => {
expect(target instanceof Procedure).toBe(true);
expect(target.type).toBe(ProcedureType.LaunchSimpleSto);
});
});
describe('LaunchSimpleSto', () => {
test('should add a transaction to the queue to launch a capped sto with cost in usd', async () => {
const addTransactionSpy = spy(target, 'addTransaction');
securityTokenMock.mock('addModuleWithLabel', Promise.resolve('AddModuleWithLabel'));
// Real call
await target.prepareTransactions();
// Verifications
expect(
addTransactionSpy
.getCall(0)
.calledWith(securityTokenMock.getMockInstance().addModuleWithLabel)
).toEqual(true);
expect(addTransactionSpy.getCall(0).lastArg.fees).toEqual({
usd: costInUsd,
poly: costInPoly,
});
expect(addTransactionSpy.getCall(0).lastArg.tag).toEqual(PolyTransactionTag.EnableCappedSto);
expect(addTransactionSpy.callCount).toEqual(1);
});
test("should transfer POLY to the security token if the token's balance doesn't cover the launch fee", async () => {
const addProcedureSpy = spy(target, 'addProcedure');
const addTransactionSpy = spy(target, 'addTransaction');
securityTokenMock.mock('addModuleWithLabel', Promise.resolve('AddModuleWithLabel'));
polyTokenMock
.mock('balanceOf', Promise.resolve(new BigNumber(20)))
.withArgs({ owner: securityTokenAddress })
.returns(Promise.resolve(new BigNumber(1)));
// Real call
await target.prepareTransactions();
// Verifications
expect(
addTransactionSpy
.getCall(0)
.calledWith(securityTokenMock.getMockInstance().addModuleWithLabel)
).toEqual(true);
expect(addTransactionSpy.getCall(0).lastArg.fees).toEqual({
usd: costInUsd,
poly: costInPoly,
});
expect(addTransactionSpy.getCall(0).lastArg.tag).toEqual(PolyTransactionTag.EnableCappedSto);
expect(addTransactionSpy.callCount).toEqual(1);
expect(addProcedureSpy.getCall(0).calledWith(TransferErc20));
expect(addProcedureSpy.callCount).toEqual(1);
});
test('should add a transaction to the queue to launch a capped sto with cost in poly', async () => {
const addTransactionSpy = spy(target, 'addTransaction');
moduleFactoryMock.mock('isCostInPoly', Promise.resolve(true));
securityTokenMock.mock('addModuleWithLabel', Promise.resolve('AddModuleWithLabel'));
// Real call
await target.prepareTransactions();
// Verifications
expect(
addTransactionSpy
.getCall(0)
.calledWith(securityTokenMock.getMockInstance().addModuleWithLabel)
).toEqual(true);
expect(addTransactionSpy.getCall(0).lastArg.fees).toEqual({
usd: null,
poly: costInPoly,
});
expect(addTransactionSpy.getCall(0).lastArg.tag).toEqual(PolyTransactionTag.EnableCappedSto);
expect(addTransactionSpy.callCount).toEqual(1);
});
test('should throw if corresponding capped sto event is not fired', async () => {
ImportMock.mockFunction(utilsModule, 'findEvents', []);
// Real call
const resolver = await target.prepareTransactions();
await expect(resolver.run({} as TransactionReceiptWithDecodedLogs)).rejects.toThrow(
new PolymathError({
code: ErrorCode.UnexpectedEventLogs,
message:
"The Capped STO was successfully launched but the corresponding event wasn't fired. Please report this issue to the Polymath team.",
})
);
});
test('should return the capped sto object information', async () => {
const stoObject = {
securityTokenId: params.symbol,
stoType: StoType.Simple,
address: securityTokenAddress,
};
const fetchStub = simpleStoFactoryMock.mock('fetch', Promise.resolve(stoObject));
const moduleAddress = '0x3333333333333333333333333333333333333333';
const findEventsStub = ImportMock.mockFunction(utilsModule, 'findEvents', [
{
args: {
_module: moduleAddress,
},
},
]);
// Real call
const resolver = await target.prepareTransactions();
await resolver.run({} as TransactionReceiptWithDecodedLogs);
// Verification for resolver result
expect(resolver.result).toEqual(stoObject);
// Verification for fetch
expect(
fetchStub.getCall(0).calledWithExactly(
SimpleSto.generateId({
securityTokenId: SecurityToken.generateId({
symbol: params.symbol,
}),
stoType: StoType.Simple,
address: moduleAddress,
})
)
).toEqual(true);
expect(fetchStub.callCount).toBe(1);
// Verifications for findEvents
expect(
findEventsStub.getCall(0).calledWithMatch({
eventName: SecurityTokenEvents.ModuleAdded,
})
).toEqual(true);
expect(findEventsStub.callCount).toBe(1);
});
test('should throw if there is no supplied valid security token', async () => {
tokenFactoryMock
.mock('getSecurityTokenInstanceFromTicker')
.withArgs(params.symbol)
.throws();
await expect(target.prepareTransactions()).rejects.toThrow(
new PolymathError({
code: ErrorCode.ProcedureValidationError,
message: `There is no Security Token with symbol ${params.symbol}`,
})
);
});
});
});