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

Commit 9ef5c15

Browse files
author
Victor Wiebe
committed
feat: dding the alternative ERC20 wrapper back in
1 parent e26c334 commit 9ef5c15

File tree

2 files changed

+216
-0
lines changed

2 files changed

+216
-0
lines changed
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
// PolymathRegistryWrapper test
2+
import { mock, instance, reset, when, verify } from 'ts-mockito';
3+
import { Web3Wrapper } from '@0x/web3-wrapper';
4+
import { ERC20DetailedContract, SecurityTokenRegistryEvents } from '@polymathnetwork/abi-wrappers';
5+
import { BigNumber } from '@0x/utils';
6+
import ERC20TokenWrapper from '../erc20_wrapper';
7+
import AlternativeERC20TokenWrapper from '../alternative_erc20_wrapper';
8+
import { MockedCallMethod } from '../../../test_utils/mocked_methods';
9+
import { bytes32ToString, stringToBytes32 } from '../../../utils/convert';
10+
11+
describe('AlternativeERC20TokenWrapper', () => {
12+
// Declare ERC20DetailedTokenWrapper object
13+
let target: AlternativeERC20TokenWrapper;
14+
let mockedWrapper: Web3Wrapper;
15+
let mockedContract: ERC20DetailedContract;
16+
17+
beforeAll(() => {
18+
mockedWrapper = mock(Web3Wrapper);
19+
mockedContract = mock(ERC20DetailedContract);
20+
21+
const myContractPromise = Promise.resolve(instance(mockedContract));
22+
target = new AlternativeERC20TokenWrapper(instance(mockedWrapper), myContractPromise);
23+
});
24+
25+
afterEach(() => {
26+
reset(mockedWrapper);
27+
reset(mockedContract);
28+
});
29+
30+
describe('Types', () => {
31+
test('should extend ERC20TokenWrapper', async () => {
32+
expect(target instanceof ERC20TokenWrapper).toBe(true);
33+
});
34+
});
35+
36+
describe('name', () => {
37+
test('should call to name', async () => {
38+
const expectedResult = stringToBytes32('string');
39+
// Mocked method
40+
const mockedMethod = mock(MockedCallMethod);
41+
// Stub the method
42+
when(mockedContract.name).thenReturn(instance(mockedMethod));
43+
// Stub the request
44+
when(mockedMethod.callAsync()).thenResolve(expectedResult);
45+
46+
// Real call
47+
const result = await target.name();
48+
// Result expectation
49+
expect(result).toBe(bytes32ToString(expectedResult));
50+
// Verifications
51+
verify(mockedContract.name).once();
52+
verify(mockedMethod.callAsync()).once();
53+
});
54+
});
55+
56+
describe('symbol', () => {
57+
test('should call to symbol', async () => {
58+
const expectedResult = stringToBytes32('string');
59+
// Mocked method
60+
const mockedMethod = mock(MockedCallMethod);
61+
// Stub the method
62+
when(mockedContract.symbol).thenReturn(instance(mockedMethod));
63+
// Stub the request
64+
when(mockedMethod.callAsync()).thenResolve(expectedResult);
65+
66+
// Real call
67+
const result = await target.symbol();
68+
// Result expectation
69+
expect(result).toBe(bytes32ToString(expectedResult));
70+
// Verifications
71+
verify(mockedContract.symbol).once();
72+
verify(mockedMethod.callAsync()).once();
73+
});
74+
});
75+
76+
describe('isValidContract', () => {
77+
test('should call to isValidContract', async () => {
78+
const expectedBNResult = new BigNumber(1);
79+
const expectedStringResult = stringToBytes32('string');
80+
81+
const mockedTotalSupplyMethod = mock(MockedCallMethod);
82+
when(mockedContract.totalSupply).thenReturn(instance(mockedTotalSupplyMethod));
83+
when(mockedTotalSupplyMethod.callAsync()).thenResolve(expectedBNResult);
84+
85+
const mockedSymbolMethod = mock(MockedCallMethod);
86+
when(mockedContract.symbol).thenReturn(instance(mockedSymbolMethod));
87+
when(mockedSymbolMethod.callAsync()).thenResolve(expectedStringResult);
88+
89+
const mockedNameMethod = mock(MockedCallMethod);
90+
when(mockedContract.name).thenReturn(instance(mockedNameMethod));
91+
when(mockedSymbolMethod.callAsync()).thenResolve(expectedStringResult);
92+
93+
const expectedIsValidResult = false;
94+
const result = await target.isValidContract();
95+
expect(result).toBe(expectedIsValidResult);
96+
97+
verify(mockedContract.totalSupply).once();
98+
verify(mockedContract.symbol).once();
99+
verify(mockedContract.name).once();
100+
});
101+
});
102+
103+
describe('SubscribeAsync', () => {
104+
test('should throw as eventName does not belong to SecurityTokenRegistryEvents', async () => {
105+
// Mocked parameters
106+
const mockedParams = {
107+
eventName: SecurityTokenRegistryEvents.ChangeExpiryLimit,
108+
indexFilterValues: {},
109+
callback: () => {},
110+
isVerbose: false,
111+
};
112+
113+
// Real call
114+
await expect(target.subscribeAsync(mockedParams)).rejects.toEqual(
115+
new Error(`Expected eventName to be one of: 'Transfer', 'Approval', encountered: ChangeExpiryLimit`),
116+
);
117+
});
118+
});
119+
});
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
import { ERC20DetailedContract, ERC20DetailedEventArgs, ERC20DetailedEvents } from '@polymathnetwork/abi-wrappers';
2+
import { ERC20Detailed } from '@polymathnetwork/contract-artifacts';
3+
import { LogWithDecodedArgs, Web3Wrapper } from '@0x/web3-wrapper';
4+
import { ContractAbi } from 'ethereum-types';
5+
import { schemas } from '@0x/json-schemas';
6+
import { bytes32ToString } from '../../utils/convert';
7+
import ERC20TokenWrapper from './erc20_wrapper';
8+
import { GetLogs, GetLogsAsyncParams, Subscribe, SubscribeAsyncParams } from '../../types';
9+
10+
import assert from '../../utils/assert';
11+
12+
import _ = require('lodash');
13+
14+
/**
15+
* This class includes the functionality related to interacting with the AlternativeERC20 contract.
16+
*/
17+
export default class AlternativeERC20Wrapper extends ERC20TokenWrapper {
18+
public abi: ContractAbi = ERC20Detailed.abi;
19+
20+
protected contract: Promise<ERC20DetailedContract>;
21+
22+
/**
23+
* Instantiate AlternativeERC20Wrapper
24+
* @param web3Wrapper Web3Wrapper instance to use
25+
* @param contract
26+
*/
27+
public constructor(web3Wrapper: Web3Wrapper, contract: Promise<ERC20DetailedContract>) {
28+
super(web3Wrapper, contract);
29+
this.contract = contract;
30+
}
31+
32+
/**
33+
* Returns the token name
34+
*/
35+
public name = async () => {
36+
const name = (await this.contract).name.callAsync();
37+
return bytes32ToString(await name);
38+
};
39+
40+
/**
41+
* Returns the token symbol
42+
*/
43+
public symbol = async () => {
44+
const symbol = (await this.contract).symbol.callAsync();
45+
return bytes32ToString(await symbol);
46+
};
47+
48+
public async isValidContract() {
49+
try {
50+
const contract = await this.contract;
51+
const totalSupply = await contract.totalSupply.callAsync();
52+
const symbol = await contract.symbol.callAsync();
53+
const name = await contract.name.callAsync();
54+
if (bytes32ToString(symbol) === '' || bytes32ToString(name) === '' || totalSupply.isZero()) {
55+
return false;
56+
}
57+
return true;
58+
} catch (error) {
59+
return false;
60+
}
61+
}
62+
63+
public subscribeAsync: Subscribe = async <ArgsType extends ERC20DetailedEventArgs>(
64+
params: SubscribeAsyncParams,
65+
): Promise<string> => {
66+
assert.doesBelongToStringEnum('eventName', params.eventName, ERC20DetailedEvents);
67+
assert.doesConformToSchema('indexFilterValues', params.indexFilterValues, schemas.indexFilterValuesSchema);
68+
assert.isFunction('callback', params.callback);
69+
const normalizedContractAddress = (await this.contract).address.toLowerCase();
70+
const subscriptionToken = this.subscribeInternal<ArgsType>(
71+
normalizedContractAddress,
72+
params.eventName,
73+
params.indexFilterValues,
74+
ERC20Detailed.abi,
75+
params.callback,
76+
!_.isUndefined(params.isVerbose),
77+
);
78+
return subscriptionToken;
79+
};
80+
81+
public getLogsAsync: GetLogs = async <ArgsType extends ERC20DetailedEventArgs>(
82+
params: GetLogsAsyncParams,
83+
): Promise<LogWithDecodedArgs<ArgsType>[]> => {
84+
assert.doesBelongToStringEnum('eventName', params.eventName, ERC20DetailedEvents);
85+
assert.doesConformToSchema('blockRange', params.blockRange, schemas.blockRangeSchema);
86+
assert.doesConformToSchema('indexFilterValues', params.indexFilterValues, schemas.indexFilterValuesSchema);
87+
const normalizedContractAddress = (await this.contract).address.toLowerCase();
88+
const logs = await this.getLogsAsyncInternal<ArgsType>(
89+
normalizedContractAddress,
90+
params.eventName,
91+
params.blockRange,
92+
params.indexFilterValues,
93+
ERC20Detailed.abi,
94+
);
95+
return logs;
96+
};
97+
}

0 commit comments

Comments
 (0)