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

Commit f363bfa

Browse files
author
Victor Wiebe
committed
feat: adding more module factory methods with tests
1 parent 5574fef commit f363bfa

File tree

3 files changed

+187
-6
lines changed

3 files changed

+187
-6
lines changed

src/contract_wrappers/modules/__tests__/module_factory_wrapper.test.ts

Lines changed: 119 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,23 @@
11
// ModuleFactoryWrapper test
2-
import { mock, instance, reset, when, verify } from 'ts-mockito';
2+
import {mock, instance, reset, when, verify, objectContaining} from 'ts-mockito';
33
import {
44
ModuleFactoryContract,
55
BigNumber,
66
Web3Wrapper,
77
EtherDividendCheckpointEvents,
88
} from '@polymathnetwork/abi-wrappers';
9-
import { MockedCallMethod, MockedSendMethod } from '../../../test_utils/mocked_methods';
9+
import {getMockedPolyResponse, MockedCallMethod, MockedSendMethod} from '../../../test_utils/mocked_methods';
1010
import ContractFactory from '../../../factories/contractFactory';
11-
import { bytes32ToString, stringToBytes32, weiToValue } from '../../../utils/convert';
11+
import {
12+
bytes32ToString,
13+
parseModuleTypeValue,
14+
stringArrayToBytes32Array,
15+
stringToBytes32,
16+
weiToValue
17+
} from '../../../utils/convert';
1218
import ModuleFactoryWrapper from '../module_factory_wrapper';
1319
import ContractWrapper from '../../contract_wrapper';
14-
import { FULL_DECIMALS } from '../../../types';
20+
import {FULL_DECIMALS, ModuleType} from '../../../types';
1521

1622
describe('ModuleFactoryWrapper', () => {
1723
let target: ModuleFactoryWrapper;
@@ -40,6 +46,27 @@ describe('ModuleFactoryWrapper', () => {
4046
});
4147
});
4248

49+
describe('owner', () => {
50+
test('should call to owner', async () => {
51+
const expectedResult = '0x0123456789012345678901234567890123456789';
52+
// Mocked method
53+
const mockedMethod = mock(MockedCallMethod);
54+
// Stub the method
55+
when(mockedContract.owner).thenReturn(instance(mockedMethod));
56+
// Stub the request
57+
when(mockedMethod.callAsync()).thenResolve(expectedResult);
58+
59+
// Real call
60+
const result = await target.owner();
61+
// Result expectation
62+
expect(result).toBe(expectedResult);
63+
// Verifications
64+
verify(mockedContract.owner).once();
65+
verify(mockedMethod.callAsync()).once();
66+
});
67+
});
68+
69+
4370
describe('Name', () => {
4471
test('should get name', async () => {
4572
// Address expected
@@ -145,6 +172,94 @@ describe('ModuleFactoryWrapper', () => {
145172
});
146173
});
147174

175+
describe('GetTypes', () => {
176+
test('should get types', async () => {
177+
// Address expected
178+
const expectedResult = [new BigNumber(ModuleType.STO), new BigNumber(ModuleType.Dividends)];
179+
// Mocked method
180+
const mockedMethod = mock(MockedCallMethod);
181+
// Stub the method
182+
when(mockedContract.getTypes).thenReturn(instance(mockedMethod));
183+
// Stub the request
184+
when(mockedMethod.callAsync()).thenResolve(expectedResult);
185+
186+
// Real call
187+
const result = await target.getTypes();
188+
// Result expectation
189+
expect(result).toEqual(expectedResult.map(parseModuleTypeValue));
190+
// Verifications
191+
verify(mockedContract.getTypes).once();
192+
verify(mockedMethod.callAsync()).once();
193+
});
194+
});
195+
196+
describe('getTags', () => {
197+
test('should get tags', async () => {
198+
// Address expected
199+
const expectedResult = stringArrayToBytes32Array(['Tag1', 'Tag2']);
200+
// Mocked method
201+
const mockedMethod = mock(MockedCallMethod);
202+
// Stub the method
203+
when(mockedContract.getTags).thenReturn(instance(mockedMethod));
204+
// Stub the request
205+
when(mockedMethod.callAsync()).thenResolve(expectedResult);
206+
207+
// Real call
208+
const result = await target.getTags();
209+
// Result expectation
210+
expect(stringArrayToBytes32Array(result)).toEqual(expectedResult);
211+
// Verifications
212+
verify(mockedContract.getTags).once();
213+
verify(mockedMethod.callAsync()).once();
214+
});
215+
});
216+
217+
describe('changeSetupCost', () => {
218+
test.todo('should fail as changeSetupCost is 0');
219+
test('should send the transaction to changeSetupCost', async () => {
220+
// Mocked parameters
221+
const mockedParams = {
222+
setupCost: new BigNumber(100),
223+
txData: {},
224+
safetyFactor: 10,
225+
};
226+
const expectedResult = getMockedPolyResponse();
227+
// Mocked method
228+
const mockedMethod = mock(MockedSendMethod);
229+
// Stub the method
230+
when(mockedContract.changeSetupCost).thenReturn(instance(mockedMethod));
231+
// Stub the request
232+
when(
233+
mockedMethod.sendTransactionAsync(objectContaining(mockedParams.setupCost), mockedParams.txData, mockedParams.safetyFactor),
234+
).thenResolve(expectedResult);
235+
236+
// Owner Address expected
237+
const expectedOwnerResult = '0x5555555555555555555555555555555555555555';
238+
// Mocked method
239+
const mockedOwnerMethod = mock(MockedCallMethod);
240+
// Stub the method
241+
when(mockedContract.owner).thenReturn(instance(mockedOwnerMethod));
242+
// Stub the request
243+
when(mockedOwnerMethod.callAsync()).thenResolve(expectedOwnerResult);
244+
// Mock web3 wrapper owner
245+
when(mockedWrapper.getAvailableAddressesAsync()).thenResolve([expectedOwnerResult]);
246+
247+
// Real call
248+
const result = await target.changeSetupCost(mockedParams);
249+
250+
// Result expectation
251+
expect(result).toBe(expectedResult);
252+
// Verifications
253+
verify(mockedContract.owner).once();
254+
verify(mockedOwnerMethod.callAsync()).once();
255+
verify(mockedContract.changeSetupCost).once();
256+
verify(
257+
mockedMethod.sendTransactionAsync(objectContaining(mockedParams.setupCost), mockedParams.txData, mockedParams.safetyFactor),
258+
).once();
259+
verify(mockedWrapper.getAvailableAddressesAsync()).once();
260+
});
261+
});
262+
148263
describe('setupCost', () => {
149264
test('should get setupCost', async () => {
150265
// Address expected

src/contract_wrappers/modules/module_factory_wrapper.ts

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
Web3Wrapper,
1111
ContractAbi,
1212
LogWithDecodedArgs,
13+
TxData,
1314
} from '@polymathnetwork/abi-wrappers';
1415
import { schemas } from '@0x/json-schemas';
1516
import assert from '../../utils/assert';
@@ -21,8 +22,11 @@ import {
2122
Subscribe,
2223
GetLogs,
2324
FULL_DECIMALS,
25+
ModuleType,
26+
TxParams,
2427
} from '../../types';
25-
import { weiToValue, bytes32ToString } from '../../utils/convert';
28+
import { weiToValue, bytes32ToString, bytes32ArrayToStringArray, parseModuleTypeValue } from '../../utils/convert';
29+
import functionsUtils from '../../utils/functions_utils';
2630

2731
interface OwnershipTransferredSubscribeAsyncParams extends SubscribeAsyncParams {
2832
eventName: ModuleFactoryEvents.OwnershipTransferred;
@@ -66,6 +70,10 @@ interface ModuleFactorySubscribeAsyncParams extends Subscribe {
6670
>;
6771
}
6872

73+
interface ChangeSetupCostParams extends TxParams {
74+
setupCost: BigNumber;
75+
}
76+
6977
/**
7078
* This class includes the functionality related to interacting with the ModuleFactory contract.
7179
*/
@@ -84,6 +92,13 @@ export default class ModuleFactoryWrapper extends ContractWrapper {
8492
this.contract = contract;
8593
}
8694

95+
/**
96+
* Get the owner of the Module Factory
97+
*/
98+
public owner = async () => {
99+
return (await this.contract).owner.callAsync();
100+
};
101+
87102
/**
88103
* Get the name of the Module
89104
*/
@@ -120,6 +135,34 @@ export default class ModuleFactoryWrapper extends ContractWrapper {
120135
return (await this.contract).version.callAsync();
121136
};
122137

138+
/**
139+
* Get the types
140+
*/
141+
public getTypes = async (): Promise<ModuleType[]> => {
142+
return (await (await this.contract).getTypes.callAsync()).map(type => {
143+
return parseModuleTypeValue(type);
144+
});
145+
};
146+
147+
/**
148+
* Get the tags
149+
*/
150+
public getTags = async (): Promise<string[]> => {
151+
return bytes32ArrayToStringArray(await (await this.contract).getTags.callAsync());
152+
};
153+
154+
/**
155+
* Change the setupCost
156+
*/
157+
public changeSetupCost = async (params: ChangeSetupCostParams) => {
158+
await this.checkOnlyOwner(params.txData);
159+
return (await this.contract).changeSetupCost.sendTransactionAsync(
160+
params.setupCost,
161+
params.txData,
162+
params.safetyFactor,
163+
);
164+
};
165+
123166
/**
124167
* Get setup cost
125168
*/
@@ -192,4 +235,11 @@ export default class ModuleFactoryWrapper extends ContractWrapper {
192235
);
193236
return logs;
194237
};
238+
239+
private checkOnlyOwner = async (txData: Partial<TxData> | undefined) => {
240+
assert.assert(
241+
functionsUtils.checksumAddressComparision(await this.owner(), await this.getCallerAddress(txData)),
242+
'Msg sender must be owner',
243+
);
244+
};
195245
}

src/utils/convert.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { ethers, BigNumber } from '@polymathnetwork/abi-wrappers';
2-
import { Partition, Perm } from '../types';
2+
import { ModuleType, Partition, Perm } from '../types';
33

44
const BASE = new BigNumber(10);
55

@@ -100,4 +100,20 @@ export function parsePermBytes32Value(value: string): Perm {
100100
default:
101101
throw new Error('Permission not recognized');
102102
}
103+
}
104+
export function parseModuleTypeValue(value: BigNumber): ModuleType {
105+
switch (value.toNumber()) {
106+
case ModuleType.Dividends:
107+
return ModuleType.Dividends;
108+
case ModuleType.STO:
109+
return ModuleType.STO;
110+
case ModuleType.TransferManager:
111+
return ModuleType.TransferManager;
112+
case ModuleType.PermissionManager:
113+
return ModuleType.PermissionManager;
114+
case ModuleType.Burn:
115+
return ModuleType.Burn;
116+
default:
117+
throw new Error('Module Type not recognized');
118+
}
103119
}

0 commit comments

Comments
 (0)