Merge branch 'master' into erc20Wrapper
# Conflicts: # src/0x.js.ts
This commit is contained in:
commit
1f09470838
@ -7,9 +7,13 @@ import * as ExchangeArtifacts from '../artifacts/Exchange.json';
|
|||||||
import {ecSignatureSchema} from '../schemas/ec_signature_schema';
|
import {ecSignatureSchema} from '../schemas/ec_signature_schema';
|
||||||
|
|
||||||
export class ExchangeWrapper extends ContractWrapper {
|
export class ExchangeWrapper extends ContractWrapper {
|
||||||
|
private exchangeContractIfExists?: ExchangeContract;
|
||||||
constructor(web3Wrapper: Web3Wrapper) {
|
constructor(web3Wrapper: Web3Wrapper) {
|
||||||
super(web3Wrapper);
|
super(web3Wrapper);
|
||||||
}
|
}
|
||||||
|
public invalidateContractInstance(): void {
|
||||||
|
delete this.exchangeContractIfExists;
|
||||||
|
}
|
||||||
public async isValidSignatureAsync(dataHex: string, ecSignature: ECSignature,
|
public async isValidSignatureAsync(dataHex: string, ecSignature: ECSignature,
|
||||||
signerAddressHex: string): Promise<boolean> {
|
signerAddressHex: string): Promise<boolean> {
|
||||||
assert.isHexString('dataHex', dataHex);
|
assert.isHexString('dataHex', dataHex);
|
||||||
@ -19,10 +23,9 @@ export class ExchangeWrapper extends ContractWrapper {
|
|||||||
const senderAddressIfExists = await this.web3Wrapper.getSenderAddressIfExistsAsync();
|
const senderAddressIfExists = await this.web3Wrapper.getSenderAddressIfExistsAsync();
|
||||||
assert.assert(!_.isUndefined(senderAddressIfExists), ZeroExError.USER_HAS_NO_ASSOCIATED_ADDRESSES);
|
assert.assert(!_.isUndefined(senderAddressIfExists), ZeroExError.USER_HAS_NO_ASSOCIATED_ADDRESSES);
|
||||||
|
|
||||||
const contractInstance = await this.instantiateContractIfExistsAsync((ExchangeArtifacts as any));
|
const exchangeContract = await this.getExchangeContractAsync();
|
||||||
const exchangeInstance = contractInstance as ExchangeContract;
|
|
||||||
|
|
||||||
const isValidSignature = await exchangeInstance.isValidSignature.call(
|
const isValidSignature = await exchangeContract.isValidSignature.call(
|
||||||
signerAddressHex,
|
signerAddressHex,
|
||||||
dataHex,
|
dataHex,
|
||||||
ecSignature.v,
|
ecSignature.v,
|
||||||
@ -34,4 +37,12 @@ export class ExchangeWrapper extends ContractWrapper {
|
|||||||
);
|
);
|
||||||
return isValidSignature;
|
return isValidSignature;
|
||||||
}
|
}
|
||||||
|
private async getExchangeContractAsync(): Promise<ExchangeContract> {
|
||||||
|
if (!_.isUndefined(this.exchangeContractIfExists)) {
|
||||||
|
return this.exchangeContractIfExists;
|
||||||
|
}
|
||||||
|
const contractInstance = await this.instantiateContractIfExistsAsync((ExchangeArtifacts as any));
|
||||||
|
this.exchangeContractIfExists = contractInstance as ExchangeContract;
|
||||||
|
return this.exchangeContractIfExists;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,20 +1,23 @@
|
|||||||
import * as _ from 'lodash';
|
import * as _ from 'lodash';
|
||||||
import {Web3Wrapper} from '../web3_wrapper';
|
import {Web3Wrapper} from '../web3_wrapper';
|
||||||
import {ZeroExError, Token, TokenRegistryContract} from '../types';
|
import {Token, TokenRegistryContract, TokenMetadata} from '../types';
|
||||||
import {assert} from '../utils/assert';
|
import {assert} from '../utils/assert';
|
||||||
import {ContractWrapper} from './contract_wrapper';
|
import {ContractWrapper} from './contract_wrapper';
|
||||||
import * as TokenRegistryArtifacts from '../artifacts/TokenRegistry.json';
|
import * as TokenRegistryArtifacts from '../artifacts/TokenRegistry.json';
|
||||||
|
|
||||||
export class TokenRegistryWrapper extends ContractWrapper {
|
export class TokenRegistryWrapper extends ContractWrapper {
|
||||||
|
private tokenRegistryContractIfExists?: TokenRegistryContract;
|
||||||
constructor(web3Wrapper: Web3Wrapper) {
|
constructor(web3Wrapper: Web3Wrapper) {
|
||||||
super(web3Wrapper);
|
super(web3Wrapper);
|
||||||
}
|
}
|
||||||
|
public invalidateContractInstance(): void {
|
||||||
|
delete this.tokenRegistryContractIfExists;
|
||||||
|
}
|
||||||
public async getTokensAsync(): Promise<Token[]> {
|
public async getTokensAsync(): Promise<Token[]> {
|
||||||
const contractInstance = await this.instantiateContractIfExistsAsync((TokenRegistryArtifacts as any));
|
const tokenRegistryContract = await this.getTokenRegistryContractAsync();
|
||||||
const tokenRegistryContract = contractInstance as TokenRegistryContract;
|
|
||||||
|
|
||||||
const addresses = await tokenRegistryContract.getTokenAddresses.call();
|
const addresses = await tokenRegistryContract.getTokenAddresses.call();
|
||||||
const tokenMetadataPromises: Array<Promise<any[]>> = _.map(
|
const tokenMetadataPromises: Array<Promise<TokenMetadata>> = _.map(
|
||||||
addresses,
|
addresses,
|
||||||
(address: string) => (tokenRegistryContract.getTokenMetaData.call(address)),
|
(address: string) => (tokenRegistryContract.getTokenMetaData.call(address)),
|
||||||
);
|
);
|
||||||
@ -30,4 +33,12 @@ export class TokenRegistryWrapper extends ContractWrapper {
|
|||||||
});
|
});
|
||||||
return tokens;
|
return tokens;
|
||||||
}
|
}
|
||||||
|
private async getTokenRegistryContractAsync(): Promise<TokenRegistryContract> {
|
||||||
|
if (!_.isUndefined(this.tokenRegistryContractIfExists)) {
|
||||||
|
return this.tokenRegistryContractIfExists;
|
||||||
|
}
|
||||||
|
const contractInstance = await this.instantiateContractIfExistsAsync((TokenRegistryArtifacts as any));
|
||||||
|
this.tokenRegistryContractIfExists = contractInstance as TokenRegistryContract;
|
||||||
|
return this.tokenRegistryContractIfExists;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
11
src/types.ts
11
src/types.ts
@ -37,8 +37,12 @@ export interface ERC20Contract {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export interface TokenRegistryContract {
|
export interface TokenRegistryContract {
|
||||||
getTokenMetaData: any;
|
getTokenMetaData: {
|
||||||
getTokenAddresses: any;
|
call: (address: string) => Promise<TokenMetadata>;
|
||||||
|
};
|
||||||
|
getTokenAddresses: {
|
||||||
|
call: () => Promise<string[]>;
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
export const SolidityTypes = strEnum([
|
export const SolidityTypes = strEnum([
|
||||||
@ -47,6 +51,9 @@ export const SolidityTypes = strEnum([
|
|||||||
]);
|
]);
|
||||||
export type SolidityTypes = keyof typeof SolidityTypes;
|
export type SolidityTypes = keyof typeof SolidityTypes;
|
||||||
|
|
||||||
|
// [address, name, symbol, projectUrl, decimals, ipfsHash, swarmHash]
|
||||||
|
export type TokenMetadata = [string, string, string, string, BigNumber.BigNumber, string, string];
|
||||||
|
|
||||||
export interface Token {
|
export interface Token {
|
||||||
name: string;
|
name: string;
|
||||||
address: string;
|
address: string;
|
||||||
|
@ -9,6 +9,9 @@ export class Web3Wrapper {
|
|||||||
this.web3 = new Web3();
|
this.web3 = new Web3();
|
||||||
this.web3.setProvider(web3.currentProvider);
|
this.web3.setProvider(web3.currentProvider);
|
||||||
}
|
}
|
||||||
|
public setProvider(provider: Web3.Provider) {
|
||||||
|
this.web3.setProvider(provider);
|
||||||
|
}
|
||||||
public isAddress(address: string): boolean {
|
public isAddress(address: string): boolean {
|
||||||
return this.web3.isAddress(address);
|
return this.web3.isAddress(address);
|
||||||
}
|
}
|
||||||
@ -49,10 +52,9 @@ export class Web3Wrapper {
|
|||||||
}
|
}
|
||||||
public async doesContractExistAtAddressAsync(address: string): Promise<boolean> {
|
public async doesContractExistAtAddressAsync(address: string): Promise<boolean> {
|
||||||
const code = await promisify(this.web3.eth.getCode)(address);
|
const code = await promisify(this.web3.eth.getCode)(address);
|
||||||
// Regex matches 0x0, 0x00, 0x in order to accomodate poorly implemented clients
|
// Regex matches 0x0, 0x00, 0x in order to accommodate poorly implemented clients
|
||||||
const zeroHexAddressRegex = /^0x0*$/i;
|
const codeIsEmpty = /^0x0{0,40}$/i.test(code);
|
||||||
const didFindCode = _.isNull(code.match(zeroHexAddressRegex));
|
return !codeIsEmpty;
|
||||||
return didFindCode;
|
|
||||||
}
|
}
|
||||||
public async signTransactionAsync(address: string, message: string): Promise<string> {
|
public async signTransactionAsync(address: string, message: string): Promise<string> {
|
||||||
const signData = await promisify(this.web3.eth.sign)(address, message);
|
const signData = await promisify(this.web3.eth.sign)(address, message);
|
||||||
|
@ -13,6 +13,34 @@ chai.use(ChaiBigNumber());
|
|||||||
const expect = chai.expect;
|
const expect = chai.expect;
|
||||||
|
|
||||||
describe('ZeroEx library', () => {
|
describe('ZeroEx library', () => {
|
||||||
|
describe('#setProvider', () => {
|
||||||
|
it('overrides the provider in the nested web3 instance and invalidates contractInstances', async () => {
|
||||||
|
const web3 = web3Factory.create();
|
||||||
|
const zeroEx = new ZeroEx(web3);
|
||||||
|
// Instantiate the contract instances with the current provider
|
||||||
|
await (zeroEx.exchange as any).getExchangeContractAsync();
|
||||||
|
await (zeroEx.tokenRegistry as any).getTokenRegistryContractAsync();
|
||||||
|
expect((zeroEx.exchange as any).exchangeContractIfExists).to.not.be.undefined;
|
||||||
|
expect((zeroEx.tokenRegistry as any).tokenRegistryContractIfExists).to.not.be.undefined;
|
||||||
|
|
||||||
|
const newProvider = web3Factory.getRpcProvider();
|
||||||
|
// Add property to newProvider so that we can differentiate it from old provider
|
||||||
|
(newProvider as any).zeroExTestId = 1;
|
||||||
|
zeroEx.setProvider(newProvider);
|
||||||
|
|
||||||
|
// Check that contractInstances with old provider are removed after provider update
|
||||||
|
expect((zeroEx.exchange as any).exchangeContractIfExists).to.be.undefined;
|
||||||
|
expect((zeroEx.tokenRegistry as any).tokenRegistryContractIfExists).to.be.undefined;
|
||||||
|
|
||||||
|
// Check that all nested web3 instances return the updated provider
|
||||||
|
const nestedWeb3WrapperProvider = (zeroEx as any).web3Wrapper.getCurrentProvider();
|
||||||
|
expect((nestedWeb3WrapperProvider as any).zeroExTestId).to.be.a('number');
|
||||||
|
const exchangeWeb3WrapperProvider = zeroEx.exchange.web3Wrapper.getCurrentProvider();
|
||||||
|
expect((exchangeWeb3WrapperProvider as any).zeroExTestId).to.be.a('number');
|
||||||
|
const tokenRegistryWeb3WrapperProvider = zeroEx.tokenRegistry.web3Wrapper.getCurrentProvider();
|
||||||
|
expect((tokenRegistryWeb3WrapperProvider as any).zeroExTestId).to.be.a('number');
|
||||||
|
});
|
||||||
|
});
|
||||||
describe('#getOrderHash', () => {
|
describe('#getOrderHash', () => {
|
||||||
const expectedOrderHash = '0x103a5e97dab5dbeb8f385636f86a7d1e458a7ccbe1bd194727f0b2f85ab116c7';
|
const expectedOrderHash = '0x103a5e97dab5dbeb8f385636f86a7d1e458a7ccbe1bd194727f0b2f85ab116c7';
|
||||||
it('defaults takerAddress to NULL address', () => {
|
it('defaults takerAddress to NULL address', () => {
|
||||||
@ -194,9 +222,9 @@ describe('ZeroEx library', () => {
|
|||||||
const web3 = web3Factory.create();
|
const web3 = web3Factory.create();
|
||||||
const zeroEx = new ZeroEx(web3);
|
const zeroEx = new ZeroEx(web3);
|
||||||
stubs = [
|
stubs = [
|
||||||
Sinon.stub(zeroEx.web3Wrapper, 'getNodeVersionAsync')
|
Sinon.stub((zeroEx as any).web3Wrapper, 'getNodeVersionAsync')
|
||||||
.returns(Promise.resolve(newParityNodeVersion)),
|
.returns(Promise.resolve(newParityNodeVersion)),
|
||||||
Sinon.stub(zeroEx.web3Wrapper, 'signTransactionAsync')
|
Sinon.stub((zeroEx as any).web3Wrapper, 'signTransactionAsync')
|
||||||
.returns(Promise.resolve(signature)),
|
.returns(Promise.resolve(signature)),
|
||||||
Sinon.stub(ZeroEx, 'isValidSignature').returns(true),
|
Sinon.stub(ZeroEx, 'isValidSignature').returns(true),
|
||||||
];
|
];
|
||||||
@ -218,9 +246,9 @@ describe('ZeroEx library', () => {
|
|||||||
const web3 = web3Factory.create();
|
const web3 = web3Factory.create();
|
||||||
const zeroEx = new ZeroEx(web3);
|
const zeroEx = new ZeroEx(web3);
|
||||||
stubs = [
|
stubs = [
|
||||||
Sinon.stub(zeroEx.web3Wrapper, 'getNodeVersionAsync')
|
Sinon.stub((zeroEx as any).web3Wrapper, 'getNodeVersionAsync')
|
||||||
.returns(Promise.resolve(newParityNodeVersion)),
|
.returns(Promise.resolve(newParityNodeVersion)),
|
||||||
Sinon.stub(zeroEx.web3Wrapper, 'signTransactionAsync')
|
Sinon.stub((zeroEx as any).web3Wrapper, 'signTransactionAsync')
|
||||||
.returns(Promise.resolve(signature)),
|
.returns(Promise.resolve(signature)),
|
||||||
Sinon.stub(ZeroEx, 'isValidSignature').returns(true),
|
Sinon.stub(ZeroEx, 'isValidSignature').returns(true),
|
||||||
];
|
];
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
import 'mocha';
|
import 'mocha';
|
||||||
import * as chai from 'chai';
|
import * as chai from 'chai';
|
||||||
import chaiAsPromised = require('chai-as-promised');
|
import chaiAsPromised = require('chai-as-promised');
|
||||||
import * as Web3 from 'web3';
|
|
||||||
import {web3Factory} from './utils/web3_factory';
|
import {web3Factory} from './utils/web3_factory';
|
||||||
import {ZeroEx} from '../src/0x.js';
|
import {ZeroEx} from '../src/0x.js';
|
||||||
import {BlockchainLifecycle} from './utils/blockchain_lifecycle';
|
import {BlockchainLifecycle} from './utils/blockchain_lifecycle';
|
||||||
|
@ -31,12 +31,12 @@ describe('TokenRegistryWrapper', () => {
|
|||||||
describe('#getTokensAsync', () => {
|
describe('#getTokensAsync', () => {
|
||||||
it('should return all the tokens added to the tokenRegistry during the migration', async () => {
|
it('should return all the tokens added to the tokenRegistry during the migration', async () => {
|
||||||
const tokens = await zeroEx.tokenRegistry.getTokensAsync();
|
const tokens = await zeroEx.tokenRegistry.getTokensAsync();
|
||||||
expect(tokens.length).to.be.equal(TOKEN_REGISTRY_SIZE_AFTER_MIGRATION);
|
expect(tokens).to.have.lengthOf(TOKEN_REGISTRY_SIZE_AFTER_MIGRATION);
|
||||||
|
|
||||||
const schemaValidator = new SchemaValidator();
|
const schemaValidator = new SchemaValidator();
|
||||||
_.each(tokens, token => {
|
_.each(tokens, token => {
|
||||||
const validationResult = schemaValidator.validate(token, tokenSchema);
|
const validationResult = schemaValidator.validate(token, tokenSchema);
|
||||||
expect(validationResult.errors.length).to.be.equal(0);
|
expect(validationResult.errors).to.have.lengthOf(0);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -10,14 +10,18 @@ import {constants} from './constants';
|
|||||||
|
|
||||||
export const web3Factory = {
|
export const web3Factory = {
|
||||||
create(): Web3 {
|
create(): Web3 {
|
||||||
|
const provider = this.getRpcProvider();
|
||||||
|
const web3 = new Web3();
|
||||||
|
web3.setProvider(provider);
|
||||||
|
return web3;
|
||||||
|
},
|
||||||
|
getRpcProvider(): Web3.Provider {
|
||||||
const provider = new ProviderEngine();
|
const provider = new ProviderEngine();
|
||||||
const rpcUrl = `http://${constants.RPC_HOST}:${constants.RPC_PORT}`;
|
const rpcUrl = `http://${constants.RPC_HOST}:${constants.RPC_PORT}`;
|
||||||
provider.addProvider(new RpcSubprovider({
|
provider.addProvider(new RpcSubprovider({
|
||||||
rpcUrl,
|
rpcUrl,
|
||||||
}));
|
}));
|
||||||
provider.start();
|
provider.start();
|
||||||
const web3 = new Web3();
|
return provider;
|
||||||
web3.setProvider(provider);
|
|
||||||
return web3;
|
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user