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
8 changed files with 76 additions and 27 deletions

View File

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

View File

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

View File

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

View File

@@ -1,12 +1,13 @@
import * as _ from 'lodash';
import {artifacts} from '../artifacts';
import {Token, TokenMetadata, TokenRegistryContract, ZeroExError} from '../types';
import {Token, TokenMetadata, ZeroExError} from '../types';
import {assert} from '../utils/assert';
import {constants} from '../utils/constants';
import {Web3Wrapper} from '../web3_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.
@@ -116,9 +117,12 @@ export class TokenRegistryWrapper extends ContractWrapper {
if (!_.isUndefined(this._tokenRegistryContractIfExists)) {
return this._tokenRegistryContractIfExists;
}
const contractInstance = await this._instantiateContractIfExistsAsync<TokenRegistryContract>(
const web3ContractInstance = await this._instantiateContractIfExistsAsync(
artifacts.TokenRegistryArtifact, this._contractAddressIfExists,
);
const contractInstance = new TokenRegistryContract(
web3ContractInstance, this._web3Wrapper.getContractDefaults(),
);
this._tokenRegistryContractIfExists = contractInstance;
return this._tokenRegistryContractIfExists;
}

View File

@@ -1,10 +1,11 @@
import * as _ from 'lodash';
import {artifacts} from '../artifacts';
import {TokenTransferProxyContract, ZeroExError} from '../types';
import {ZeroExError} from '../types';
import {Web3Wrapper} from '../web3_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.
@@ -53,9 +54,12 @@ export class TokenTransferProxyWrapper extends ContractWrapper {
if (!_.isUndefined(this._tokenTransferProxyContractIfExists)) {
return this._tokenTransferProxyContractIfExists;
}
const contractInstance = await this._instantiateContractIfExistsAsync<TokenTransferProxyContract>(
const web3ContractInstance = await this._instantiateContractIfExistsAsync(
artifacts.TokenTransferProxyArtifact, this._contractAddressIfExists,
);
const contractInstance = new TokenTransferProxyContract(
web3ContractInstance, this._web3Wrapper.getContractDefaults(),
);
this._tokenTransferProxyContractIfExists = contractInstance;
return this._tokenTransferProxyContractIfExists;
}

View File

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

View File

@@ -4,7 +4,7 @@ import * as _ from 'lodash';
import * as Web3 from 'web3';
import {Contract} from './contract';
import {Artifact, ArtifactContractName, TransactionReceipt, ZeroExError} from './types';
import {Artifact, ArtifactContractName, TransactionReceipt, TxData, ZeroExError} from './types';
interface RawLogEntry {
logIndex: string|null;
@@ -29,9 +29,9 @@ const CONTRACT_NAME_TO_NOT_FOUND_ERROR: {[contractName: string]: ZeroExError} =
export class Web3Wrapper {
private web3: Web3;
private networkId: number;
private defaults: Partial<Web3.TxData>;
private defaults: Partial<TxData>;
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)) {
// 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`
@@ -44,6 +44,9 @@ export class Web3Wrapper {
this.defaults = defaults || {};
this.jsonRpcRequestId = 0;
}
public getContractDefaults(): Partial<TxData> {
return this.defaults;
}
public setProvider(provider: Web3.Provider, networkId: number) {
this.networkId = networkId;
this.web3.setProvider(provider);
@@ -72,8 +75,9 @@ export class Web3Wrapper {
public getNetworkId(): number {
return this.networkId;
}
public async getContractInstanceFromArtifactAsync<A extends Web3.ContractInstance>(artifact: Artifact,
address?: string): Promise<A> {
public async getContractInstanceFromArtifactAsync(
artifact: Artifact, address?: string,
): Promise<Web3.ContractInstance> {
let contractAddress: string;
if (_.isUndefined(address)) {
const networkId = this.getNetworkId();
@@ -88,7 +92,7 @@ export class Web3Wrapper {
if (!doesContractExist) {
throw new Error(CONTRACT_NAME_TO_NOT_FOUND_ERROR[artifact.contract_name]);
}
const contractInstance = this.getContractInstance<A>(
const contractInstance = this.getContractInstance(
artifact.abi, contractAddress,
);
return contractInstance;
@@ -152,10 +156,9 @@ export class Web3Wrapper {
const formattedLogs = _.map(rawLogs, this.formatLog.bind(this));
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 contractInstance = new Contract(web3ContractInstance, this.defaults) as any as A;
return contractInstance;
return web3ContractInstance;
}
private async getNetworkAsync(): Promise<number> {
const networkId = await promisify(this.web3.version.getNetwork)();