diff options
Diffstat (limited to 'packages/0x.js/src/web3_wrapper.ts')
-rw-r--r-- | packages/0x.js/src/web3_wrapper.ts | 44 |
1 files changed, 23 insertions, 21 deletions
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; } |