diff options
author | Fabio Berger <me@fabioberger.com> | 2017-11-28 07:15:18 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-11-28 07:15:18 +0800 |
commit | 54ef916b93ba0939dcb8824149c9a9fb74df4f2e (patch) | |
tree | 6559179e99d077ae8d78350580a082e75d50a7c8 /packages/0x.js/src/contract_wrappers/token_wrapper.ts | |
parent | 4a770dee84ee49e8038da7dd32ce26338176bf36 (diff) | |
parent | f862a2af6d9802c2c75f813025517e0c52cd513c (diff) | |
download | dexon-sol-tools-54ef916b93ba0939dcb8824149c9a9fb74df4f2e.tar dexon-sol-tools-54ef916b93ba0939dcb8824149c9a9fb74df4f2e.tar.gz dexon-sol-tools-54ef916b93ba0939dcb8824149c9a9fb74df4f2e.tar.bz2 dexon-sol-tools-54ef916b93ba0939dcb8824149c9a9fb74df4f2e.tar.lz dexon-sol-tools-54ef916b93ba0939dcb8824149c9a9fb74df4f2e.tar.xz dexon-sol-tools-54ef916b93ba0939dcb8824149c9a9fb74df4f2e.tar.zst dexon-sol-tools-54ef916b93ba0939dcb8824149c9a9fb74df4f2e.zip |
Merge pull request #233 from 0xProject/feature/passNetworkId
Forces the users of 0x.js to pass the network id
Diffstat (limited to 'packages/0x.js/src/contract_wrappers/token_wrapper.ts')
-rw-r--r-- | packages/0x.js/src/contract_wrappers/token_wrapper.ts | 51 |
1 files changed, 25 insertions, 26 deletions
diff --git a/packages/0x.js/src/contract_wrappers/token_wrapper.ts b/packages/0x.js/src/contract_wrappers/token_wrapper.ts index 4b89d3cfc..5c6cfeaed 100644 --- a/packages/0x.js/src/contract_wrappers/token_wrapper.ts +++ b/packages/0x.js/src/contract_wrappers/token_wrapper.ts @@ -1,23 +1,26 @@ -import * as _ from 'lodash'; -import BigNumber from 'bignumber.js'; import {schemas} from '@0xproject/json-schemas'; -import {Web3Wrapper} from '../web3_wrapper'; -import {assert} from '../utils/assert'; -import {constants} from '../utils/constants'; -import {ContractWrapper} from './contract_wrapper'; -import {AbiDecoder} from '../utils/abi_decoder'; +import BigNumber from 'bignumber.js'; +import * as _ from 'lodash'; + import {artifacts} from '../artifacts'; import { - TokenContract, - ZeroExError, - TokenEvents, + EventCallback, IndexedFilterValues, - SubscriptionOpts, - MethodOpts, LogWithDecodedArgs, - EventCallback, + MethodOpts, + SubscriptionOpts, + TokenContract, TokenContractEventArgs, + TokenEvents, + ZeroExError, } from '../types'; +import {AbiDecoder} from '../utils/abi_decoder'; +import {assert} from '../utils/assert'; +import {constants} from '../utils/constants'; +import {Web3Wrapper} from '../web3_wrapper'; + +import {ContractWrapper} from './contract_wrapper'; +import {TokenTransferProxyWrapper} from './token_transfer_proxy_wrapper'; const ALLOWANCE_TO_ZERO_GAS_AMOUNT = 47275; @@ -29,12 +32,12 @@ const ALLOWANCE_TO_ZERO_GAS_AMOUNT = 47275; export class TokenWrapper extends ContractWrapper { public UNLIMITED_ALLOWANCE_IN_BASE_UNITS = constants.UNLIMITED_ALLOWANCE_IN_BASE_UNITS; private _tokenContractsByAddress: {[address: string]: TokenContract}; - private _tokenTransferProxyContractAddressFetcher: () => Promise<string>; + private _tokenTransferProxyWrapper: TokenTransferProxyWrapper; constructor(web3Wrapper: Web3Wrapper, abiDecoder: AbiDecoder, - tokenTransferProxyContractAddressFetcher: () => Promise<string>) { + tokenTransferProxyWrapper: TokenTransferProxyWrapper) { super(web3Wrapper, abiDecoder); this._tokenContractsByAddress = {}; - this._tokenTransferProxyContractAddressFetcher = tokenTransferProxyContractAddressFetcher; + this._tokenTransferProxyWrapper = tokenTransferProxyWrapper; } /** * Retrieves an owner's ERC20 token balance. @@ -76,8 +79,8 @@ export class TokenWrapper extends ContractWrapper { // Hack: for some reason default estimated gas amount causes `base fee exceeds gas limit` exception // on testrpc. Probably related to https://github.com/ethereumjs/testrpc/issues/294 // TODO: Debug issue in testrpc and submit a PR, then remove this hack - const networkIdIfExists = await this._web3Wrapper.getNetworkIdIfExistsAsync(); - const gas = networkIdIfExists === constants.TESTRPC_NETWORK_ID ? ALLOWANCE_TO_ZERO_GAS_AMOUNT : undefined; + const networkId = this._web3Wrapper.getNetworkId(); + const gas = networkId === constants.TESTRPC_NETWORK_ID ? ALLOWANCE_TO_ZERO_GAS_AMOUNT : undefined; const txHash = await tokenContract.approve.sendTransactionAsync(spenderAddress, amountInBaseUnits, { from: ownerAddress, gas, @@ -133,7 +136,7 @@ export class TokenWrapper extends ContractWrapper { assert.isETHAddressHex('ownerAddress', ownerAddress); assert.isETHAddressHex('tokenAddress', tokenAddress); - const proxyAddress = await this._getTokenTransferProxyAddressAsync(); + const proxyAddress = this._tokenTransferProxyWrapper.getContractAddress(); const allowanceInBaseUnits = await this.getAllowanceAsync(tokenAddress, ownerAddress, proxyAddress, methodOpts); return allowanceInBaseUnits; } @@ -152,7 +155,7 @@ export class TokenWrapper extends ContractWrapper { assert.isETHAddressHex('tokenAddress', tokenAddress); assert.isValidBaseUnitAmount('amountInBaseUnits', amountInBaseUnits); - const proxyAddress = await this._getTokenTransferProxyAddressAsync(); + const proxyAddress = this._tokenTransferProxyWrapper.getContractAddress(); const txHash = await this.setAllowanceAsync(tokenAddress, ownerAddress, proxyAddress, amountInBaseUnits); return txHash; } @@ -290,7 +293,7 @@ export class TokenWrapper extends ContractWrapper { ); return logs; } - private _invalidateContractInstancesAsync(): void { + private _invalidateContractInstances(): void { this.unsubscribeAll(); this._tokenContractsByAddress = {}; } @@ -302,12 +305,8 @@ export class TokenWrapper extends ContractWrapper { const contractInstance = await this._instantiateContractIfExistsAsync<TokenContract>( artifacts.TokenArtifact, tokenAddress, ); - tokenContract = contractInstance as TokenContract; + tokenContract = contractInstance; this._tokenContractsByAddress[tokenAddress] = tokenContract; return tokenContract; } - private async _getTokenTransferProxyAddressAsync(): Promise<string> { - const tokenTransferProxyContractAddress = await this._tokenTransferProxyContractAddressFetcher(); - return tokenTransferProxyContractAddress; - } } |