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 | |
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')
-rw-r--r-- | src/0x.ts | 14 | ||||
-rw-r--r-- | src/artifacts/TokenTransferProxy.json | 128 | ||||
-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 | ||||
-rw-r--r-- | src/types.ts | 3 |
6 files changed, 39 insertions, 142 deletions
@@ -194,9 +194,15 @@ export class ZeroEx { gasPrice, }; this._web3Wrapper = new Web3Wrapper(provider, defaults); - this.token = new TokenWrapper(this._web3Wrapper); - this.proxy = new TokenTransferProxyWrapper(this._web3Wrapper); + this.token = new TokenWrapper( + this._web3Wrapper, + this._getTokenTransferProxyAddressAsync.bind(this), + ); this.exchange = new ExchangeWrapper(this._web3Wrapper, this.token); + this.proxy = new TokenTransferProxyWrapper( + this._web3Wrapper, + this._getTokenTransferProxyAddressAsync.bind(this), + ); this.tokenRegistry = new TokenRegistryWrapper(this._web3Wrapper); this.etherToken = new EtherTokenWrapper(this._web3Wrapper, this.token); } @@ -306,4 +312,8 @@ export class ZeroEx { }); return txReceiptPromise; } + private async _getTokenTransferProxyAddressAsync(): Promise<string> { + const tokenTransferProxyAddress = await (this.exchange as any)._getTokenTransferProxyAddressAsync(); + return tokenTransferProxyAddress; + } } diff --git a/src/artifacts/TokenTransferProxy.json b/src/artifacts/TokenTransferProxy.json index e3f6f5e51..beeb16cfe 100644 --- a/src/artifacts/TokenTransferProxy.json +++ b/src/artifacts/TokenTransferProxy.json @@ -168,131 +168,7 @@ } ], "unlinked_binary": "0x60606040525b60008054600160a060020a03191633600160a060020a03161790555b5b6106e6806100316000396000f300606060405236156100725763ffffffff60e060020a60003504166315dacbea811461007457806342f1181e146100b3578063494503d4146100d157806370712939146101005780638da5cb5b1461011e578063b91816111461014a578063d39de6e91461017a578063f2fde38b146101e5575bfe5b341561007c57fe5b61009f600160a060020a0360043581169060243581169060443516606435610203565b604080519115158252519081900360200190f35b34156100bb57fe5b6100cf600160a060020a03600435166102ae565b005b34156100d957fe5b6100e4600435610390565b60408051600160a060020a039092168252519081900360200190f35b341561010857fe5b6100cf600160a060020a03600435166103c2565b005b341561012657fe5b6100e461055a565b60408051600160a060020a039092168252519081900360200190f35b341561015257fe5b61009f600160a060020a0360043516610569565b604080519115158252519081900360200190f35b341561018257fe5b61018a61057e565b60408051602080825283518183015283519192839290830191858101910280838382156101d2575b8051825260208311156101d257601f1990920191602091820191016101b2565b5050509050019250505060405180910390f35b34156101ed57fe5b6100cf600160a060020a03600435166105e7565b005b600160a060020a03331660009081526001602052604081205460ff16151561022b5760006000fd5b6040805160006020918201819052825160e060020a6323b872dd028152600160a060020a0388811660048301528781166024830152604482018790529351938916936323b872dd9360648084019491938390030190829087803b151561028d57fe5b6102c65a03f1151561029b57fe5b5050604051519150505b5b949350505050565b60005433600160a060020a039081169116146102ca5760006000fd5b600160a060020a038116600090815260016020526040902054819060ff16156102f35760006000fd5b600160a060020a0382166000908152600160208190526040909120805460ff191682179055600280549091810161032a8382610633565b916000526020600020900160005b81546101009190910a600160a060020a0381810219909216868316918202179092556040513390911692507f94bb87f4c15c4587ff559a7584006fa01ddf9299359be6b512b94527aa961aca90600090a35b5b505b50565b600280548290811061039e57fe5b906000526020600020900160005b915054906101000a9004600160a060020a031681565b6000805433600160a060020a039081169116146103df5760006000fd5b600160a060020a038216600090815260016020526040902054829060ff1615156104095760006000fd5b600160a060020a0383166000908152600160205260408120805460ff1916905591505b6002548210156105195782600160a060020a031660028381548110151561044f57fe5b906000526020600020900160005b9054906101000a9004600160a060020a0316600160a060020a0316141561050d5760028054600019810190811061049057fe5b906000526020600020900160005b9054906101000a9004600160a060020a03166002838154811015156104bf57fe5b906000526020600020900160005b6101000a815481600160a060020a030219169083600160a060020a0316021790555060016002818180549050039150816105079190610633565b50610519565b5b60019091019061042c565b604051600160a060020a0333811691908516907ff5b347a1e40749dd050f5f07fbdbeb7e3efa9756903044dd29401fd1d4bb4a1c90600090a35b5b505b5050565b600054600160a060020a031681565b60016020526000908152604090205460ff1681565b610586610687565b60028054806020026020016040519081016040528092919081815260200182805480156105dc57602002820191906000526020600020905b8154600160a060020a031681526001909101906020018083116105be575b505050505090505b90565b60005433600160a060020a039081169116146106035760006000fd5b600160a060020a0381161561038d5760008054600160a060020a031916600160a060020a0383161790555b5b5b50565b81548183558181151161055357600083815260209020610553918101908301610699565b5b505050565b81548183558181151161055357600083815260209020610553918101908301610699565b5b505050565b60408051602081019091526000815290565b6105e491905b808211156106b3576000815560010161069f565b5090565b905600a165627a7a72305820d2924957bb88a128789172e164d874fe5445218fc2dde2f5eb265839a1f341a20029", - "networks": { - "1": { - "links": {}, - "events": { - "0x94bb87f4c15c4587ff559a7584006fa01ddf9299359be6b512b94527aa961aca": { - "anonymous": false, - "inputs": [ - { - "indexed": true, - "name": "target", - "type": "address" - }, - { - "indexed": true, - "name": "caller", - "type": "address" - } - ], - "name": "LogAuthorizedAddressAdded", - "type": "event" - }, - "0xf5b347a1e40749dd050f5f07fbdbeb7e3efa9756903044dd29401fd1d4bb4a1c": { - "anonymous": false, - "inputs": [ - { - "indexed": true, - "name": "target", - "type": "address" - }, - { - "indexed": true, - "name": "caller", - "type": "address" - } - ], - "name": "LogAuthorizedAddressRemoved", - "type": "event" - } - }, - "updated_at": 1502478966000, - "address": "0x8da0d80f5007ef1e431dd2127178d224e32c2ef4" - }, - "42": { - "links": {}, - "events": { - "0x94bb87f4c15c4587ff559a7584006fa01ddf9299359be6b512b94527aa961aca": { - "anonymous": false, - "inputs": [ - { - "indexed": true, - "name": "target", - "type": "address" - }, - { - "indexed": true, - "name": "caller", - "type": "address" - } - ], - "name": "LogAuthorizedAddressAdded", - "type": "event" - }, - "0xf5b347a1e40749dd050f5f07fbdbeb7e3efa9756903044dd29401fd1d4bb4a1c": { - "anonymous": false, - "inputs": [ - { - "indexed": true, - "name": "target", - "type": "address" - }, - { - "indexed": true, - "name": "caller", - "type": "address" - } - ], - "name": "LogAuthorizedAddressRemoved", - "type": "event" - } - }, - "updated_at": 1502391794384, - "address": "0x087eed4bc1ee3de49befbd66c662b434b15d49d4" - }, - "50": { - "links": {}, - "events": { - "0x94bb87f4c15c4587ff559a7584006fa01ddf9299359be6b512b94527aa961aca": { - "anonymous": false, - "inputs": [ - { - "indexed": true, - "name": "target", - "type": "address" - }, - { - "indexed": true, - "name": "caller", - "type": "address" - } - ], - "name": "LogAuthorizedAddressAdded", - "type": "event" - }, - "0xf5b347a1e40749dd050f5f07fbdbeb7e3efa9756903044dd29401fd1d4bb4a1c": { - "anonymous": false, - "inputs": [ - { - "indexed": true, - "name": "target", - "type": "address" - }, - { - "indexed": true, - "name": "caller", - "type": "address" - } - ], - "name": "LogAuthorizedAddressRemoved", - "type": "event" - } - }, - "updated_at": 1503318938227, - "address": "0x871dd7c2b4b25e1aa18728e9d5f2af4c4e431f5c" - } - }, + "networks": {}, "schema_version": "0.0.5", "updated_at": 1503318938227 -}
\ No newline at end of file +} 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; } } diff --git a/src/types.ts b/src/types.ts index eb83d7c2b..c7204a9fc 100644 --- a/src/types.ts +++ b/src/types.ts @@ -51,6 +51,9 @@ export interface ExchangeContract extends Web3.ContractInstance { ZRX_TOKEN_CONTRACT: { callAsync: () => Promise<string>; }; + TOKEN_TRANSFER_PROXY_CONTRACT: { + callAsync: () => Promise<string>; + }; getUnavailableTakerTokenAmount: { callAsync: (orderHash: string, defaultBlock?: Web3.BlockParam) => Promise<BigNumber.BigNumber>; }; |