Adjust 0x.js to use generated wrappers

This commit is contained in:
Leonid Logvinov 2017-12-01 22:47:05 -06:00
parent 6cbd0d4537
commit eb667f653c
No known key found for this signature in database
GPG Key ID: 0DD294BFDE8C95D4
8 changed files with 76 additions and 27 deletions

View File

@ -90,11 +90,13 @@ export class ContractWrapper {
const logWithDecodedArgs = this._abiDecoder.tryToDecodeLogOrNoop(log); const logWithDecodedArgs = this._abiDecoder.tryToDecodeLogOrNoop(log);
return logWithDecodedArgs; return logWithDecodedArgs;
} }
protected async _instantiateContractIfExistsAsync<ContractType extends Web3.ContractInstance>( protected async _instantiateContractIfExistsAsync(
artifact: Artifact, addressIfExists?: string): Promise<ContractType> { artifact: Artifact, addressIfExists?: string,
const contractInstance = ): Promise<Web3.ContractInstance> {
await this._web3Wrapper.getContractInstanceFromArtifactAsync<ContractType>(artifact, addressIfExists); const web3ContractInstance = await this._web3Wrapper.getContractInstanceFromArtifactAsync(
return contractInstance; artifact, addressIfExists,
);
return web3ContractInstance;
} }
protected _getContractAddress(artifact: Artifact, addressIfExists?: string): string { protected _getContractAddress(artifact: Artifact, addressIfExists?: string): string {
if (_.isUndefined(addressIfExists)) { if (_.isUndefined(addressIfExists)) {

View File

@ -2,11 +2,12 @@ import BigNumber from 'bignumber.js';
import * as _ from 'lodash'; import * as _ from 'lodash';
import {artifacts} from '../artifacts'; import {artifacts} from '../artifacts';
import {EtherTokenContract, TransactionOpts, ZeroExError} from '../types'; import {TransactionOpts, ZeroExError} from '../types';
import {assert} from '../utils/assert'; import {assert} from '../utils/assert';
import {Web3Wrapper} from '../web3_wrapper'; import {Web3Wrapper} from '../web3_wrapper';
import {ContractWrapper} from './contract_wrapper'; import {ContractWrapper} from './contract_wrapper';
import {EtherTokenContract} from './generated/ether_token';
import {TokenWrapper} from './token_wrapper'; import {TokenWrapper} from './token_wrapper';
/** /**
@ -92,9 +93,10 @@ export class EtherTokenWrapper extends ContractWrapper {
if (!_.isUndefined(this._etherTokenContractIfExists)) { if (!_.isUndefined(this._etherTokenContractIfExists)) {
return this._etherTokenContractIfExists; return this._etherTokenContractIfExists;
} }
const contractInstance = await this._instantiateContractIfExistsAsync<EtherTokenContract>( const web3ContractInstance = await this._instantiateContractIfExistsAsync(
artifacts.EtherTokenArtifact, this._contractAddressIfExists, artifacts.EtherTokenArtifact, this._contractAddressIfExists,
); );
const contractInstance = new EtherTokenContract(web3ContractInstance, this._web3Wrapper.getContractDefaults());
this._etherTokenContractIfExists = contractInstance; this._etherTokenContractIfExists = contractInstance;
return this._etherTokenContractIfExists; return this._etherTokenContractIfExists;
} }

View File

@ -9,7 +9,6 @@ import {
DecodedLogArgs, DecodedLogArgs,
ECSignature, ECSignature,
EventCallback, EventCallback,
ExchangeContract,
ExchangeContractErrCodes, ExchangeContractErrCodes,
ExchangeContractErrs, ExchangeContractErrs,
ExchangeContractEventArgs, ExchangeContractEventArgs,
@ -40,6 +39,7 @@ import {utils} from '../utils/utils';
import {Web3Wrapper} from '../web3_wrapper'; import {Web3Wrapper} from '../web3_wrapper';
import {ContractWrapper} from './contract_wrapper'; import {ContractWrapper} from './contract_wrapper';
import {ExchangeContract} from './generated/exchange';
import {TokenWrapper} from './token_wrapper'; import {TokenWrapper} from './token_wrapper';
const SHOULD_VALIDATE_BY_DEFAULT = true; const SHOULD_VALIDATE_BY_DEFAULT = true;
@ -789,9 +789,10 @@ export class ExchangeWrapper extends ContractWrapper {
if (!_.isUndefined(this._exchangeContractIfExists)) { if (!_.isUndefined(this._exchangeContractIfExists)) {
return this._exchangeContractIfExists; return this._exchangeContractIfExists;
} }
const contractInstance = await this._instantiateContractIfExistsAsync<ExchangeContract>( const web3ContractInstance = await this._instantiateContractIfExistsAsync(
artifacts.ExchangeArtifact, this._contractAddressIfExists, artifacts.ExchangeArtifact, this._contractAddressIfExists,
); );
const contractInstance = new ExchangeContract(web3ContractInstance, this._web3Wrapper.getContractDefaults());
this._exchangeContractIfExists = contractInstance; this._exchangeContractIfExists = contractInstance;
return this._exchangeContractIfExists; return this._exchangeContractIfExists;
} }

View File

@ -1,12 +1,13 @@
import * as _ from 'lodash'; import * as _ from 'lodash';
import {artifacts} from '../artifacts'; import {artifacts} from '../artifacts';
import {Token, TokenMetadata, TokenRegistryContract, ZeroExError} from '../types'; import {Token, TokenMetadata, ZeroExError} from '../types';
import {assert} from '../utils/assert'; import {assert} from '../utils/assert';
import {constants} from '../utils/constants'; import {constants} from '../utils/constants';
import {Web3Wrapper} from '../web3_wrapper'; import {Web3Wrapper} from '../web3_wrapper';
import {ContractWrapper} from './contract_wrapper'; import {ContractWrapper} from './contract_wrapper';
import {TokenRegistryContract} from './generated/token_registry';
/** /**
* This class includes all the functionality related to interacting with the 0x Token Registry smart contract. * This class includes all the functionality related to interacting with the 0x Token Registry smart contract.
@ -116,9 +117,12 @@ export class TokenRegistryWrapper extends ContractWrapper {
if (!_.isUndefined(this._tokenRegistryContractIfExists)) { if (!_.isUndefined(this._tokenRegistryContractIfExists)) {
return this._tokenRegistryContractIfExists; return this._tokenRegistryContractIfExists;
} }
const contractInstance = await this._instantiateContractIfExistsAsync<TokenRegistryContract>( const web3ContractInstance = await this._instantiateContractIfExistsAsync(
artifacts.TokenRegistryArtifact, this._contractAddressIfExists, artifacts.TokenRegistryArtifact, this._contractAddressIfExists,
); );
const contractInstance = new TokenRegistryContract(
web3ContractInstance, this._web3Wrapper.getContractDefaults(),
);
this._tokenRegistryContractIfExists = contractInstance; this._tokenRegistryContractIfExists = contractInstance;
return this._tokenRegistryContractIfExists; return this._tokenRegistryContractIfExists;
} }

View File

@ -1,10 +1,11 @@
import * as _ from 'lodash'; import * as _ from 'lodash';
import {artifacts} from '../artifacts'; import {artifacts} from '../artifacts';
import {TokenTransferProxyContract, ZeroExError} from '../types'; import {ZeroExError} from '../types';
import {Web3Wrapper} from '../web3_wrapper'; import {Web3Wrapper} from '../web3_wrapper';
import {ContractWrapper} from './contract_wrapper'; import {ContractWrapper} from './contract_wrapper';
import {TokenTransferProxyContract} from './generated/token_transfer_proxy';
/** /**
* This class includes the functionality related to interacting with the TokenTransferProxy contract. * This class includes the functionality related to interacting with the TokenTransferProxy contract.
@ -53,9 +54,12 @@ export class TokenTransferProxyWrapper extends ContractWrapper {
if (!_.isUndefined(this._tokenTransferProxyContractIfExists)) { if (!_.isUndefined(this._tokenTransferProxyContractIfExists)) {
return this._tokenTransferProxyContractIfExists; return this._tokenTransferProxyContractIfExists;
} }
const contractInstance = await this._instantiateContractIfExistsAsync<TokenTransferProxyContract>( const web3ContractInstance = await this._instantiateContractIfExistsAsync(
artifacts.TokenTransferProxyArtifact, this._contractAddressIfExists, artifacts.TokenTransferProxyArtifact, this._contractAddressIfExists,
); );
const contractInstance = new TokenTransferProxyContract(
web3ContractInstance, this._web3Wrapper.getContractDefaults(),
);
this._tokenTransferProxyContractIfExists = contractInstance; this._tokenTransferProxyContractIfExists = contractInstance;
return this._tokenTransferProxyContractIfExists; return this._tokenTransferProxyContractIfExists;
} }

View File

@ -9,7 +9,6 @@ import {
LogWithDecodedArgs, LogWithDecodedArgs,
MethodOpts, MethodOpts,
SubscriptionOpts, SubscriptionOpts,
TokenContract,
TokenContractEventArgs, TokenContractEventArgs,
TokenEvents, TokenEvents,
TransactionOpts, TransactionOpts,
@ -21,6 +20,7 @@ import {constants} from '../utils/constants';
import {Web3Wrapper} from '../web3_wrapper'; import {Web3Wrapper} from '../web3_wrapper';
import {ContractWrapper} from './contract_wrapper'; import {ContractWrapper} from './contract_wrapper';
import {TokenContract} from './generated/token';
import {TokenTransferProxyWrapper} from './token_transfer_proxy_wrapper'; import {TokenTransferProxyWrapper} from './token_transfer_proxy_wrapper';
const ALLOWANCE_TO_ZERO_GAS_AMOUNT = 47275; const ALLOWANCE_TO_ZERO_GAS_AMOUNT = 47275;
@ -313,9 +313,12 @@ export class TokenWrapper extends ContractWrapper {
if (!_.isUndefined(tokenContract)) { if (!_.isUndefined(tokenContract)) {
return tokenContract; return tokenContract;
} }
const contractInstance = await this._instantiateContractIfExistsAsync<TokenContract>( const web3ContractInstance = await this._instantiateContractIfExistsAsync(
artifacts.TokenArtifact, tokenAddress, artifacts.TokenArtifact, tokenAddress,
); );
const contractInstance = new TokenContract(
web3ContractInstance, this._web3Wrapper.getContractDefaults(),
);
tokenContract = contractInstance; tokenContract = contractInstance;
this._tokenContractsByAddress[tokenAddress] = tokenContract; this._tokenContractsByAddress[tokenAddress] = tokenContract;
return tokenContract; return tokenContract;

View File

@ -4,7 +4,7 @@ import * as _ from 'lodash';
import * as Web3 from 'web3'; import * as Web3 from 'web3';
import {Contract} from './contract'; import {Contract} from './contract';
import {Artifact, ArtifactContractName, TransactionReceipt, ZeroExError} from './types'; import {Artifact, ArtifactContractName, TransactionReceipt, TxData, ZeroExError} from './types';
interface RawLogEntry { interface RawLogEntry {
logIndex: string|null; logIndex: string|null;
@ -29,9 +29,9 @@ const CONTRACT_NAME_TO_NOT_FOUND_ERROR: {[contractName: string]: ZeroExError} =
export class Web3Wrapper { export class Web3Wrapper {
private web3: Web3; private web3: Web3;
private networkId: number; private networkId: number;
private defaults: Partial<Web3.TxData>; private defaults: Partial<TxData>;
private jsonRpcRequestId: number; private jsonRpcRequestId: number;
constructor(provider: Web3.Provider, networkId: number, defaults?: Partial<Web3.TxData>) { constructor(provider: Web3.Provider, networkId: number, defaults?: Partial<TxData>) {
if (_.isUndefined((provider as any).sendAsync)) { if (_.isUndefined((provider as any).sendAsync)) {
// Web3@1.0 provider doesn't support synchronous http requests, // Web3@1.0 provider doesn't support synchronous http requests,
// so it only has an async `send` method, instead of a `send` and `sendAsync` in web3@0.x.x` // so it only has an async `send` method, instead of a `send` and `sendAsync` in web3@0.x.x`
@ -44,6 +44,9 @@ export class Web3Wrapper {
this.defaults = defaults || {}; this.defaults = defaults || {};
this.jsonRpcRequestId = 0; this.jsonRpcRequestId = 0;
} }
public getContractDefaults(): Partial<TxData> {
return this.defaults;
}
public setProvider(provider: Web3.Provider, networkId: number) { public setProvider(provider: Web3.Provider, networkId: number) {
this.networkId = networkId; this.networkId = networkId;
this.web3.setProvider(provider); this.web3.setProvider(provider);
@ -72,8 +75,9 @@ export class Web3Wrapper {
public getNetworkId(): number { public getNetworkId(): number {
return this.networkId; return this.networkId;
} }
public async getContractInstanceFromArtifactAsync<A extends Web3.ContractInstance>(artifact: Artifact, public async getContractInstanceFromArtifactAsync(
address?: string): Promise<A> { artifact: Artifact, address?: string,
): Promise<Web3.ContractInstance> {
let contractAddress: string; let contractAddress: string;
if (_.isUndefined(address)) { if (_.isUndefined(address)) {
const networkId = this.getNetworkId(); const networkId = this.getNetworkId();
@ -88,7 +92,7 @@ export class Web3Wrapper {
if (!doesContractExist) { if (!doesContractExist) {
throw new Error(CONTRACT_NAME_TO_NOT_FOUND_ERROR[artifact.contract_name]); throw new Error(CONTRACT_NAME_TO_NOT_FOUND_ERROR[artifact.contract_name]);
} }
const contractInstance = this.getContractInstance<A>( const contractInstance = this.getContractInstance(
artifact.abi, contractAddress, artifact.abi, contractAddress,
); );
return contractInstance; return contractInstance;
@ -152,10 +156,9 @@ export class Web3Wrapper {
const formattedLogs = _.map(rawLogs, this.formatLog.bind(this)); const formattedLogs = _.map(rawLogs, this.formatLog.bind(this));
return formattedLogs; return formattedLogs;
} }
private getContractInstance<A extends Web3.ContractInstance>(abi: Web3.ContractAbi, address: string): A { private getContractInstance(abi: Web3.ContractAbi, address: string): Web3.ContractInstance {
const web3ContractInstance = this.web3.eth.contract(abi).at(address); const web3ContractInstance = this.web3.eth.contract(abi).at(address);
const contractInstance = new Contract(web3ContractInstance, this.defaults) as any as A; return web3ContractInstance;
return contractInstance;
} }
private async getNetworkAsync(): Promise<number> { private async getNetworkAsync(): Promise<number> {
const networkId = await promisify(this.web3.version.getNetwork)(); const networkId = await promisify(this.web3.version.getNetwork)();

View File

@ -83,14 +83,14 @@
dependencies: dependencies:
"@types/node" "*" "@types/node" "*"
"@types/glob@*": "@types/glob@*", "@types/glob@^5.0.33":
version "5.0.33" version "5.0.33"
resolved "https://registry.yarnpkg.com/@types/glob/-/glob-5.0.33.tgz#3dff7c6ce09d65abe919c7961dc3dee016f36ad7" resolved "https://registry.yarnpkg.com/@types/glob/-/glob-5.0.33.tgz#3dff7c6ce09d65abe919c7961dc3dee016f36ad7"
dependencies: dependencies:
"@types/minimatch" "*" "@types/minimatch" "*"
"@types/node" "*" "@types/node" "*"
"@types/handlebars@^4.0.31": "@types/handlebars@^4.0.31", "@types/handlebars@^4.0.36":
version "4.0.36" version "4.0.36"
resolved "https://registry.yarnpkg.com/@types/handlebars/-/handlebars-4.0.36.tgz#ff57c77fa1ab6713bb446534ddc4d979707a3a79" resolved "https://registry.yarnpkg.com/@types/handlebars/-/handlebars-4.0.36.tgz#ff57c77fa1ab6713bb446534ddc4d979707a3a79"
@ -147,6 +147,12 @@
version "2.0.29" version "2.0.29"
resolved "https://registry.yarnpkg.com/@types/minimatch/-/minimatch-2.0.29.tgz#5002e14f75e2d71e564281df0431c8c1b4a2a36a" resolved "https://registry.yarnpkg.com/@types/minimatch/-/minimatch-2.0.29.tgz#5002e14f75e2d71e564281df0431c8c1b4a2a36a"
"@types/mkdirp@^0.5.1":
version "0.5.1"
resolved "https://registry.yarnpkg.com/@types/mkdirp/-/mkdirp-0.5.1.tgz#ea887cd024f691c1ca67cce20b7606b053e43b0f"
dependencies:
"@types/node" "*"
"@types/mocha@^2.2.41", "@types/mocha@^2.2.42": "@types/mocha@^2.2.41", "@types/mocha@^2.2.42":
version "2.2.44" version "2.2.44"
resolved "https://registry.yarnpkg.com/@types/mocha/-/mocha-2.2.44.tgz#1d4a798e53f35212fd5ad4d04050620171cd5b5e" resolved "https://registry.yarnpkg.com/@types/mocha/-/mocha-2.2.44.tgz#1d4a798e53f35212fd5ad4d04050620171cd5b5e"
@ -165,6 +171,10 @@
version "7.0.48" version "7.0.48"
resolved "https://registry.yarnpkg.com/@types/node/-/node-7.0.48.tgz#24bfdc0aa82e8f6dbd017159c58094a2e06d0abb" resolved "https://registry.yarnpkg.com/@types/node/-/node-7.0.48.tgz#24bfdc0aa82e8f6dbd017159c58094a2e06d0abb"
"@types/node@^8.0.53":
version "8.0.53"
resolved "https://registry.yarnpkg.com/@types/node/-/node-8.0.53.tgz#396b35af826fa66aad472c8cb7b8d5e277f4e6d8"
"@types/query-string@^5.0.0", "@types/query-string@^5.0.1": "@types/query-string@^5.0.0", "@types/query-string@^5.0.1":
version "5.0.1" version "5.0.1"
resolved "https://registry.yarnpkg.com/@types/query-string/-/query-string-5.0.1.tgz#6cb41c724cb1644d56c2d1dae7c7b204e706b39e" resolved "https://registry.yarnpkg.com/@types/query-string/-/query-string-5.0.1.tgz#6cb41c724cb1644d56c2d1dae7c7b204e706b39e"
@ -1271,6 +1281,10 @@ bignumber.js@^4.0.2, bignumber.js@^4.1.0, bignumber.js@~4.1.0:
version "4.1.0" version "4.1.0"
resolved "https://registry.yarnpkg.com/bignumber.js/-/bignumber.js-4.1.0.tgz#db6f14067c140bd46624815a7916c92d9b6c24b1" resolved "https://registry.yarnpkg.com/bignumber.js/-/bignumber.js-4.1.0.tgz#db6f14067c140bd46624815a7916c92d9b6c24b1"
bignumber.js@^5.0.0:
version "5.0.0"
resolved "https://registry.yarnpkg.com/bignumber.js/-/bignumber.js-5.0.0.tgz#fbce63f09776b3000a83185badcde525daf34833"
"bignumber.js@git+https://github.com/debris/bignumber.js#master": "bignumber.js@git+https://github.com/debris/bignumber.js#master":
version "2.0.7" version "2.0.7"
resolved "git+https://github.com/debris/bignumber.js#c7a38de919ed75e6fb6ba38051986e294b328df9" resolved "git+https://github.com/debris/bignumber.js#c7a38de919ed75e6fb6ba38051986e294b328df9"
@ -3764,7 +3778,7 @@ handle-thing@^1.2.5:
version "1.2.5" version "1.2.5"
resolved "https://registry.yarnpkg.com/handle-thing/-/handle-thing-1.2.5.tgz#fd7aad726bf1a5fd16dfc29b2f7a6601d27139c4" resolved "https://registry.yarnpkg.com/handle-thing/-/handle-thing-1.2.5.tgz#fd7aad726bf1a5fd16dfc29b2f7a6601d27139c4"
handlebars@^4.0.2, handlebars@^4.0.3, handlebars@^4.0.6: handlebars@^4.0.11, handlebars@^4.0.2, handlebars@^4.0.3, handlebars@^4.0.6:
version "4.0.11" version "4.0.11"
resolved "https://registry.yarnpkg.com/handlebars/-/handlebars-4.0.11.tgz#630a35dfe0294bc281edae6ffc5d329fc7982dcc" resolved "https://registry.yarnpkg.com/handlebars/-/handlebars-4.0.11.tgz#630a35dfe0294bc281edae6ffc5d329fc7982dcc"
dependencies: dependencies:
@ -8164,6 +8178,10 @@ to-fast-properties@^1.0.3:
version "1.0.3" version "1.0.3"
resolved "https://registry.yarnpkg.com/to-fast-properties/-/to-fast-properties-1.0.3.tgz#b83571fa4d8c25b82e231b06e3a3055de4ca1a47" resolved "https://registry.yarnpkg.com/to-fast-properties/-/to-fast-properties-1.0.3.tgz#b83571fa4d8c25b82e231b06e3a3055de4ca1a47"
to-no-case@^1.0.0:
version "1.0.2"
resolved "https://registry.yarnpkg.com/to-no-case/-/to-no-case-1.0.2.tgz#c722907164ef6b178132c8e69930212d1b4aa16a"
to-object-path@^0.3.0: to-object-path@^0.3.0:
version "0.3.0" version "0.3.0"
resolved "https://registry.yarnpkg.com/to-object-path/-/to-object-path-0.3.0.tgz#297588b7b0e7e0ac08e04e672f85c1f4999e17af" resolved "https://registry.yarnpkg.com/to-object-path/-/to-object-path-0.3.0.tgz#297588b7b0e7e0ac08e04e672f85c1f4999e17af"
@ -8185,6 +8203,18 @@ to-regex@^3.0.1:
extend-shallow "^2.0.1" extend-shallow "^2.0.1"
regex-not "^1.0.0" regex-not "^1.0.0"
to-snake-case@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/to-snake-case/-/to-snake-case-1.0.0.tgz#ce746913897946019a87e62edfaeaea4c608ab8c"
dependencies:
to-space-case "^1.0.0"
to-space-case@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/to-space-case/-/to-space-case-1.0.0.tgz#b052daafb1b2b29dc770cea0163e5ec0ebc9fc17"
dependencies:
to-no-case "^1.0.0"
toggle-selection@^1.0.3: toggle-selection@^1.0.3:
version "1.0.6" version "1.0.6"
resolved "https://registry.yarnpkg.com/toggle-selection/-/toggle-selection-1.0.6.tgz#6e45b1263f2017fa0acc7d89d78b15b8bf77da32" resolved "https://registry.yarnpkg.com/toggle-selection/-/toggle-selection-1.0.6.tgz#6e45b1263f2017fa0acc7d89d78b15b8bf77da32"