feat(asset-buyer): throw SignatureRequestDenied and TransactionValueTooLow errors when executing buy

This commit is contained in:
Brandon Millman 2018-10-17 10:03:46 -07:00
parent dcd428a4a2
commit 117ee19370
3 changed files with 33 additions and 16 deletions

View File

@ -25,6 +25,10 @@
{ {
"note": "Add missing types to public interface", "note": "Add missing types to public interface",
"pr": 1139 "pr": 1139
},
{
"note": "Throw `SignatureRequestDenied` and `TransactionValueTooLow` errors when executing buy",
"pr": 1147
} }
], ],
"timestamp": 1539871071 "timestamp": 1539871071

View File

@ -1,4 +1,4 @@
import { ContractWrappers } from '@0x/contract-wrappers'; import { ContractWrappers, ContractWrappersError, ForwarderWrapperError } from '@0x/contract-wrappers';
import { schemas } from '@0x/json-schemas'; import { schemas } from '@0x/json-schemas';
import { SignedOrder } from '@0x/order-utils'; import { SignedOrder } from '@0x/order-utils';
import { ObjectMap } from '@0x/types'; import { ObjectMap } from '@0x/types';
@ -210,6 +210,7 @@ export class AssetBuyer {
throw new Error(AssetBuyerError.NoAddressAvailable); throw new Error(AssetBuyerError.NoAddressAvailable);
} }
} }
try {
// if no ethAmount is provided, default to the worst ethAmount from buyQuote // if no ethAmount is provided, default to the worst ethAmount from buyQuote
const txHash = await this._contractWrappers.forwarder.marketBuyOrdersWithEthAsync( const txHash = await this._contractWrappers.forwarder.marketBuyOrdersWithEthAsync(
orders, orders,
@ -222,9 +223,19 @@ export class AssetBuyer {
{ {
gasLimit, gasLimit,
gasPrice, gasPrice,
shouldValidate: true,
}, },
); );
return txHash; return txHash;
} catch (err) {
if (_.includes(err.message, ContractWrappersError.SignatureRequestDenied)) {
throw new Error(AssetBuyerError.SignatureRequestDenied);
} else if (_.includes(err.message, ForwarderWrapperError.CompleteFillFailed)) {
throw new Error(AssetBuyerError.TransactionValueTooLow);
} else {
throw err;
}
}
} }
/** /**
* Grab orders from the map, if there is a miss or it is time to refresh, fetch and process the orders * Grab orders from the map, if there is a miss or it is time to refresh, fetch and process the orders

View File

@ -112,6 +112,8 @@ export enum AssetBuyerError {
NoAddressAvailable = 'NO_ADDRESS_AVAILABLE', NoAddressAvailable = 'NO_ADDRESS_AVAILABLE',
InvalidOrderProviderResponse = 'INVALID_ORDER_PROVIDER_RESPONSE', InvalidOrderProviderResponse = 'INVALID_ORDER_PROVIDER_RESPONSE',
AssetUnavailable = 'ASSET_UNAVAILABLE', AssetUnavailable = 'ASSET_UNAVAILABLE',
SignatureRequestDenied = 'SIGNATURE_REQUEST_DENIED',
TransactionValueTooLow = 'TRANSACTION_VALUE_TOO_LOW',
} }
export interface OrdersAndFillableAmounts { export interface OrdersAndFillableAmounts {