Skip to content
This repository was archived by the owner on Jul 6, 2022. It is now read-only.

Commit 08a6e12

Browse files
author
Victor Wiebe
committed
feat: 🎸 callAsync methods with tests in blacklistTM
1 parent 0ac18e9 commit 08a6e12

File tree

2 files changed

+256
-14
lines changed

2 files changed

+256
-14
lines changed

‎src/contract_wrappers/modules/transfer_manager/__tests__/blacklist_transfer_manager_wrapper.test.ts‎

Lines changed: 163 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,14 @@ import BlacklistTransferManagerWrapper from '../blacklist_transfer_manager_wrapp
1212
import ContractFactory from '../../../../factories/contractFactory';
1313
import ModuleWrapper from '../../module_wrapper';
1414
import {
15+
bytes32ArrayToStringArray, dateToBigNumber,
16+
parsePermBytes32Value,
17+
stringArrayToBytes32Array,
18+
stringToBytes32,
1519
valueToWei,
20+
weiToValue,
1621
} from '../../../../utils/convert';
22+
import { Partition, Perm } from '../../../../types';
1723

1824
describe('BlacklistTransferManagerWrapper', () => {
1925
let target: BlacklistTransferManagerWrapper;
@@ -196,7 +202,6 @@ describe('BlacklistTransferManagerWrapper', () => {
196202
});
197203
});
198204

199-
200205
describe('verifyTransfer', () => {
201206
test('should verify Transfer', async () => {
202207
const statusCode = new BigNumber(2);
@@ -259,6 +264,163 @@ describe('BlacklistTransferManagerWrapper', () => {
259264
});
260265
});
261266

267+
describe('getListOfAddresses', () => {
268+
test.todo('should fail as blacklist name is an empty string');
269+
270+
test('should call to getListOfAddresses', async () => {
271+
const expectedResult = [
272+
'0x8888888888888888888888888888888888888888',
273+
'0x9999999999999999999999999999999999999999',
274+
];
275+
const mockedParams = {
276+
blacklistName: 'Blacklist1',
277+
};
278+
279+
// Mocked method
280+
const mockedMethod = mock(MockedCallMethod);
281+
// Stub the method
282+
when(mockedContract.getListOfAddresses).thenReturn(instance(mockedMethod));
283+
// Stub the request
284+
when(mockedMethod.callAsync(objectContaining(stringToBytes32(mockedParams.blacklistName)))).thenResolve(
285+
expectedResult,
286+
);
287+
288+
// Real call
289+
const result = await target.getListOfAddresses(mockedParams);
290+
// Result expectation
291+
expect(result).toEqual(expectedResult);
292+
293+
// Verifications
294+
verify(mockedContract.getListOfAddresses).once();
295+
verify(mockedMethod.callAsync(objectContaining(stringToBytes32(mockedParams.blacklistName)))).once();
296+
});
297+
});
298+
299+
describe('getBlacklistNamesToUser', () => {
300+
test('should call to getBlacklistNamesToUser', async () => {
301+
const expectedResult = stringArrayToBytes32Array(['Blacklist1', 'Blacklist2']);
302+
const mockedParams = {
303+
user: '0x8888888888888888888888888888888888888888',
304+
};
305+
// Mocked method
306+
const mockedMethod = mock(MockedCallMethod);
307+
// Stub the method
308+
when(mockedContract.getBlacklistNamesToUser).thenReturn(instance(mockedMethod));
309+
// Stub the request
310+
when(mockedMethod.callAsync(mockedParams.user)).thenResolve(expectedResult);
311+
312+
// Real call
313+
const result = await target.getBlacklistNamesToUser(mockedParams);
314+
// Result expectation
315+
expect(result).toEqual(bytes32ArrayToStringArray(expectedResult));
316+
317+
// Verifications
318+
verify(mockedContract.getBlacklistNamesToUser).once();
319+
verify(mockedMethod.callAsync(mockedParams.user)).once();
320+
});
321+
});
322+
323+
describe('getAllBlacklists', () => {
324+
test('should call to getAllBlacklists', async () => {
325+
const expectedResult = stringArrayToBytes32Array(['Blacklist1', 'Blacklist2']);
326+
327+
// Mocked method
328+
const mockedMethod = mock(MockedCallMethod);
329+
// Stub the method
330+
when(mockedContract.getAllBlacklists).thenReturn(instance(mockedMethod));
331+
// Stub the request
332+
when(mockedMethod.callAsync()).thenResolve(expectedResult);
333+
334+
// Real call
335+
const result = await target.getAllBlacklists();
336+
// Result expectation
337+
expect(result).toEqual(bytes32ArrayToStringArray(expectedResult));
338+
339+
// Verifications
340+
verify(mockedContract.getAllBlacklists).once();
341+
verify(mockedMethod.callAsync()).once();
342+
});
343+
});
344+
345+
describe('getTokensByPartition', () => {
346+
test('should call to getTokensByPartition', async () => {
347+
const expectedDecimalsResult = new BigNumber(18);
348+
const expectedResult = valueToWei(new BigNumber(100), expectedDecimalsResult);
349+
const mockedParams = {
350+
partition: Partition.Unlocked,
351+
tokenHolder: '0x8888888888888888888888888888888888888888',
352+
additionalBalance: new BigNumber(10),
353+
};
354+
355+
// Security Token Address expected
356+
const expectedSecurityTokenAddress = '0x3333333333333333333333333333333333333333';
357+
// Setup get Security Token Address
358+
const mockedGetSecurityTokenAddressMethod = mock(MockedCallMethod);
359+
when(mockedContract.securityToken).thenReturn(instance(mockedGetSecurityTokenAddressMethod));
360+
when(mockedGetSecurityTokenAddressMethod.callAsync()).thenResolve(expectedSecurityTokenAddress);
361+
when(mockedContractFactory.getSecurityTokenContract(expectedSecurityTokenAddress)).thenResolve(
362+
instance(mockedSecurityTokenContract),
363+
);
364+
const mockedSecurityTokenDecimalsMethod = mock(MockedCallMethod);
365+
when(mockedSecurityTokenDecimalsMethod.callAsync()).thenResolve(expectedDecimalsResult);
366+
when(mockedSecurityTokenContract.decimals).thenReturn(instance(mockedSecurityTokenDecimalsMethod));
367+
368+
// Mocked method
369+
const mockedMethod = mock(MockedCallMethod);
370+
// Stub the method
371+
when(mockedContract.getTokensByPartition).thenReturn(instance(mockedMethod));
372+
// Stub the request
373+
when(
374+
mockedMethod.callAsync(
375+
mockedParams.partition,
376+
mockedParams.tokenHolder,
377+
objectContaining(valueToWei(mockedParams.additionalBalance, expectedDecimalsResult)),
378+
),
379+
).thenResolve(expectedResult);
380+
381+
// Real call
382+
const result = await target.getTokensByPartition(mockedParams);
383+
// Result expectation
384+
expect(result).toEqual(weiToValue(expectedResult, expectedDecimalsResult));
385+
386+
// Verifications
387+
verify(mockedContract.getTokensByPartition).once();
388+
verify(
389+
mockedMethod.callAsync(
390+
mockedParams.partition,
391+
mockedParams.tokenHolder,
392+
objectContaining(valueToWei(mockedParams.additionalBalance, expectedDecimalsResult)),
393+
),
394+
).once();
395+
verify(mockedContract.securityToken).once();
396+
verify(mockedGetSecurityTokenAddressMethod.callAsync()).once();
397+
verify(mockedContractFactory.getSecurityTokenContract(expectedSecurityTokenAddress)).once();
398+
verify(mockedSecurityTokenDecimalsMethod.callAsync()).once();
399+
verify(mockedSecurityTokenContract.decimals).once();
400+
});
401+
});
402+
403+
describe('getPermissions', () => {
404+
test('should call to getPermissions', async () => {
405+
const expectedResult = stringArrayToBytes32Array([Perm.Admin]);
406+
407+
// Mocked method
408+
const mockedMethod = mock(MockedCallMethod);
409+
// Stub the method
410+
when(mockedContract.getPermissions).thenReturn(instance(mockedMethod));
411+
// Stub the request
412+
when(mockedMethod.callAsync()).thenResolve(expectedResult);
413+
414+
// Real call
415+
const result = await target.getPermissions();
416+
// Result expectation
417+
expect(result).toEqual(expectedResult.map(parsePermBytes32Value));
418+
// Verifications
419+
verify(mockedContract.getPermissions).once();
420+
verify(mockedMethod.callAsync()).once();
421+
});
422+
});
423+
262424
describe('SubscribeAsync', () => {
263425
test('should throw as eventName does not belong to BlacklistTransferManager', async () => {
264426
// Mocked parameters

‎src/contract_wrappers/modules/transfer_manager/blacklist_transfer_manager_wrapper.ts‎

Lines changed: 93 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,22 @@ import {
2323
EventCallback,
2424
GetLogs,
2525
GetLogsAsyncParams,
26+
Partition,
27+
Perm,
2628
Subscribe,
27-
SubscribeAsyncParams, TransferResult,
29+
SubscribeAsyncParams,
30+
TransferResult,
2831
TxParams,
2932
} from '../../../types';
30-
import {parseTransferResult, valueToWei} from '../../../utils/convert';
33+
import {
34+
bigNumberToDate,
35+
bytes32ArrayToStringArray,
36+
parsePermBytes32Value,
37+
parseTransferResult,
38+
stringToBytes32,
39+
valueToWei,
40+
weiToValue,
41+
} from '../../../utils/convert';
3142

3243
interface DeleteInvestorFromBlacklistSubscribeAsyncParams extends SubscribeAsyncParams {
3344
eventName: BlacklistTransferManagerEvents.DeleteInvestorFromBlacklist;
@@ -104,10 +115,18 @@ interface BlacklistTransferManagerSubscribeAsyncParams extends Subscribe {
104115

105116
interface GetBlacklistTransferManagerLogsAsyncParams extends GetLogs {
106117
(params: GetAddBlacklistTypeLogsAsyncParams): Promise<LogWithDecodedArgs<BlacklistTransferManagerPauseEventArgs>[]>;
107-
(params: GetModifyBlacklistTypeLogsAsyncParams): Promise<LogWithDecodedArgs<BlacklistTransferManagerPauseEventArgs>[]>;
108-
(params: GetDeleteBlacklistTypeLogsAsyncParams): Promise<LogWithDecodedArgs<BlacklistTransferManagerPauseEventArgs>[]>;
109-
(params: GetAddInvestorToBlacklistLogsAsyncParams): Promise<LogWithDecodedArgs<BlacklistTransferManagerPauseEventArgs>[]>;
110-
(params: GetDeleteInvestorFromBlacklistLogsAsyncParams): Promise<LogWithDecodedArgs<BlacklistTransferManagerPauseEventArgs>[]>;
118+
(params: GetModifyBlacklistTypeLogsAsyncParams): Promise<
119+
LogWithDecodedArgs<BlacklistTransferManagerPauseEventArgs>[]
120+
>;
121+
(params: GetDeleteBlacklistTypeLogsAsyncParams): Promise<
122+
LogWithDecodedArgs<BlacklistTransferManagerPauseEventArgs>[]
123+
>;
124+
(params: GetAddInvestorToBlacklistLogsAsyncParams): Promise<
125+
LogWithDecodedArgs<BlacklistTransferManagerPauseEventArgs>[]
126+
>;
127+
(params: GetDeleteInvestorFromBlacklistLogsAsyncParams): Promise<
128+
LogWithDecodedArgs<BlacklistTransferManagerPauseEventArgs>[]
129+
>;
111130
(params: GetPauseLogsAsyncParams): Promise<LogWithDecodedArgs<BlacklistTransferManagerPauseEventArgs>[]>;
112131
(params: GetUnpauseLogsAsyncParams): Promise<LogWithDecodedArgs<BlacklistTransferManagerUnpauseEventArgs>[]>;
113132
}
@@ -119,6 +138,20 @@ interface VerifyTransferParams {
119138
data: string;
120139
}
121140

141+
interface GetTokensByPartitionParams {
142+
partition: Partition;
143+
tokenHolder: string;
144+
additionalBalance: BigNumber;
145+
}
146+
147+
interface BlacklistParams extends TxParams {
148+
blacklistName: string;
149+
}
150+
151+
interface UserAddressParams {
152+
user: string;
153+
}
154+
122155
// Return Types
123156

124157
interface VerifyTransfer {
@@ -165,18 +198,65 @@ export default class BlacklistTransferManagerWrapper extends ModuleWrapper {
165198
return (await this.contract).pause.sendTransactionAsync(params.txData, params.safetyFactor);
166199
};
167200

201+
/**
202+
* getListOfAddresses
203+
*/
204+
public getListOfAddresses = async (params: BlacklistParams): Promise<string[]> => {
205+
assert.assert(params.blacklistName.length > 0, 'LockUp Details must not be an empty string');
206+
return (await this.contract).getListOfAddresses.callAsync(stringToBytes32(params.blacklistName));
207+
};
208+
209+
/**
210+
* getBlacklistNamesToUser
211+
*/
212+
public getBlacklistNamesToUser = async (params: UserAddressParams): Promise<string[]> => {
213+
assert.isNonZeroETHAddressHex('User Address', params.user);
214+
return bytes32ArrayToStringArray(await (await this.contract).getBlacklistNamesToUser.callAsync(params.user));
215+
};
216+
217+
/**
218+
* getAllBlacklists
219+
*/
220+
public getAllBlacklists = async (): Promise<string[]> => {
221+
return bytes32ArrayToStringArray(await (await this.contract).getAllBlacklists.callAsync());
222+
};
223+
224+
/**
225+
* getTokensByPartition
226+
*/
227+
public getTokensByPartition = async (params: GetTokensByPartitionParams): Promise<BigNumber> => {
228+
assert.isNonZeroETHAddressHex('Token Holder', params.tokenHolder);
229+
const decimals = await (await this.securityTokenContract()).decimals.callAsync();
230+
return weiToValue(
231+
await (await this.contract).getTokensByPartition.callAsync(
232+
params.partition,
233+
params.tokenHolder,
234+
valueToWei(params.additionalBalance, decimals),
235+
),
236+
decimals,
237+
);
238+
};
239+
240+
/**
241+
* getPermissions
242+
*/
243+
public getPermissions = async (): Promise<Perm[]> => {
244+
const permissions = await (await this.contract).getPermissions.callAsync();
245+
return permissions.map(parsePermBytes32Value);
246+
};
247+
168248
/*
169-
* verifyTransfer
170-
*/
249+
* verifyTransfer
250+
*/
171251
public verifyTransfer = async (params: VerifyTransferParams): Promise<VerifyTransfer> => {
172252
assert.isETHAddressHex('from', params.from);
173253
assert.isETHAddressHex('to', params.to);
174254
const decimals = await (await this.securityTokenContract()).decimals.callAsync();
175255
const result = await (await this.contract).verifyTransfer.callAsync(
176-
params.from,
177-
params.to,
178-
valueToWei(params.amount, decimals),
179-
params.data,
256+
params.from,
257+
params.to,
258+
valueToWei(params.amount, decimals),
259+
params.data,
180260
);
181261
const transferResult = parseTransferResult(result[0]);
182262
return {
@@ -227,7 +307,7 @@ export default class BlacklistTransferManagerWrapper extends ModuleWrapper {
227307
params.eventName,
228308
params.blockRange,
229309
params.indexFilterValues,
230-
BlacklistTransferManager.abi,
310+
BlacklistTransferManager.abi,
231311
);
232312
return logs;
233313
};

0 commit comments

Comments
 (0)