diff options
Diffstat (limited to 'src/contract_wrappers')
-rw-r--r-- | src/contract_wrappers/contract_wrapper.ts | 5 | ||||
-rw-r--r-- | src/contract_wrappers/ether_token_wrapper.ts | 6 | ||||
-rw-r--r-- | src/contract_wrappers/exchange_wrapper.ts | 12 | ||||
-rw-r--r-- | src/contract_wrappers/token_registry_wrapper.ts | 16 | ||||
-rw-r--r-- | src/contract_wrappers/token_transfer_proxy_wrapper.ts | 9 | ||||
-rw-r--r-- | src/contract_wrappers/token_wrapper.ts | 21 |
6 files changed, 47 insertions, 22 deletions
diff --git a/src/contract_wrappers/contract_wrapper.ts b/src/contract_wrappers/contract_wrapper.ts index 4e3fb5029..2a55b53d9 100644 --- a/src/contract_wrappers/contract_wrapper.ts +++ b/src/contract_wrappers/contract_wrapper.ts @@ -10,9 +10,10 @@ export class ContractWrapper { this._web3Wrapper = web3Wrapper; } protected async _instantiateContractIfExistsAsync<A extends Web3.ContractInstance>(artifact: Artifact, - address?: string): Promise<A> { + addressIfExists?: string, + ): Promise<A> { const contractInstance = - await this._web3Wrapper.getContractInstanceFromArtifactAsync<A>(artifact, address); + await this._web3Wrapper.getContractInstanceFromArtifactAsync<A>(artifact, addressIfExists); return contractInstance; } } diff --git a/src/contract_wrappers/ether_token_wrapper.ts b/src/contract_wrappers/ether_token_wrapper.ts index b86309f90..f15e766f0 100644 --- a/src/contract_wrappers/ether_token_wrapper.ts +++ b/src/contract_wrappers/ether_token_wrapper.ts @@ -13,9 +13,11 @@ import {artifacts} from '../artifacts'; export class EtherTokenWrapper extends ContractWrapper { private _etherTokenContractIfExists?: EtherTokenContract; private _tokenWrapper: TokenWrapper; - constructor(web3Wrapper: Web3Wrapper, tokenWrapper: TokenWrapper) { + private _contractAddressIfExists?: string; + constructor(web3Wrapper: Web3Wrapper, tokenWrapper: TokenWrapper, contractAddressIfExists?: string) { super(web3Wrapper); this._tokenWrapper = tokenWrapper; + this._contractAddressIfExists = contractAddressIfExists; } /** * Deposit ETH into the Wrapped ETH smart contract and issues the equivalent number of wrapped ETH tokens @@ -76,7 +78,7 @@ export class EtherTokenWrapper extends ContractWrapper { return this._etherTokenContractIfExists; } const contractInstance = await this._instantiateContractIfExistsAsync<EtherTokenContract>( - artifacts.EtherTokenArtifact, + artifacts.EtherTokenArtifact, this._contractAddressIfExists, ); this._etherTokenContractIfExists = contractInstance as EtherTokenContract; return this._etherTokenContractIfExists; diff --git a/src/contract_wrappers/exchange_wrapper.ts b/src/contract_wrappers/exchange_wrapper.ts index 73c4d935b..54d7f62d5 100644 --- a/src/contract_wrappers/exchange_wrapper.ts +++ b/src/contract_wrappers/exchange_wrapper.ts @@ -56,6 +56,7 @@ export class ExchangeWrapper extends ContractWrapper { [ExchangeContractErrCodes.ERROR_FILL_TRUNCATION]: ExchangeContractErrs.OrderFillRoundingError, [ExchangeContractErrCodes.ERROR_FILL_BALANCE_ALLOWANCE]: ExchangeContractErrs.FillBalanceAllowanceError, }; + private _contractAddressIfExists?: string; private static _getOrderAddressesAndValues(order: Order): [OrderAddresses, OrderValues] { const orderAddresses: OrderAddresses = [ order.maker, @@ -74,11 +75,12 @@ export class ExchangeWrapper extends ContractWrapper { ]; return [orderAddresses, orderValues]; } - constructor(web3Wrapper: Web3Wrapper, tokenWrapper: TokenWrapper) { + constructor(web3Wrapper: Web3Wrapper, tokenWrapper: TokenWrapper, contractAddressIfExists?: string) { super(web3Wrapper); this._tokenWrapper = tokenWrapper; this._orderValidationUtils = new OrderValidationUtils(tokenWrapper, this); this._exchangeLogEventEmitters = []; + this._contractAddressIfExists = contractAddressIfExists; } /** * Returns the unavailable takerAmount of an order. Unavailable amount is defined as the total @@ -738,7 +740,7 @@ export class ExchangeWrapper extends ContractWrapper { return this._exchangeContractIfExists; } const contractInstance = await this._instantiateContractIfExistsAsync<ExchangeContract>( - artifacts.ExchangeArtifact, + artifacts.ExchangeArtifact, this._contractAddressIfExists, ); this._exchangeContractIfExists = contractInstance as ExchangeContract; return this._exchangeContractIfExists; @@ -748,4 +750,10 @@ 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(); + const tokenTransferProxyAddressLowerCase = tokenTransferProxyAddress.toLowerCase(); + return tokenTransferProxyAddressLowerCase; + } } diff --git a/src/contract_wrappers/token_registry_wrapper.ts b/src/contract_wrappers/token_registry_wrapper.ts index 528a88e06..2cc5a9aa0 100644 --- a/src/contract_wrappers/token_registry_wrapper.ts +++ b/src/contract_wrappers/token_registry_wrapper.ts @@ -11,8 +11,10 @@ import {artifacts} from '../artifacts'; */ export class TokenRegistryWrapper extends ContractWrapper { private _tokenRegistryContractIfExists?: TokenRegistryContract; - constructor(web3Wrapper: Web3Wrapper) { + private _contractAddressIfExists?: string; + constructor(web3Wrapper: Web3Wrapper, contractAddressIfExists?: string) { super(web3Wrapper); + this._contractAddressIfExists = contractAddressIfExists; } /** * Retrieves all the tokens currently listed in the Token Registry smart contract @@ -82,6 +84,16 @@ export class TokenRegistryWrapper extends ContractWrapper { const token = this._createTokenFromMetadata(metadata); return token; } + /** + * Retrieves the Ethereum address of the TokenRegistry contract deployed on the network + * that the user-passed web3 provider is connected to. + * @returns The Ethereum address of the TokenRegistry contract being used. + */ + public async getContractAddressAsync(): Promise<string> { + const tokenRegistryInstance = await this._getTokenRegistryContractAsync(); + const tokenRegistryAddress = tokenRegistryInstance.address; + return tokenRegistryAddress; + } private _createTokenFromMetadata(metadata: TokenMetadata): Token|undefined { if (metadata[0] === constants.NULL_ADDRESS) { return undefined; @@ -102,7 +114,7 @@ export class TokenRegistryWrapper extends ContractWrapper { return this._tokenRegistryContractIfExists; } const contractInstance = await this._instantiateContractIfExistsAsync<TokenRegistryContract>( - artifacts.TokenRegistryArtifact, + artifacts.TokenRegistryArtifact, this._contractAddressIfExists, ); this._tokenRegistryContractIfExists = contractInstance as TokenRegistryContract; return this._tokenRegistryContractIfExists; 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..f7f0a0ce3 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,8 @@ 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(); + return tokenTransferProxyContractAddress; } } |