diff options
author | Leonid Logvinov <logvinov.leon@gmail.com> | 2017-09-05 00:14:48 +0800 |
---|---|---|
committer | Leonid Logvinov <logvinov.leon@gmail.com> | 2017-09-05 00:14:48 +0800 |
commit | 1ad395cf86b2006c09bdae814607c2baf9790b91 (patch) | |
tree | d05ed1febd8979b7967a42faaed45c3dd10356dd /src/web3_wrapper.ts | |
parent | 1c2d4cbb1af9a5d4442857def622dc8068086953 (diff) | |
download | dexon-sol-tools-1ad395cf86b2006c09bdae814607c2baf9790b91.tar dexon-sol-tools-1ad395cf86b2006c09bdae814607c2baf9790b91.tar.gz dexon-sol-tools-1ad395cf86b2006c09bdae814607c2baf9790b91.tar.bz2 dexon-sol-tools-1ad395cf86b2006c09bdae814607c2baf9790b91.tar.lz dexon-sol-tools-1ad395cf86b2006c09bdae814607c2baf9790b91.tar.xz dexon-sol-tools-1ad395cf86b2006c09bdae814607c2baf9790b91.tar.zst dexon-sol-tools-1ad395cf86b2006c09bdae814607c2baf9790b91.zip |
Make the functions immidiately return txHash instead of awaiting for a transaction to be mined
Diffstat (limited to 'src/web3_wrapper.ts')
-rw-r--r-- | src/web3_wrapper.ts | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/src/web3_wrapper.ts b/src/web3_wrapper.ts index 61bac45c9..129017e7c 100644 --- a/src/web3_wrapper.ts +++ b/src/web3_wrapper.ts @@ -2,6 +2,8 @@ import * as _ from 'lodash'; import * as Web3 from 'web3'; import * as BigNumber from 'bignumber.js'; import promisify = require('es6-promisify'); +import {ZeroExError} from './types'; +import {Contract} from './contract'; export class Web3Wrapper { private web3: Web3; @@ -41,6 +43,27 @@ export class Web3Wrapper { return undefined; } } + public async getContractInstanceFromArtifactAsync<A extends Web3.ContractInstance>(artifact: Artifact, + address?: string): Promise<A> { + if (_.isUndefined(address)) { + const networkIdIfExists = await this.getNetworkIdIfExistsAsync(); + if (_.isUndefined(networkIdIfExists)) { + throw new Error(ZeroExError.NoNetworkId); + } + if (_.isUndefined(artifact.networks[networkIdIfExists])) { + throw new Error(ZeroExError.ContractNotDeployedOnNetwork); + } + address = artifact.networks[networkIdIfExists].address.toLowerCase(); + } + const doesContractExist = await this.doesContractExistAtAddressAsync(address); + if (!doesContractExist) { + throw new Error(ZeroExError.ContractDoesNotExist); + } + const contractInstance = this.getContractInstance<A>( + artifact.abi, address, + ); + return contractInstance; + } public toWei(ethAmount: BigNumber.BigNumber): BigNumber.BigNumber { const balanceWei = this.web3.toWei(ethAmount, 'ether'); return balanceWei; @@ -68,6 +91,11 @@ export class Web3Wrapper { const addresses: string[] = await promisify(this.web3.eth.getAccounts)(); return addresses; } + private getContractInstance<A extends Web3.ContractInstance>(abi: Web3.ContractAbi, address: string): A { + const web3ContractInstance = this.web3.eth.contract(abi).at(address); + const contractInstance = new Contract(web3ContractInstance) as any as A; + return contractInstance; + } private async getNetworkAsync(): Promise<number> { const networkId = await promisify(this.web3.version.getNetwork)(); return networkId; |