diff options
author | Leonid <logvinov.leon@gmail.com> | 2017-12-06 03:39:36 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-12-06 03:39:36 +0800 |
commit | 1153fa093b5a20863a2a7c1237a39ffdf7aaec49 (patch) | |
tree | b6d9bdad0c339d02336698f326f00352992be6d4 /packages/0x.js/src | |
parent | c0015c2c118c0fd563fa8d2ee672c28dca7ef809 (diff) | |
parent | c64ec92fb23fd130d0c54a4d42147bb468e434d9 (diff) | |
download | dexon-sol-tools-1153fa093b5a20863a2a7c1237a39ffdf7aaec49.tar dexon-sol-tools-1153fa093b5a20863a2a7c1237a39ffdf7aaec49.tar.gz dexon-sol-tools-1153fa093b5a20863a2a7c1237a39ffdf7aaec49.tar.bz2 dexon-sol-tools-1153fa093b5a20863a2a7c1237a39ffdf7aaec49.tar.lz dexon-sol-tools-1153fa093b5a20863a2a7c1237a39ffdf7aaec49.tar.xz dexon-sol-tools-1153fa093b5a20863a2a7c1237a39ffdf7aaec49.tar.zst dexon-sol-tools-1153fa093b5a20863a2a7c1237a39ffdf7aaec49.zip |
Merge pull request #249 from 0xProject/feature/typed-contracts
ABI to TS generator
Diffstat (limited to 'packages/0x.js/src')
18 files changed, 2301 insertions, 294 deletions
diff --git a/packages/0x.js/src/contract.ts b/packages/0x.js/src/contract.ts deleted file mode 100644 index a4ee03910..000000000 --- a/packages/0x.js/src/contract.ts +++ /dev/null @@ -1,98 +0,0 @@ -import {schemas, SchemaValidator} from '@0xproject/json-schemas'; -import promisify = require('es6-promisify'); -import * as _ from 'lodash'; -import * as Web3 from 'web3'; - -import {AbiType} from './types'; - -// HACK: Gas estimates on testrpc don't take into account gas refunds. -// Our calls can trigger max 8 gas refunds for SSTORE per transaction for 15k gas each which gives 120k. -const GAS_MARGIN = 120000; - -export class Contract implements Web3.ContractInstance { - public address: string; - public abi: Web3.ContractAbi; - private contract: Web3.ContractInstance; - private defaults: Partial<Web3.TxData>; - private validator: SchemaValidator; - // This class instance is going to be populated with functions and events depending on the ABI - // and we don't know their types in advance - [name: string]: any; - constructor(web3ContractInstance: Web3.ContractInstance, defaults: Partial<Web3.TxData>) { - this.contract = web3ContractInstance; - this.address = web3ContractInstance.address; - this.abi = web3ContractInstance.abi; - this.defaults = defaults; - this.populateEvents(); - this.populateFunctions(); - this.validator = new SchemaValidator(); - } - private populateFunctions(): void { - const functionsAbi = _.filter(this.abi, abiPart => abiPart.type === AbiType.Function); - _.forEach(functionsAbi, (functionAbi: Web3.MethodAbi) => { - if (functionAbi.constant) { - const cbStyleCallFunction = this.contract[functionAbi.name].call; - this[functionAbi.name] = { - callAsync: promisify(cbStyleCallFunction, this.contract), - }; - } else { - const cbStyleFunction = this.contract[functionAbi.name]; - const cbStyleEstimateGasFunction = this.contract[functionAbi.name].estimateGas; - const estimateGasAsync = promisify(cbStyleEstimateGasFunction, this.contract); - this[functionAbi.name] = { - estimateGasAsync, - sendTransactionAsync: this.promisifyWithDefaultParams(cbStyleFunction, estimateGasAsync), - }; - } - }); - } - private populateEvents(): void { - const eventsAbi = _.filter(this.abi, abiPart => abiPart.type === AbiType.Event); - _.forEach(eventsAbi, (eventAbi: Web3.EventAbi) => { - this[eventAbi.name] = this.contract[eventAbi.name]; - }); - } - private promisifyWithDefaultParams( - web3CbStyleFunction: (...args: any[]) => void, - estimateGasAsync: (...args: any[]) => Promise<number>, - ): (...args: any[]) => Promise<any> { - const promisifiedWithDefaultParams = async (...args: any[]) => { - const promise = new Promise(async (resolve, reject) => { - const lastArg = args[args.length - 1]; - let txData: Partial<Web3.TxData> = {}; - if (!_.isUndefined(lastArg) && this.isTxData(lastArg)) { - txData = args.pop(); - } - // Gas amount sourced with the following priorities: - // 1. Optional param passed in to public method call - // 2. Global config passed in at library instantiation - // 3. Gas estimate calculation + safety margin - const removeUndefinedProperties = _.pickBy; - txData = { - ...removeUndefinedProperties(this.defaults), - ...removeUndefinedProperties(txData), - }; - if (_.isUndefined(txData.gas)) { - try { - const estimatedGas = await estimateGasAsync.apply(this.contract, [...args, txData]); - const gas = estimatedGas + GAS_MARGIN; - txData.gas = gas; - } catch (err) { - reject(err); - return; - } - } - const callback = (err: Error, data: any) => _.isNull(err) ? resolve(data) : reject(err); - args.push(txData); - args.push(callback); - web3CbStyleFunction.apply(this.contract, args); - }); - return promise; - }; - return promisifiedWithDefaultParams; - } - private isTxData(lastArg: any): boolean { - const isValid = this.validator.isValid(lastArg, schemas.txDataSchema); - return isValid; - } -} diff --git a/packages/0x.js/src/contract_wrappers/contract_wrapper.ts b/packages/0x.js/src/contract_wrappers/contract_wrapper.ts index 8c92931b4..5e5a38f8c 100644 --- a/packages/0x.js/src/contract_wrappers/contract_wrapper.ts +++ b/packages/0x.js/src/contract_wrappers/contract_wrapper.ts @@ -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)) { diff --git a/packages/0x.js/src/contract_wrappers/ether_token_wrapper.ts b/packages/0x.js/src/contract_wrappers/ether_token_wrapper.ts index ede0460bd..26025f6f9 100644 --- a/packages/0x.js/src/contract_wrappers/ether_token_wrapper.ts +++ b/packages/0x.js/src/contract_wrappers/ether_token_wrapper.ts @@ -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; } diff --git a/packages/0x.js/src/contract_wrappers/exchange_wrapper.ts b/packages/0x.js/src/contract_wrappers/exchange_wrapper.ts index 273b348ff..aaf6256a3 100644 --- a/packages/0x.js/src/contract_wrappers/exchange_wrapper.ts +++ b/packages/0x.js/src/contract_wrappers/exchange_wrapper.ts @@ -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; } diff --git a/packages/0x.js/src/contract_wrappers/generated/base_contract.ts b/packages/0x.js/src/contract_wrappers/generated/base_contract.ts new file mode 100644 index 000000000..396a4d593 --- /dev/null +++ b/packages/0x.js/src/contract_wrappers/generated/base_contract.ts @@ -0,0 +1,34 @@ +import * as _ from 'lodash'; +import * as Web3 from 'web3'; + +import {TxData, TxDataPayable} from '../../types'; + +export class BaseContract { + protected web3ContractInstance: Web3.ContractInstance; + protected defaults: Partial<TxData>; + protected async applyDefaultsToTxDataAsync<T extends TxData|TxDataPayable>( + txData: T, + estimateGasAsync?: (txData: T) => Promise<number>, + ): Promise<TxData> { + // Gas amount sourced with the following priorities: + // 1. Optional param passed in to public method call + // 2. Global config passed in at library instantiation + // 3. Gas estimate calculation + safety margin + const removeUndefinedProperties = _.pickBy; + const txDataWithDefaults = { + ...removeUndefinedProperties(this.defaults), + ...removeUndefinedProperties(txData as any), + // HACK: TS can't prove that T is spreadable. + // Awaiting https://github.com/Microsoft/TypeScript/pull/13288 to be merged + }; + if (_.isUndefined(txDataWithDefaults.gas) && !_.isUndefined(estimateGasAsync)) { + const estimatedGas = await estimateGasAsync(txData); + txDataWithDefaults.gas = estimatedGas; + } + return txDataWithDefaults; + } + constructor(web3ContractInstance: Web3.ContractInstance, defaults: Partial<TxData>) { + this.web3ContractInstance = web3ContractInstance; + this.defaults = defaults; + } +} diff --git a/packages/0x.js/src/contract_wrappers/generated/ether_token.ts b/packages/0x.js/src/contract_wrappers/generated/ether_token.ts new file mode 100644 index 000000000..eed5e4686 --- /dev/null +++ b/packages/0x.js/src/contract_wrappers/generated/ether_token.ts @@ -0,0 +1,363 @@ +/** + * This file is auto-generated using abi-gen. Don't edit directly. + * Templates can be found at https://github.com/0xProject/0x.js/tree/development/packages/abi-gen-templates. + */ +import {BigNumber} from 'bignumber.js'; +import * as Web3 from 'web3'; + +import {TxData, TxDataPayable} from '../../types'; +import {classUtils} from '../../utils/class_utils'; +import {promisify} from '../../utils/promisify'; + +import {BaseContract} from './base_contract'; + +export class EtherTokenContract extends BaseContract { + public name = { + async callAsync( + defaultBlock?: Web3.BlockParam, + ): Promise<string + > { + const self = this as EtherTokenContract; + const result = await promisify<string + >( + self.web3ContractInstance.name.call, + self.web3ContractInstance, + )( + ); + return result; + }, + }; + public approve = { + async sendTransactionAsync( + _spender: string, + _value: BigNumber, + txData: TxData = {}, + ): Promise<string> { + const self = this as EtherTokenContract; + const txDataWithDefaults = await self.applyDefaultsToTxDataAsync( + txData, + self.approve.estimateGasAsync.bind( + self, + _spender, + _value, + ), + ); + const txHash = await promisify<string>( + self.web3ContractInstance.approve, self.web3ContractInstance, + )( + _spender, + _value, + txDataWithDefaults, + ); + return txHash; + }, + async estimateGasAsync( + _spender: string, + _value: BigNumber, + txData: TxData = {}, + ): Promise<number> { + const self = this as EtherTokenContract; + const txDataWithDefaults = await self.applyDefaultsToTxDataAsync( + txData, + ); + const gas = await promisify<number>( + self.web3ContractInstance.approve.estimateGas, self.web3ContractInstance, + )( + _spender, + _value, + txDataWithDefaults, + ); + return gas; + }, + getABIEncodedTransactionData( + _spender: string, + _value: BigNumber, + txData: TxData = {}, + ): string { + const self = this as EtherTokenContract; + const abiEncodedTransactionData = self.web3ContractInstance.approve.getData(); + return abiEncodedTransactionData; + }, + }; + public totalSupply = { + async callAsync( + defaultBlock?: Web3.BlockParam, + ): Promise<BigNumber + > { + const self = this as EtherTokenContract; + const result = await promisify<BigNumber + >( + self.web3ContractInstance.totalSupply.call, + self.web3ContractInstance, + )( + ); + return result; + }, + }; + public transferFrom = { + async sendTransactionAsync( + _from: string, + _to: string, + _value: BigNumber, + txData: TxData = {}, + ): Promise<string> { + const self = this as EtherTokenContract; + const txDataWithDefaults = await self.applyDefaultsToTxDataAsync( + txData, + self.transferFrom.estimateGasAsync.bind( + self, + _from, + _to, + _value, + ), + ); + const txHash = await promisify<string>( + self.web3ContractInstance.transferFrom, self.web3ContractInstance, + )( + _from, + _to, + _value, + txDataWithDefaults, + ); + return txHash; + }, + async estimateGasAsync( + _from: string, + _to: string, + _value: BigNumber, + txData: TxData = {}, + ): Promise<number> { + const self = this as EtherTokenContract; + const txDataWithDefaults = await self.applyDefaultsToTxDataAsync( + txData, + ); + const gas = await promisify<number>( + self.web3ContractInstance.transferFrom.estimateGas, self.web3ContractInstance, + )( + _from, + _to, + _value, + txDataWithDefaults, + ); + return gas; + }, + getABIEncodedTransactionData( + _from: string, + _to: string, + _value: BigNumber, + txData: TxData = {}, + ): string { + const self = this as EtherTokenContract; + const abiEncodedTransactionData = self.web3ContractInstance.transferFrom.getData(); + return abiEncodedTransactionData; + }, + }; + public withdraw = { + async sendTransactionAsync( + amount: BigNumber, + txData: TxData = {}, + ): Promise<string> { + const self = this as EtherTokenContract; + const txDataWithDefaults = await self.applyDefaultsToTxDataAsync( + txData, + self.withdraw.estimateGasAsync.bind( + self, + amount, + ), + ); + const txHash = await promisify<string>( + self.web3ContractInstance.withdraw, self.web3ContractInstance, + )( + amount, + txDataWithDefaults, + ); + return txHash; + }, + async estimateGasAsync( + amount: BigNumber, + txData: TxData = {}, + ): Promise<number> { + const self = this as EtherTokenContract; + const txDataWithDefaults = await self.applyDefaultsToTxDataAsync( + txData, + ); + const gas = await promisify<number>( + self.web3ContractInstance.withdraw.estimateGas, self.web3ContractInstance, + )( + amount, + txDataWithDefaults, + ); + return gas; + }, + getABIEncodedTransactionData( + amount: BigNumber, + txData: TxData = {}, + ): string { + const self = this as EtherTokenContract; + const abiEncodedTransactionData = self.web3ContractInstance.withdraw.getData(); + return abiEncodedTransactionData; + }, + }; + public decimals = { + async callAsync( + defaultBlock?: Web3.BlockParam, + ): Promise<BigNumber + > { + const self = this as EtherTokenContract; + const result = await promisify<BigNumber + >( + self.web3ContractInstance.decimals.call, + self.web3ContractInstance, + )( + ); + return result; + }, + }; + public balanceOf = { + async callAsync( + _owner: string, + defaultBlock?: Web3.BlockParam, + ): Promise<BigNumber + > { + const self = this as EtherTokenContract; + const result = await promisify<BigNumber + >( + self.web3ContractInstance.balanceOf.call, + self.web3ContractInstance, + )( + _owner, + ); + return result; + }, + }; + public symbol = { + async callAsync( + defaultBlock?: Web3.BlockParam, + ): Promise<string + > { + const self = this as EtherTokenContract; + const result = await promisify<string + >( + self.web3ContractInstance.symbol.call, + self.web3ContractInstance, + )( + ); + return result; + }, + }; + public transfer = { + async sendTransactionAsync( + _to: string, + _value: BigNumber, + txData: TxData = {}, + ): Promise<string> { + const self = this as EtherTokenContract; + const txDataWithDefaults = await self.applyDefaultsToTxDataAsync( + txData, + self.transfer.estimateGasAsync.bind( + self, + _to, + _value, + ), + ); + const txHash = await promisify<string>( + self.web3ContractInstance.transfer, self.web3ContractInstance, + )( + _to, + _value, + txDataWithDefaults, + ); + return txHash; + }, + async estimateGasAsync( + _to: string, + _value: BigNumber, + txData: TxData = {}, + ): Promise<number> { + const self = this as EtherTokenContract; + const txDataWithDefaults = await self.applyDefaultsToTxDataAsync( + txData, + ); + const gas = await promisify<number>( + self.web3ContractInstance.transfer.estimateGas, self.web3ContractInstance, + )( + _to, + _value, + txDataWithDefaults, + ); + return gas; + }, + getABIEncodedTransactionData( + _to: string, + _value: BigNumber, + txData: TxData = {}, + ): string { + const self = this as EtherTokenContract; + const abiEncodedTransactionData = self.web3ContractInstance.transfer.getData(); + return abiEncodedTransactionData; + }, + }; + public deposit = { + async sendTransactionAsync( + txData: TxDataPayable = {}, + ): Promise<string> { + const self = this as EtherTokenContract; + const txDataWithDefaults = await self.applyDefaultsToTxDataAsync( + txData, + self.deposit.estimateGasAsync.bind( + self, + ), + ); + const txHash = await promisify<string>( + self.web3ContractInstance.deposit, self.web3ContractInstance, + )( + txDataWithDefaults, + ); + return txHash; + }, + async estimateGasAsync( + txData: TxData = {}, + ): Promise<number> { + const self = this as EtherTokenContract; + const txDataWithDefaults = await self.applyDefaultsToTxDataAsync( + txData, + ); + const gas = await promisify<number>( + self.web3ContractInstance.deposit.estimateGas, self.web3ContractInstance, + )( + txDataWithDefaults, + ); + return gas; + }, + getABIEncodedTransactionData( + txData: TxData = {}, + ): string { + const self = this as EtherTokenContract; + const abiEncodedTransactionData = self.web3ContractInstance.deposit.getData(); + return abiEncodedTransactionData; + }, + }; + public allowance = { + async callAsync( + _owner: string, + _spender: string, + defaultBlock?: Web3.BlockParam, + ): Promise<BigNumber + > { + const self = this as EtherTokenContract; + const result = await promisify<BigNumber + >( + self.web3ContractInstance.allowance.call, + self.web3ContractInstance, + )( + _owner, + _spender, + ); + return result; + }, + }; + constructor(web3ContractInstance: Web3.ContractInstance, defaults: Partial<TxData>) { + super(web3ContractInstance, defaults); + classUtils.bindAll(this, ['web3ContractInstance', 'defaults']); + } +} // tslint:disable:max-file-line-count diff --git a/packages/0x.js/src/contract_wrappers/generated/exchange.ts b/packages/0x.js/src/contract_wrappers/generated/exchange.ts new file mode 100644 index 000000000..8c25ca014 --- /dev/null +++ b/packages/0x.js/src/contract_wrappers/generated/exchange.ts @@ -0,0 +1,730 @@ +/** + * This file is auto-generated using abi-gen. Don't edit directly. + * Templates can be found at https://github.com/0xProject/0x.js/tree/development/packages/abi-gen-templates. + */ +import {BigNumber} from 'bignumber.js'; +import * as Web3 from 'web3'; + +import {TxData, TxDataPayable} from '../../types'; +import {classUtils} from '../../utils/class_utils'; +import {promisify} from '../../utils/promisify'; + +import {BaseContract} from './base_contract'; + +export class ExchangeContract extends BaseContract { + public isRoundingError = { + async callAsync( + numerator: BigNumber, + denominator: BigNumber, + target: BigNumber, + defaultBlock?: Web3.BlockParam, + ): Promise<boolean + > { + const self = this as ExchangeContract; + const result = await promisify<boolean + >( + self.web3ContractInstance.isRoundingError.call, + self.web3ContractInstance, + )( + numerator, + denominator, + target, + ); + return result; + }, + }; + public filled = { + async callAsync( + index: string, + defaultBlock?: Web3.BlockParam, + ): Promise<BigNumber + > { + const self = this as ExchangeContract; + const result = await promisify<BigNumber + >( + self.web3ContractInstance.filled.call, + self.web3ContractInstance, + )( + index, + ); + return result; + }, + }; + public cancelled = { + async callAsync( + index: string, + defaultBlock?: Web3.BlockParam, + ): Promise<BigNumber + > { + const self = this as ExchangeContract; + const result = await promisify<BigNumber + >( + self.web3ContractInstance.cancelled.call, + self.web3ContractInstance, + )( + index, + ); + return result; + }, + }; + public fillOrdersUpTo = { + async sendTransactionAsync( + orderAddresses: string[][], + orderValues: BigNumber[][], + fillTakerTokenAmount: BigNumber, + shouldThrowOnInsufficientBalanceOrAllowance: boolean, + v: number|BigNumber[], + r: string[], + s: string[], + txData: TxData = {}, + ): Promise<string> { + const self = this as ExchangeContract; + const txDataWithDefaults = await self.applyDefaultsToTxDataAsync( + txData, + self.fillOrdersUpTo.estimateGasAsync.bind( + self, + orderAddresses, + orderValues, + fillTakerTokenAmount, + shouldThrowOnInsufficientBalanceOrAllowance, + v, + r, + s, + ), + ); + const txHash = await promisify<string>( + self.web3ContractInstance.fillOrdersUpTo, self.web3ContractInstance, + )( + orderAddresses, + orderValues, + fillTakerTokenAmount, + shouldThrowOnInsufficientBalanceOrAllowance, + v, + r, + s, + txDataWithDefaults, + ); + return txHash; + }, + async estimateGasAsync( + orderAddresses: string[][], + orderValues: BigNumber[][], + fillTakerTokenAmount: BigNumber, + shouldThrowOnInsufficientBalanceOrAllowance: boolean, + v: number|BigNumber[], + r: string[], + s: string[], + txData: TxData = {}, + ): Promise<number> { + const self = this as ExchangeContract; + const txDataWithDefaults = await self.applyDefaultsToTxDataAsync( + txData, + ); + const gas = await promisify<number>( + self.web3ContractInstance.fillOrdersUpTo.estimateGas, self.web3ContractInstance, + )( + orderAddresses, + orderValues, + fillTakerTokenAmount, + shouldThrowOnInsufficientBalanceOrAllowance, + v, + r, + s, + txDataWithDefaults, + ); + return gas; + }, + getABIEncodedTransactionData( + orderAddresses: string[][], + orderValues: BigNumber[][], + fillTakerTokenAmount: BigNumber, + shouldThrowOnInsufficientBalanceOrAllowance: boolean, + v: number|BigNumber[], + r: string[], + s: string[], + txData: TxData = {}, + ): string { + const self = this as ExchangeContract; + const abiEncodedTransactionData = self.web3ContractInstance.fillOrdersUpTo.getData(); + return abiEncodedTransactionData; + }, + }; + public cancelOrder = { + async sendTransactionAsync( + orderAddresses: string[], + orderValues: BigNumber[], + cancelTakerTokenAmount: BigNumber, + txData: TxData = {}, + ): Promise<string> { + const self = this as ExchangeContract; + const txDataWithDefaults = await self.applyDefaultsToTxDataAsync( + txData, + self.cancelOrder.estimateGasAsync.bind( + self, + orderAddresses, + orderValues, + cancelTakerTokenAmount, + ), + ); + const txHash = await promisify<string>( + self.web3ContractInstance.cancelOrder, self.web3ContractInstance, + )( + orderAddresses, + orderValues, + cancelTakerTokenAmount, + txDataWithDefaults, + ); + return txHash; + }, + async estimateGasAsync( + orderAddresses: string[], + orderValues: BigNumber[], + cancelTakerTokenAmount: BigNumber, + txData: TxData = {}, + ): Promise<number> { + const self = this as ExchangeContract; + const txDataWithDefaults = await self.applyDefaultsToTxDataAsync( + txData, + ); + const gas = await promisify<number>( + self.web3ContractInstance.cancelOrder.estimateGas, self.web3ContractInstance, + )( + orderAddresses, + orderValues, + cancelTakerTokenAmount, + txDataWithDefaults, + ); + return gas; + }, + getABIEncodedTransactionData( + orderAddresses: string[], + orderValues: BigNumber[], + cancelTakerTokenAmount: BigNumber, + txData: TxData = {}, + ): string { + const self = this as ExchangeContract; + const abiEncodedTransactionData = self.web3ContractInstance.cancelOrder.getData(); + return abiEncodedTransactionData; + }, + }; + public ZRX_TOKEN_CONTRACT = { + async callAsync( + defaultBlock?: Web3.BlockParam, + ): Promise<string + > { + const self = this as ExchangeContract; + const result = await promisify<string + >( + self.web3ContractInstance.ZRX_TOKEN_CONTRACT.call, + self.web3ContractInstance, + )( + ); + return result; + }, + }; + public batchFillOrKillOrders = { + async sendTransactionAsync( + orderAddresses: string[][], + orderValues: BigNumber[][], + fillTakerTokenAmounts: BigNumber[], + v: number|BigNumber[], + r: string[], + s: string[], + txData: TxData = {}, + ): Promise<string> { + const self = this as ExchangeContract; + const txDataWithDefaults = await self.applyDefaultsToTxDataAsync( + txData, + self.batchFillOrKillOrders.estimateGasAsync.bind( + self, + orderAddresses, + orderValues, + fillTakerTokenAmounts, + v, + r, + s, + ), + ); + const txHash = await promisify<string>( + self.web3ContractInstance.batchFillOrKillOrders, self.web3ContractInstance, + )( + orderAddresses, + orderValues, + fillTakerTokenAmounts, + v, + r, + s, + txDataWithDefaults, + ); + return txHash; + }, + async estimateGasAsync( + orderAddresses: string[][], + orderValues: BigNumber[][], + fillTakerTokenAmounts: BigNumber[], + v: number|BigNumber[], + r: string[], + s: string[], + txData: TxData = {}, + ): Promise<number> { + const self = this as ExchangeContract; + const txDataWithDefaults = await self.applyDefaultsToTxDataAsync( + txData, + ); + const gas = await promisify<number>( + self.web3ContractInstance.batchFillOrKillOrders.estimateGas, self.web3ContractInstance, + )( + orderAddresses, + orderValues, + fillTakerTokenAmounts, + v, + r, + s, + txDataWithDefaults, + ); + return gas; + }, + getABIEncodedTransactionData( + orderAddresses: string[][], + orderValues: BigNumber[][], + fillTakerTokenAmounts: BigNumber[], + v: number|BigNumber[], + r: string[], + s: string[], + txData: TxData = {}, + ): string { + const self = this as ExchangeContract; + const abiEncodedTransactionData = self.web3ContractInstance.batchFillOrKillOrders.getData(); + return abiEncodedTransactionData; + }, + }; + public fillOrKillOrder = { + async sendTransactionAsync( + orderAddresses: string[], + orderValues: BigNumber[], + fillTakerTokenAmount: BigNumber, + v: number|BigNumber, + r: string, + s: string, + txData: TxData = {}, + ): Promise<string> { + const self = this as ExchangeContract; + const txDataWithDefaults = await self.applyDefaultsToTxDataAsync( + txData, + self.fillOrKillOrder.estimateGasAsync.bind( + self, + orderAddresses, + orderValues, + fillTakerTokenAmount, + v, + r, + s, + ), + ); + const txHash = await promisify<string>( + self.web3ContractInstance.fillOrKillOrder, self.web3ContractInstance, + )( + orderAddresses, + orderValues, + fillTakerTokenAmount, + v, + r, + s, + txDataWithDefaults, + ); + return txHash; + }, + async estimateGasAsync( + orderAddresses: string[], + orderValues: BigNumber[], + fillTakerTokenAmount: BigNumber, + v: number|BigNumber, + r: string, + s: string, + txData: TxData = {}, + ): Promise<number> { + const self = this as ExchangeContract; + const txDataWithDefaults = await self.applyDefaultsToTxDataAsync( + txData, + ); + const gas = await promisify<number>( + self.web3ContractInstance.fillOrKillOrder.estimateGas, self.web3ContractInstance, + )( + orderAddresses, + orderValues, + fillTakerTokenAmount, + v, + r, + s, + txDataWithDefaults, + ); + return gas; + }, + getABIEncodedTransactionData( + orderAddresses: string[], + orderValues: BigNumber[], + fillTakerTokenAmount: BigNumber, + v: number|BigNumber, + r: string, + s: string, + txData: TxData = {}, + ): string { + const self = this as ExchangeContract; + const abiEncodedTransactionData = self.web3ContractInstance.fillOrKillOrder.getData(); + return abiEncodedTransactionData; + }, + }; + public getUnavailableTakerTokenAmount = { + async callAsync( + orderHash: string, + defaultBlock?: Web3.BlockParam, + ): Promise<BigNumber + > { + const self = this as ExchangeContract; + const result = await promisify<BigNumber + >( + self.web3ContractInstance.getUnavailableTakerTokenAmount.call, + self.web3ContractInstance, + )( + orderHash, + ); + return result; + }, + }; + public isValidSignature = { + async callAsync( + signer: string, + hash: string, + v: number|BigNumber, + r: string, + s: string, + defaultBlock?: Web3.BlockParam, + ): Promise<boolean + > { + const self = this as ExchangeContract; + const result = await promisify<boolean + >( + self.web3ContractInstance.isValidSignature.call, + self.web3ContractInstance, + )( + signer, + hash, + v, + r, + s, + ); + return result; + }, + }; + public getPartialAmount = { + async callAsync( + numerator: BigNumber, + denominator: BigNumber, + target: BigNumber, + defaultBlock?: Web3.BlockParam, + ): Promise<BigNumber + > { + const self = this as ExchangeContract; + const result = await promisify<BigNumber + >( + self.web3ContractInstance.getPartialAmount.call, + self.web3ContractInstance, + )( + numerator, + denominator, + target, + ); + return result; + }, + }; + public TOKEN_TRANSFER_PROXY_CONTRACT = { + async callAsync( + defaultBlock?: Web3.BlockParam, + ): Promise<string + > { + const self = this as ExchangeContract; + const result = await promisify<string + >( + self.web3ContractInstance.TOKEN_TRANSFER_PROXY_CONTRACT.call, + self.web3ContractInstance, + )( + ); + return result; + }, + }; + public batchFillOrders = { + async sendTransactionAsync( + orderAddresses: string[][], + orderValues: BigNumber[][], + fillTakerTokenAmounts: BigNumber[], + shouldThrowOnInsufficientBalanceOrAllowance: boolean, + v: number|BigNumber[], + r: string[], + s: string[], + txData: TxData = {}, + ): Promise<string> { + const self = this as ExchangeContract; + const txDataWithDefaults = await self.applyDefaultsToTxDataAsync( + txData, + self.batchFillOrders.estimateGasAsync.bind( + self, + orderAddresses, + orderValues, + fillTakerTokenAmounts, + shouldThrowOnInsufficientBalanceOrAllowance, + v, + r, + s, + ), + ); + const txHash = await promisify<string>( + self.web3ContractInstance.batchFillOrders, self.web3ContractInstance, + )( + orderAddresses, + orderValues, + fillTakerTokenAmounts, + shouldThrowOnInsufficientBalanceOrAllowance, + v, + r, + s, + txDataWithDefaults, + ); + return txHash; + }, + async estimateGasAsync( + orderAddresses: string[][], + orderValues: BigNumber[][], + fillTakerTokenAmounts: BigNumber[], + shouldThrowOnInsufficientBalanceOrAllowance: boolean, + v: number|BigNumber[], + r: string[], + s: string[], + txData: TxData = {}, + ): Promise<number> { + const self = this as ExchangeContract; + const txDataWithDefaults = await self.applyDefaultsToTxDataAsync( + txData, + ); + const gas = await promisify<number>( + self.web3ContractInstance.batchFillOrders.estimateGas, self.web3ContractInstance, + )( + orderAddresses, + orderValues, + fillTakerTokenAmounts, + shouldThrowOnInsufficientBalanceOrAllowance, + v, + r, + s, + txDataWithDefaults, + ); + return gas; + }, + getABIEncodedTransactionData( + orderAddresses: string[][], + orderValues: BigNumber[][], + fillTakerTokenAmounts: BigNumber[], + shouldThrowOnInsufficientBalanceOrAllowance: boolean, + v: number|BigNumber[], + r: string[], + s: string[], + txData: TxData = {}, + ): string { + const self = this as ExchangeContract; + const abiEncodedTransactionData = self.web3ContractInstance.batchFillOrders.getData(); + return abiEncodedTransactionData; + }, + }; + public batchCancelOrders = { + async sendTransactionAsync( + orderAddresses: string[][], + orderValues: BigNumber[][], + cancelTakerTokenAmounts: BigNumber[], + txData: TxData = {}, + ): Promise<string> { + const self = this as ExchangeContract; + const txDataWithDefaults = await self.applyDefaultsToTxDataAsync( + txData, + self.batchCancelOrders.estimateGasAsync.bind( + self, + orderAddresses, + orderValues, + cancelTakerTokenAmounts, + ), + ); + const txHash = await promisify<string>( + self.web3ContractInstance.batchCancelOrders, self.web3ContractInstance, + )( + orderAddresses, + orderValues, + cancelTakerTokenAmounts, + txDataWithDefaults, + ); + return txHash; + }, + async estimateGasAsync( + orderAddresses: string[][], + orderValues: BigNumber[][], + cancelTakerTokenAmounts: BigNumber[], + txData: TxData = {}, + ): Promise<number> { + const self = this as ExchangeContract; + const txDataWithDefaults = await self.applyDefaultsToTxDataAsync( + txData, + ); + const gas = await promisify<number>( + self.web3ContractInstance.batchCancelOrders.estimateGas, self.web3ContractInstance, + )( + orderAddresses, + orderValues, + cancelTakerTokenAmounts, + txDataWithDefaults, + ); + return gas; + }, + getABIEncodedTransactionData( + orderAddresses: string[][], + orderValues: BigNumber[][], + cancelTakerTokenAmounts: BigNumber[], + txData: TxData = {}, + ): string { + const self = this as ExchangeContract; + const abiEncodedTransactionData = self.web3ContractInstance.batchCancelOrders.getData(); + return abiEncodedTransactionData; + }, + }; + public fillOrder = { + async sendTransactionAsync( + orderAddresses: string[], + orderValues: BigNumber[], + fillTakerTokenAmount: BigNumber, + shouldThrowOnInsufficientBalanceOrAllowance: boolean, + v: number|BigNumber, + r: string, + s: string, + txData: TxData = {}, + ): Promise<string> { + const self = this as ExchangeContract; + const txDataWithDefaults = await self.applyDefaultsToTxDataAsync( + txData, + self.fillOrder.estimateGasAsync.bind( + self, + orderAddresses, + orderValues, + fillTakerTokenAmount, + shouldThrowOnInsufficientBalanceOrAllowance, + v, + r, + s, + ), + ); + const txHash = await promisify<string>( + self.web3ContractInstance.fillOrder, self.web3ContractInstance, + )( + orderAddresses, + orderValues, + fillTakerTokenAmount, + shouldThrowOnInsufficientBalanceOrAllowance, + v, + r, + s, + txDataWithDefaults, + ); + return txHash; + }, + async estimateGasAsync( + orderAddresses: string[], + orderValues: BigNumber[], + fillTakerTokenAmount: BigNumber, + shouldThrowOnInsufficientBalanceOrAllowance: boolean, + v: number|BigNumber, + r: string, + s: string, + txData: TxData = {}, + ): Promise<number> { + const self = this as ExchangeContract; + const txDataWithDefaults = await self.applyDefaultsToTxDataAsync( + txData, + ); + const gas = await promisify<number>( + self.web3ContractInstance.fillOrder.estimateGas, self.web3ContractInstance, + )( + orderAddresses, + orderValues, + fillTakerTokenAmount, + shouldThrowOnInsufficientBalanceOrAllowance, + v, + r, + s, + txDataWithDefaults, + ); + return gas; + }, + getABIEncodedTransactionData( + orderAddresses: string[], + orderValues: BigNumber[], + fillTakerTokenAmount: BigNumber, + shouldThrowOnInsufficientBalanceOrAllowance: boolean, + v: number|BigNumber, + r: string, + s: string, + txData: TxData = {}, + ): string { + const self = this as ExchangeContract; + const abiEncodedTransactionData = self.web3ContractInstance.fillOrder.getData(); + return abiEncodedTransactionData; + }, + }; + public getOrderHash = { + async callAsync( + orderAddresses: string[], + orderValues: BigNumber[], + defaultBlock?: Web3.BlockParam, + ): Promise<string + > { + const self = this as ExchangeContract; + const result = await promisify<string + >( + self.web3ContractInstance.getOrderHash.call, + self.web3ContractInstance, + )( + orderAddresses, + orderValues, + ); + return result; + }, + }; + public EXTERNAL_QUERY_GAS_LIMIT = { + async callAsync( + defaultBlock?: Web3.BlockParam, + ): Promise<BigNumber + > { + const self = this as ExchangeContract; + const result = await promisify<BigNumber + >( + self.web3ContractInstance.EXTERNAL_QUERY_GAS_LIMIT.call, + self.web3ContractInstance, + )( + ); + return result; + }, + }; + public VERSION = { + async callAsync( + defaultBlock?: Web3.BlockParam, + ): Promise<string + > { + const self = this as ExchangeContract; + const result = await promisify<string + >( + self.web3ContractInstance.VERSION.call, + self.web3ContractInstance, + )( + ); + return result; + }, + }; + constructor(web3ContractInstance: Web3.ContractInstance, defaults: Partial<TxData>) { + super(web3ContractInstance, defaults); + classUtils.bindAll(this, ['web3ContractInstance', 'defaults']); + } +} // tslint:disable:max-file-line-count diff --git a/packages/0x.js/src/contract_wrappers/generated/token.ts b/packages/0x.js/src/contract_wrappers/generated/token.ts new file mode 100644 index 000000000..30b06292f --- /dev/null +++ b/packages/0x.js/src/contract_wrappers/generated/token.ts @@ -0,0 +1,232 @@ +/** + * This file is auto-generated using abi-gen. Don't edit directly. + * Templates can be found at https://github.com/0xProject/0x.js/tree/development/packages/abi-gen-templates. + */ +import {BigNumber} from 'bignumber.js'; +import * as Web3 from 'web3'; + +import {TxData, TxDataPayable} from '../../types'; +import {classUtils} from '../../utils/class_utils'; +import {promisify} from '../../utils/promisify'; + +import {BaseContract} from './base_contract'; + +export class TokenContract extends BaseContract { + public approve = { + async sendTransactionAsync( + _spender: string, + _value: BigNumber, + txData: TxData = {}, + ): Promise<string> { + const self = this as TokenContract; + const txDataWithDefaults = await self.applyDefaultsToTxDataAsync( + txData, + self.approve.estimateGasAsync.bind( + self, + _spender, + _value, + ), + ); + const txHash = await promisify<string>( + self.web3ContractInstance.approve, self.web3ContractInstance, + )( + _spender, + _value, + txDataWithDefaults, + ); + return txHash; + }, + async estimateGasAsync( + _spender: string, + _value: BigNumber, + txData: TxData = {}, + ): Promise<number> { + const self = this as TokenContract; + const txDataWithDefaults = await self.applyDefaultsToTxDataAsync( + txData, + ); + const gas = await promisify<number>( + self.web3ContractInstance.approve.estimateGas, self.web3ContractInstance, + )( + _spender, + _value, + txDataWithDefaults, + ); + return gas; + }, + getABIEncodedTransactionData( + _spender: string, + _value: BigNumber, + txData: TxData = {}, + ): string { + const self = this as TokenContract; + const abiEncodedTransactionData = self.web3ContractInstance.approve.getData(); + return abiEncodedTransactionData; + }, + }; + public totalSupply = { + async callAsync( + defaultBlock?: Web3.BlockParam, + ): Promise<BigNumber + > { + const self = this as TokenContract; + const result = await promisify<BigNumber + >( + self.web3ContractInstance.totalSupply.call, + self.web3ContractInstance, + )( + ); + return result; + }, + }; + public transferFrom = { + async sendTransactionAsync( + _from: string, + _to: string, + _value: BigNumber, + txData: TxData = {}, + ): Promise<string> { + const self = this as TokenContract; + const txDataWithDefaults = await self.applyDefaultsToTxDataAsync( + txData, + self.transferFrom.estimateGasAsync.bind( + self, + _from, + _to, + _value, + ), + ); + const txHash = await promisify<string>( + self.web3ContractInstance.transferFrom, self.web3ContractInstance, + )( + _from, + _to, + _value, + txDataWithDefaults, + ); + return txHash; + }, + async estimateGasAsync( + _from: string, + _to: string, + _value: BigNumber, + txData: TxData = {}, + ): Promise<number> { + const self = this as TokenContract; + const txDataWithDefaults = await self.applyDefaultsToTxDataAsync( + txData, + ); + const gas = await promisify<number>( + self.web3ContractInstance.transferFrom.estimateGas, self.web3ContractInstance, + )( + _from, + _to, + _value, + txDataWithDefaults, + ); + return gas; + }, + getABIEncodedTransactionData( + _from: string, + _to: string, + _value: BigNumber, + txData: TxData = {}, + ): string { + const self = this as TokenContract; + const abiEncodedTransactionData = self.web3ContractInstance.transferFrom.getData(); + return abiEncodedTransactionData; + }, + }; + public balanceOf = { + async callAsync( + _owner: string, + defaultBlock?: Web3.BlockParam, + ): Promise<BigNumber + > { + const self = this as TokenContract; + const result = await promisify<BigNumber + >( + self.web3ContractInstance.balanceOf.call, + self.web3ContractInstance, + )( + _owner, + ); + return result; + }, + }; + public transfer = { + async sendTransactionAsync( + _to: string, + _value: BigNumber, + txData: TxData = {}, + ): Promise<string> { + const self = this as TokenContract; + const txDataWithDefaults = await self.applyDefaultsToTxDataAsync( + txData, + self.transfer.estimateGasAsync.bind( + self, + _to, + _value, + ), + ); + const txHash = await promisify<string>( + self.web3ContractInstance.transfer, self.web3ContractInstance, + )( + _to, + _value, + txDataWithDefaults, + ); + return txHash; + }, + async estimateGasAsync( + _to: string, + _value: BigNumber, + txData: TxData = {}, + ): Promise<number> { + const self = this as TokenContract; + const txDataWithDefaults = await self.applyDefaultsToTxDataAsync( + txData, + ); + const gas = await promisify<number>( + self.web3ContractInstance.transfer.estimateGas, self.web3ContractInstance, + )( + _to, + _value, + txDataWithDefaults, + ); + return gas; + }, + getABIEncodedTransactionData( + _to: string, + _value: BigNumber, + txData: TxData = {}, + ): string { + const self = this as TokenContract; + const abiEncodedTransactionData = self.web3ContractInstance.transfer.getData(); + return abiEncodedTransactionData; + }, + }; + public allowance = { + async callAsync( + _owner: string, + _spender: string, + defaultBlock?: Web3.BlockParam, + ): Promise<BigNumber + > { + const self = this as TokenContract; + const result = await promisify<BigNumber + >( + self.web3ContractInstance.allowance.call, + self.web3ContractInstance, + )( + _owner, + _spender, + ); + return result; + }, + }; + constructor(web3ContractInstance: Web3.ContractInstance, defaults: Partial<TxData>) { + super(web3ContractInstance, defaults); + classUtils.bindAll(this, ['web3ContractInstance', 'defaults']); + } +} // tslint:disable:max-file-line-count diff --git a/packages/0x.js/src/contract_wrappers/generated/token_registry.ts b/packages/0x.js/src/contract_wrappers/generated/token_registry.ts new file mode 100644 index 000000000..6aacc4336 --- /dev/null +++ b/packages/0x.js/src/contract_wrappers/generated/token_registry.ts @@ -0,0 +1,550 @@ +/** + * This file is auto-generated using abi-gen. Don't edit directly. + * Templates can be found at https://github.com/0xProject/0x.js/tree/development/packages/abi-gen-templates. + */ +import {BigNumber} from 'bignumber.js'; +import * as Web3 from 'web3'; + +import {TxData, TxDataPayable} from '../../types'; +import {classUtils} from '../../utils/class_utils'; +import {promisify} from '../../utils/promisify'; + +import {BaseContract} from './base_contract'; + +export class TokenRegistryContract extends BaseContract { + public removeToken = { + async sendTransactionAsync( + _token: string, + _index: BigNumber, + txData: TxData = {}, + ): Promise<string> { + const self = this as TokenRegistryContract; + const txDataWithDefaults = await self.applyDefaultsToTxDataAsync( + txData, + self.removeToken.estimateGasAsync.bind( + self, + _token, + _index, + ), + ); + const txHash = await promisify<string>( + self.web3ContractInstance.removeToken, self.web3ContractInstance, + )( + _token, + _index, + txDataWithDefaults, + ); + return txHash; + }, + async estimateGasAsync( + _token: string, + _index: BigNumber, + txData: TxData = {}, + ): Promise<number> { + const self = this as TokenRegistryContract; + const txDataWithDefaults = await self.applyDefaultsToTxDataAsync( + txData, + ); + const gas = await promisify<number>( + self.web3ContractInstance.removeToken.estimateGas, self.web3ContractInstance, + )( + _token, + _index, + txDataWithDefaults, + ); + return gas; + }, + getABIEncodedTransactionData( + _token: string, + _index: BigNumber, + txData: TxData = {}, + ): string { + const self = this as TokenRegistryContract; + const abiEncodedTransactionData = self.web3ContractInstance.removeToken.getData(); + return abiEncodedTransactionData; + }, + }; + public getTokenAddressByName = { + async callAsync( + _name: string, + defaultBlock?: Web3.BlockParam, + ): Promise<string + > { + const self = this as TokenRegistryContract; + const result = await promisify<string + >( + self.web3ContractInstance.getTokenAddressByName.call, + self.web3ContractInstance, + )( + _name, + ); + return result; + }, + }; + public getTokenAddressBySymbol = { + async callAsync( + _symbol: string, + defaultBlock?: Web3.BlockParam, + ): Promise<string + > { + const self = this as TokenRegistryContract; + const result = await promisify<string + >( + self.web3ContractInstance.getTokenAddressBySymbol.call, + self.web3ContractInstance, + )( + _symbol, + ); + return result; + }, + }; + public setTokenSwarmHash = { + async sendTransactionAsync( + _token: string, + _swarmHash: string, + txData: TxData = {}, + ): Promise<string> { + const self = this as TokenRegistryContract; + const txDataWithDefaults = await self.applyDefaultsToTxDataAsync( + txData, + self.setTokenSwarmHash.estimateGasAsync.bind( + self, + _token, + _swarmHash, + ), + ); + const txHash = await promisify<string>( + self.web3ContractInstance.setTokenSwarmHash, self.web3ContractInstance, + )( + _token, + _swarmHash, + txDataWithDefaults, + ); + return txHash; + }, + async estimateGasAsync( + _token: string, + _swarmHash: string, + txData: TxData = {}, + ): Promise<number> { + const self = this as TokenRegistryContract; + const txDataWithDefaults = await self.applyDefaultsToTxDataAsync( + txData, + ); + const gas = await promisify<number>( + self.web3ContractInstance.setTokenSwarmHash.estimateGas, self.web3ContractInstance, + )( + _token, + _swarmHash, + txDataWithDefaults, + ); + return gas; + }, + getABIEncodedTransactionData( + _token: string, + _swarmHash: string, + txData: TxData = {}, + ): string { + const self = this as TokenRegistryContract; + const abiEncodedTransactionData = self.web3ContractInstance.setTokenSwarmHash.getData(); + return abiEncodedTransactionData; + }, + }; + public getTokenMetaData = { + async callAsync( + _token: string, + defaultBlock?: Web3.BlockParam, + ): Promise<[string, string, string, BigNumber, string, string] + > { + const self = this as TokenRegistryContract; + const result = await promisify<[string, string, string, BigNumber, string, string] + >( + self.web3ContractInstance.getTokenMetaData.call, + self.web3ContractInstance, + )( + _token, + ); + return result; + }, + }; + public owner = { + async callAsync( + defaultBlock?: Web3.BlockParam, + ): Promise<string + > { + const self = this as TokenRegistryContract; + const result = await promisify<string + >( + self.web3ContractInstance.owner.call, + self.web3ContractInstance, + )( + ); + return result; + }, + }; + public addToken = { + async sendTransactionAsync( + _token: string, + _name: string, + _symbol: string, + _decimals: number|BigNumber, + _ipfsHash: string, + _swarmHash: string, + txData: TxData = {}, + ): Promise<string> { + const self = this as TokenRegistryContract; + const txDataWithDefaults = await self.applyDefaultsToTxDataAsync( + txData, + self.addToken.estimateGasAsync.bind( + self, + _token, + _name, + _symbol, + _decimals, + _ipfsHash, + _swarmHash, + ), + ); + const txHash = await promisify<string>( + self.web3ContractInstance.addToken, self.web3ContractInstance, + )( + _token, + _name, + _symbol, + _decimals, + _ipfsHash, + _swarmHash, + txDataWithDefaults, + ); + return txHash; + }, + async estimateGasAsync( + _token: string, + _name: string, + _symbol: string, + _decimals: number|BigNumber, + _ipfsHash: string, + _swarmHash: string, + txData: TxData = {}, + ): Promise<number> { + const self = this as TokenRegistryContract; + const txDataWithDefaults = await self.applyDefaultsToTxDataAsync( + txData, + ); + const gas = await promisify<number>( + self.web3ContractInstance.addToken.estimateGas, self.web3ContractInstance, + )( + _token, + _name, + _symbol, + _decimals, + _ipfsHash, + _swarmHash, + txDataWithDefaults, + ); + return gas; + }, + getABIEncodedTransactionData( + _token: string, + _name: string, + _symbol: string, + _decimals: number|BigNumber, + _ipfsHash: string, + _swarmHash: string, + txData: TxData = {}, + ): string { + const self = this as TokenRegistryContract; + const abiEncodedTransactionData = self.web3ContractInstance.addToken.getData(); + return abiEncodedTransactionData; + }, + }; + public setTokenName = { + async sendTransactionAsync( + _token: string, + _name: string, + txData: TxData = {}, + ): Promise<string> { + const self = this as TokenRegistryContract; + const txDataWithDefaults = await self.applyDefaultsToTxDataAsync( + txData, + self.setTokenName.estimateGasAsync.bind( + self, + _token, + _name, + ), + ); + const txHash = await promisify<string>( + self.web3ContractInstance.setTokenName, self.web3ContractInstance, + )( + _token, + _name, + txDataWithDefaults, + ); + return txHash; + }, + async estimateGasAsync( + _token: string, + _name: string, + txData: TxData = {}, + ): Promise<number> { + const self = this as TokenRegistryContract; + const txDataWithDefaults = await self.applyDefaultsToTxDataAsync( + txData, + ); + const gas = await promisify<number>( + self.web3ContractInstance.setTokenName.estimateGas, self.web3ContractInstance, + )( + _token, + _name, + txDataWithDefaults, + ); + return gas; + }, + getABIEncodedTransactionData( + _token: string, + _name: string, + txData: TxData = {}, + ): string { + const self = this as TokenRegistryContract; + const abiEncodedTransactionData = self.web3ContractInstance.setTokenName.getData(); + return abiEncodedTransactionData; + }, + }; + public tokens = { + async callAsync( + index: string, + defaultBlock?: Web3.BlockParam, + ): Promise<[string, string, string, BigNumber, string, string] + > { + const self = this as TokenRegistryContract; + const result = await promisify<[string, string, string, BigNumber, string, string] + >( + self.web3ContractInstance.tokens.call, + self.web3ContractInstance, + )( + index, + ); + return result; + }, + }; + public tokenAddresses = { + async callAsync( + index: BigNumber, + defaultBlock?: Web3.BlockParam, + ): Promise<string + > { + const self = this as TokenRegistryContract; + const result = await promisify<string + >( + self.web3ContractInstance.tokenAddresses.call, + self.web3ContractInstance, + )( + index, + ); + return result; + }, + }; + public getTokenByName = { + async callAsync( + _name: string, + defaultBlock?: Web3.BlockParam, + ): Promise<[string, string, string, BigNumber, string, string] + > { + const self = this as TokenRegistryContract; + const result = await promisify<[string, string, string, BigNumber, string, string] + >( + self.web3ContractInstance.getTokenByName.call, + self.web3ContractInstance, + )( + _name, + ); + return result; + }, + }; + public getTokenAddresses = { + async callAsync( + defaultBlock?: Web3.BlockParam, + ): Promise<string[] + > { + const self = this as TokenRegistryContract; + const result = await promisify<string[] + >( + self.web3ContractInstance.getTokenAddresses.call, + self.web3ContractInstance, + )( + ); + return result; + }, + }; + public setTokenIpfsHash = { + async sendTransactionAsync( + _token: string, + _ipfsHash: string, + txData: TxData = {}, + ): Promise<string> { + const self = this as TokenRegistryContract; + const txDataWithDefaults = await self.applyDefaultsToTxDataAsync( + txData, + self.setTokenIpfsHash.estimateGasAsync.bind( + self, + _token, + _ipfsHash, + ), + ); + const txHash = await promisify<string>( + self.web3ContractInstance.setTokenIpfsHash, self.web3ContractInstance, + )( + _token, + _ipfsHash, + txDataWithDefaults, + ); + return txHash; + }, + async estimateGasAsync( + _token: string, + _ipfsHash: string, + txData: TxData = {}, + ): Promise<number> { + const self = this as TokenRegistryContract; + const txDataWithDefaults = await self.applyDefaultsToTxDataAsync( + txData, + ); + const gas = await promisify<number>( + self.web3ContractInstance.setTokenIpfsHash.estimateGas, self.web3ContractInstance, + )( + _token, + _ipfsHash, + txDataWithDefaults, + ); + return gas; + }, + getABIEncodedTransactionData( + _token: string, + _ipfsHash: string, + txData: TxData = {}, + ): string { + const self = this as TokenRegistryContract; + const abiEncodedTransactionData = self.web3ContractInstance.setTokenIpfsHash.getData(); + return abiEncodedTransactionData; + }, + }; + public getTokenBySymbol = { + async callAsync( + _symbol: string, + defaultBlock?: Web3.BlockParam, + ): Promise<[string, string, string, BigNumber, string, string] + > { + const self = this as TokenRegistryContract; + const result = await promisify<[string, string, string, BigNumber, string, string] + >( + self.web3ContractInstance.getTokenBySymbol.call, + self.web3ContractInstance, + )( + _symbol, + ); + return result; + }, + }; + public setTokenSymbol = { + async sendTransactionAsync( + _token: string, + _symbol: string, + txData: TxData = {}, + ): Promise<string> { + const self = this as TokenRegistryContract; + const txDataWithDefaults = await self.applyDefaultsToTxDataAsync( + txData, + self.setTokenSymbol.estimateGasAsync.bind( + self, + _token, + _symbol, + ), + ); + const txHash = await promisify<string>( + self.web3ContractInstance.setTokenSymbol, self.web3ContractInstance, + )( + _token, + _symbol, + txDataWithDefaults, + ); + return txHash; + }, + async estimateGasAsync( + _token: string, + _symbol: string, + txData: TxData = {}, + ): Promise<number> { + const self = this as TokenRegistryContract; + const txDataWithDefaults = await self.applyDefaultsToTxDataAsync( + txData, + ); + const gas = await promisify<number>( + self.web3ContractInstance.setTokenSymbol.estimateGas, self.web3ContractInstance, + )( + _token, + _symbol, + txDataWithDefaults, + ); + return gas; + }, + getABIEncodedTransactionData( + _token: string, + _symbol: string, + txData: TxData = {}, + ): string { + const self = this as TokenRegistryContract; + const abiEncodedTransactionData = self.web3ContractInstance.setTokenSymbol.getData(); + return abiEncodedTransactionData; + }, + }; + public transferOwnership = { + async sendTransactionAsync( + newOwner: string, + txData: TxData = {}, + ): Promise<string> { + const self = this as TokenRegistryContract; + const txDataWithDefaults = await self.applyDefaultsToTxDataAsync( + txData, + self.transferOwnership.estimateGasAsync.bind( + self, + newOwner, + ), + ); + const txHash = await promisify<string>( + self.web3ContractInstance.transferOwnership, self.web3ContractInstance, + )( + newOwner, + txDataWithDefaults, + ); + return txHash; + }, + async estimateGasAsync( + newOwner: string, + txData: TxData = {}, + ): Promise<number> { + const self = this as TokenRegistryContract; + const txDataWithDefaults = await self.applyDefaultsToTxDataAsync( + txData, + ); + const gas = await promisify<number>( + self.web3ContractInstance.transferOwnership.estimateGas, self.web3ContractInstance, + )( + newOwner, + txDataWithDefaults, + ); + return gas; + }, + getABIEncodedTransactionData( + newOwner: string, + txData: TxData = {}, + ): string { + const self = this as TokenRegistryContract; + const abiEncodedTransactionData = self.web3ContractInstance.transferOwnership.getData(); + return abiEncodedTransactionData; + }, + }; + constructor(web3ContractInstance: Web3.ContractInstance, defaults: Partial<TxData>) { + super(web3ContractInstance, defaults); + classUtils.bindAll(this, ['web3ContractInstance', 'defaults']); + } +} // tslint:disable:max-file-line-count diff --git a/packages/0x.js/src/contract_wrappers/generated/token_transfer_proxy.ts b/packages/0x.js/src/contract_wrappers/generated/token_transfer_proxy.ts new file mode 100644 index 000000000..50f1c8f25 --- /dev/null +++ b/packages/0x.js/src/contract_wrappers/generated/token_transfer_proxy.ts @@ -0,0 +1,285 @@ +/** + * This file is auto-generated using abi-gen. Don't edit directly. + * Templates can be found at https://github.com/0xProject/0x.js/tree/development/packages/abi-gen-templates. + */ +import {BigNumber} from 'bignumber.js'; +import * as Web3 from 'web3'; + +import {TxData, TxDataPayable} from '../../types'; +import {classUtils} from '../../utils/class_utils'; +import {promisify} from '../../utils/promisify'; + +import {BaseContract} from './base_contract'; + +export class TokenTransferProxyContract extends BaseContract { + public transferFrom = { + async sendTransactionAsync( + token: string, + from: string, + to: string, + value: BigNumber, + txData: TxData = {}, + ): Promise<string> { + const self = this as TokenTransferProxyContract; + const txDataWithDefaults = await self.applyDefaultsToTxDataAsync( + txData, + self.transferFrom.estimateGasAsync.bind( + self, + token, + from, + to, + value, + ), + ); + const txHash = await promisify<string>( + self.web3ContractInstance.transferFrom, self.web3ContractInstance, + )( + token, + from, + to, + value, + txDataWithDefaults, + ); + return txHash; + }, + async estimateGasAsync( + token: string, + from: string, + to: string, + value: BigNumber, + txData: TxData = {}, + ): Promise<number> { + const self = this as TokenTransferProxyContract; + const txDataWithDefaults = await self.applyDefaultsToTxDataAsync( + txData, + ); + const gas = await promisify<number>( + self.web3ContractInstance.transferFrom.estimateGas, self.web3ContractInstance, + )( + token, + from, + to, + value, + txDataWithDefaults, + ); + return gas; + }, + getABIEncodedTransactionData( + token: string, + from: string, + to: string, + value: BigNumber, + txData: TxData = {}, + ): string { + const self = this as TokenTransferProxyContract; + const abiEncodedTransactionData = self.web3ContractInstance.transferFrom.getData(); + return abiEncodedTransactionData; + }, + }; + public addAuthorizedAddress = { + async sendTransactionAsync( + target: string, + txData: TxData = {}, + ): Promise<string> { + const self = this as TokenTransferProxyContract; + const txDataWithDefaults = await self.applyDefaultsToTxDataAsync( + txData, + self.addAuthorizedAddress.estimateGasAsync.bind( + self, + target, + ), + ); + const txHash = await promisify<string>( + self.web3ContractInstance.addAuthorizedAddress, self.web3ContractInstance, + )( + target, + txDataWithDefaults, + ); + return txHash; + }, + async estimateGasAsync( + target: string, + txData: TxData = {}, + ): Promise<number> { + const self = this as TokenTransferProxyContract; + const txDataWithDefaults = await self.applyDefaultsToTxDataAsync( + txData, + ); + const gas = await promisify<number>( + self.web3ContractInstance.addAuthorizedAddress.estimateGas, self.web3ContractInstance, + )( + target, + txDataWithDefaults, + ); + return gas; + }, + getABIEncodedTransactionData( + target: string, + txData: TxData = {}, + ): string { + const self = this as TokenTransferProxyContract; + const abiEncodedTransactionData = self.web3ContractInstance.addAuthorizedAddress.getData(); + return abiEncodedTransactionData; + }, + }; + public authorities = { + async callAsync( + index: BigNumber, + defaultBlock?: Web3.BlockParam, + ): Promise<string + > { + const self = this as TokenTransferProxyContract; + const result = await promisify<string + >( + self.web3ContractInstance.authorities.call, + self.web3ContractInstance, + )( + index, + ); + return result; + }, + }; + public removeAuthorizedAddress = { + async sendTransactionAsync( + target: string, + txData: TxData = {}, + ): Promise<string> { + const self = this as TokenTransferProxyContract; + const txDataWithDefaults = await self.applyDefaultsToTxDataAsync( + txData, + self.removeAuthorizedAddress.estimateGasAsync.bind( + self, + target, + ), + ); + const txHash = await promisify<string>( + self.web3ContractInstance.removeAuthorizedAddress, self.web3ContractInstance, + )( + target, + txDataWithDefaults, + ); + return txHash; + }, + async estimateGasAsync( + target: string, + txData: TxData = {}, + ): Promise<number> { + const self = this as TokenTransferProxyContract; + const txDataWithDefaults = await self.applyDefaultsToTxDataAsync( + txData, + ); + const gas = await promisify<number>( + self.web3ContractInstance.removeAuthorizedAddress.estimateGas, self.web3ContractInstance, + )( + target, + txDataWithDefaults, + ); + return gas; + }, + getABIEncodedTransactionData( + target: string, + txData: TxData = {}, + ): string { + const self = this as TokenTransferProxyContract; + const abiEncodedTransactionData = self.web3ContractInstance.removeAuthorizedAddress.getData(); + return abiEncodedTransactionData; + }, + }; + public owner = { + async callAsync( + defaultBlock?: Web3.BlockParam, + ): Promise<string + > { + const self = this as TokenTransferProxyContract; + const result = await promisify<string + >( + self.web3ContractInstance.owner.call, + self.web3ContractInstance, + )( + ); + return result; + }, + }; + public authorized = { + async callAsync( + index: string, + defaultBlock?: Web3.BlockParam, + ): Promise<boolean + > { + const self = this as TokenTransferProxyContract; + const result = await promisify<boolean + >( + self.web3ContractInstance.authorized.call, + self.web3ContractInstance, + )( + index, + ); + return result; + }, + }; + public getAuthorizedAddresses = { + async callAsync( + defaultBlock?: Web3.BlockParam, + ): Promise<string[] + > { + const self = this as TokenTransferProxyContract; + const result = await promisify<string[] + >( + self.web3ContractInstance.getAuthorizedAddresses.call, + self.web3ContractInstance, + )( + ); + return result; + }, + }; + public transferOwnership = { + async sendTransactionAsync( + newOwner: string, + txData: TxData = {}, + ): Promise<string> { + const self = this as TokenTransferProxyContract; + const txDataWithDefaults = await self.applyDefaultsToTxDataAsync( + txData, + self.transferOwnership.estimateGasAsync.bind( + self, + newOwner, + ), + ); + const txHash = await promisify<string>( + self.web3ContractInstance.transferOwnership, self.web3ContractInstance, + )( + newOwner, + txDataWithDefaults, + ); + return txHash; + }, + async estimateGasAsync( + newOwner: string, + txData: TxData = {}, + ): Promise<number> { + const self = this as TokenTransferProxyContract; + const txDataWithDefaults = await self.applyDefaultsToTxDataAsync( + txData, + ); + const gas = await promisify<number>( + self.web3ContractInstance.transferOwnership.estimateGas, self.web3ContractInstance, + )( + newOwner, + txDataWithDefaults, + ); + return gas; + }, + getABIEncodedTransactionData( + newOwner: string, + txData: TxData = {}, + ): string { + const self = this as TokenTransferProxyContract; + const abiEncodedTransactionData = self.web3ContractInstance.transferOwnership.getData(); + return abiEncodedTransactionData; + }, + }; + constructor(web3ContractInstance: Web3.ContractInstance, defaults: Partial<TxData>) { + super(web3ContractInstance, defaults); + classUtils.bindAll(this, ['web3ContractInstance', 'defaults']); + } +} // tslint:disable:max-file-line-count diff --git a/packages/0x.js/src/contract_wrappers/token_registry_wrapper.ts b/packages/0x.js/src/contract_wrappers/token_registry_wrapper.ts index 35337fa35..27ecb8bde 100644 --- a/packages/0x.js/src/contract_wrappers/token_registry_wrapper.ts +++ b/packages/0x.js/src/contract_wrappers/token_registry_wrapper.ts @@ -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; } diff --git a/packages/0x.js/src/contract_wrappers/token_transfer_proxy_wrapper.ts b/packages/0x.js/src/contract_wrappers/token_transfer_proxy_wrapper.ts index c3df7d3eb..edc702672 100644 --- a/packages/0x.js/src/contract_wrappers/token_transfer_proxy_wrapper.ts +++ b/packages/0x.js/src/contract_wrappers/token_transfer_proxy_wrapper.ts @@ -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; } diff --git a/packages/0x.js/src/contract_wrappers/token_wrapper.ts b/packages/0x.js/src/contract_wrappers/token_wrapper.ts index 4a1dfcf8d..630ab6e3b 100644 --- a/packages/0x.js/src/contract_wrappers/token_wrapper.ts +++ b/packages/0x.js/src/contract_wrappers/token_wrapper.ts @@ -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; diff --git a/packages/0x.js/src/globals.d.ts b/packages/0x.js/src/globals.d.ts index cb3800056..4fa1cfd9c 100644 --- a/packages/0x.js/src/globals.d.ts +++ b/packages/0x.js/src/globals.d.ts @@ -39,12 +39,6 @@ declare module 'compare-versions' { export = compareVersions; } -// es6-promisify declarations -declare function promisify(original: any, settings?: any): ((...arg: any[]) => Promise<any>); -declare module 'es6-promisify' { - export = promisify; -} - declare module 'ethereumjs-abi' { const soliditySHA3: (argTypes: string[], args: any[]) => Buffer; } diff --git a/packages/0x.js/src/types.ts b/packages/0x.js/src/types.ts index 5363b02ff..3586919cb 100644 --- a/packages/0x.js/src/types.ts +++ b/packages/0x.js/src/types.ts @@ -52,156 +52,6 @@ export interface DecodedLogEvent<ArgsType> { export type EventCallback<ArgsType> = (err: null|Error, log?: DecodedLogEvent<ArgsType>) => void; export type EventWatcherCallback = (log: LogEvent) => void; -export interface ExchangeContract extends Web3.ContractInstance { - isValidSignature: { - callAsync: (signerAddressHex: string, dataHex: string, v: number, r: string, s: string, - txOpts?: TxOpts) => Promise<boolean>; - }; - ZRX_TOKEN_CONTRACT: { - callAsync: () => Promise<string>; - }; - TOKEN_TRANSFER_PROXY_CONTRACT: { - callAsync: () => Promise<string>; - }; - getUnavailableTakerTokenAmount: { - callAsync: (orderHash: string, defaultBlock?: Web3.BlockParam) => Promise<BigNumber>; - }; - isRoundingError: { - callAsync: (takerTokenFillAmount: BigNumber, takerTokenAmount: BigNumber, - makerTokenAmount: BigNumber, txOpts?: TxOpts) => Promise<boolean>; - }; - fillOrder: { - sendTransactionAsync: (orderAddresses: OrderAddresses, orderValues: OrderValues, - fillTakerTokenAmount: BigNumber, - shouldThrowOnInsufficientBalanceOrAllowance: boolean, - v: number, r: string, s: string, txOpts?: TxOpts) => Promise<string>; - estimateGasAsync: (orderAddresses: OrderAddresses, orderValues: OrderValues, - fillTakerTokenAmount: BigNumber, - shouldThrowOnInsufficientBalanceOrAllowance: boolean, - v: number, r: string, s: string, txOpts?: TxOpts) => Promise<number>; - }; - batchFillOrders: { - sendTransactionAsync: (orderAddresses: OrderAddresses[], orderValues: OrderValues[], - fillTakerTokenAmounts: BigNumber[], - shouldThrowOnInsufficientBalanceOrAllowance: boolean, - v: number[], r: string[], s: string[], txOpts?: TxOpts) => Promise<string>; - estimateGasAsync: (orderAddresses: OrderAddresses[], orderValues: OrderValues[], - fillTakerTokenAmounts: BigNumber[], - shouldThrowOnInsufficientBalanceOrAllowance: boolean, - v: number[], r: string[], s: string[], txOpts?: TxOpts) => Promise<number>; - }; - fillOrdersUpTo: { - sendTransactionAsync: (orderAddresses: OrderAddresses[], orderValues: OrderValues[], - fillTakerTokenAmount: BigNumber, - shouldThrowOnInsufficientBalanceOrAllowance: boolean, - v: number[], r: string[], s: string[], txOpts?: TxOpts) => Promise<string>; - estimateGasAsync: (orderAddresses: OrderAddresses[], orderValues: OrderValues[], - fillTakerTokenAmount: BigNumber, - shouldThrowOnInsufficientBalanceOrAllowance: boolean, - v: number[], r: string[], s: string[], txOpts?: TxOpts) => Promise<number>; - }; - cancelOrder: { - sendTransactionAsync: (orderAddresses: OrderAddresses, orderValues: OrderValues, - cancelTakerTokenAmount: BigNumber, txOpts?: TxOpts) => Promise<string>; - estimateGasAsync: (orderAddresses: OrderAddresses, orderValues: OrderValues, - cancelTakerTokenAmount: BigNumber, - txOpts?: TxOpts) => Promise<number>; - }; - batchCancelOrders: { - sendTransactionAsync: (orderAddresses: OrderAddresses[], orderValues: OrderValues[], - cancelTakerTokenAmounts: BigNumber[], txOpts?: TxOpts) => Promise<string>; - estimateGasAsync: (orderAddresses: OrderAddresses[], orderValues: OrderValues[], - cancelTakerTokenAmounts: BigNumber[], - txOpts?: TxOpts) => Promise<number>; - }; - fillOrKillOrder: { - sendTransactionAsync: (orderAddresses: OrderAddresses, orderValues: OrderValues, - fillTakerTokenAmount: BigNumber, - v: number, r: string, s: string, txOpts?: TxOpts) => Promise<string>; - estimateGasAsync: (orderAddresses: OrderAddresses, orderValues: OrderValues, - fillTakerTokenAmount: BigNumber, - v: number, r: string, s: string, txOpts?: TxOpts) => Promise<number>; - }; - batchFillOrKillOrders: { - sendTransactionAsync: (orderAddresses: OrderAddresses[], orderValues: OrderValues[], - fillTakerTokenAmounts: BigNumber[], - v: number[], r: string[], s: string[], txOpts: TxOpts) => Promise<string>; - estimateGasAsync: (orderAddresses: OrderAddresses[], orderValues: OrderValues[], - fillTakerTokenAmounts: BigNumber[], - v: number[], r: string[], s: string[], txOpts?: TxOpts) => Promise<number>; - }; - filled: { - callAsync: (orderHash: string, defaultBlock?: Web3.BlockParam) => Promise<BigNumber>; - }; - cancelled: { - callAsync: (orderHash: string, defaultBlock?: Web3.BlockParam) => Promise<BigNumber>; - }; - getOrderHash: { - callAsync: (orderAddresses: OrderAddresses, orderValues: OrderValues) => Promise<string>; - }; -} - -export interface TokenContract extends Web3.ContractInstance { - balanceOf: { - callAsync: (address: string, defaultBlock?: Web3.BlockParam) => Promise<BigNumber>; - }; - allowance: { - callAsync: (ownerAddress: string, allowedAddress: string, - defaultBlock?: Web3.BlockParam) => Promise<BigNumber>; - }; - transfer: { - sendTransactionAsync: (toAddress: string, amountInBaseUnits: BigNumber, - txOpts?: TxOpts) => Promise<string>; - }; - transferFrom: { - sendTransactionAsync: (fromAddress: string, toAddress: string, amountInBaseUnits: BigNumber, - txOpts?: TxOpts) => Promise<string>; - }; - approve: { - sendTransactionAsync: (proxyAddress: string, amountInBaseUnits: BigNumber, - txOpts?: TxOpts) => Promise<string>; - }; -} - -export interface TokenRegistryContract extends Web3.ContractInstance { - getTokenMetaData: { - callAsync: (address: string) => Promise<TokenMetadata>; - }; - getTokenAddresses: { - callAsync: () => Promise<string[]>; - }; - getTokenAddressBySymbol: { - callAsync: (symbol: string) => Promise<string>; - }; - getTokenAddressByName: { - callAsync: (name: string) => Promise<string>; - }; - getTokenBySymbol: { - callAsync: (symbol: string) => Promise<TokenMetadata>; - }; - getTokenByName: { - callAsync: (name: string) => Promise<TokenMetadata>; - }; -} - -export interface EtherTokenContract extends Web3.ContractInstance { - deposit: { - sendTransactionAsync: (txOpts: TxOpts) => Promise<string>; - }; - withdraw: { - sendTransactionAsync: (amount: BigNumber, txOpts: TxOpts) => Promise<string>; - }; -} - -export interface TokenTransferProxyContract extends Web3.ContractInstance { - getAuthorizedAddresses: { - callAsync: () => Promise<string[]>; - }; - authorized: { - callAsync: (address: string) => Promise<boolean>; - }; -} - export enum SolidityTypes { Address = 'address', Uint256 = 'uint256', @@ -393,10 +243,6 @@ export type AsyncMethod = (...args: any[]) => Promise<any>; */ export type Web3Provider = Web3.Provider; -export interface ExchangeContractByAddress { - [address: string]: ExchangeContract; -} - export interface JSONRPCPayload { params: any[]; method: string; @@ -540,6 +386,17 @@ export type OrderState = OrderStateValid|OrderStateInvalid; export type OnOrderStateChangeCallback = (orderState: OrderState) => void; +export interface TxData { + from?: string; + gas?: number; + gasPrice?: BigNumber; + nonce?: number; +} + +export interface TxDataPayable extends TxData { + value?: BigNumber; +} + export interface TransactionReceipt { blockHash: string; blockNumber: number; diff --git a/packages/0x.js/src/utils/class_utils.ts b/packages/0x.js/src/utils/class_utils.ts new file mode 100644 index 000000000..04e60ee57 --- /dev/null +++ b/packages/0x.js/src/utils/class_utils.ts @@ -0,0 +1,18 @@ +import * as _ from 'lodash'; + +export const classUtils = { + // This is useful for classes that have nested methods. Nested methods don't get bound out of the box. + bindAll(self: any, exclude: string[] = ['contructor'], thisArg?: any): void { + for (const key of Object.getOwnPropertyNames(self)) { + const val = self[key]; + if (!_.includes(exclude, key)) { + if (_.isFunction(val)) { + self[key] = val.bind(thisArg || self); + } else if (_.isObject(val)) { + classUtils.bindAll(val, exclude, self); + } + } + } + return self; + }, +}; diff --git a/packages/0x.js/src/utils/promisify.ts b/packages/0x.js/src/utils/promisify.ts new file mode 100644 index 000000000..c114cf32f --- /dev/null +++ b/packages/0x.js/src/utils/promisify.ts @@ -0,0 +1,24 @@ +import * as _ from 'lodash'; + +/** + * Transforms callback-based function -- func(arg1, arg2 .. argN, callback) -- into an ES6-compatible Promise. + * Promisify provides a default callback of the form (error, result) and rejects when `error` is not null. You can also + * supply thisArg object as the second argument which will be passed to `apply`. + */ +export function promisify<T>( + originalFn: ( + ...args: any[], + // HACK: This can't be properly typed without variadic kinds https://github.com/Microsoft/TypeScript/issues/5453 + ) => void, + thisArg?: any, +): (...callArgs: any[]) => Promise<T> { + const promisifiedFunction = async (...callArgs: any[]): Promise<T> => { + return new Promise<T>((resolve, reject) => { + const callback = (err: Error|null, data?: T) => { + _.isNull(err) ? resolve(data) : reject(err); + }; + originalFn.apply(thisArg, [...callArgs, callback]); + }); + }; + return promisifiedFunction; +} diff --git a/packages/0x.js/src/web3_wrapper.ts b/packages/0x.js/src/web3_wrapper.ts index a031de486..6a6b4e760 100644 --- a/packages/0x.js/src/web3_wrapper.ts +++ b/packages/0x.js/src/web3_wrapper.ts @@ -1,10 +1,9 @@ import BigNumber from 'bignumber.js'; -import promisify = require('es6-promisify'); 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'; +import {promisify} from './utils/promisify'; interface RawLogEntry { logIndex: string|null; @@ -29,9 +28,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 +43,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); @@ -56,11 +58,11 @@ export class Web3Wrapper { return _.includes(addresses, senderAddress); } public async getNodeVersionAsync(): Promise<string> { - const nodeVersion = await promisify(this.web3.version.getNode)(); + const nodeVersion = await promisify<string>(this.web3.version.getNode)(); return nodeVersion; } public async getTransactionReceiptAsync(txHash: string): Promise<TransactionReceipt> { - const transactionReceipt = await promisify(this.web3.eth.getTransactionReceipt)(txHash); + const transactionReceipt = await promisify<TransactionReceipt>(this.web3.eth.getTransactionReceipt)(txHash); if (!_.isNull(transactionReceipt)) { transactionReceipt.status = this.normalizeTxReceiptStatus(transactionReceipt.status); } @@ -72,8 +74,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 +91,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; @@ -98,26 +101,26 @@ export class Web3Wrapper { return balanceWei; } public async getBalanceInWeiAsync(owner: string): Promise<BigNumber> { - let balanceInWei = await promisify(this.web3.eth.getBalance)(owner); + let balanceInWei = await promisify<BigNumber>(this.web3.eth.getBalance)(owner); balanceInWei = new BigNumber(balanceInWei); return balanceInWei; } public async doesContractExistAtAddressAsync(address: string): Promise<boolean> { - const code = await promisify(this.web3.eth.getCode)(address); + const code = await promisify<string>(this.web3.eth.getCode)(address); // Regex matches 0x0, 0x00, 0x in order to accommodate poorly implemented clients const codeIsEmpty = /^0x0{0,40}$/i.test(code); return !codeIsEmpty; } public async signTransactionAsync(address: string, message: string): Promise<string> { - const signData = await promisify(this.web3.eth.sign)(address, message); + const signData = await promisify<string>(this.web3.eth.sign)(address, message); return signData; } public async getBlockNumberAsync(): Promise<number> { - const blockNumber = await promisify(this.web3.eth.getBlockNumber)(); + const blockNumber = await promisify<number>(this.web3.eth.getBlockNumber)(); return blockNumber; } public async getBlockAsync(blockParam: string|Web3.BlockParam): Promise<Web3.BlockWithoutTransactionData> { - const block = await promisify(this.web3.eth.getBlock)(blockParam); + const block = await promisify<Web3.BlockWithoutTransactionData>(this.web3.eth.getBlock)(blockParam); return block; } public async getBlockTimestampAsync(blockParam: string|Web3.BlockParam): Promise<number> { @@ -125,7 +128,7 @@ export class Web3Wrapper { return timestamp; } public async getAvailableAddressesAsync(): Promise<string[]> { - const addresses: string[] = await promisify(this.web3.eth.getAccounts)(); + const addresses = await promisify<string[]>(this.web3.eth.getAccounts)(); return addresses; } public async getLogsAsync(filter: Web3.FilterObject): Promise<Web3.LogEntry[]> { @@ -152,18 +155,17 @@ 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)(); + const networkId = await promisify<number>(this.web3.version.getNetwork)(); return networkId; } private async sendRawPayloadAsync<A>(payload: Web3.JSONRPCRequestPayload): Promise<A> { const sendAsync = this.web3.currentProvider.sendAsync.bind(this.web3.currentProvider); - const response = await promisify(sendAsync)(payload); + const response = await promisify<Web3.JSONRPCResponsePayload>(sendAsync)(payload); const result = response.result; return result; } |