Implement zeroEx.token.getProxyAllowanceAsync

This commit is contained in:
Fabio Berger 2017-05-30 15:27:51 +02:00
parent 86cdb6cb83
commit b5e496ea0f
2 changed files with 34 additions and 1 deletions

View File

@ -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;
}
}

View File

@ -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 {