From 6686443317dcb30f6737d695d2e8d298a942eb8e Mon Sep 17 00:00:00 2001 From: Fabio Berger Date: Tue, 30 May 2017 09:52:31 +0200 Subject: Implement ERC20Wrapper and it's first method getBalanceAsync --- src/0x.js.ts | 3 +++ src/contract_wrappers/erc20_wrapper.ts | 28 ++++++++++++++++++++++++++++ src/types.ts | 6 ++++++ 3 files changed, 37 insertions(+) create mode 100644 src/contract_wrappers/erc20_wrapper.ts (limited to 'src') diff --git a/src/0x.js.ts b/src/0x.js.ts index 7e48bc0a5..7932559fb 100644 --- a/src/0x.js.ts +++ b/src/0x.js.ts @@ -13,6 +13,7 @@ import compareVersions = require('compare-versions'); import {ExchangeWrapper} from './contract_wrappers/exchange_wrapper'; import {TokenRegistryWrapper} from './contract_wrappers/token_registry_wrapper'; import {ecSignatureSchema} from './schemas/ec_signature_schema'; +import {ERC20Wrapper} from './contract_wrappers/erc20_wrapper'; import {SolidityTypes, ECSignature, ZeroExError} from './types'; const MAX_DIGITS_IN_UNSIGNED_256_INT = 78; @@ -21,6 +22,7 @@ export class ZeroEx { public web3Wrapper: Web3Wrapper; public exchange: ExchangeWrapper; public tokenRegistry: TokenRegistryWrapper; + public erc20: ERC20Wrapper; /** * Computes the orderHash given the order parameters and returns it as a hex encoded string. */ @@ -135,6 +137,7 @@ export class ZeroEx { this.web3Wrapper = new Web3Wrapper(web3); this.exchange = new ExchangeWrapper(this.web3Wrapper); this.tokenRegistry = new TokenRegistryWrapper(this.web3Wrapper); + this.erc20 = new ERC20Wrapper(this.web3Wrapper); } /** * Signs an orderHash and returns it's elliptic curve signature diff --git a/src/contract_wrappers/erc20_wrapper.ts b/src/contract_wrappers/erc20_wrapper.ts new file mode 100644 index 000000000..60b1887db --- /dev/null +++ b/src/contract_wrappers/erc20_wrapper.ts @@ -0,0 +1,28 @@ +import * as _ from 'lodash'; +import * as BigNumber from 'bignumber.js'; +import {Web3Wrapper} from '../web3_wrapper'; +import {assert} from '../utils/assert'; +import {ContractWrapper} from './contract_wrapper'; +import * as TokenArtifacts from '../artifacts/Token.json'; +import {ERC20Contract} from '../types'; + +export class ERC20Wrapper extends ContractWrapper { + constructor(web3Wrapper: Web3Wrapper) { + super(web3Wrapper); + } + /** + * Returns an owner's ERC20 token balance + */ + public async getBalanceAsync(tokenAddress: string, ownerAddress: string): Promise { + assert.isETHAddressHex('ownerAddress', ownerAddress); + assert.isETHAddressHex('tokenAddress', tokenAddress); + + const contractInstance = await this.instantiateContractIfExistsAsync((TokenArtifacts as any), tokenAddress); + const tokenContract = contractInstance as ERC20Contract; + let balance = await tokenContract.balanceOf.call(ownerAddress); + // The BigNumber instance returned by Web3 is of a much older version then our own, we therefore + // should always re-instantiate the returned BigNumber after retrieval. + balance = _.isUndefined(balance) ? new BigNumber(0) : new BigNumber(balance); + return balance; + } +} diff --git a/src/types.ts b/src/types.ts index 273389480..f5b0f3e7c 100644 --- a/src/types.ts +++ b/src/types.ts @@ -30,6 +30,12 @@ export interface ExchangeContract { isValidSignature: any; } +export interface ERC20Contract { + balanceOf: { + call: (address: string) => Promise; + }; +} + export interface TokenRegistryContract { getTokenMetaData: any; getTokenAddresses: any; -- cgit From 2c3b718a4fa063277163e72f1be962dab3dfd21a Mon Sep 17 00:00:00 2001 From: Fabio Berger Date: Tue, 30 May 2017 14:48:41 +0200 Subject: rename erc20Wrapper to tokenWrapper to be more generic in case we end up supporting another spec --- src/0x.js.ts | 17 ++++++++++---- src/contract_wrappers/erc20_wrapper.ts | 28 ----------------------- src/contract_wrappers/token_wrapper.ts | 42 ++++++++++++++++++++++++++++++++++ 3 files changed, 55 insertions(+), 32 deletions(-) delete mode 100644 src/contract_wrappers/erc20_wrapper.ts create mode 100644 src/contract_wrappers/token_wrapper.ts (limited to 'src') diff --git a/src/0x.js.ts b/src/0x.js.ts index 7932559fb..96b290439 100644 --- a/src/0x.js.ts +++ b/src/0x.js.ts @@ -13,16 +13,16 @@ import compareVersions = require('compare-versions'); import {ExchangeWrapper} from './contract_wrappers/exchange_wrapper'; import {TokenRegistryWrapper} from './contract_wrappers/token_registry_wrapper'; import {ecSignatureSchema} from './schemas/ec_signature_schema'; -import {ERC20Wrapper} from './contract_wrappers/erc20_wrapper'; +import {TokenWrapper} from './contract_wrappers/token_wrapper'; import {SolidityTypes, ECSignature, ZeroExError} from './types'; const MAX_DIGITS_IN_UNSIGNED_256_INT = 78; export class ZeroEx { - public web3Wrapper: Web3Wrapper; public exchange: ExchangeWrapper; public tokenRegistry: TokenRegistryWrapper; - public erc20: ERC20Wrapper; + public erc20: TokenWrapper; + private web3Wrapper: Web3Wrapper; /** * Computes the orderHash given the order parameters and returns it as a hex encoded string. */ @@ -137,7 +137,16 @@ export class ZeroEx { this.web3Wrapper = new Web3Wrapper(web3); this.exchange = new ExchangeWrapper(this.web3Wrapper); this.tokenRegistry = new TokenRegistryWrapper(this.web3Wrapper); - this.erc20 = new ERC20Wrapper(this.web3Wrapper); + this.erc20 = new TokenWrapper(this.web3Wrapper); + } + /** + * Sets a new provider for the web3 instance used by 0x.js + */ + public setProvider(provider: Web3.Provider) { + this.web3Wrapper.setProvider(provider); + this.exchange.invalidateContractInstance(); + this.tokenRegistry.invalidateContractInstance(); + this.erc20.invalidateContractInstances(); } /** * Signs an orderHash and returns it's elliptic curve signature diff --git a/src/contract_wrappers/erc20_wrapper.ts b/src/contract_wrappers/erc20_wrapper.ts deleted file mode 100644 index 60b1887db..000000000 --- a/src/contract_wrappers/erc20_wrapper.ts +++ /dev/null @@ -1,28 +0,0 @@ -import * as _ from 'lodash'; -import * as BigNumber from 'bignumber.js'; -import {Web3Wrapper} from '../web3_wrapper'; -import {assert} from '../utils/assert'; -import {ContractWrapper} from './contract_wrapper'; -import * as TokenArtifacts from '../artifacts/Token.json'; -import {ERC20Contract} from '../types'; - -export class ERC20Wrapper extends ContractWrapper { - constructor(web3Wrapper: Web3Wrapper) { - super(web3Wrapper); - } - /** - * Returns an owner's ERC20 token balance - */ - public async getBalanceAsync(tokenAddress: string, ownerAddress: string): Promise { - assert.isETHAddressHex('ownerAddress', ownerAddress); - assert.isETHAddressHex('tokenAddress', tokenAddress); - - const contractInstance = await this.instantiateContractIfExistsAsync((TokenArtifacts as any), tokenAddress); - const tokenContract = contractInstance as ERC20Contract; - let balance = await tokenContract.balanceOf.call(ownerAddress); - // The BigNumber instance returned by Web3 is of a much older version then our own, we therefore - // should always re-instantiate the returned BigNumber after retrieval. - balance = _.isUndefined(balance) ? new BigNumber(0) : new BigNumber(balance); - return balance; - } -} diff --git a/src/contract_wrappers/token_wrapper.ts b/src/contract_wrappers/token_wrapper.ts new file mode 100644 index 000000000..bd815554a --- /dev/null +++ b/src/contract_wrappers/token_wrapper.ts @@ -0,0 +1,42 @@ +import * as _ from 'lodash'; +import * as BigNumber from 'bignumber.js'; +import {Web3Wrapper} from '../web3_wrapper'; +import {assert} from '../utils/assert'; +import {ContractWrapper} from './contract_wrapper'; +import * as TokenArtifacts from '../artifacts/Token.json'; +import {ERC20Contract} from '../types'; + +export class TokenWrapper extends ContractWrapper { + private tokenContractsByAddress: {[address: string]: ERC20Contract}; + constructor(web3Wrapper: Web3Wrapper) { + super(web3Wrapper); + this.tokenContractsByAddress = {}; + } + public invalidateContractInstances() { + this.tokenContractsByAddress = {}; + } + /** + * Returns an owner's ERC20 token balance + */ + public async getBalanceAsync(tokenAddress: string, ownerAddress: string): Promise { + assert.isETHAddressHex('ownerAddress', ownerAddress); + assert.isETHAddressHex('tokenAddress', tokenAddress); + + const tokenContract = await this.getTokenContractAsync(tokenAddress); + let balance = await tokenContract.balanceOf.call(ownerAddress); + // The BigNumber instance returned by Web3 is of a much older version then our own, we therefore + // should always re-instantiate the returned BigNumber after retrieval. + balance = _.isUndefined(balance) ? new BigNumber(0) : new BigNumber(balance); + return balance; + } + private async getTokenContractAsync(tokenAddress: string): Promise { + let tokenContract = this.tokenContractsByAddress[tokenAddress]; + if (!_.isUndefined(tokenContract)) { + return tokenContract; + } + const contractInstance = await this.instantiateContractIfExistsAsync((TokenArtifacts as any), tokenAddress); + tokenContract = contractInstance as ERC20Contract; + this.tokenContractsByAddress[tokenAddress] = tokenContract; + return tokenContract; + } +} -- cgit From 2f16c5483569b819b292554efc736b415b43ca19 Mon Sep 17 00:00:00 2001 From: Fabio Berger Date: Tue, 30 May 2017 14:49:29 +0200 Subject: rename ERC20Contract to TokenContract --- src/contract_wrappers/token_wrapper.ts | 8 ++++---- src/types.ts | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/contract_wrappers/token_wrapper.ts b/src/contract_wrappers/token_wrapper.ts index bd815554a..fe1d0c89f 100644 --- a/src/contract_wrappers/token_wrapper.ts +++ b/src/contract_wrappers/token_wrapper.ts @@ -4,10 +4,10 @@ import {Web3Wrapper} from '../web3_wrapper'; import {assert} from '../utils/assert'; import {ContractWrapper} from './contract_wrapper'; import * as TokenArtifacts from '../artifacts/Token.json'; -import {ERC20Contract} from '../types'; +import {TokenContract} from '../types'; export class TokenWrapper extends ContractWrapper { - private tokenContractsByAddress: {[address: string]: ERC20Contract}; + private tokenContractsByAddress: {[address: string]: TokenContract}; constructor(web3Wrapper: Web3Wrapper) { super(web3Wrapper); this.tokenContractsByAddress = {}; @@ -29,13 +29,13 @@ export class TokenWrapper extends ContractWrapper { balance = _.isUndefined(balance) ? new BigNumber(0) : new BigNumber(balance); return balance; } - private async getTokenContractAsync(tokenAddress: string): Promise { + private async getTokenContractAsync(tokenAddress: string): Promise { let tokenContract = this.tokenContractsByAddress[tokenAddress]; if (!_.isUndefined(tokenContract)) { return tokenContract; } const contractInstance = await this.instantiateContractIfExistsAsync((TokenArtifacts as any), tokenAddress); - tokenContract = contractInstance as ERC20Contract; + tokenContract = contractInstance as TokenContract; this.tokenContractsByAddress[tokenAddress] = tokenContract; return tokenContract; } diff --git a/src/types.ts b/src/types.ts index 9c82c1fa6..e1c68e8ae 100644 --- a/src/types.ts +++ b/src/types.ts @@ -30,7 +30,7 @@ export interface ExchangeContract { isValidSignature: any; } -export interface ERC20Contract { +export interface TokenContract { balanceOf: { call: (address: string) => Promise; }; -- cgit From 86cdb6cb83078f9f29f473e0ebb8425a2d90f5ec Mon Sep 17 00:00:00 2001 From: Fabio Berger Date: Tue, 30 May 2017 14:53:25 +0200 Subject: use token instead of erc20 --- src/0x.js.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/0x.js.ts b/src/0x.js.ts index 96b290439..d231c579e 100644 --- a/src/0x.js.ts +++ b/src/0x.js.ts @@ -21,7 +21,7 @@ const MAX_DIGITS_IN_UNSIGNED_256_INT = 78; export class ZeroEx { public exchange: ExchangeWrapper; public tokenRegistry: TokenRegistryWrapper; - public erc20: TokenWrapper; + public token: TokenWrapper; private web3Wrapper: Web3Wrapper; /** * Computes the orderHash given the order parameters and returns it as a hex encoded string. @@ -137,7 +137,7 @@ export class ZeroEx { this.web3Wrapper = new Web3Wrapper(web3); this.exchange = new ExchangeWrapper(this.web3Wrapper); this.tokenRegistry = new TokenRegistryWrapper(this.web3Wrapper); - this.erc20 = new TokenWrapper(this.web3Wrapper); + this.token = new TokenWrapper(this.web3Wrapper); } /** * Sets a new provider for the web3 instance used by 0x.js @@ -146,7 +146,7 @@ export class ZeroEx { this.web3Wrapper.setProvider(provider); this.exchange.invalidateContractInstance(); this.tokenRegistry.invalidateContractInstance(); - this.erc20.invalidateContractInstances(); + this.token.invalidateContractInstances(); } /** * Signs an orderHash and returns it's elliptic curve signature -- cgit From b5e496ea0f04206b74b37d71d6ba3e6975206efc Mon Sep 17 00:00:00 2001 From: Fabio Berger Date: Tue, 30 May 2017 15:27:51 +0200 Subject: Implement zeroEx.token.getProxyAllowanceAsync --- src/contract_wrappers/token_wrapper.ts | 27 ++++++++++++++++++++++++++- src/types.ts | 8 ++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) (limited to 'src') 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 { 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; }; + allowance: { + call: (ownerAddress: string, allowedAddress: string) => Promise; + }; } export interface TokenRegistryContract { -- cgit From 141d23f78afc822023f59a4b4dce175a8ba7cffd Mon Sep 17 00:00:00 2001 From: Fabio Berger Date: Tue, 30 May 2017 16:30:29 +0200 Subject: Implement setProxyAllowanceAsync --- src/contract_wrappers/token_wrapper.ts | 36 ++++++++++++++++++++++++++++++---- src/types.ts | 1 + src/utils/constants.ts | 1 + 3 files changed, 34 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/contract_wrappers/token_wrapper.ts b/src/contract_wrappers/token_wrapper.ts index 0282d8266..59dfc0667 100644 --- a/src/contract_wrappers/token_wrapper.ts +++ b/src/contract_wrappers/token_wrapper.ts @@ -2,11 +2,14 @@ import * as _ from 'lodash'; import * as BigNumber from 'bignumber.js'; import {Web3Wrapper} from '../web3_wrapper'; import {assert} from '../utils/assert'; +import {constants} from '../utils/constants'; import {ContractWrapper} from './contract_wrapper'; import * as TokenArtifacts from '../artifacts/Token.json'; import * as ProxyArtifacts from '../artifacts/Proxy.json'; import {TokenContract, InternalError} from '../types'; +const ALLOWANCE_TO_ZERO_GAS_AMOUNT = 45730; + export class TokenWrapper extends ContractWrapper { private tokenContractsByAddress: {[address: string]: TokenContract}; constructor(web3Wrapper: Web3Wrapper) { @@ -31,7 +34,8 @@ export class TokenWrapper extends ContractWrapper { return balance; } /** - * Retrieves the allowance of an ERC20 token set to the 0x proxy contract by an owner address + * Retrieves the allowance in baseUnits of the ERC20 token set to the 0x proxy contract + * by an owner address */ public async getProxyAllowanceAsync(tokenAddress: string, ownerAddress: string) { assert.isETHAddressHex('ownerAddress', ownerAddress); @@ -39,9 +43,33 @@ export class TokenWrapper extends ContractWrapper { 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; + let allowanceInBaseUnits = await tokenContract.allowance.call(ownerAddress, proxyAddress); + allowanceInBaseUnits = _.isUndefined(allowanceInBaseUnits) ? + new BigNumber(0) : + new BigNumber(allowanceInBaseUnits); + return allowanceInBaseUnits; + } + /** + * Sets the 0x proxy contract's allowance to a specified number of a tokens' baseUnits on behalf + * of an owner address. + */ + public async setProxyAllowanceAsync(tokenAddress: string, ownerAddress: string, + amountInBaseUnits: BigNumber.BigNumber) { + assert.isETHAddressHex('ownerAddress', ownerAddress); + assert.isETHAddressHex('tokenAddress', tokenAddress); + assert.isBigNumber('amountInBaseUnits', amountInBaseUnits); + + const tokenContract = await this.getTokenContractAsync(tokenAddress); + const proxyAddress = await this.getProxyAddressAsync(); + // Hack: for some reason default estimated gas amount causes `base fee exceeds gas limit` exception + // on testrpc. Probably related to https://github.com/ethereumjs/testrpc/issues/294 + // TODO: Debug issue in testrpc and submit a PR, then remove this hack + const networkIdIfExists = await this.web3Wrapper.getNetworkIdIfExistsAsync(); + const gas = networkIdIfExists === constants.TESTRPC_NETWORK_ID ? ALLOWANCE_TO_ZERO_GAS_AMOUNT : undefined; + await tokenContract.approve(proxyAddress, amountInBaseUnits, { + from: ownerAddress, + gas, + }); } private async getTokenContractAsync(tokenAddress: string): Promise { let tokenContract = this.tokenContractsByAddress[tokenAddress]; diff --git a/src/types.ts b/src/types.ts index c2a048e8a..bf0a1d8b4 100644 --- a/src/types.ts +++ b/src/types.ts @@ -42,6 +42,7 @@ export interface TokenContract { allowance: { call: (ownerAddress: string, allowedAddress: string) => Promise; }; + approve: (proxyAddress: string, amountInBaseUnits: BigNumber.BigNumber, opts: any) => void; } export interface TokenRegistryContract { diff --git a/src/utils/constants.ts b/src/utils/constants.ts index ec2fe744a..5a5ba0e0a 100644 --- a/src/utils/constants.ts +++ b/src/utils/constants.ts @@ -1,3 +1,4 @@ export const constants = { NULL_ADDRESS: '0x0000000000000000000000000000000000000000', + TESTRPC_NETWORK_ID: 50, }; -- cgit From 274fe87da754ac45cecd6c19123a1c144d4c4080 Mon Sep 17 00:00:00 2001 From: Fabio Berger Date: Wed, 31 May 2017 10:32:59 +0200 Subject: Add full stop to comments --- src/contract_wrappers/token_wrapper.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/contract_wrappers/token_wrapper.ts b/src/contract_wrappers/token_wrapper.ts index 59dfc0667..9e496b648 100644 --- a/src/contract_wrappers/token_wrapper.ts +++ b/src/contract_wrappers/token_wrapper.ts @@ -20,7 +20,7 @@ export class TokenWrapper extends ContractWrapper { this.tokenContractsByAddress = {}; } /** - * Returns an owner's ERC20 token balance + * Returns an owner's ERC20 token balance. */ public async getBalanceAsync(tokenAddress: string, ownerAddress: string): Promise { assert.isETHAddressHex('ownerAddress', ownerAddress); @@ -35,7 +35,7 @@ export class TokenWrapper extends ContractWrapper { } /** * Retrieves the allowance in baseUnits of the ERC20 token set to the 0x proxy contract - * by an owner address + * by an owner address. */ public async getProxyAllowanceAsync(tokenAddress: string, ownerAddress: string) { assert.isETHAddressHex('ownerAddress', ownerAddress); -- cgit From d1d66f563581c7f56d4956903cfb1b23d149cf18 Mon Sep 17 00:00:00 2001 From: Fabio Berger Date: Wed, 31 May 2017 10:33:22 +0200 Subject: Remove superfluous undefined check --- src/contract_wrappers/token_wrapper.ts | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/contract_wrappers/token_wrapper.ts b/src/contract_wrappers/token_wrapper.ts index 9e496b648..928b67a03 100644 --- a/src/contract_wrappers/token_wrapper.ts +++ b/src/contract_wrappers/token_wrapper.ts @@ -30,7 +30,7 @@ export class TokenWrapper extends ContractWrapper { let balance = await tokenContract.balanceOf.call(ownerAddress); // The BigNumber instance returned by Web3 is of a much older version then our own, we therefore // should always re-instantiate the returned BigNumber after retrieval. - balance = _.isUndefined(balance) ? new BigNumber(0) : new BigNumber(balance); + balance = new BigNumber(balance); return balance; } /** @@ -44,9 +44,7 @@ export class TokenWrapper extends ContractWrapper { const tokenContract = await this.getTokenContractAsync(tokenAddress); const proxyAddress = await this.getProxyAddressAsync(); let allowanceInBaseUnits = await tokenContract.allowance.call(ownerAddress, proxyAddress); - allowanceInBaseUnits = _.isUndefined(allowanceInBaseUnits) ? - new BigNumber(0) : - new BigNumber(allowanceInBaseUnits); + allowanceInBaseUnits = new BigNumber(allowanceInBaseUnits); return allowanceInBaseUnits; } /** -- cgit From 03072049d67f460e2420ed5e05a91ab1c06972ac Mon Sep 17 00:00:00 2001 From: Fabio Berger Date: Wed, 31 May 2017 10:35:40 +0200 Subject: remove InternalError type since this error could surface through the public interface --- src/contract_wrappers/token_wrapper.ts | 4 ++-- src/types.ts | 6 +----- 2 files changed, 3 insertions(+), 7 deletions(-) (limited to 'src') diff --git a/src/contract_wrappers/token_wrapper.ts b/src/contract_wrappers/token_wrapper.ts index 928b67a03..35e008905 100644 --- a/src/contract_wrappers/token_wrapper.ts +++ b/src/contract_wrappers/token_wrapper.ts @@ -6,7 +6,7 @@ import {constants} from '../utils/constants'; import {ContractWrapper} from './contract_wrapper'; import * as TokenArtifacts from '../artifacts/Token.json'; import * as ProxyArtifacts from '../artifacts/Proxy.json'; -import {TokenContract, InternalError} from '../types'; +import {TokenContract, ZeroExError} from '../types'; const ALLOWANCE_TO_ZERO_GAS_AMOUNT = 45730; @@ -85,7 +85,7 @@ export class TokenWrapper extends ContractWrapper { undefined : (ProxyArtifacts as any).networks[networkIdIfExists]; if (_.isUndefined(proxyNetworkConfigsIfExists)) { - throw new Error(InternalError.PROXY_ADDRESS_NOT_FOUND); + throw new Error(ZeroExError.CONTRACT_NOT_DEPLOYED_ON_NETWORK); } const proxyAddress = proxyNetworkConfigsIfExists.address; return proxyAddress; diff --git a/src/types.ts b/src/types.ts index bf0a1d8b4..caef50993 100644 --- a/src/types.ts +++ b/src/types.ts @@ -14,14 +14,10 @@ export const ZeroExError = strEnum([ 'UNHANDLED_ERROR', 'USER_HAS_NO_ASSOCIATED_ADDRESSES', 'INVALID_SIGNATURE', + 'CONTRACT_NOT_DEPLOYED_ON_NETWORK', ]); export type ZeroExError = keyof typeof ZeroExError; -export const InternalError = strEnum([ - 'PROXY_ADDRESS_NOT_FOUND', -]); -export type InternalError = keyof typeof InternalError; - /** * Elliptic Curve signature */ -- cgit From 2acdc627e2eaa1d562de6243045c701caa9468c8 Mon Sep 17 00:00:00 2001 From: Fabio Berger Date: Wed, 31 May 2017 10:43:09 +0200 Subject: Add TxOpts type --- src/types.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/types.ts b/src/types.ts index caef50993..3619c800f 100644 --- a/src/types.ts +++ b/src/types.ts @@ -38,7 +38,7 @@ export interface TokenContract { allowance: { call: (ownerAddress: string, allowedAddress: string) => Promise; }; - approve: (proxyAddress: string, amountInBaseUnits: BigNumber.BigNumber, opts: any) => void; + approve: (proxyAddress: string, amountInBaseUnits: BigNumber.BigNumber, txOpts: TxOpts) => void; } export interface TokenRegistryContract { @@ -66,3 +66,8 @@ export interface Token { decimals: number; url: string; }; + +export interface TxOpts { + from: string; + gas?: number; +}; -- cgit