From 10e8e89fee0cb36e2c5c06255cb65e2b8b9eced8 Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Sat, 10 Jun 2017 13:55:36 +0200 Subject: Add initial error handling decorator implementation --- src/contract_wrappers/exchange_wrapper.ts | 8 +++++++ src/types.ts | 4 ++++ src/utils/constants.ts | 2 ++ src/utils/decorators.ts | 35 +++++++++++++++++++++++++++++++ tsconfig.json | 1 + 5 files changed, 50 insertions(+) create mode 100644 src/utils/decorators.ts diff --git a/src/contract_wrappers/exchange_wrapper.ts b/src/contract_wrappers/exchange_wrapper.ts index 65a873a9f..b03e55d8c 100644 --- a/src/contract_wrappers/exchange_wrapper.ts +++ b/src/contract_wrappers/exchange_wrapper.ts @@ -35,6 +35,7 @@ import {orderFillOrKillRequestsSchema} from '../schemas/order_fill_or_kill_reque import {signedOrderSchema, orderSchema} from '../schemas/order_schemas'; import {constants} from '../utils/constants'; import {TokenWrapper} from './token_wrapper'; +import {decorators} from '../utils/decorators'; export class ExchangeWrapper extends ContractWrapper { private exchangeContractErrCodesToMsg = { @@ -121,6 +122,7 @@ export class ExchangeWrapper extends ContractWrapper { * executing, the parties do not have sufficient balances/allowances, preserving gas costs. Setting it to * false forgoes this check and causes the smart contract to throw instead. */ + @decorators.contractCallErrorHandler public async fillOrderAsync(signedOrder: SignedOrder, takerTokenFillAmount: BigNumber.BigNumber, shouldCheckTransfer: boolean, takerAddress: string): Promise { assert.doesConformToSchema('signedOrder', signedOrder, signedOrderSchema); @@ -165,6 +167,7 @@ export class ExchangeWrapper extends ContractWrapper { * If the fill amount is reached - it succeeds and does not fill the rest of the orders. * If fill amount is not reached - it fills as much of the fill amount as possible and succeeds. */ + @decorators.contractCallErrorHandler public async fillOrdersUpToAsync(signedOrders: SignedOrder[], takerTokenFillAmount: BigNumber.BigNumber, shouldCheckTransfer: boolean, takerAddress: string): Promise { const takerTokenAddresses = _.map(signedOrders, signedOrder => signedOrder.takerTokenAddress); @@ -229,6 +232,7 @@ export class ExchangeWrapper extends ContractWrapper { * If shouldCheckTransfer is set to true, it will continue filling subsequent orders even when earlier ones fail. * When shouldCheckTransfer is set to false, if any fill fails, the entire batch fails. */ + @decorators.contractCallErrorHandler public async batchFillOrderAsync(orderFillRequests: OrderFillRequest[], shouldCheckTransfer: boolean, takerAddress: string): Promise { assert.isBoolean('shouldCheckTransfer', shouldCheckTransfer); @@ -288,6 +292,7 @@ export class ExchangeWrapper extends ContractWrapper { * Attempts to fill a specific amount of an order. If the entire amount specified cannot be filled, * the fill order is abandoned. */ + @decorators.contractCallErrorHandler public async fillOrKillOrderAsync(signedOrder: SignedOrder, fillTakerAmount: BigNumber.BigNumber, takerAddress: string) { assert.doesConformToSchema('signedOrder', signedOrder, signedOrderSchema); @@ -331,6 +336,7 @@ export class ExchangeWrapper extends ContractWrapper { * Batch version of fillOrKill. Allows a taker to specify a batch of orders that will either be atomically * filled (each to the specified fillAmount) or aborted. */ + @decorators.contractCallErrorHandler public async batchFillOrKillAsync(orderFillOrKillRequests: OrderFillOrKillRequest[], takerAddress: string) { await assert.isSenderAddressAsync('takerAddress', takerAddress, this.web3Wrapper); @@ -383,6 +389,7 @@ export class ExchangeWrapper extends ContractWrapper { /** * Cancel a given fill amount of an order. Cancellations are cumulative. */ + @decorators.contractCallErrorHandler public async cancelOrderAsync( order: Order|SignedOrder, takerTokenCancelAmount: BigNumber.BigNumber): Promise { assert.doesConformToSchema('order', order, orderSchema); @@ -416,6 +423,7 @@ export class ExchangeWrapper extends ContractWrapper { * Batch version of cancelOrderAsync. Atomically cancels multiple orders in a single transaction. * All orders must be from the same maker. */ + @decorators.contractCallErrorHandler public async batchCancelOrderAsync(orderCancellationRequests: OrderCancellationRequest[]): Promise { const makers = _.map(orderCancellationRequests, cancellationRequest => cancellationRequest.order.maker); assert.hasAtMostOneUniqueValue(makers, ExchangeContractErrs.MULTIPLE_MAKERS_IN_SINGLE_CANCEL_BATCH_DISALLOWED); diff --git a/src/types.ts b/src/types.ts index 5237fdb1b..4b9250654 100644 --- a/src/types.ts +++ b/src/types.ts @@ -18,6 +18,8 @@ export const ZeroExError = strEnum([ 'ZRX_NOT_IN_TOKEN_REGISTRY', 'INSUFFICIENT_ALLOWANCE_FOR_TRANSFER', 'INSUFFICIENT_BALANCE_FOR_TRANSFER', + 'INVALID_JUMP', + 'OUT_OF_GAS', ]); export type ZeroExError = keyof typeof ZeroExError; @@ -263,3 +265,5 @@ export interface OrderFillRequest { signedOrder: SignedOrder; takerTokenFillAmount: BigNumber.BigNumber; } + +export type AsyncMethod = (...args: any[]) => Promise; diff --git a/src/utils/constants.ts b/src/utils/constants.ts index fef0a91a0..d56ee16c5 100644 --- a/src/utils/constants.ts +++ b/src/utils/constants.ts @@ -2,4 +2,6 @@ export const constants = { NULL_ADDRESS: '0x0000000000000000000000000000000000000000', TESTRPC_NETWORK_ID: 50, MAX_DIGITS_IN_UNSIGNED_256_INT: 78, + INVALID_JUMP_PATTERN: 'invalid JUMP at', + OUT_OF_GAS_PATTERN: 'out of gas', }; diff --git a/src/utils/decorators.ts b/src/utils/decorators.ts new file mode 100644 index 000000000..a25f2cff5 --- /dev/null +++ b/src/utils/decorators.ts @@ -0,0 +1,35 @@ +import * as _ from 'lodash'; +import {constants} from './constants'; +import {AsyncMethod, ZeroExError} from '../types'; + +export const decorators = { + /** + * Source: https://stackoverflow.com/a/29837695/3546986 + */ + contractCallErrorHandler(target: object, + key: string|symbol, + descriptor: TypedPropertyDescriptor, + ): TypedPropertyDescriptor { + const originalMethod = (descriptor.value as AsyncMethod); + + // Do not use arrow syntax here. Use a function expression in + // order to use the correct value of `this` in this method + // tslint:disable-next-line:only-arrow-functions + descriptor.value = async function(...args: any[]) { + try { + const result = await originalMethod.apply(this, args); + return result; + } catch (error) { + if (_.includes(error.message, constants.INVALID_JUMP_PATTERN)) { + throw new Error(ZeroExError.INVALID_JUMP); + } + if (_.includes(error.message, constants.OUT_OF_GAS_PATTERN)) { + throw new Error(ZeroExError.OUT_OF_GAS); + } + throw error; + } + }; + + return descriptor; + }, +}; diff --git a/tsconfig.json b/tsconfig.json index 6e49168b7..e26c01048 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -7,6 +7,7 @@ "sourceMap": true, "declaration": true, "noImplicitAny": true, + "experimentalDecorators": true, "strictNullChecks": true }, "include": [ -- cgit v1.2.3 From 6b41ef680c90ec19fabc603e9288bde7ec3bc6b1 Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Sat, 10 Jun 2017 15:25:14 +0200 Subject: Remove Hex suffix from public functions parameters names --- src/0x.js.ts | 44 +++++++++++++++---------------- src/contract_wrappers/exchange_wrapper.ts | 28 ++++++++++---------- 2 files changed, 36 insertions(+), 36 deletions(-) diff --git a/src/0x.js.ts b/src/0x.js.ts index b1e4e25ff..b641739ac 100644 --- a/src/0x.js.ts +++ b/src/0x.js.ts @@ -36,18 +36,18 @@ export class ZeroEx { private web3Wrapper: Web3Wrapper; /** * Verifies that the elliptic curve signature `signature` was generated - * by signing `dataHex` with the private key corresponding to the `signerAddressHex` address. - * @param dataHex The hex encoded data signed by the supplied signature. - * @param signature A JS object containing the elliptic curve signature parameters. - * @param signerAddressHex The hex encoded address that signed the dataHex, producing the supplied signature. - * @return Whether the signature is valid for the supplied signerAddressHex and dataHex. + * by signing `data` with the private key corresponding to the `signerAddress` address. + * @param data The hex encoded data signed by the supplied signature. + * @param signature A JS object containing the elliptic curve signature parameters. + * @param signerAddress The hex encoded address that signed the data, producing the supplied signature. + * @return Whether the signature is valid for the supplied signerAddress and data. */ - public static isValidSignature(dataHex: string, signature: ECSignature, signerAddressHex: string): boolean { - assert.isHexString('dataHex', dataHex); + public static isValidSignature(data: string, signature: ECSignature, signerAddress: string): boolean { + assert.isHexString('data', data); assert.doesConformToSchema('signature', signature, ecSignatureSchema); - assert.isETHAddressHex('signerAddressHex', signerAddressHex); + assert.isETHAddressHex('signerAddress', signerAddress); - const dataBuff = ethUtil.toBuffer(dataHex); + const dataBuff = ethUtil.toBuffer(data); const msgHashBuff = ethUtil.hashPersonalMessage(dataBuff); try { const pubKey = ethUtil.ecrecover( @@ -56,7 +56,7 @@ export class ZeroEx { ethUtil.toBuffer(signature.r), ethUtil.toBuffer(signature.s)); const retrievedAddress = ethUtil.bufferToHex(ethUtil.pubToAddress(pubKey)); - return retrievedAddress === signerAddressHex; + return retrievedAddress === signerAddress; } catch (err) { return false; } @@ -79,14 +79,14 @@ export class ZeroEx { * Checks if the supplied hex encoded order hash is valid. * Note: Valid means it has the expected format, not that an order with the orderHash exists. * Use this method when processing orderHashes submitted as user input. - * @param orderHashHex Hex encoded orderHash. - * @return Whether the supplied orderHashHex has the expected format. + * @param orderHash Hex encoded orderHash. + * @return Whether the supplied orderHash has the expected format. */ - public static isValidOrderHash(orderHashHex: string): boolean { + public static isValidOrderHash(orderHash: string): boolean { // Since this method can be called to check if any arbitrary string conforms to an orderHash's // format, we only assert that we were indeed passed a string. - assert.isString('orderHashHex', orderHashHex); - const isValidOrderHash = utils.isValidOrderHash(orderHashHex); + assert.isString('orderHash', orderHash); + const isValidOrderHash = utils.isValidOrderHash(orderHash); return isValidOrderHash; } /** @@ -167,13 +167,13 @@ export class ZeroEx { /** * Signs an orderHash and returns it's elliptic curve signature. * This method currently supports TestRPC, Geth and Parity above and below V1.6.6 - * @param orderHashHex Hex encoded orderHash to sign. + * @param orderHash Hex encoded orderHash to sign. * @param signerAddress The hex encoded Ethereum address you wish to sign it with. This address * must be available via the Web3.Provider supplied to 0x.js. - * @return A JS object containing the Elliptic curve signature parameters generated by signing the orderHashHex. + * @return A JS object containing the Elliptic curve signature parameters generated by signing the orderHash. */ - public async signOrderHashAsync(orderHashHex: string, signerAddress: string): Promise { - assert.isHexString('orderHashHex', orderHashHex); + public async signOrderHashAsync(orderHash: string, signerAddress: string): Promise { + assert.isHexString('orderHash', orderHash); await assert.isSenderAddressAsync('signerAddress', signerAddress, this.web3Wrapper); let msgHashHex; @@ -181,9 +181,9 @@ export class ZeroEx { const isParityNode = utils.isParityNode(nodeVersion); if (isParityNode) { // Parity node adds the personalMessage prefix itself - msgHashHex = orderHashHex; + msgHashHex = orderHash; } else { - const orderHashBuff = ethUtil.toBuffer(orderHashHex); + const orderHashBuff = ethUtil.toBuffer(orderHash); const msgHashBuff = ethUtil.hashPersonalMessage(orderHashBuff); msgHashHex = ethUtil.bufferToHex(msgHashBuff); } @@ -218,7 +218,7 @@ export class ZeroEx { r: ethUtil.bufferToHex(r), s: ethUtil.bufferToHex(s), }; - const isValidSignature = ZeroEx.isValidSignature(orderHashHex, ecSignature, signerAddress); + const isValidSignature = ZeroEx.isValidSignature(orderHash, ecSignature, signerAddress); if (!isValidSignature) { throw new Error(ZeroExError.INVALID_SIGNATURE); } diff --git a/src/contract_wrappers/exchange_wrapper.ts b/src/contract_wrappers/exchange_wrapper.ts index 76f86e464..9e1eb609f 100644 --- a/src/contract_wrappers/exchange_wrapper.ts +++ b/src/contract_wrappers/exchange_wrapper.ts @@ -79,44 +79,44 @@ export class ExchangeWrapper extends ContractWrapper { * Returns the unavailable takerAmount of an order. Unavailable amount is defined as the total * amount that has been filled or cancelled. The remaining takerAmount can be calculated by * subtracting the unavailable amount from the total order takerAmount. - * @param orderHashHex The hex encoded orderHash for which you would like to retrieve the - * unavailable takerAmount. + * @param orderHash The hex encoded orderHash for which you would like to retrieve the + * unavailable takerAmount. * @return The amount of the order (in taker tokens) that has either been filled or canceled. */ - public async getUnavailableTakerAmountAsync(orderHashHex: string): Promise { - assert.isValidOrderHash('orderHashHex', orderHashHex); + public async getUnavailableTakerAmountAsync(orderHash: string): Promise { + assert.isValidOrderHash('orderHash', orderHash); const exchangeContract = await this.getExchangeContractAsync(); - let unavailableAmountInBaseUnits = await exchangeContract.getUnavailableValueT.call(orderHashHex); + let unavailableAmountInBaseUnits = await exchangeContract.getUnavailableValueT.call(orderHash); // Wrap BigNumbers returned from web3 with our own (later) version of BigNumber unavailableAmountInBaseUnits = new BigNumber(unavailableAmountInBaseUnits); return unavailableAmountInBaseUnits; } /** * Retrieve the takerAmount of an order that has already been filled. - * @param orderHashHex The hex encoded orderHash for which you would like to retrieve the filled takerAmount. + * @param orderHash The hex encoded orderHash for which you would like to retrieve the filled takerAmount. * @return The amount of the order (in taker tokens) that has already been filled. */ - public async getFilledTakerAmountAsync(orderHashHex: string): Promise { - assert.isValidOrderHash('orderHashHex', orderHashHex); + public async getFilledTakerAmountAsync(orderHash: string): Promise { + assert.isValidOrderHash('orderHash', orderHash); const exchangeContract = await this.getExchangeContractAsync(); - let fillAmountInBaseUnits = await exchangeContract.filled.call(orderHashHex); + let fillAmountInBaseUnits = await exchangeContract.filled.call(orderHash); // Wrap BigNumbers returned from web3 with our own (later) version of BigNumber fillAmountInBaseUnits = new BigNumber(fillAmountInBaseUnits); return fillAmountInBaseUnits; } /** * Retrieve the takerAmount of an order that has been cancelled. - * @param orderHashHex The hex encoded orderHash for which you would like to retrieve the - * cancelled takerAmount. + * @param orderHash The hex encoded orderHash for which you would like to retrieve the + * cancelled takerAmount. * @return The amount of the order (in taker tokens) that has been cancelled. */ - public async getCanceledTakerAmountAsync(orderHashHex: string): Promise { - assert.isValidOrderHash('orderHashHex', orderHashHex); + public async getCanceledTakerAmountAsync(orderHash: string): Promise { + assert.isValidOrderHash('orderHash', orderHash); const exchangeContract = await this.getExchangeContractAsync(); - let cancelledAmountInBaseUnits = await exchangeContract.cancelled.call(orderHashHex); + let cancelledAmountInBaseUnits = await exchangeContract.cancelled.call(orderHash); // Wrap BigNumbers returned from web3 with our own (later) version of BigNumber cancelledAmountInBaseUnits = new BigNumber(cancelledAmountInBaseUnits); return cancelledAmountInBaseUnits; -- cgit v1.2.3