diff options
author | Leonid Logvinov <logvinov.leon@gmail.com> | 2017-07-12 05:16:09 +0800 |
---|---|---|
committer | Leonid Logvinov <logvinov.leon@gmail.com> | 2017-07-12 08:54:57 +0800 |
commit | 6033c1a5d5f494d1ed27b00488ca458952341ae6 (patch) | |
tree | 17b53c7971047ca57b235c5351e972da721c09a4 | |
parent | 38fbf028a6699c7d8e853db4699a37cdf60e70c2 (diff) | |
download | dexon-sol-tools-6033c1a5d5f494d1ed27b00488ca458952341ae6.tar dexon-sol-tools-6033c1a5d5f494d1ed27b00488ca458952341ae6.tar.gz dexon-sol-tools-6033c1a5d5f494d1ed27b00488ca458952341ae6.tar.bz2 dexon-sol-tools-6033c1a5d5f494d1ed27b00488ca458952341ae6.tar.lz dexon-sol-tools-6033c1a5d5f494d1ed27b00488ca458952341ae6.tar.xz dexon-sol-tools-6033c1a5d5f494d1ed27b00488ca458952341ae6.tar.zst dexon-sol-tools-6033c1a5d5f494d1ed27b00488ca458952341ae6.zip |
Use PascalCase names as string enum keys
-rw-r--r-- | src/0x.ts | 2 | ||||
-rw-r--r-- | src/contract_wrappers/contract_wrapper.ts | 6 | ||||
-rw-r--r-- | src/contract_wrappers/ether_token_wrapper.ts | 4 | ||||
-rw-r--r-- | src/contract_wrappers/exchange_wrapper.ts | 80 | ||||
-rw-r--r-- | src/contract_wrappers/token_wrapper.ts | 8 | ||||
-rw-r--r-- | src/types.ts | 70 | ||||
-rw-r--r-- | src/utils/assert.ts | 3 | ||||
-rw-r--r-- | src/utils/decorators.ts | 4 | ||||
-rw-r--r-- | src/utils/utils.ts | 24 | ||||
-rw-r--r-- | test/ether_token_wrapper_test.ts | 4 | ||||
-rw-r--r-- | test/exchange_wrapper_test.ts | 34 | ||||
-rw-r--r-- | test/token_wrapper_test.ts | 20 | ||||
-rw-r--r-- | test/utils/token_utils.ts | 2 |
13 files changed, 144 insertions, 117 deletions
@@ -242,6 +242,6 @@ export class ZeroEx { } } - throw new Error(ZeroExError.INVALID_SIGNATURE); + throw new Error(ZeroExError.InvalidSignature); } } diff --git a/src/contract_wrappers/contract_wrapper.ts b/src/contract_wrappers/contract_wrapper.ts index f9c1bc1cf..1cce1b235 100644 --- a/src/contract_wrappers/contract_wrapper.ts +++ b/src/contract_wrappers/contract_wrapper.ts @@ -28,7 +28,7 @@ export class ContractWrapper { if (!_.isUndefined(contractAddress)) { const doesContractExist = await this._web3Wrapper.doesContractExistAtAddressAsync(contractAddress); if (!doesContractExist) { - throw new Error(ZeroExError.CONTRACT_DOES_NOT_EXIST); + throw new Error(ZeroExError.ContractDoesNotExist); } } @@ -38,10 +38,10 @@ export class ContractWrapper { } catch (err) { const errMsg = `${err}`; if (_.includes(errMsg, 'not been deployed to detected network')) { - throw new Error(ZeroExError.CONTRACT_DOES_NOT_EXIST); + throw new Error(ZeroExError.ContractDoesNotExist); } else { utils.consoleLog(`Notice: Error encountered: ${err} ${err.stack}`); - throw new Error(ZeroExError.UNHANDLED_ERROR); + throw new Error(ZeroExError.UnhandledError); } } } diff --git a/src/contract_wrappers/ether_token_wrapper.ts b/src/contract_wrappers/ether_token_wrapper.ts index 03d714bd7..ee0ac2d8c 100644 --- a/src/contract_wrappers/ether_token_wrapper.ts +++ b/src/contract_wrappers/ether_token_wrapper.ts @@ -29,7 +29,7 @@ export class EtherTokenWrapper extends ContractWrapper { await assert.isSenderAddressAsync('depositor', depositor, this._web3Wrapper); const ethBalanceInWei = await this._web3Wrapper.getBalanceInWeiAsync(depositor); - assert.assert(ethBalanceInWei.gte(amountInWei), ZeroExError.INSUFFICIENT_ETH_BALANCE_FOR_DEPOSIT); + assert.assert(ethBalanceInWei.gte(amountInWei), ZeroExError.InsufficientEthBalanceForDeposit); const wethContract = await this._getEtherTokenContractAsync(); await wethContract.deposit({ @@ -49,7 +49,7 @@ export class EtherTokenWrapper extends ContractWrapper { const wethContractAddress = await this.getContractAddressAsync(); const WETHBalanceInBaseUnits = await this._tokenWrapper.getBalanceAsync(wethContractAddress, withdrawer); - assert.assert(WETHBalanceInBaseUnits.gte(amountInWei), ZeroExError.INSUFFICIENT_WETH_BALANCE_FOR_WITHDRAWAL); + assert.assert(WETHBalanceInBaseUnits.gte(amountInWei), ZeroExError.InsufficientWEthBalanceForWithdrawal); const wethContract = await this._getEtherTokenContractAsync(); await wethContract.withdraw(amountInWei, { diff --git a/src/contract_wrappers/exchange_wrapper.ts b/src/contract_wrappers/exchange_wrapper.ts index a6eab0fa2..2501ef24b 100644 --- a/src/contract_wrappers/exchange_wrapper.ts +++ b/src/contract_wrappers/exchange_wrapper.ts @@ -57,12 +57,12 @@ import * as ExchangeArtifacts from '../artifacts/Exchange.json'; */ export class ExchangeWrapper extends ContractWrapper { private _exchangeContractErrCodesToMsg = { - [ExchangeContractErrCodes.ERROR_FILL_EXPIRED]: ExchangeContractErrs.ORDER_FILL_EXPIRED, - [ExchangeContractErrCodes.ERROR_CANCEL_EXPIRED]: ExchangeContractErrs.ORDER_FILL_EXPIRED, - [ExchangeContractErrCodes.ERROR_FILL_NO_VALUE]: ExchangeContractErrs.ORDER_REMAINING_FILL_AMOUNT_ZERO, - [ExchangeContractErrCodes.ERROR_CANCEL_NO_VALUE]: ExchangeContractErrs.ORDER_REMAINING_FILL_AMOUNT_ZERO, - [ExchangeContractErrCodes.ERROR_FILL_TRUNCATION]: ExchangeContractErrs.ORDER_FILL_ROUNDING_ERROR, - [ExchangeContractErrCodes.ERROR_FILL_BALANCE_ALLOWANCE]: ExchangeContractErrs.FILL_BALANCE_ALLOWANCE_ERROR, + [ExchangeContractErrCodes.ERROR_FILL_EXPIRED]: ExchangeContractErrs.OrderFillExpired, + [ExchangeContractErrCodes.ERROR_CANCEL_EXPIRED]: ExchangeContractErrs.OrderFillExpired, + [ExchangeContractErrCodes.ERROR_FILL_NO_VALUE]: ExchangeContractErrs.OrderRemainingFillAmountZero, + [ExchangeContractErrCodes.ERROR_CANCEL_NO_VALUE]: ExchangeContractErrs.OrderRemainingFillAmountZero, + [ExchangeContractErrCodes.ERROR_FILL_TRUNCATION]: ExchangeContractErrs.OrderFillRoundingError, + [ExchangeContractErrCodes.ERROR_FILL_BALANCE_ALLOWANCE]: ExchangeContractErrs.FillBalanceAllowanceError, }; private _exchangeContractIfExists?: ExchangeContract; private _exchangeLogEventEmitters: ContractEventEmitter[]; @@ -220,7 +220,10 @@ export class ExchangeWrapper extends ContractWrapper { assert.doesConformToSchema('signedOrders', signedOrders, signedOrdersSchema); const takerTokenAddresses = _.map(signedOrders, signedOrder => signedOrder.takerTokenAddress); assert.hasAtMostOneUniqueValue(takerTokenAddresses, - ExchangeContractErrs.MULTIPLE_TAKER_TOKENS_IN_FILL_UP_TO_DISALLOWED); + ExchangeContractErrs.MultipleTakerTokensInFillUpToDisallowed); + const exchangeContractAddresses = _.map(signedOrders, signedOrder => signedOrder.exchangeContractAddress); + assert.hasAtMostOneUniqueValue(exchangeContractAddresses, + ExchangeContractErrs.BatchOrdersMustHaveSameExchangeAddress); assert.isBigNumber('fillTakerTokenAmount', fillTakerTokenAmount); assert.isBoolean('shouldThrowOnInsufficientBalanceOrAllowance', shouldThrowOnInsufficientBalanceOrAllowance); await assert.isSenderAddressAsync('takerAddress', takerAddress, this._web3Wrapper); @@ -300,6 +303,12 @@ export class ExchangeWrapper extends ContractWrapper { shouldThrowOnInsufficientBalanceOrAllowance: boolean, takerAddress: string): Promise<void> { assert.doesConformToSchema('orderFillRequests', orderFillRequests, orderFillRequestsSchema); + const exchangeContractAddresses = _.map( + orderFillRequests, + orderFillRequest => orderFillRequest.signedOrder.exchangeContractAddress, + ); + assert.hasAtMostOneUniqueValue(exchangeContractAddresses, + ExchangeContractErrs.BatchOrdersMustHaveSameExchangeAddress); assert.isBoolean('shouldThrowOnInsufficientBalanceOrAllowance', shouldThrowOnInsufficientBalanceOrAllowance); await assert.isSenderAddressAsync('takerAddress', takerAddress, this._web3Wrapper); for (const orderFillRequest of orderFillRequests) { @@ -416,7 +425,7 @@ export class ExchangeWrapper extends ContractWrapper { orderFillOrKillRequest => orderFillOrKillRequest.signedOrder.exchangeContractAddress, ); assert.hasAtMostOneUniqueValue(exchangeContractAddresses, - ExchangeContractErrs.BATCH_ORDERS_MUST_HAVE_SAME_EXCHANGE_ADDRESS); + ExchangeContractErrs.BatchOrdersMustHaveSameExchangeAddress); await assert.isSenderAddressAsync('takerAddress', takerAddress, this._web3Wrapper); if (_.isEmpty(orderFillOrKillRequests)) { return; // no-op @@ -520,9 +529,9 @@ export class ExchangeWrapper extends ContractWrapper { orderCancellationRequest => orderCancellationRequest.order.exchangeContractAddress, ); assert.hasAtMostOneUniqueValue(exchangeContractAddresses, - ExchangeContractErrs.BATCH_ORDERS_MUST_HAVE_SAME_EXCHANGE_ADDRESS); + ExchangeContractErrs.BatchOrdersMustHaveSameExchangeAddress); const makers = _.map(orderCancellationRequests, cancellationRequest => cancellationRequest.order.maker); - assert.hasAtMostOneUniqueValue(makers, ExchangeContractErrs.MULTIPLE_MAKERS_IN_SINGLE_CANCEL_BATCH_DISALLOWED); + assert.hasAtMostOneUniqueValue(makers, ExchangeContractErrs.MultipleMakersInSingleCancelBatchDisallowed); const maker = makers[0]; await assert.isSenderAddressAsync('maker', maker, this._web3Wrapper); for (const cancellationRequest of orderCancellationRequests) { @@ -648,14 +657,14 @@ export class ExchangeWrapper extends ContractWrapper { fillTakerAmount: BigNumber.BigNumber, senderAddress: string): Promise<void> { if (fillTakerAmount.eq(0)) { - throw new Error(ExchangeContractErrs.ORDER_REMAINING_FILL_AMOUNT_ZERO); + throw new Error(ExchangeContractErrs.OrderRemainingFillAmountZero); } if (signedOrder.taker !== constants.NULL_ADDRESS && signedOrder.taker !== senderAddress) { - throw new Error(ExchangeContractErrs.TRANSACTION_SENDER_IS_NOT_FILL_ORDER_TAKER); + throw new Error(ExchangeContractErrs.TransactionSenderIsNotFillOrderTaker); } const currentUnixTimestampSec = utils.getCurrentUnixTimestamp(); if (signedOrder.expirationUnixTimestampSec.lessThan(currentUnixTimestampSec)) { - throw new Error(ExchangeContractErrs.ORDER_FILL_EXPIRED); + throw new Error(ExchangeContractErrs.OrderFillExpired); } const zrxTokenAddress = await this._getZRXTokenAddressAsync(signedOrder.exchangeContractAddress); await this._validateFillOrderBalancesAndAllowancesAndThrowIfInvalidAsync(signedOrder, fillTakerAmount, @@ -665,22 +674,22 @@ export class ExchangeWrapper extends ContractWrapper { signedOrder.takerTokenAmount, fillTakerAmount, signedOrder.makerTokenAmount, ); if (wouldRoundingErrorOccur) { - throw new Error(ExchangeContractErrs.ORDER_FILL_ROUNDING_ERROR); + throw new Error(ExchangeContractErrs.OrderFillRoundingError); } } private async _validateCancelOrderAndThrowIfInvalidAsync( order: Order, takerTokenCancelAmount: BigNumber.BigNumber): Promise<void> { if (takerTokenCancelAmount.eq(0)) { - throw new Error(ExchangeContractErrs.ORDER_CANCEL_AMOUNT_ZERO); + throw new Error(ExchangeContractErrs.OrderCancelAmountZero); } const orderHash = utils.getOrderHashHex(order); const unavailableAmount = await this.getUnavailableTakerAmountAsync(orderHash); if (order.takerTokenAmount.minus(unavailableAmount).eq(0)) { - throw new Error(ExchangeContractErrs.ORDER_ALREADY_CANCELLED_OR_FILLED); + throw new Error(ExchangeContractErrs.OrderAlreadyCancelledOrFilled); } const currentUnixTimestampSec = utils.getCurrentUnixTimestamp(); if (order.expirationUnixTimestampSec.lessThan(currentUnixTimestampSec)) { - throw new Error(ExchangeContractErrs.ORDER_CANCEL_EXPIRED); + throw new Error(ExchangeContractErrs.OrderCancelExpired); } } private async _validateFillOrKillOrderAndThrowIfInvalidAsync(signedOrder: SignedOrder, @@ -690,7 +699,7 @@ export class ExchangeWrapper extends ContractWrapper { const unavailableTakerAmount = await this.getUnavailableTakerAmountAsync(orderHashHex); const remainingTakerAmount = signedOrder.takerTokenAmount.minus(unavailableTakerAmount); if (remainingTakerAmount < fillTakerAmount) { - throw new Error(ExchangeContractErrs.INSUFFICIENT_REMAINING_FILL_AMOUNT); + throw new Error(ExchangeContractErrs.InsufficientRemainingFillAmount); } } /** @@ -721,16 +730,16 @@ export class ExchangeWrapper extends ContractWrapper { const fillMakerAmountInBaseUnits = fillTakerAmount.div(exchangeRate); if (fillTakerAmount.greaterThan(takerBalance)) { - throw new Error(ExchangeContractErrs.INSUFFICIENT_TAKER_BALANCE); + throw new Error(ExchangeContractErrs.InsufficientTakerBalance); } if (fillTakerAmount.greaterThan(takerAllowance)) { - throw new Error(ExchangeContractErrs.INSUFFICIENT_TAKER_ALLOWANCE); + throw new Error(ExchangeContractErrs.InsufficientTakerAllowance); } if (fillMakerAmountInBaseUnits.greaterThan(makerBalance)) { - throw new Error(ExchangeContractErrs.INSUFFICIENT_MAKER_BALANCE); + throw new Error(ExchangeContractErrs.InsufficientMakerBalance); } if (fillMakerAmountInBaseUnits.greaterThan(makerAllowance)) { - throw new Error(ExchangeContractErrs.INSUFFICIENT_MAKER_ALLOWANCE); + throw new Error(ExchangeContractErrs.InsufficientMakerAllowance); } const makerFeeBalance = await this._tokenWrapper.getBalanceAsync(zrxTokenAddress, @@ -742,16 +751,16 @@ export class ExchangeWrapper extends ContractWrapper { senderAddress); if (signedOrder.takerFee.greaterThan(takerFeeBalance)) { - throw new Error(ExchangeContractErrs.INSUFFICIENT_TAKER_FEE_BALANCE); + throw new Error(ExchangeContractErrs.InsufficientTakerFeeBalance); } if (signedOrder.takerFee.greaterThan(takerFeeAllowance)) { - throw new Error(ExchangeContractErrs.INSUFFICIENT_TAKER_FEE_ALLOWANCE); + throw new Error(ExchangeContractErrs.InsufficientTakerFeeAllowance); } if (signedOrder.makerFee.greaterThan(makerFeeBalance)) { - throw new Error(ExchangeContractErrs.INSUFFICIENT_MAKER_FEE_BALANCE); + throw new Error(ExchangeContractErrs.InsufficientMakerFeeBalance); } if (signedOrder.makerFee.greaterThan(makerFeeAllowance)) { - throw new Error(ExchangeContractErrs.INSUFFICIENT_MAKER_FEE_ALLOWANCE); + throw new Error(ExchangeContractErrs.InsufficientMakerFeeAllowance); } } private _throwErrorLogsAsErrors(logs: ContractEvent[]): void { @@ -777,8 +786,27 @@ export class ExchangeWrapper extends ContractWrapper { return this._exchangeContractIfExists; } const contractInstance = await this._instantiateContractIfExistsAsync((ExchangeArtifacts as any)); +<<<<<<< HEAD this._exchangeContractIfExists = contractInstance as ExchangeContract; return this._exchangeContractIfExists; +======= + this._exchangeContractByAddress[exchangeContractAddress] = contractInstance as ExchangeContract; + return this._exchangeContractByAddress[exchangeContractAddress]; + } + private _getExchangeArtifactsByAddressOrThrow(exchangeContractAddress: string): ContractArtifact { + const exchangeArtifacts = _.values<ContractArtifact>(ExchangeArtifactsByName); + for (const exchangeArtifact of exchangeArtifacts) { + const networkSpecificExchangeArtifactValues = _.values(exchangeArtifact.networks); + const exchangeAddressesInArtifact = _.map( + networkSpecificExchangeArtifactValues, + networkSpecificExchangeArtifact => networkSpecificExchangeArtifact.address, + ); + if (_.includes(exchangeAddressesInArtifact, exchangeContractAddress)) { + return exchangeArtifact; + } + } + throw new Error(ZeroExError.ExchangeContractDoesNotExist); +>>>>>>> Use PascalCase names as string enum keys } private async _getZRXTokenAddressAsync(exchangeContractAddress: string): Promise<string> { const exchangeInstance = await this._getExchangeContractAsync(); diff --git a/src/contract_wrappers/token_wrapper.ts b/src/contract_wrappers/token_wrapper.ts index fdf711823..9c073f30b 100644 --- a/src/contract_wrappers/token_wrapper.ts +++ b/src/contract_wrappers/token_wrapper.ts @@ -146,7 +146,7 @@ export class TokenWrapper extends ContractWrapper { const fromAddressBalance = await this.getBalanceAsync(tokenAddress, fromAddress); if (fromAddressBalance.lessThan(amountInBaseUnits)) { - throw new Error(ZeroExError.INSUFFICIENT_BALANCE_FOR_TRANSFER); + throw new Error(ZeroExError.InsufficientBalanceForTransfer); } await tokenContract.transfer(toAddress, amountInBaseUnits, { @@ -178,12 +178,12 @@ export class TokenWrapper extends ContractWrapper { const fromAddressAllowance = await this.getAllowanceAsync(tokenAddress, fromAddress, senderAddress); if (fromAddressAllowance.lessThan(amountInBaseUnits)) { - throw new Error(ZeroExError.INSUFFICIENT_ALLOWANCE_FOR_TRANSFER); + throw new Error(ZeroExError.InsufficientAllowanceForTransfer); } const fromAddressBalance = await this.getBalanceAsync(tokenAddress, fromAddress); if (fromAddressBalance.lessThan(amountInBaseUnits)) { - throw new Error(ZeroExError.INSUFFICIENT_BALANCE_FOR_TRANSFER); + throw new Error(ZeroExError.InsufficientBalanceForTransfer); } await tokenContract.transferFrom(fromAddress, toAddress, amountInBaseUnits, { @@ -252,7 +252,7 @@ export class TokenWrapper extends ContractWrapper { undefined : (ProxyArtifacts as any).networks[networkIdIfExists]; if (_.isUndefined(proxyNetworkConfigsIfExists)) { - throw new Error(ZeroExError.CONTRACT_NOT_DEPLOYED_ON_NETWORK); + throw new Error(ZeroExError.ContractNotDeployedOnNetwork); } const proxyAddress = proxyNetworkConfigsIfExists.address; return proxyAddress; diff --git a/src/types.ts b/src/types.ts index db25c4f64..974057fed 100644 --- a/src/types.ts +++ b/src/types.ts @@ -2,19 +2,19 @@ import * as _ from 'lodash'; import * as Web3 from 'web3'; export enum ZeroExError { - CONTRACT_DOES_NOT_EXIST = 'CONTRACT_DOES_NOT_EXIST', - EXCHANGE_CONTRACT_DOES_NOT_EXIST = 'EXCHANGE_CONTRACT_DOES_NOT_EXIST', - UNHANDLED_ERROR = 'UNHANDLED_ERROR', - USER_HAS_NO_ASSOCIATED_ADDRESSES = 'USER_HAS_NO_ASSOCIATED_ADDRESSES', - INVALID_SIGNATURE = 'INVALID_SIGNATURE', - CONTRACT_NOT_DEPLOYED_ON_NETWORK = 'CONTRACT_NOT_DEPLOYED_ON_NETWORK', - ZRX_NOT_IN_TOKEN_REGISTRY = 'ZRX_NOT_IN_TOKEN_REGISTRY', - INSUFFICIENT_ALLOWANCE_FOR_TRANSFER = 'INSUFFICIENT_ALLOWANCE_FOR_TRANSFER', - INSUFFICIENT_BALANCE_FOR_TRANSFER = 'INSUFFICIENT_BALANCE_FOR_TRANSFER', - INSUFFICIENT_ETH_BALANCE_FOR_DEPOSIT = 'INSUFFICIENT_ETH_BALANCE_FOR_DEPOSIT', - INSUFFICIENT_WETH_BALANCE_FOR_WITHDRAWAL = 'INSUFFICIENT_WETH_BALANCE_FOR_WITHDRAWAL', - INVALID_JUMP = 'INVALID_JUMP', - OUT_OF_GAS = 'OUT_OF_GAS', + ContractDoesNotExist = 'CONTRACT_DOES_NOT_EXIST', + ExchangeContractDoesNotExist = 'EXCHANGE_CONTRACT_DOES_NOT_EXIST', + UnhandledError = 'UNHANDLED_ERROR', + UserHasNoAssociatedAddress = 'USER_HAS_NO_ASSOCIATED_ADDRESSES', + InvalidSignature = 'INVALID_SIGNATURE', + ContractNotDeployedOnNetwork = 'CONTRACT_NOT_DEPLOYED_ON_NETWORK', + ZrxNotInTokenRegistry = 'ZRX_NOT_IN_TOKEN_REGISTRY', + InsufficientAllowanceForTransfer = 'INSUFFICIENT_ALLOWANCE_FOR_TRANSFER', + InsufficientBalanceForTransfer = 'INSUFFICIENT_BALANCE_FOR_TRANSFER', + InsufficientEthBalanceForDeposit = 'INSUFFICIENT_ETH_BALANCE_FOR_DEPOSIT', + InsufficientWEthBalanceForWithdrawal = 'INSUFFICIENT_WETH_BALANCE_FOR_WITHDRAWAL', + InvalidJump = 'INVALID_JUMP', + OutOfGas = 'OUT_OF_GAS', } /** @@ -163,8 +163,8 @@ export interface ProxyContract extends ContractInstance { } export enum SolidityTypes { - address = 'address', - uint256 = 'uint256', + Address = 'address', + Uint256 = 'uint256', } export enum ExchangeContractErrCodes { @@ -177,26 +177,26 @@ export enum ExchangeContractErrCodes { } export enum ExchangeContractErrs { - ORDER_FILL_EXPIRED = 'ORDER_FILL_EXPIRED', - ORDER_CANCEL_EXPIRED = 'ORDER_CANCEL_EXPIRED', - ORDER_CANCEL_AMOUNT_ZERO = 'ORDER_CANCEL_AMOUNT_ZERO', - ORDER_ALREADY_CANCELLED_OR_FILLED = 'ORDER_ALREADY_CANCELLED_OR_FILLED', - ORDER_REMAINING_FILL_AMOUNT_ZERO = 'ORDER_REMAINING_FILL_AMOUNT_ZERO', - ORDER_FILL_ROUNDING_ERROR = 'ORDER_FILL_ROUNDING_ERROR', - FILL_BALANCE_ALLOWANCE_ERROR = 'FILL_BALANCE_ALLOWANCE_ERROR', - INSUFFICIENT_TAKER_BALANCE = 'INSUFFICIENT_TAKER_BALANCE', - INSUFFICIENT_TAKER_ALLOWANCE = 'INSUFFICIENT_TAKER_ALLOWANCE', - INSUFFICIENT_MAKER_BALANCE = 'INSUFFICIENT_MAKER_BALANCE', - INSUFFICIENT_MAKER_ALLOWANCE = 'INSUFFICIENT_MAKER_ALLOWANCE', - INSUFFICIENT_TAKER_FEE_BALANCE = 'INSUFFICIENT_TAKER_FEE_BALANCE', - INSUFFICIENT_TAKER_FEE_ALLOWANCE = 'INSUFFICIENT_TAKER_FEE_ALLOWANCE', - INSUFFICIENT_MAKER_FEE_BALANCE = 'INSUFFICIENT_MAKER_FEE_BALANCE', - INSUFFICIENT_MAKER_FEE_ALLOWANCE = 'INSUFFICIENT_MAKER_FEE_ALLOWANCE', - TRANSACTION_SENDER_IS_NOT_FILL_ORDER_TAKER = 'TRANSACTION_SENDER_IS_NOT_FILL_ORDER_TAKER', - MULTIPLE_MAKERS_IN_SINGLE_CANCEL_BATCH_DISALLOWED = 'MULTIPLE_MAKERS_IN_SINGLE_CANCEL_BATCH_DISALLOWED', - INSUFFICIENT_REMAINING_FILL_AMOUNT = 'INSUFFICIENT_REMAINING_FILL_AMOUNT', - MULTIPLE_TAKER_TOKENS_IN_FILL_UP_TO_DISALLOWED = 'MULTIPLE_TAKER_TOKENS_IN_FILL_UP_TO_DISALLOWED', - BATCH_ORDERS_MUST_HAVE_SAME_EXCHANGE_ADDRESS = 'BATCH_ORDERS_MUST_HAVE_SAME_EXCHANGE_ADDRESS', + OrderFillExpired = 'ORDER_FILL_EXPIRED', + OrderCancelExpired = 'ORDER_CANCEL_EXPIRED', + OrderCancelAmountZero = 'ORDER_CANCEL_AMOUNT_ZERO', + OrderAlreadyCancelledOrFilled = 'ORDER_ALREADY_CANCELLED_OR_FILLED', + OrderRemainingFillAmountZero = 'ORDER_REMAINING_FILL_AMOUNT_ZERO', + OrderFillRoundingError = 'ORDER_FILL_ROUNDING_ERROR', + FillBalanceAllowanceError = 'FILL_BALANCE_ALLOWANCE_ERROR', + InsufficientTakerBalance = 'INSUFFICIENT_TAKER_BALANCE', + InsufficientTakerAllowance = 'INSUFFICIENT_TAKER_ALLOWANCE', + InsufficientMakerBalance = 'INSUFFICIENT_MAKER_BALANCE', + InsufficientMakerAllowance = 'INSUFFICIENT_MAKER_ALLOWANCE', + InsufficientTakerFeeBalance = 'INSUFFICIENT_TAKER_FEE_BALANCE', + InsufficientTakerFeeAllowance = 'INSUFFICIENT_TAKER_FEE_ALLOWANCE', + InsufficientMakerFeeBalance = 'INSUFFICIENT_MAKER_FEE_BALANCE', + InsufficientMakerFeeAllowance = 'INSUFFICIENT_MAKER_FEE_ALLOWANCE', + TransactionSenderIsNotFillOrderTaker = 'TRANSACTION_SENDER_IS_NOT_FILL_ORDER_TAKER', + MultipleMakersInSingleCancelBatchDisallowed = 'MULTIPLE_MAKERS_IN_SINGLE_CANCEL_BATCH_DISALLOWED', + InsufficientRemainingFillAmount = 'INSUFFICIENT_REMAINING_FILL_AMOUNT', + MultipleTakerTokensInFillUpToDisallowed = 'MULTIPLE_TAKER_TOKENS_IN_FILL_UP_TO_DISALLOWED', + BatchOrdersMustHaveSameExchangeAddress = 'BATCH_ORDERS_MUST_HAVE_SAME_EXCHANGE_ADDRESS', } export interface ContractResponse { diff --git a/src/utils/assert.ts b/src/utils/assert.ts index bdd38721e..9ac021d4a 100644 --- a/src/utils/assert.ts +++ b/src/utils/assert.ts @@ -4,7 +4,6 @@ import * as Web3 from 'web3'; import {Web3Wrapper} from '../web3_wrapper'; import {SchemaValidator} from './schema_validator'; import {utils} from './utils'; -import {StringEnum} from '../types'; const HEX_REGEX = /^0x[0-9A-F]*$/i; @@ -27,7 +26,7 @@ export const assert = { const web3 = new Web3(); this.assert(web3.isAddress(value), this.typeAssertionMessage(variableName, 'ETHAddressHex', value)); }, - doesBelongToStringEnum(variableName: string, value: string, stringEnum: StringEnum): void { + doesBelongToStringEnum(variableName: string, value: string, stringEnum: any): void { const doesBelongToStringEnum = !_.isUndefined(stringEnum[value]); const enumValues = _.keys(stringEnum); const enumValuesAsStrings = _.map(enumValues, enumValue => `'${enumValue}'`); diff --git a/src/utils/decorators.ts b/src/utils/decorators.ts index a25f2cff5..ec750b891 100644 --- a/src/utils/decorators.ts +++ b/src/utils/decorators.ts @@ -21,10 +21,10 @@ export const decorators = { return result; } catch (error) { if (_.includes(error.message, constants.INVALID_JUMP_PATTERN)) { - throw new Error(ZeroExError.INVALID_JUMP); + throw new Error(ZeroExError.InvalidJump); } if (_.includes(error.message, constants.OUT_OF_GAS_PATTERN)) { - throw new Error(ZeroExError.OUT_OF_GAS); + throw new Error(ZeroExError.OutOfGas); } throw error; } diff --git a/src/utils/utils.ts b/src/utils/utils.ts index ecc171bfe..6702de218 100644 --- a/src/utils/utils.ts +++ b/src/utils/utils.ts @@ -30,18 +30,18 @@ export const utils = { }, getOrderHashHex(order: Order|SignedOrder): string { const orderParts = [ - {value: order.exchangeContractAddress, type: SolidityTypes.address}, - {value: order.maker, type: SolidityTypes.address}, - {value: order.taker, type: SolidityTypes.address}, - {value: order.makerTokenAddress, type: SolidityTypes.address}, - {value: order.takerTokenAddress, type: SolidityTypes.address}, - {value: order.feeRecipient, type: SolidityTypes.address}, - {value: utils.bigNumberToBN(order.makerTokenAmount), type: SolidityTypes.uint256}, - {value: utils.bigNumberToBN(order.takerTokenAmount), type: SolidityTypes.uint256}, - {value: utils.bigNumberToBN(order.makerFee), type: SolidityTypes.uint256}, - {value: utils.bigNumberToBN(order.takerFee), type: SolidityTypes.uint256}, - {value: utils.bigNumberToBN(order.expirationUnixTimestampSec), type: SolidityTypes.uint256}, - {value: utils.bigNumberToBN(order.salt), type: SolidityTypes.uint256}, + {value: order.exchangeContractAddress, type: SolidityTypes.Address}, + {value: order.maker, type: SolidityTypes.Address}, + {value: order.taker, type: SolidityTypes.Address}, + {value: order.makerTokenAddress, type: SolidityTypes.Address}, + {value: order.takerTokenAddress, type: SolidityTypes.Address}, + {value: order.feeRecipient, type: SolidityTypes.Address}, + {value: utils.bigNumberToBN(order.makerTokenAmount), type: SolidityTypes.Uint256}, + {value: utils.bigNumberToBN(order.takerTokenAmount), type: SolidityTypes.Uint256}, + {value: utils.bigNumberToBN(order.makerFee), type: SolidityTypes.Uint256}, + {value: utils.bigNumberToBN(order.takerFee), type: SolidityTypes.Uint256}, + {value: utils.bigNumberToBN(order.expirationUnixTimestampSec), type: SolidityTypes.Uint256}, + {value: utils.bigNumberToBN(order.salt), type: SolidityTypes.Uint256}, ]; const types = _.map(orderParts, o => o.type); const values = _.map(orderParts, o => o.value); diff --git a/test/ether_token_wrapper_test.ts b/test/ether_token_wrapper_test.ts index ebce81e97..5ed800fc7 100644 --- a/test/ether_token_wrapper_test.ts +++ b/test/ether_token_wrapper_test.ts @@ -66,7 +66,7 @@ describe('EtherTokenWrapper', () => { return expect( zeroEx.etherToken.depositAsync(overETHBalanceinWei, addressWithETH), - ).to.be.rejectedWith(ZeroExError.INSUFFICIENT_ETH_BALANCE_FOR_DEPOSIT); + ).to.be.rejectedWith(ZeroExError.InsufficientEthBalanceForDeposit); }); }); describe('#withdrawAsync', () => { @@ -100,7 +100,7 @@ describe('EtherTokenWrapper', () => { return expect( zeroEx.etherToken.withdrawAsync(overWETHBalance, addressWithETH), - ).to.be.rejectedWith(ZeroExError.INSUFFICIENT_WETH_BALANCE_FOR_WITHDRAWAL); + ).to.be.rejectedWith(ZeroExError.InsufficientWEthBalanceForWithdrawal); }); }); }); diff --git a/test/exchange_wrapper_test.ts b/test/exchange_wrapper_test.ts index eb07b09ba..0dc67bb01 100644 --- a/test/exchange_wrapper_test.ts +++ b/test/exchange_wrapper_test.ts @@ -111,7 +111,7 @@ describe('ExchangeWrapper', () => { return expect(zeroEx.exchange.fillOrKillOrderAsync( signedOrder, tooLargeFillAmount, takerAddress, - )).to.be.rejectedWith(ExchangeContractErrs.INSUFFICIENT_REMAINING_FILL_AMOUNT); + )).to.be.rejectedWith(ExchangeContractErrs.InsufficientRemainingFillAmount); }); }); describe('successful fills', () => { @@ -183,7 +183,7 @@ describe('ExchangeWrapper', () => { const zeroFillAmount = new BigNumber(0); return expect(zeroEx.exchange.fillOrderAsync( signedOrder, zeroFillAmount, shouldCheckTransfer, takerAddress, - )).to.be.rejectedWith(ExchangeContractErrs.ORDER_REMAINING_FILL_AMOUNT_ZERO); + )).to.be.rejectedWith(ExchangeContractErrs.OrderRemainingFillAmountZero); }); it('should throw when sender is not a taker', async () => { const signedOrder = await fillScenarios.createFillableSignedOrderAsync( @@ -192,7 +192,7 @@ describe('ExchangeWrapper', () => { const nonTakerAddress = userAddresses[6]; return expect(zeroEx.exchange.fillOrderAsync( signedOrder, fillTakerAmount, shouldCheckTransfer, nonTakerAddress, - )).to.be.rejectedWith(ExchangeContractErrs.TRANSACTION_SENDER_IS_NOT_FILL_ORDER_TAKER); + )).to.be.rejectedWith(ExchangeContractErrs.TransactionSenderIsNotFillOrderTaker); }); it('should throw when order is expired', async () => { const expirationInPast = new BigNumber(1496826058); // 7th Jun 2017 @@ -202,7 +202,7 @@ describe('ExchangeWrapper', () => { ); return expect(zeroEx.exchange.fillOrderAsync( signedOrder, fillTakerAmount, shouldCheckTransfer, takerAddress, - )).to.be.rejectedWith(ExchangeContractErrs.ORDER_FILL_EXPIRED); + )).to.be.rejectedWith(ExchangeContractErrs.OrderFillExpired); }); describe('should throw when not enough balance or allowance to fulfill the order', () => { const balanceToSubtractFromMaker = new BigNumber(3); @@ -219,7 +219,7 @@ describe('ExchangeWrapper', () => { ); return expect(zeroEx.exchange.fillOrderAsync( signedOrder, fillTakerAmount, shouldCheckTransfer, takerAddress, - )).to.be.rejectedWith(ExchangeContractErrs.INSUFFICIENT_TAKER_BALANCE); + )).to.be.rejectedWith(ExchangeContractErrs.InsufficientTakerBalance); }); it('should throw when taker allowance is less than fill amount', async () => { const newAllowanceWhichIsLessThanFillAmount = fillTakerAmount.minus(lackingAllowance); @@ -227,7 +227,7 @@ describe('ExchangeWrapper', () => { newAllowanceWhichIsLessThanFillAmount); return expect(zeroEx.exchange.fillOrderAsync( signedOrder, fillTakerAmount, shouldCheckTransfer, takerAddress, - )).to.be.rejectedWith(ExchangeContractErrs.INSUFFICIENT_TAKER_ALLOWANCE); + )).to.be.rejectedWith(ExchangeContractErrs.InsufficientTakerAllowance); }); it('should throw when maker balance is less than maker fill amount', async () => { await zeroEx.token.transferAsync( @@ -235,7 +235,7 @@ describe('ExchangeWrapper', () => { ); return expect(zeroEx.exchange.fillOrderAsync( signedOrder, fillTakerAmount, shouldCheckTransfer, takerAddress, - )).to.be.rejectedWith(ExchangeContractErrs.INSUFFICIENT_MAKER_BALANCE); + )).to.be.rejectedWith(ExchangeContractErrs.InsufficientMakerBalance); }); it('should throw when maker allowance is less than maker fill amount', async () => { const newAllowanceWhichIsLessThanFillAmount = fillTakerAmount.minus(lackingAllowance); @@ -243,7 +243,7 @@ describe('ExchangeWrapper', () => { newAllowanceWhichIsLessThanFillAmount); return expect(zeroEx.exchange.fillOrderAsync( signedOrder, fillTakerAmount, shouldCheckTransfer, takerAddress, - )).to.be.rejectedWith(ExchangeContractErrs.INSUFFICIENT_MAKER_ALLOWANCE); + )).to.be.rejectedWith(ExchangeContractErrs.InsufficientMakerAllowance); }); }); it('should throw when there a rounding error would have occurred', async () => { @@ -256,7 +256,7 @@ describe('ExchangeWrapper', () => { const fillTakerAmountThatCausesRoundingError = new BigNumber(3); return expect(zeroEx.exchange.fillOrderAsync( signedOrder, fillTakerAmountThatCausesRoundingError, shouldCheckTransfer, takerAddress, - )).to.be.rejectedWith(ExchangeContractErrs.ORDER_FILL_ROUNDING_ERROR); + )).to.be.rejectedWith(ExchangeContractErrs.OrderFillRoundingError); }); describe('should throw when not enough balance or allowance to pay fees', () => { const makerFee = new BigNumber(2); @@ -275,7 +275,7 @@ describe('ExchangeWrapper', () => { ); return expect(zeroEx.exchange.fillOrderAsync( signedOrder, fillTakerAmount, shouldCheckTransfer, takerAddress, - )).to.be.rejectedWith(ExchangeContractErrs.INSUFFICIENT_MAKER_FEE_BALANCE); + )).to.be.rejectedWith(ExchangeContractErrs.InsufficientMakerFeeBalance); }); it('should throw when maker doesn\'t have enough allowance to pay fees', async () => { const newAllowanceWhichIsLessThanFees = makerFee.minus(1); @@ -283,7 +283,7 @@ describe('ExchangeWrapper', () => { newAllowanceWhichIsLessThanFees); return expect(zeroEx.exchange.fillOrderAsync( signedOrder, fillTakerAmount, shouldCheckTransfer, takerAddress, - )).to.be.rejectedWith(ExchangeContractErrs.INSUFFICIENT_MAKER_FEE_ALLOWANCE); + )).to.be.rejectedWith(ExchangeContractErrs.InsufficientMakerFeeAllowance); }); it('should throw when taker doesn\'t have enough balance to pay fees', async () => { const balanceToSubtractFromTaker = new BigNumber(1); @@ -292,7 +292,7 @@ describe('ExchangeWrapper', () => { ); return expect(zeroEx.exchange.fillOrderAsync( signedOrder, fillTakerAmount, shouldCheckTransfer, takerAddress, - )).to.be.rejectedWith(ExchangeContractErrs.INSUFFICIENT_TAKER_FEE_BALANCE); + )).to.be.rejectedWith(ExchangeContractErrs.InsufficientTakerFeeBalance); }); it('should throw when taker doesn\'t have enough allowance to pay fees', async () => { const newAllowanceWhichIsLessThanFees = makerFee.minus(1); @@ -300,7 +300,7 @@ describe('ExchangeWrapper', () => { newAllowanceWhichIsLessThanFees); return expect(zeroEx.exchange.fillOrderAsync( signedOrder, fillTakerAmount, shouldCheckTransfer, takerAddress, - )).to.be.rejectedWith(ExchangeContractErrs.INSUFFICIENT_TAKER_FEE_ALLOWANCE); + )).to.be.rejectedWith(ExchangeContractErrs.InsufficientTakerFeeAllowance); }); }); }); @@ -491,7 +491,7 @@ describe('ExchangeWrapper', () => { it('should throw when cancel amount is zero', async () => { const zeroCancelAmount = new BigNumber(0); return expect(zeroEx.exchange.cancelOrderAsync(signedOrder, zeroCancelAmount)) - .to.be.rejectedWith(ExchangeContractErrs.ORDER_CANCEL_AMOUNT_ZERO); + .to.be.rejectedWith(ExchangeContractErrs.OrderCancelAmountZero); }); it('should throw when order is expired', async () => { const expirationInPast = new BigNumber(1496826058); // 7th Jun 2017 @@ -501,12 +501,12 @@ describe('ExchangeWrapper', () => { ); orderHashHex = ZeroEx.getOrderHashHex(expiredSignedOrder); return expect(zeroEx.exchange.cancelOrderAsync(expiredSignedOrder, cancelAmount)) - .to.be.rejectedWith(ExchangeContractErrs.ORDER_CANCEL_EXPIRED); + .to.be.rejectedWith(ExchangeContractErrs.OrderCancelExpired); }); it('should throw when order is already cancelled or filled', async () => { await zeroEx.exchange.cancelOrderAsync(signedOrder, fillableAmount); return expect(zeroEx.exchange.cancelOrderAsync(signedOrder, fillableAmount)) - .to.be.rejectedWith(ExchangeContractErrs.ORDER_ALREADY_CANCELLED_OR_FILLED); + .to.be.rejectedWith(ExchangeContractErrs.OrderAlreadyCancelledOrFilled); }); }); describe('successful cancels', () => { @@ -552,7 +552,7 @@ describe('ExchangeWrapper', () => { order: signedOrderWithDifferentMaker, takerTokenCancelAmount: cancelAmount, }, - ])).to.be.rejectedWith(ExchangeContractErrs.MULTIPLE_MAKERS_IN_SINGLE_CANCEL_BATCH); + ])).to.be.rejectedWith(ExchangeContractErrs.MultipleMakersInSingleCancelBatchDisallowed); }); }); describe('successful batch cancels', () => { diff --git a/test/token_wrapper_test.ts b/test/token_wrapper_test.ts index 06e373bfa..19be27b93 100644 --- a/test/token_wrapper_test.ts +++ b/test/token_wrapper_test.ts @@ -64,15 +64,15 @@ describe('TokenWrapper', () => { const toAddress = coinbase; return expect(zeroEx.token.transferAsync( token.address, fromAddress, toAddress, transferAmount, - )).to.be.rejectedWith(ZeroExError.INSUFFICIENT_BALANCE_FOR_TRANSFER); + )).to.be.rejectedWith(ZeroExError.InsufficientBalanceForTransfer); }); - it('should throw a CONTRACT_DOES_NOT_EXIST error for a non-existent token contract', async () => { + it('should throw a ContractDoesNotExist error for a non-existent token contract', async () => { const nonExistentTokenAddress = '0x9dd402f14d67e001d8efbe6583e51bf9706aa065'; const fromAddress = coinbase; const toAddress = coinbase; return expect(zeroEx.token.transferAsync( nonExistentTokenAddress, fromAddress, toAddress, transferAmount, - )).to.be.rejectedWith(ZeroExError.CONTRACT_DOES_NOT_EXIST); + )).to.be.rejectedWith(ZeroExError.ContractDoesNotExist); }); }); describe('#transferFromAsync', () => { @@ -97,7 +97,7 @@ describe('TokenWrapper', () => { return expect(zeroEx.token.transferFromAsync( token.address, fromAddress, toAddress, senderAddress, transferAmount, - )).to.be.rejectedWith(ZeroExError.INSUFFICIENT_ALLOWANCE_FOR_TRANSFER); + )).to.be.rejectedWith(ZeroExError.InsufficientAllowanceForTransfer); }); it('[regression] should fail to transfer tokens if set allowance for toAddress instead of senderAddress', async () => { @@ -108,7 +108,7 @@ describe('TokenWrapper', () => { return expect(zeroEx.token.transferFromAsync( token.address, fromAddress, toAddress, senderAddress, transferAmount, - )).to.be.rejectedWith(ZeroExError.INSUFFICIENT_ALLOWANCE_FOR_TRANSFER); + )).to.be.rejectedWith(ZeroExError.InsufficientAllowanceForTransfer); }); it('should fail to transfer tokens if fromAddress has insufficient balance', async () => { const fromAddress = addressWithoutFunds; @@ -124,7 +124,7 @@ describe('TokenWrapper', () => { return expect(zeroEx.token.transferFromAsync( token.address, fromAddress, toAddress, senderAddress, transferAmount, - )).to.be.rejectedWith(ZeroExError.INSUFFICIENT_BALANCE_FOR_TRANSFER); + )).to.be.rejectedWith(ZeroExError.InsufficientBalanceForTransfer); }); it('should successfully transfer tokens', async () => { const fromAddress = coinbase; @@ -140,12 +140,12 @@ describe('TokenWrapper', () => { const postBalance = await zeroEx.token.getBalanceAsync(token.address, toAddress); return expect(postBalance).to.be.bignumber.equal(transferAmount); }); - it('should throw a CONTRACT_DOES_NOT_EXIST error for a non-existent token contract', async () => { + it('should throw a ContractDoesNotExist error for a non-existent token contract', async () => { const fromAddress = coinbase; const nonExistentTokenAddress = '0x9dd402f14d67e001d8efbe6583e51bf9706aa065'; return expect(zeroEx.token.transferFromAsync( nonExistentTokenAddress, fromAddress, toAddress, senderAddress, new BigNumber(42), - )).to.be.rejectedWith(ZeroExError.CONTRACT_DOES_NOT_EXIST); + )).to.be.rejectedWith(ZeroExError.ContractDoesNotExist); }); }); describe('#getBalanceAsync', () => { @@ -156,11 +156,11 @@ describe('TokenWrapper', () => { const expectedBalance = new BigNumber('100000000000000000000000000'); return expect(balance).to.be.bignumber.equal(expectedBalance); }); - it('should throw a CONTRACT_DOES_NOT_EXIST error for a non-existent token contract', async () => { + it('should throw a ContractDoesNotExist error for a non-existent token contract', async () => { const nonExistentTokenAddress = '0x9dd402f14d67e001d8efbe6583e51bf9706aa065'; const ownerAddress = coinbase; return expect(zeroEx.token.getBalanceAsync(nonExistentTokenAddress, ownerAddress)) - .to.be.rejectedWith(ZeroExError.CONTRACT_DOES_NOT_EXIST); + .to.be.rejectedWith(ZeroExError.ContractDoesNotExist); }); it('should return a balance of 0 for a non-existent owner address', async () => { const token = tokens[0]; diff --git a/test/utils/token_utils.ts b/test/utils/token_utils.ts index f4fa7ac31..60cf4527b 100644 --- a/test/utils/token_utils.ts +++ b/test/utils/token_utils.ts @@ -11,7 +11,7 @@ export class TokenUtils { public getProtocolTokenOrThrow(): Token { const zrxToken = _.find(this.tokens, {symbol: PROTOCOL_TOKEN_SYMBOL}); if (_.isUndefined(zrxToken)) { - throw new Error(ZeroExError.ZRX_NOT_IN_TOKEN_REGISTRY); + throw new Error(ZeroExError.ZrxNotInTokenRegistry); } return zrxToken; } |