Skip to content
This repository was archived by the owner on Apr 3, 2019. It is now read-only.

Commit 0d10966

Browse files
authored
Merge pull request #41 from PolymathNetwork/test-subscribe
Test subscribe
2 parents 276fa49 + 71c4be4 commit 0d10966

10 files changed

+557
-42
lines changed

src/contract_wrappers/ContractWrapper.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,9 @@ export default class ContractWrapper {
9595
* Unsubscribes from all events subscribed to on the contract.
9696
*/
9797
unsubscribeAll() {
98-
this._filters.forEach(filter => {
99-
this.unsubscribe(filter);
98+
// get an array of keys from the this._filters object, and then map the subscriptionIDs into unsubscribe
99+
Object.keys(this._filters).map((subscriptionIDKey, index) => {
100+
this.unsubscribe(subscriptionIDKey);
100101
});
101102
}
102103

src/contract_wrappers/Customers.js

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -57,23 +57,26 @@ export default class Customers extends ContractWrapper {
5757
indexedFilterValues: IndexedFilterValues,
5858
callback: EventCallback<CustomersEventArgs>,
5959
): string {
60-
let wrappedCallback = callback;
60+
const wrappedCallback = callback;
6161

62-
if (eventName === 'LogCustomerVerified') {
63-
// Convert from number roles to string enum roles.
64-
wrappedCallback = (args: any) => {
65-
const role = numberToRole(args.role.toNumber());
62+
//* *temporarily green this out, it isnt working, might not need */
6663

67-
if (role === null) {
68-
return;
69-
}
64+
// if (eventName === 'LogCustomerVerified') {
65+
// // Convert from number roles to string enum roles.
66+
// wrappedCallback = (args: any) => {
67+
// // console.log(args)
68+
// const role = numberToRole(args.role.toNumber());
7069

71-
callback({
72-
...args,
73-
role,
74-
});
75-
};
76-
}
70+
// if (role === null) {
71+
// return;
72+
// }
73+
74+
// callback({
75+
// ...args,
76+
// role,
77+
// });
78+
// };
79+
// }
7780

7881
return super._subscribe(eventName, indexedFilterValues, wrappedCallback);
7982
}
@@ -211,7 +214,6 @@ export default class Customers extends ContractWrapper {
211214
);
212215
}
213216

214-
215217
/**
216218
* Retrieve a Polymath user by their associated KYC provider and their Ethereum address. Users can be associated with multiple KYC providers.
217219
* @param kycProviderAddress The Ethereum address of the associated KYC provider

src/contract_wrappers/SecurityToken.js

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -66,28 +66,30 @@ export default class SecurityToken extends ContractWrapper {
6666
| 'LogUpdatedComplianceProof'
6767
| 'LogSetSTOContract'
6868
| 'LogNewWhitelistedAddress'
69+
| 'LogNewBlacklistedAddress'
6970
| 'LogVoteToFreeze'
7071
| 'LogTokenIssued',
7172
indexedFilterValues: IndexedFilterValues,
7273
callback: EventCallback<SecurityTokenEventArgs>,
7374
): string {
74-
let wrappedCallback = callback;
75+
const wrappedCallback = callback;
7576

76-
if (eventName === 'LogNewWhitelistedAddress') {
77-
// Convert from number roles to string enum roles.
78-
wrappedCallback = (args: any) => {
79-
const role = numberToRole(args._role.toNumber());
77+
// Temporary comment out, dont know if we need
78+
// if (eventName === 'LogNewWhitelistedAddress') {
79+
// // Convert from number roles to string enum roles.
80+
// wrappedCallback = (args: any) => {
81+
// const role = numberToRole(args._role.toNumber());
8082

81-
if (role === null) {
82-
return;
83-
}
83+
// if (role === null) {
84+
// return;
85+
// }
8486

85-
callback({
86-
...args,
87-
_role: role,
88-
});
89-
};
90-
}
87+
// callback({
88+
// ...args,
89+
// _role: role,
90+
// });
91+
// };
92+
// }
9193

9294
return super._subscribe(eventName, indexedFilterValues, wrappedCallback);
9395
}
@@ -480,11 +482,14 @@ export default class SecurityToken extends ContractWrapper {
480482

481483
/**
482484
* Add a verified address to the Security Token blacklist
485+
* @param owner Owner of the security token contract
483486
* @param blacklistAddress The address being added to the blacklist
484487
*/
485488
async addToBlacklist(ownerAddress: string, blacklistAddress: string) {
486489
await this._contract.addToBlacklist(blacklistAddress, {
487490
from: ownerAddress,
491+
gas: 1000000,
488492
});
489493
}
494+
490495
}

src/contract_wrappers/SecurityTokenRegistrar.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import SecurityToken from './SecurityToken';
88
import Compliance from './Compliance';
99
import type {
1010
BlockRange,
11-
LogNewSecurityToken,
11+
SecurityTokenRegistrarEventArgs,
1212
EventCallback,
1313
IndexedFilterValues,
1414
Log,
@@ -44,7 +44,7 @@ export default class SecurityTokenRegistrar extends ContractWrapper {
4444
subscribe(
4545
eventName: 'LogNewSecurityToken',
4646
indexedFilterValues: IndexedFilterValues,
47-
callback: EventCallback<LogNewSecurityToken>,
47+
callback: EventCallback<SecurityTokenRegistrarEventArgs>,
4848
): string {
4949
return super._subscribe(eventName, indexedFilterValues, callback);
5050
}
@@ -60,7 +60,7 @@ export default class SecurityTokenRegistrar extends ContractWrapper {
6060
eventName: 'LogNewSecurityToken',
6161
indexedFilterValues: IndexedFilterValues,
6262
blockRange?: BlockRange,
63-
): Promise<Array<Log<LogNewSecurityToken>>> {
63+
): Promise<Array<Log<SecurityTokenRegistrarEventArgs>>> {
6464
return super._getLogs(eventName, indexedFilterValues, blockRange);
6565
}
6666

@@ -160,5 +160,4 @@ export default class SecurityTokenRegistrar extends ContractWrapper {
160160
async getComplianceAddress(): Promise<string> {
161161
return this._contract.polyComplianceAddress.call();
162162
}
163-
164163
}

src/types.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,7 @@ export type SecurityTokenEventArgs =
290290
| LogUpdatedComplianceProof
291291
| LogSetSTOContract
292292
| LogNewWhitelistedAddress
293+
| LogNewBlacklistedAddress
293294
| LogVoteToFreeze
294295
| LogTokenIssued;
295296

@@ -320,11 +321,9 @@ export type LogNewSecurityToken = {
320321
};
321322

322323
/**
323-
* Arguments for the SecurityTokenRegistrar.sol LogSecurityToken event
324+
* Arguments for the SecurityTokenRegistrar events
324325
*/
325-
export type LogSecurityToken = {
326-
securityTokenAddress: string,
327-
};
326+
export type SecurityTokenRegistrarEventArgs = LogNewSecurityToken;
328327

329328
// STOContract Events
330329

test/Compliance_test.js

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,4 +129,75 @@ describe('Compliance wrapper', () => {
129129

130130
// })
131131

132+
it('subscribe, unsubscribe, unsubscribeAll', async () => {
133+
134+
//subscribtion setup
135+
let subscriptionID1 = null;
136+
const eventName1 = 'LogTemplateCreated';
137+
const indexedFilterValues1 = ["_creator"];
138+
139+
//the callback is passed into the filter.watch function, and is operated on when a new event comes in
140+
const logTemplateCreatedArgsPromise = new Promise((resolve, reject) => {
141+
subscriptionID1 = compliance.subscribe(eventName1, indexedFilterValues1, (err, log) => {
142+
if (err !== null) {
143+
reject(err);
144+
return;
145+
}
146+
resolve(log.args);
147+
});
148+
});
149+
150+
//subscribtion setup
151+
let subscriptionID2 = null;
152+
const eventName2 = 'LogNewTemplateProposal';
153+
const indexedFilterValues2 = "_securityToken";
154+
155+
//the callback is passed into the filter.watch function, and is operated on when a new event comes in
156+
const logNewTemplateProposalArgsPromise = new Promise((resolve, reject) => {
157+
158+
subscriptionID2 = compliance.subscribe(eventName2, indexedFilterValues2, (err, log) => {
159+
if (err !== null) {
160+
reject(err);
161+
return;
162+
}
163+
resolve(log.args);
164+
});
165+
});
166+
167+
//subscribtion setup
168+
let subscriptionID3 = null;
169+
const eventName3 = 'LogNewContractProposal';
170+
const indexedFilterValues3 = ["_securityToken"];
171+
172+
//the callback is passed into the filter.watch function, and is operated on when a new event comes in
173+
const logNewContractProposalArgsPromise = new Promise((resolve, reject) => {
174+
175+
subscriptionID3 = compliance.subscribe(eventName3, indexedFilterValues3, (err, log) => {
176+
if (err !== null) {
177+
reject(err);
178+
return;
179+
}
180+
resolve(log.args);
181+
});
182+
});
183+
184+
await makeKYCProvider(customers, accounts[1]);
185+
await makeLegalDelegate(polyToken, customers, accounts[1], accounts[2]);
186+
const templateAddress = await makeTemplate(
187+
compliance,
188+
accounts[1],
189+
accounts[2],
190+
);
191+
192+
193+
const logTemplateCreated = await logTemplateCreatedArgsPromise;
194+
assert.equal(logTemplateCreated._creator, accounts[2], 'legal delegate creator address wasnt found in event subscription'); //'offeringtype' from make_examples.js
195+
assert.isAbove(logTemplateCreated._template.length, 20, 'template address wasnt found in event subscription');
196+
assert.equal(logTemplateCreated._offeringType, "offeringtype", 'offering type wasnt found in event subscription'); //'offeringtype' from make_examples.js
197+
await compliance.unsubscribe(subscriptionID1);
198+
199+
200+
})
201+
202+
132203
});

test/Customers_test.js

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,4 +122,73 @@ describe('Customers wrapper', () => {
122122
);
123123
});
124124

125+
126+
it('subscribe, unsubscribe, unsubscribeAll', async () => {
127+
128+
//subscribtion setup
129+
let subscriptionID1 = null;
130+
const eventName1 = 'LogNewProvider';
131+
const indexedFilterValues1 = null;
132+
133+
//the callback is passed into the filter.watch function, and is operated on when a new event comes in
134+
const logNewProviderArgsPromise = new Promise((resolve, reject) => {
135+
subscriptionID1 = customers.subscribe(eventName1, indexedFilterValues1, (err, log) => {
136+
if (err !== null) {
137+
reject(err);
138+
return;
139+
}
140+
resolve(log.args);
141+
});
142+
});
143+
144+
//subscribtion setup
145+
let subscriptionID2 = null;
146+
const eventName2 = 'LogCustomerVerified';
147+
const indexedFilterValues2 = null;
148+
149+
//the callback is passed into the filter.watch function, and is operated on when a new event comes in
150+
const logCustomerVerifiedArgsPromise = new Promise((resolve, reject) => {
151+
152+
subscriptionID2 = customers.subscribe(eventName2, indexedFilterValues2, (err, log) => {
153+
if (err !== null) {
154+
reject(err);
155+
return;
156+
}
157+
resolve(log.args);
158+
});
159+
});
160+
161+
162+
const owner = accounts[0]
163+
const kycProvider = accounts[1];
164+
const investor = accounts[2];
165+
166+
await makeKYCProvider(customers, accounts[1]);
167+
168+
const logNewProvider = await logNewProviderArgsPromise;
169+
assert.equal(logNewProvider.providerAddress, kycProvider, 'kycProvider address wasnt found in event subscription');
170+
assert.equal(logNewProvider.name, 'Provider', 'Name of kycProvider wasnt found in event subscription');
171+
assert.equal(logNewProvider.details, '0xe3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855', 'Details wasnt found in event subscription'); //details hash from make_examples.js
172+
await customers.unsubscribe(subscriptionID1);
173+
174+
175+
await polyToken.approve(investor, customers.address, new BigNumber(100));
176+
await customers.verifyCustomer(
177+
kycProvider,
178+
investor,
179+
'US',
180+
'CA',
181+
'investor',
182+
false,
183+
new BigNumber(15163975079),
184+
);
185+
186+
const logCustomerVerified = await logCustomerVerifiedArgsPromise;
187+
assert.equal(logCustomerVerified.customer, investor, 'Customer address wasnt found in event subscription');
188+
assert.equal(logCustomerVerified.provider, kycProvider, 'kyc provider address wasnt found in event subscription');
189+
assert.equal(logCustomerVerified.role, 1, 'Role wasnt found in event subscription');
190+
await customers.unsubscribeAll();
191+
192+
})
193+
125194
});

test/SecurityTokenRegistrar_test.js

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,4 +143,72 @@ describe('Registrar wrapper', () => {
143143
);
144144
});
145145

146+
it('subscribe, unsubscibe, unsubscribeAll', async () => {
147+
//subscribtion setup
148+
let subscriptionID = null;
149+
const eventName1 = 'LogNewSecurityToken';
150+
const indexedFilterValues1 = null;
151+
152+
//the callback is passed into the filter.watch function, and is operated on when a new event comes in
153+
const logNewSecurityTokenArgsPromise = new Promise((resolve, reject) => {
154+
subscriptionID = registrar.subscribe(eventName1, indexedFilterValues1, (err, log) => {
155+
if (err !== null) {
156+
reject(err);
157+
return;
158+
}
159+
resolve(log.args);
160+
});
161+
});
162+
163+
//functions to test for first token made
164+
const creator = accounts[0];
165+
const name = 'FUNTOKEN';
166+
const ticker = 'FUNT';
167+
const totalSupply = 1234567;
168+
const decimals = 8;
169+
const owner = accounts[0];
170+
const host = accounts[1];
171+
const fee = 1000;
172+
const type = 1;
173+
const maxPoly = 100000;
174+
const lockupPeriod = 1516397507 + 31557600; // one year from jan 19 2017
175+
const quorum = 75;
176+
177+
await polyToken.approve(owner, registrar.address, fee);
178+
await registrar.createSecurityToken(
179+
creator,
180+
name,
181+
ticker,
182+
totalSupply,
183+
decimals,
184+
owner,
185+
maxPoly,
186+
host,
187+
fee,
188+
type,
189+
lockupPeriod,
190+
quorum,
191+
);
192+
193+
const logNewSecurityToken = await logNewSecurityTokenArgsPromise;
194+
assert.equal(logNewSecurityToken.ticker, ticker, 'Ticker wasnt found in event subscription');
195+
assert.isAbove(logNewSecurityToken.securityTokenAddress.length, 20, 'Address wasnt found in event subscription');
196+
assert.equal(logNewSecurityToken.owner, owner, 'Owner wasnt found in event subscription');
197+
assert.equal(logNewSecurityToken.host, host, 'Host wasnt found in event subscription');
198+
assert.equal(logNewSecurityToken.fee, fee, 'Fee wasnt found in event subscription');
199+
assert.equal(logNewSecurityToken._type, type, 'Type wasnt found in event subscription');
200+
201+
//note: if unsubscribe does not work, the test in the terminal will be stuck running a process
202+
//and you can see the process is running by looking at testrpc and seeing 'eth_getFilterChanges' constantly repeated
203+
await registrar.unsubscribeAll();
204+
205+
const logs = await registrar.getLogs(
206+
'LogNewSecurityToken',
207+
{ owner: accounts[0] },
208+
{ fromBlock: 1, toBlock: 'latest' },
209+
);
210+
assert.equal(logs.length, 1, 'One log should appear in the array since we made 1 security tokens');
211+
assert(logs[0].args.fee.equals(fee), 'Retrieved first Transfer log');
212+
})
213+
146214
});

0 commit comments

Comments
 (0)