diff options
author | Leonid Logvinov <logvinov.leon@gmail.com> | 2017-09-18 18:26:16 +0800 |
---|---|---|
committer | Leonid Logvinov <logvinov.leon@gmail.com> | 2017-09-18 21:18:04 +0800 |
commit | 8db90538a17a0458fba6d2ed8a8b51bf9edf0fa4 (patch) | |
tree | 0b7eecf9ec6b3f394203d6f3f6240eb2825ae9a9 /src/contract_wrappers | |
parent | fe9f692a4f472e5decbda96aad6afaf98c10d850 (diff) | |
download | dexon-sol-tools-8db90538a17a0458fba6d2ed8a8b51bf9edf0fa4.tar dexon-sol-tools-8db90538a17a0458fba6d2ed8a8b51bf9edf0fa4.tar.gz dexon-sol-tools-8db90538a17a0458fba6d2ed8a8b51bf9edf0fa4.tar.bz2 dexon-sol-tools-8db90538a17a0458fba6d2ed8a8b51bf9edf0fa4.tar.lz dexon-sol-tools-8db90538a17a0458fba6d2ed8a8b51bf9edf0fa4.tar.xz dexon-sol-tools-8db90538a17a0458fba6d2ed8a8b51bf9edf0fa4.tar.zst dexon-sol-tools-8db90538a17a0458fba6d2ed8a8b51bf9edf0fa4.zip |
Fetch tokenTransferProxy address from exchange contract
Diffstat (limited to 'src/contract_wrappers')
-rw-r--r-- | src/contract_wrappers/exchange_wrapper.ts | 5 | ||||
-rw-r--r-- | src/contract_wrappers/token_transfer_proxy_wrapper.ts | 9 | ||||
-rw-r--r-- | src/contract_wrappers/token_wrapper.ts | 22 |
3 files changed, 22 insertions, 14 deletions
diff --git a/src/contract_wrappers/exchange_wrapper.ts b/src/contract_wrappers/exchange_wrapper.ts index 73c4d935b..17e8095a4 100644 --- a/src/contract_wrappers/exchange_wrapper.ts +++ b/src/contract_wrappers/exchange_wrapper.ts @@ -748,4 +748,9 @@ export class ExchangeWrapper extends ContractWrapper { const ZRXtokenAddress = await exchangeInstance.ZRX_TOKEN_CONTRACT.callAsync(); return ZRXtokenAddress; } + private async _getTokenTransferProxyAddressAsync(): Promise<string> { + const exchangeInstance = await this._getExchangeContractAsync(); + const tokenTransferProxyAddress = await exchangeInstance.TOKEN_TRANSFER_PROXY_CONTRACT.callAsync(); + return tokenTransferProxyAddress; + } } diff --git a/src/contract_wrappers/token_transfer_proxy_wrapper.ts b/src/contract_wrappers/token_transfer_proxy_wrapper.ts index 528d661d1..f81845af9 100644 --- a/src/contract_wrappers/token_transfer_proxy_wrapper.ts +++ b/src/contract_wrappers/token_transfer_proxy_wrapper.ts @@ -1,4 +1,5 @@ import * as _ from 'lodash'; +import {Web3Wrapper} from '../web3_wrapper'; import {ContractWrapper} from './contract_wrapper'; import {artifacts} from '../artifacts'; import {TokenTransferProxyContract} from '../types'; @@ -8,6 +9,11 @@ import {TokenTransferProxyContract} from '../types'; */ export class TokenTransferProxyWrapper extends ContractWrapper { private _tokenTransferProxyContractIfExists?: TokenTransferProxyContract; + private _tokenTransferProxyContractAddressFetcher: () => Promise<string>; + constructor(web3Wrapper: Web3Wrapper, tokenTransferProxyContractAddressFetcher: () => Promise<string>) { + super(web3Wrapper); + this._tokenTransferProxyContractAddressFetcher = tokenTransferProxyContractAddressFetcher; + } /** * Check if the Exchange contract address is authorized by the TokenTransferProxy contract. * @param exchangeContractAddress The hex encoded address of the Exchange contract to call. @@ -44,8 +50,9 @@ export class TokenTransferProxyWrapper extends ContractWrapper { if (!_.isUndefined(this._tokenTransferProxyContractIfExists)) { return this._tokenTransferProxyContractIfExists; } + const contractAddress = await this._tokenTransferProxyContractAddressFetcher(); const contractInstance = await this._instantiateContractIfExistsAsync<TokenTransferProxyContract>( - artifacts.TokenTransferProxyArtifact, + artifacts.TokenTransferProxyArtifact, contractAddress, ); this._tokenTransferProxyContractIfExists = contractInstance as TokenTransferProxyContract; return this._tokenTransferProxyContractIfExists; diff --git a/src/contract_wrappers/token_wrapper.ts b/src/contract_wrappers/token_wrapper.ts index bdfdf0c74..59c4a4e7b 100644 --- a/src/contract_wrappers/token_wrapper.ts +++ b/src/contract_wrappers/token_wrapper.ts @@ -31,10 +31,12 @@ export class TokenWrapper extends ContractWrapper { public UNLIMITED_ALLOWANCE_IN_BASE_UNITS = constants.UNLIMITED_ALLOWANCE_IN_BASE_UNITS; private _tokenContractsByAddress: {[address: string]: TokenContract}; private _tokenLogEventEmitters: ContractEventEmitter[]; - constructor(web3Wrapper: Web3Wrapper) { + private _tokenTransferProxyContractAddressFetcher: () => Promise<string>; + constructor(web3Wrapper: Web3Wrapper, tokenTransferProxyContractAddressFetcher: () => Promise<string>) { super(web3Wrapper); this._tokenContractsByAddress = {}; this._tokenLogEventEmitters = []; + this._tokenTransferProxyContractAddressFetcher = tokenTransferProxyContractAddressFetcher; } /** * Retrieves an owner's ERC20 token balance. @@ -133,7 +135,7 @@ export class TokenWrapper extends ContractWrapper { assert.isETHAddressHex('ownerAddress', ownerAddress); assert.isETHAddressHex('tokenAddress', tokenAddress); - const proxyAddress = await this._getProxyAddressAsync(); + const proxyAddress = await this._getTokentransferProxyAddressAsync(); const allowanceInBaseUnits = await this.getAllowanceAsync(tokenAddress, ownerAddress, proxyAddress, methodOpts); return allowanceInBaseUnits; } @@ -152,7 +154,7 @@ export class TokenWrapper extends ContractWrapper { assert.isETHAddressHex('tokenAddress', tokenAddress); assert.isBigNumber('amountInBaseUnits', amountInBaseUnits); - const proxyAddress = await this._getProxyAddressAsync(); + const proxyAddress = await this._getTokentransferProxyAddressAsync(); const txHash = await this.setAllowanceAsync(tokenAddress, ownerAddress, proxyAddress, amountInBaseUnits); return txHash; } @@ -299,15 +301,9 @@ export class TokenWrapper extends ContractWrapper { this._tokenContractsByAddress[tokenAddress] = tokenContract; return tokenContract; } - private async _getProxyAddressAsync() { - const networkIdIfExists = await this._web3Wrapper.getNetworkIdIfExistsAsync(); - const proxyNetworkConfigsIfExists = _.isUndefined(networkIdIfExists) ? - undefined : - artifacts.TokenTransferProxyArtifact.networks[networkIdIfExists]; - if (_.isUndefined(proxyNetworkConfigsIfExists)) { - throw new Error(ZeroExError.ContractNotDeployedOnNetwork); - } - const proxyAddress = proxyNetworkConfigsIfExists.address.toLowerCase(); - return proxyAddress; + private async _getTokentransferProxyAddressAsync(): Promise<string> { + const tokenTransferProxyContractAddress = await this._tokenTransferProxyContractAddressFetcher(); + const lowerCaseTokenTransferProxyContractAddress = tokenTransferProxyContractAddress.toLowerCase(); + return lowerCaseTokenTransferProxyContractAddress; } } |