Add initial error handling decorator implementation
This commit is contained in:
parent
0e971fcc89
commit
10e8e89fee
@ -35,6 +35,7 @@ import {orderFillOrKillRequestsSchema} from '../schemas/order_fill_or_kill_reque
|
|||||||
import {signedOrderSchema, orderSchema} from '../schemas/order_schemas';
|
import {signedOrderSchema, orderSchema} from '../schemas/order_schemas';
|
||||||
import {constants} from '../utils/constants';
|
import {constants} from '../utils/constants';
|
||||||
import {TokenWrapper} from './token_wrapper';
|
import {TokenWrapper} from './token_wrapper';
|
||||||
|
import {decorators} from '../utils/decorators';
|
||||||
|
|
||||||
export class ExchangeWrapper extends ContractWrapper {
|
export class ExchangeWrapper extends ContractWrapper {
|
||||||
private exchangeContractErrCodesToMsg = {
|
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
|
* 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.
|
* false forgoes this check and causes the smart contract to throw instead.
|
||||||
*/
|
*/
|
||||||
|
@decorators.contractCallErrorHandler
|
||||||
public async fillOrderAsync(signedOrder: SignedOrder, takerTokenFillAmount: BigNumber.BigNumber,
|
public async fillOrderAsync(signedOrder: SignedOrder, takerTokenFillAmount: BigNumber.BigNumber,
|
||||||
shouldCheckTransfer: boolean, takerAddress: string): Promise<void> {
|
shouldCheckTransfer: boolean, takerAddress: string): Promise<void> {
|
||||||
assert.doesConformToSchema('signedOrder', signedOrder, signedOrderSchema);
|
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 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.
|
* 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,
|
public async fillOrdersUpToAsync(signedOrders: SignedOrder[], takerTokenFillAmount: BigNumber.BigNumber,
|
||||||
shouldCheckTransfer: boolean, takerAddress: string): Promise<void> {
|
shouldCheckTransfer: boolean, takerAddress: string): Promise<void> {
|
||||||
const takerTokenAddresses = _.map(signedOrders, signedOrder => signedOrder.takerTokenAddress);
|
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.
|
* 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.
|
* When shouldCheckTransfer is set to false, if any fill fails, the entire batch fails.
|
||||||
*/
|
*/
|
||||||
|
@decorators.contractCallErrorHandler
|
||||||
public async batchFillOrderAsync(orderFillRequests: OrderFillRequest[],
|
public async batchFillOrderAsync(orderFillRequests: OrderFillRequest[],
|
||||||
shouldCheckTransfer: boolean, takerAddress: string): Promise<void> {
|
shouldCheckTransfer: boolean, takerAddress: string): Promise<void> {
|
||||||
assert.isBoolean('shouldCheckTransfer', shouldCheckTransfer);
|
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,
|
* Attempts to fill a specific amount of an order. If the entire amount specified cannot be filled,
|
||||||
* the fill order is abandoned.
|
* the fill order is abandoned.
|
||||||
*/
|
*/
|
||||||
|
@decorators.contractCallErrorHandler
|
||||||
public async fillOrKillOrderAsync(signedOrder: SignedOrder, fillTakerAmount: BigNumber.BigNumber,
|
public async fillOrKillOrderAsync(signedOrder: SignedOrder, fillTakerAmount: BigNumber.BigNumber,
|
||||||
takerAddress: string) {
|
takerAddress: string) {
|
||||||
assert.doesConformToSchema('signedOrder', signedOrder, signedOrderSchema);
|
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
|
* 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.
|
* filled (each to the specified fillAmount) or aborted.
|
||||||
*/
|
*/
|
||||||
|
@decorators.contractCallErrorHandler
|
||||||
public async batchFillOrKillAsync(orderFillOrKillRequests: OrderFillOrKillRequest[],
|
public async batchFillOrKillAsync(orderFillOrKillRequests: OrderFillOrKillRequest[],
|
||||||
takerAddress: string) {
|
takerAddress: string) {
|
||||||
await assert.isSenderAddressAsync('takerAddress', takerAddress, this.web3Wrapper);
|
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.
|
* Cancel a given fill amount of an order. Cancellations are cumulative.
|
||||||
*/
|
*/
|
||||||
|
@decorators.contractCallErrorHandler
|
||||||
public async cancelOrderAsync(
|
public async cancelOrderAsync(
|
||||||
order: Order|SignedOrder, takerTokenCancelAmount: BigNumber.BigNumber): Promise<void> {
|
order: Order|SignedOrder, takerTokenCancelAmount: BigNumber.BigNumber): Promise<void> {
|
||||||
assert.doesConformToSchema('order', order, orderSchema);
|
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.
|
* Batch version of cancelOrderAsync. Atomically cancels multiple orders in a single transaction.
|
||||||
* All orders must be from the same maker.
|
* All orders must be from the same maker.
|
||||||
*/
|
*/
|
||||||
|
@decorators.contractCallErrorHandler
|
||||||
public async batchCancelOrderAsync(orderCancellationRequests: OrderCancellationRequest[]): Promise<void> {
|
public async batchCancelOrderAsync(orderCancellationRequests: OrderCancellationRequest[]): Promise<void> {
|
||||||
const makers = _.map(orderCancellationRequests, cancellationRequest => cancellationRequest.order.maker);
|
const makers = _.map(orderCancellationRequests, cancellationRequest => cancellationRequest.order.maker);
|
||||||
assert.hasAtMostOneUniqueValue(makers, ExchangeContractErrs.MULTIPLE_MAKERS_IN_SINGLE_CANCEL_BATCH_DISALLOWED);
|
assert.hasAtMostOneUniqueValue(makers, ExchangeContractErrs.MULTIPLE_MAKERS_IN_SINGLE_CANCEL_BATCH_DISALLOWED);
|
||||||
|
@ -18,6 +18,8 @@ export const ZeroExError = strEnum([
|
|||||||
'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',
|
||||||
|
'INVALID_JUMP',
|
||||||
|
'OUT_OF_GAS',
|
||||||
]);
|
]);
|
||||||
export type ZeroExError = keyof typeof ZeroExError;
|
export type ZeroExError = keyof typeof ZeroExError;
|
||||||
|
|
||||||
@ -263,3 +265,5 @@ export interface OrderFillRequest {
|
|||||||
signedOrder: SignedOrder;
|
signedOrder: SignedOrder;
|
||||||
takerTokenFillAmount: BigNumber.BigNumber;
|
takerTokenFillAmount: BigNumber.BigNumber;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export type AsyncMethod = (...args: any[]) => Promise<any>;
|
||||||
|
@ -2,4 +2,6 @@ export const constants = {
|
|||||||
NULL_ADDRESS: '0x0000000000000000000000000000000000000000',
|
NULL_ADDRESS: '0x0000000000000000000000000000000000000000',
|
||||||
TESTRPC_NETWORK_ID: 50,
|
TESTRPC_NETWORK_ID: 50,
|
||||||
MAX_DIGITS_IN_UNSIGNED_256_INT: 78,
|
MAX_DIGITS_IN_UNSIGNED_256_INT: 78,
|
||||||
|
INVALID_JUMP_PATTERN: 'invalid JUMP at',
|
||||||
|
OUT_OF_GAS_PATTERN: 'out of gas',
|
||||||
};
|
};
|
||||||
|
35
src/utils/decorators.ts
Normal file
35
src/utils/decorators.ts
Normal file
@ -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<AsyncMethod>,
|
||||||
|
): TypedPropertyDescriptor<AsyncMethod> {
|
||||||
|
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;
|
||||||
|
},
|
||||||
|
};
|
@ -7,6 +7,7 @@
|
|||||||
"sourceMap": true,
|
"sourceMap": true,
|
||||||
"declaration": true,
|
"declaration": true,
|
||||||
"noImplicitAny": true,
|
"noImplicitAny": true,
|
||||||
|
"experimentalDecorators": true,
|
||||||
"strictNullChecks": true
|
"strictNullChecks": true
|
||||||
},
|
},
|
||||||
"include": [
|
"include": [
|
||||||
|
Loading…
x
Reference in New Issue
Block a user