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

Move web3 wrapper from devDependencies to dependencies. Add Complianc… #64

Merged
merged 7 commits into from
Feb 13, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
install:
- yarn install
- yarn prepack
env:
- CI="true"

Expand Down
38 changes: 23 additions & 15 deletions src/contract_wrappers/Compliance.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ import Web3 from 'web3';
import { Web3Wrapper } from '@0xproject/web3-wrapper';

import ContractWrapper from './ContractWrapper';
import Customers from './Customers';
import SecurityTokenRegistrar from './SecurityTokenRegistrar';

import Template from './Template';
import complianceArtifact from '../artifacts/Compliance.json';
import type {
BlockRange,
Expand All @@ -28,13 +26,9 @@ export default class Compliance extends ContractWrapper {
*/
constructor(
web3Wrapper: Web3Wrapper,
customers: Customers,
deployedAddress?: string,
) {
super(web3Wrapper, complianceArtifact, deployedAddress);

this.customers = customers;
this.securityTokenRegistrar = SecurityTokenRegistrar;
}

/**
Expand Down Expand Up @@ -87,7 +81,7 @@ export default class Compliance extends ContractWrapper {
* @param fee Amount of POLY to use the template (held in escrow until issuance)
* @param quorum Minimum percent of shareholders which need to vote to freeze
* @param vestingPeriod Length of time to vest funds
* @return The address of the created template
* @return The created template
*/
async createTemplate(
legalDelegateAddress: string,
Expand All @@ -100,7 +94,7 @@ export default class Compliance extends ContractWrapper {
fee: BigNumber,
quorum: BigNumber,
vestingPeriod: BigNumber,
): Promise<string> {
): Promise<Template> {
const receipt = await this._contract.createTemplate(
offeringType,
Web3.prototype.fromAscii(issuerJurisdiction),
Expand All @@ -116,16 +110,30 @@ export default class Compliance extends ContractWrapper {
gas: 4000000,
},
);
const logs = receipt.logs.filter(log => log.event === 'LogTemplateCreated');

if (logs.length === 0) {
throw new Error('createTemplate couldn\'t find an event log.');
}

for (let i = 0; i < receipt.logs.length; i++) {
const log = receipt.logs[i];
const address = logs[0].args._template;

if (log.event === 'LogTemplateCreated') {
return log.args._template;
}
if (!address) {
throw new Error('createTemplate couldn\'t get template address.');
}

throw new Error('createTemplate should have emitted LogTemplateCreated.');
return this.getTemplateFromAddress(address);
}

/**
* Instantiates a template given its contract address.
* @param address The address of the template contract
* @return The template instance
*/
async getTemplateFromAddress(address: string): Promise<Template> {
const template = new Template(this._web3Wrapper, address);
await template.initialize();
return template;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/contract_wrappers/Customers.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ type LogCustomerVerifiedArgsUnprocessed = {
* Wrapper for the Customers Solidity contract
*/
export default class Customers extends ContractWrapper {
// polyToken: PolyToken;
polyToken: PolyToken;

/**
* @hideconstructor
Expand Down
11 changes: 0 additions & 11 deletions src/contract_wrappers/SecurityToken.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,25 +32,14 @@ export type LogNewWhitelistedAddress = {
* Wrapper for the SecurityToken Solidity contract
*/
export default class SecurityToken extends ContractWrapper {
polyToken: PolyToken;
customers: Customers;
compliance: Compliance;

/**
* @hideconstructor
*/
constructor(
web3Wrapper: Web3Wrapper,
polyToken: PolyToken,
customers: Customers,
compliance: Compliance,
deployedAddress: string,
) {
super(web3Wrapper, securityTokenArtifact, deployedAddress);

this.polyToken = polyToken;
this.customers = customers;
this.compliance = compliance;
}

/**
Expand Down
40 changes: 30 additions & 10 deletions src/contract_wrappers/SecurityTokenRegistrar.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

import { Web3Wrapper } from '@0xproject/web3-wrapper';
import { BigNumber } from 'bignumber.js';

import ContractWrapper from './ContractWrapper';
import securityTokenRegistrarArtifact from '../artifacts/SecurityTokenRegistrar.json';
import SecurityToken from './SecurityToken';
import Compliance from './Compliance';
import securityTokenRegistrarArtifact from '../artifacts/SecurityTokenRegistrar.json';
import type {
BlockRange,
SecurityTokenRegistrarEventArgs,
Expand All @@ -24,14 +24,9 @@ export default class SecurityTokenRegistrar extends ContractWrapper {
*/
constructor(
web3Wrapper: Web3Wrapper,
securityToken: SecurityToken,
compliance: Compliance,
deployedAddress?: string,
) {
super(web3Wrapper, securityTokenRegistrarArtifact, deployedAddress);

this.securityToken = securityToken;
this.compliance = compliance;
}

/**
Expand Down Expand Up @@ -65,7 +60,7 @@ export default class SecurityTokenRegistrar extends ContractWrapper {
}

/**
* Creates a security token and stores it in the security token registry. Returns a promise of true it the security token was successfully created. This is done by event watching for the event {@link LogNewSecurityToken()}.
* Creates a security token and stores it in the security token registry.
*
* @param creator The address from which the token is created
* @param name Name of the security token
Expand All @@ -79,6 +74,7 @@ export default class SecurityTokenRegistrar extends ContractWrapper {
* @param type Type of security being tokenized (NEED TOKEN NUMBERS ie. security:1, somethingelse:2)
* @param lockupPeriod Length of time (unix) raised POLY will be locked up for dispute
* @param quorum Percent of initial investors required to freeze POLY raise
* @return The security token created
*/
async createSecurityToken(
creator: string,
Expand All @@ -93,8 +89,8 @@ export default class SecurityTokenRegistrar extends ContractWrapper {
type: number,
lockupPeriod: BigNumber,
quorum: number,
) {
await this._contract.createSecurityToken(
): Promise<SecurityToken> {
const receipt = await this._contract.createSecurityToken(
name,
ticker,
totalSupply,
Expand All @@ -111,6 +107,19 @@ export default class SecurityTokenRegistrar extends ContractWrapper {
gas: 6700000,
},
);
const logs = receipt.logs.filter(log => log.event === 'LogNewSecurityToken');

if (logs.length === 0) {
throw new Error('createSecurityToken couldn\'t find an event log.');
}

const address = logs[0].args.securityTokenAddress;

if (!address) {
throw new Error('createSecurityToken couldn\'t get security token address.');
}

return this.getSecurityTokenByAddress(address);
}

/**
Expand All @@ -129,6 +138,17 @@ export default class SecurityTokenRegistrar extends ContractWrapper {
return dataToNumber;
}

/**
* Instantiates a `SecurityToken` given its contract address.
* @param address The address of the security tokens
* @return The security token instance
*/
async getSecurityTokenByAddress(address: string): Promise<SecurityToken> {
const securityToken = new SecurityToken(this._web3Wrapper, address);
await securityToken.initialize();
return securityToken;
}

/**
* Getter function for ST addresses by passing the ticker/symbol as the argument.
* @param ticker The security token ticker
Expand Down
10 changes: 9 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import { Web3Wrapper } from '@0xproject/web3-wrapper';

// import { ContractNotFoundError } from './types';
import { Customers, PolyToken } from './contract_wrappers';
import { Compliance, Customers, PolyToken, SecurityTokenRegistrar } from './contract_wrappers';
import type { Web3Provider } from './types';

export * from './contract_wrappers';
Expand All @@ -21,6 +21,8 @@ export class Polymath {

polyToken: PolyToken;
customers: Customers;
compliance: Compliance;
securityTokenRegistrar: SecurityTokenRegistrar;

constructor(web3Provider: Web3Provider) {
this._web3Wrapper = new Web3Wrapper(web3Provider);
Expand All @@ -33,6 +35,12 @@ export class Polymath {
this.customers = new Customers(this._web3Wrapper, this.polyToken);
initializePromises.push(this.customers.initialize());

this.compliance = new Compliance(this._web3Wrapper);
initializePromises.push(this.compliance.initialize());

this.securityTokenRegistrar = new SecurityTokenRegistrar(this._web3Wrapper);
initializePromises.push(this.securityTokenRegistrar.initialize());

this.initializedPromise = Promise.all(initializePromises);
}
}
Expand Down
18 changes: 8 additions & 10 deletions test/Compliance_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ describe('Compliance wrapper', () => {
polyToken,
customers,
compliance,
securityToken,
accounts[0],
accounts[1],
expiryTime,
Expand Down Expand Up @@ -87,12 +86,12 @@ describe('Compliance wrapper', () => {
it('createTemplate', async () => {
await makeKYCProvider(customers, accounts[1], expiryTime);
await makeLegalDelegate(polyToken, customers, accounts[1], accounts[2], expiryTime);
const templateAddress = await makeTemplate(
const templateAddress = (await makeTemplate(
compliance,
accounts[1],
accounts[2],
expiryTime,
);
)).address;

assert.isAbove(templateAddress.length, 0);
});
Expand Down Expand Up @@ -136,13 +135,12 @@ describe('Compliance wrapper', () => {

await makeKYCProvider(customers, accounts[1]);
await makeLegalDelegate(polyToken, customers, accounts[1], accounts[2], expiryTime);
const templateAddress = await makeTemplateWithFinalized(
const templateAddress = (await makeTemplateWithFinalized(
compliance,
accounts[1],
accounts[2],
expiryTime,

);
)).address;


// Propose Template
Expand Down Expand Up @@ -236,12 +234,12 @@ describe('Compliance wrapper', () => {
await makeKYCProvider(customers, kycProvider);

await makeLegalDelegate(polyToken, customers, accounts[1], accounts[2], expiryTime);
const templateAddress = await makeTemplateWithFinalized(
const templateAddress = (await makeTemplateWithFinalized(
compliance,
kycProvider,
legalDelegate,
expiryTime,
);
)).address;

await makeSelectedTemplateForSecurityToken(
securityToken,
Expand Down Expand Up @@ -326,12 +324,12 @@ describe('Compliance wrapper', () => {

await makeKYCProvider(customers, accounts[1]);
await makeLegalDelegate(polyToken, customers, accounts[1], accounts[2], expiryTime);
const templateAddress = await makeTemplate(
const templateAddress = (await makeTemplate(
compliance,
accounts[1],
accounts[2],
expiryTime,
);
)).address;

const logTemplateCreated = await logTemplateCreatedArgsPromise;
assert.equal(logTemplateCreated._creator, accounts[2], 'legal delegate creator address wasnt found in event subscription'); //'offeringtype' from make_examples.js
Expand Down
6 changes: 4 additions & 2 deletions test/SecurityTokenRegistrar_test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import BigNumber from 'bignumber.js';
import chai from 'chai';
import 'mocha';
import Web3 from 'web3';

import {
makePolyToken,
Expand Down Expand Up @@ -49,7 +50,6 @@ describe('Registrar wrapper', () => {
polyToken,
customers,
compliance,
securityToken,
accounts[0],
);

Expand Down Expand Up @@ -79,7 +79,7 @@ describe('Registrar wrapper', () => {
const quorum = 75;

await polyToken.approve(owner, registrar.address, fee);
await registrar.createSecurityToken(
const securityToken = await registrar.createSecurityToken(
creator,
name,
ticker,
Expand All @@ -94,6 +94,8 @@ describe('Registrar wrapper', () => {
quorum,
);

assert(Web3.prototype.isAddress(securityToken.address), 'Returned security token has valid address.');

const logs = await registrar.getLogs(
'LogNewSecurityToken',
{},
Expand Down
Loading