diff options
-rw-r--r-- | src/contract_wrappers/token_wrapper.ts | 27 | ||||
-rw-r--r-- | src/types.ts | 8 |
2 files changed, 34 insertions, 1 deletions
diff --git a/src/contract_wrappers/token_wrapper.ts b/src/contract_wrappers/token_wrapper.ts index fe1d0c89f..0282d8266 100644 --- a/src/contract_wrappers/token_wrapper.ts +++ b/src/contract_wrappers/token_wrapper.ts @@ -4,7 +4,8 @@ import {Web3Wrapper} from '../web3_wrapper'; import {assert} from '../utils/assert'; import {ContractWrapper} from './contract_wrapper'; import * as TokenArtifacts from '../artifacts/Token.json'; -import {TokenContract} from '../types'; +import * as ProxyArtifacts from '../artifacts/Proxy.json'; +import {TokenContract, InternalError} from '../types'; export class TokenWrapper extends ContractWrapper { private tokenContractsByAddress: {[address: string]: TokenContract}; @@ -29,6 +30,19 @@ export class TokenWrapper extends ContractWrapper { balance = _.isUndefined(balance) ? new BigNumber(0) : new BigNumber(balance); return balance; } + /** + * Retrieves the allowance of an ERC20 token set to the 0x proxy contract by an owner address + */ + public async getProxyAllowanceAsync(tokenAddress: string, ownerAddress: string) { + assert.isETHAddressHex('ownerAddress', ownerAddress); + assert.isETHAddressHex('tokenAddress', tokenAddress); + + const tokenContract = await this.getTokenContractAsync(tokenAddress); + const proxyAddress = await this.getProxyAddressAsync(); + let allowance = await tokenContract.allowance.call(ownerAddress, proxyAddress); + allowance = _.isUndefined(allowance) ? new BigNumber(0) : new BigNumber(allowance); + return allowance; + } private async getTokenContractAsync(tokenAddress: string): Promise<TokenContract> { let tokenContract = this.tokenContractsByAddress[tokenAddress]; if (!_.isUndefined(tokenContract)) { @@ -39,4 +53,15 @@ 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 : + (ProxyArtifacts as any).networks[networkIdIfExists]; + if (_.isUndefined(proxyNetworkConfigsIfExists)) { + throw new Error(InternalError.PROXY_ADDRESS_NOT_FOUND); + } + const proxyAddress = proxyNetworkConfigsIfExists.address; + return proxyAddress; + } } diff --git a/src/types.ts b/src/types.ts index e1c68e8ae..c2a048e8a 100644 --- a/src/types.ts +++ b/src/types.ts @@ -17,6 +17,11 @@ export const ZeroExError = strEnum([ ]); export type ZeroExError = keyof typeof ZeroExError; +export const InternalError = strEnum([ + 'PROXY_ADDRESS_NOT_FOUND', +]); +export type InternalError = keyof typeof InternalError; + /** * Elliptic Curve signature */ @@ -34,6 +39,9 @@ export interface TokenContract { balanceOf: { call: (address: string) => Promise<BigNumber.BigNumber>; }; + allowance: { + call: (ownerAddress: string, allowedAddress: string) => Promise<BigNumber.BigNumber>; + }; } export interface TokenRegistryContract { |