aboutsummaryrefslogtreecommitdiffstats
path: root/packages/asset-buyer/src/utils/assert.ts
diff options
context:
space:
mode:
authorFrancesco Agosti <francesco.agosti93@gmail.com>2018-09-24 23:47:59 +0800
committerGitHub <noreply@github.com>2018-09-24 23:47:59 +0800
commitb830c28d83a33c0170d4f150ee287ea97ccf7865 (patch)
tree34aa214865e941723a66b34fd45c59036015ae53 /packages/asset-buyer/src/utils/assert.ts
parent4a316571ff9b539a540f0503b5753eb03509fea8 (diff)
parentd8d1c98a40f5cc895329f73af4d8d000c6a5195b (diff)
downloaddexon-sol-tools-b830c28d83a33c0170d4f150ee287ea97ccf7865.tar
dexon-sol-tools-b830c28d83a33c0170d4f150ee287ea97ccf7865.tar.gz
dexon-sol-tools-b830c28d83a33c0170d4f150ee287ea97ccf7865.tar.bz2
dexon-sol-tools-b830c28d83a33c0170d4f150ee287ea97ccf7865.tar.lz
dexon-sol-tools-b830c28d83a33c0170d4f150ee287ea97ccf7865.tar.xz
dexon-sol-tools-b830c28d83a33c0170d4f150ee287ea97ccf7865.tar.zst
dexon-sol-tools-b830c28d83a33c0170d4f150ee287ea97ccf7865.zip
Merge pull request #1037 from 0xProject/feature/forwarder-helper/sra-and-rpc
[asset-buyer] Create new AssetBuyer class
Diffstat (limited to 'packages/asset-buyer/src/utils/assert.ts')
-rw-r--r--packages/asset-buyer/src/utils/assert.ts51
1 files changed, 51 insertions, 0 deletions
diff --git a/packages/asset-buyer/src/utils/assert.ts b/packages/asset-buyer/src/utils/assert.ts
new file mode 100644
index 000000000..04f425237
--- /dev/null
+++ b/packages/asset-buyer/src/utils/assert.ts
@@ -0,0 +1,51 @@
+import { assert as sharedAssert } from '@0xproject/assert';
+import { schemas } from '@0xproject/json-schemas';
+import { SignedOrder } from '@0xproject/types';
+import * as _ from 'lodash';
+
+import { BuyQuote, OrderProvider, OrderProviderRequest } from '../types';
+
+export const assert = {
+ ...sharedAssert,
+ isValidBuyQuote(variableName: string, buyQuote: BuyQuote): void {
+ sharedAssert.isHexString(`${variableName}.assetData`, buyQuote.assetData);
+ sharedAssert.doesConformToSchema(`${variableName}.orders`, buyQuote.orders, schemas.signedOrdersSchema);
+ sharedAssert.doesConformToSchema(`${variableName}.feeOrders`, buyQuote.feeOrders, schemas.signedOrdersSchema);
+ sharedAssert.isBigNumber(`${variableName}.minRate`, buyQuote.minRate);
+ sharedAssert.isBigNumber(`${variableName}.maxRate`, buyQuote.maxRate);
+ sharedAssert.isBigNumber(`${variableName}.assetBuyAmount`, buyQuote.assetBuyAmount);
+ if (!_.isUndefined(buyQuote.feePercentage)) {
+ sharedAssert.isNumber(`${variableName}.feePercentage`, buyQuote.feePercentage);
+ }
+ },
+ isValidOrderProvider(variableName: string, orderFetcher: OrderProvider): void {
+ sharedAssert.isFunction(`${variableName}.getOrdersAsync`, orderFetcher.getOrdersAsync);
+ },
+ isValidOrderProviderRequest(variableName: string, orderFetcherRequest: OrderProviderRequest): void {
+ sharedAssert.isHexString(`${variableName}.makerAssetData`, orderFetcherRequest.makerAssetData);
+ sharedAssert.isHexString(`${variableName}.takerAssetData`, orderFetcherRequest.takerAssetData);
+ sharedAssert.isNumber(`${variableName}.networkId`, orderFetcherRequest.networkId);
+ },
+ areValidProvidedOrders(variableName: string, orders: SignedOrder[]): void {
+ if (orders.length === 0) {
+ return;
+ }
+ const makerAssetData = orders[0].makerAssetData;
+ const takerAssetData = orders[0].takerAssetData;
+ const filteredOrders = _.filter(
+ orders,
+ order => order.makerAssetData === makerAssetData && order.takerAssetData === takerAssetData,
+ );
+ sharedAssert.assert(
+ orders.length === filteredOrders.length,
+ `Expected all orders in ${variableName} to have the same makerAssetData and takerAssetData.`,
+ );
+ },
+ isValidPercentage(variableName: string, percentage: number): void {
+ assert.isNumber(variableName, percentage);
+ assert.assert(
+ percentage >= 0 && percentage <= 1,
+ `Expected ${variableName} to be between 0 and 1, but is ${percentage}`,
+ );
+ },
+};