From 19e93440b39161e8b1ecb79f29548faae1be4b1b Mon Sep 17 00:00:00 2001 From: Amir Bandeali Date: Fri, 30 Nov 2018 16:13:35 -0800 Subject: fix: Add error message if assetProxyId is not ERC20 or ERC721 --- packages/fill-scenarios/CHANGELOG.json | 9 +++++++++ packages/fill-scenarios/src/fill_scenarios.ts | 25 ++++++++++++++++++------- 2 files changed, 27 insertions(+), 7 deletions(-) (limited to 'packages/fill-scenarios') diff --git a/packages/fill-scenarios/CHANGELOG.json b/packages/fill-scenarios/CHANGELOG.json index ca256399a..176267df9 100644 --- a/packages/fill-scenarios/CHANGELOG.json +++ b/packages/fill-scenarios/CHANGELOG.json @@ -1,4 +1,13 @@ [ + { + "version": "1.0.17", + "changes": [ + { + "note": "Add error message if assetProxyId is not ERC20 or ERC721", + "pr": 1363 + } + ] + }, { "version": "1.0.16", "changes": [ diff --git a/packages/fill-scenarios/src/fill_scenarios.ts b/packages/fill-scenarios/src/fill_scenarios.ts index 0154bcd0a..e08c6ae5f 100644 --- a/packages/fill-scenarios/src/fill_scenarios.ts +++ b/packages/fill-scenarios/src/fill_scenarios.ts @@ -2,7 +2,14 @@ import { DummyERC20TokenContract, DummyERC721TokenContract, ExchangeContract } f import * as artifacts from '@0x/contract-artifacts'; import { assetDataUtils } from '@0x/order-utils'; import { orderFactory } from '@0x/order-utils/lib/src/order_factory'; -import { AssetProxyId, ERC721AssetData, OrderWithoutExchangeAddress, SignedOrder } from '@0x/types'; +import { + AssetData, + AssetProxyId, + ERC20AssetData, + ERC721AssetData, + OrderWithoutExchangeAddress, + SignedOrder, +} from '@0x/types'; import { BigNumber } from '@0x/utils'; import { Web3Wrapper } from '@0x/web3-wrapper'; import { Provider } from 'ethereum-types'; @@ -153,35 +160,39 @@ export class FillScenarios { const decodedMakerAssetData = assetDataUtils.decodeAssetDataOrThrow(makerAssetData); if (decodedMakerAssetData.assetProxyId === AssetProxyId.ERC20) { await this._increaseERC20BalanceAndAllowanceAsync( - decodedMakerAssetData.tokenAddress, + (decodedMakerAssetData as ERC20AssetData).tokenAddress, makerAddress, makerFillableAmount, ); - } else { + } else if (decodedMakerAssetData.assetProxyId === AssetProxyId.ERC721) { if (!makerFillableAmount.equals(1)) { throw new Error(`ERC721 makerFillableAmount should be equal 1. Found: ${makerFillableAmount}`); } await this._increaseERC721BalanceAndAllowanceAsync( - decodedMakerAssetData.tokenAddress, + (decodedMakerAssetData as ERC721AssetData).tokenAddress, makerAddress, // tslint:disable-next-line:no-unnecessary-type-assertion (decodedMakerAssetData as ERC721AssetData).tokenId, ); + } else { + throw new Error(`Proxy with id ${decodedMakerAssetData.assetProxyId} not supported`); } const decodedTakerAssetData = assetDataUtils.decodeAssetDataOrThrow(takerAssetData); if (decodedTakerAssetData.assetProxyId === AssetProxyId.ERC20) { - const takerTokenAddress = decodedTakerAssetData.tokenAddress; + const takerTokenAddress = (decodedTakerAssetData as ERC20AssetData).tokenAddress; await this._increaseERC20BalanceAndAllowanceAsync(takerTokenAddress, takerAddress, takerFillableAmount); - } else { + } else if (decodedTakerAssetData.assetProxyId === AssetProxyId.ERC721) { if (!takerFillableAmount.equals(1)) { throw new Error(`ERC721 takerFillableAmount should be equal 1. Found: ${takerFillableAmount}`); } await this._increaseERC721BalanceAndAllowanceAsync( - decodedTakerAssetData.tokenAddress, + (decodedTakerAssetData as ERC721AssetData).tokenAddress, takerAddress, // tslint:disable-next-line:no-unnecessary-type-assertion (decodedMakerAssetData as ERC721AssetData).tokenId, ); + } else { + throw new Error(`Proxy with id ${decodedTakerAssetData.assetProxyId} not supported`); } // Fees await Promise.all([ -- cgit v1.2.3 From 46359c098b0f6f3b0377f9426c78aa2ce2e4cf5a Mon Sep 17 00:00:00 2001 From: Amir Bandeali Date: Sat, 1 Dec 2018 23:32:50 -0800 Subject: Add support for MAP in fill-scenarios --- packages/fill-scenarios/CHANGELOG.json | 4 +- packages/fill-scenarios/src/fill_scenarios.ts | 81 ++++++++++++++------------- 2 files changed, 45 insertions(+), 40 deletions(-) (limited to 'packages/fill-scenarios') diff --git a/packages/fill-scenarios/CHANGELOG.json b/packages/fill-scenarios/CHANGELOG.json index 176267df9..2c3e261cb 100644 --- a/packages/fill-scenarios/CHANGELOG.json +++ b/packages/fill-scenarios/CHANGELOG.json @@ -1,9 +1,9 @@ [ { - "version": "1.0.17", + "version": "1.1.0", "changes": [ { - "note": "Add error message if assetProxyId is not ERC20 or ERC721", + "note": "Add support for MultiAssetProxy", "pr": 1363 } ] diff --git a/packages/fill-scenarios/src/fill_scenarios.ts b/packages/fill-scenarios/src/fill_scenarios.ts index e08c6ae5f..b11b01a22 100644 --- a/packages/fill-scenarios/src/fill_scenarios.ts +++ b/packages/fill-scenarios/src/fill_scenarios.ts @@ -3,10 +3,10 @@ import * as artifacts from '@0x/contract-artifacts'; import { assetDataUtils } from '@0x/order-utils'; import { orderFactory } from '@0x/order-utils/lib/src/order_factory'; import { - AssetData, AssetProxyId, ERC20AssetData, ERC721AssetData, + MultiAssetData, OrderWithoutExchangeAddress, SignedOrder, } from '@0x/types'; @@ -157,43 +157,8 @@ export class FillScenarios { feeRecipientAddress: string, expirationTimeSeconds?: BigNumber, ): Promise { - const decodedMakerAssetData = assetDataUtils.decodeAssetDataOrThrow(makerAssetData); - if (decodedMakerAssetData.assetProxyId === AssetProxyId.ERC20) { - await this._increaseERC20BalanceAndAllowanceAsync( - (decodedMakerAssetData as ERC20AssetData).tokenAddress, - makerAddress, - makerFillableAmount, - ); - } else if (decodedMakerAssetData.assetProxyId === AssetProxyId.ERC721) { - if (!makerFillableAmount.equals(1)) { - throw new Error(`ERC721 makerFillableAmount should be equal 1. Found: ${makerFillableAmount}`); - } - await this._increaseERC721BalanceAndAllowanceAsync( - (decodedMakerAssetData as ERC721AssetData).tokenAddress, - makerAddress, - // tslint:disable-next-line:no-unnecessary-type-assertion - (decodedMakerAssetData as ERC721AssetData).tokenId, - ); - } else { - throw new Error(`Proxy with id ${decodedMakerAssetData.assetProxyId} not supported`); - } - const decodedTakerAssetData = assetDataUtils.decodeAssetDataOrThrow(takerAssetData); - if (decodedTakerAssetData.assetProxyId === AssetProxyId.ERC20) { - const takerTokenAddress = (decodedTakerAssetData as ERC20AssetData).tokenAddress; - await this._increaseERC20BalanceAndAllowanceAsync(takerTokenAddress, takerAddress, takerFillableAmount); - } else if (decodedTakerAssetData.assetProxyId === AssetProxyId.ERC721) { - if (!takerFillableAmount.equals(1)) { - throw new Error(`ERC721 takerFillableAmount should be equal 1. Found: ${takerFillableAmount}`); - } - await this._increaseERC721BalanceAndAllowanceAsync( - (decodedTakerAssetData as ERC721AssetData).tokenAddress, - takerAddress, - // tslint:disable-next-line:no-unnecessary-type-assertion - (decodedMakerAssetData as ERC721AssetData).tokenId, - ); - } else { - throw new Error(`Proxy with id ${decodedTakerAssetData.assetProxyId} not supported`); - } + await this._increaseBalanceAndAllowanceWithAssetDataAsync(makerAssetData, makerAddress, makerFillableAmount); + await this._increaseBalanceAndAllowanceWithAssetDataAsync(takerAssetData, takerAddress, takerFillableAmount); // Fees await Promise.all([ this._increaseERC20BalanceAndAllowanceAsync(this._zrxTokenAddress, makerAddress, makerFee), @@ -309,4 +274,44 @@ export class FillScenarios { }); await this._web3Wrapper.awaitTransactionSuccessAsync(txHash, constants.AWAIT_TRANSACTION_MINED_MS); } + private async _increaseBalanceAndAllowanceWithAssetDataAsync( + assetData: string, + userAddress: string, + amount: BigNumber, + ): Promise { + const decodedAssetData = assetDataUtils.decodeAssetDataOrThrow(assetData); + switch (decodedAssetData.assetProxyId) { + case AssetProxyId.ERC20: + await this._increaseERC20BalanceAndAllowanceAsync( + (decodedAssetData as ERC20AssetData).tokenAddress, + userAddress, + amount, + ); + break; + case AssetProxyId.ERC721: + await this._increaseERC721BalanceAndAllowanceAsync( + (decodedAssetData as ERC721AssetData).tokenAddress, + userAddress, + // tslint:disable-next-line:no-unnecessary-type-assertion + (decodedAssetData as ERC721AssetData).tokenId, + ); + break; + case AssetProxyId.MultiAsset: + for (const [ + index, + nestedAssetDataElement, + ] of (decodedAssetData as MultiAssetData).nestedAssetData.entries()) { + const amountsElement = (decodedAssetData as MultiAssetData).amounts[index]; + const totalAmount = amount.times(amountsElement); + await this._increaseBalanceAndAllowanceWithAssetDataAsync( + nestedAssetDataElement, + userAddress, + totalAmount, + ); + } + break; + default: + throw new Error(`Proxy with id ${decodedAssetData.assetProxyId} not supported`); + } + } } -- cgit v1.2.3 From aae32037da737e77b3ffaf0115494a1d7966a972 Mon Sep 17 00:00:00 2001 From: Amir Bandeali Date: Sat, 1 Dec 2018 23:40:12 -0800 Subject: Run linter --- packages/fill-scenarios/src/fill_scenarios.ts | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'packages/fill-scenarios') diff --git a/packages/fill-scenarios/src/fill_scenarios.ts b/packages/fill-scenarios/src/fill_scenarios.ts index b11b01a22..2e87e60cc 100644 --- a/packages/fill-scenarios/src/fill_scenarios.ts +++ b/packages/fill-scenarios/src/fill_scenarios.ts @@ -283,6 +283,7 @@ export class FillScenarios { switch (decodedAssetData.assetProxyId) { case AssetProxyId.ERC20: await this._increaseERC20BalanceAndAllowanceAsync( + // tslint:disable-next-line:no-unnecessary-type-assertion (decodedAssetData as ERC20AssetData).tokenAddress, userAddress, amount, @@ -290,6 +291,7 @@ export class FillScenarios { break; case AssetProxyId.ERC721: await this._increaseERC721BalanceAndAllowanceAsync( + // tslint:disable-next-line:no-unnecessary-type-assertion (decodedAssetData as ERC721AssetData).tokenAddress, userAddress, // tslint:disable-next-line:no-unnecessary-type-assertion @@ -300,7 +302,9 @@ export class FillScenarios { for (const [ index, nestedAssetDataElement, + // tslint:disable-next-line:no-unnecessary-type-assertion ] of (decodedAssetData as MultiAssetData).nestedAssetData.entries()) { + // tslint:disable-next-line:no-unnecessary-type-assertion const amountsElement = (decodedAssetData as MultiAssetData).amounts[index]; const totalAmount = amount.times(amountsElement); await this._increaseBalanceAndAllowanceWithAssetDataAsync( -- cgit v1.2.3 From 24564b986daa703f66e54f85abf4782d99a40f94 Mon Sep 17 00:00:00 2001 From: Amir Bandeali Date: Fri, 4 Jan 2019 14:31:25 -0800 Subject: Minimize unnecessary type assertions --- packages/fill-scenarios/src/fill_scenarios.ts | 59 ++++++++------------------- 1 file changed, 17 insertions(+), 42 deletions(-) (limited to 'packages/fill-scenarios') diff --git a/packages/fill-scenarios/src/fill_scenarios.ts b/packages/fill-scenarios/src/fill_scenarios.ts index 2e87e60cc..ce1f7f9ff 100644 --- a/packages/fill-scenarios/src/fill_scenarios.ts +++ b/packages/fill-scenarios/src/fill_scenarios.ts @@ -2,14 +2,7 @@ import { DummyERC20TokenContract, DummyERC721TokenContract, ExchangeContract } f import * as artifacts from '@0x/contract-artifacts'; import { assetDataUtils } from '@0x/order-utils'; import { orderFactory } from '@0x/order-utils/lib/src/order_factory'; -import { - AssetProxyId, - ERC20AssetData, - ERC721AssetData, - MultiAssetData, - OrderWithoutExchangeAddress, - SignedOrder, -} from '@0x/types'; +import { OrderWithoutExchangeAddress, SignedOrder } from '@0x/types'; import { BigNumber } from '@0x/utils'; import { Web3Wrapper } from '@0x/web3-wrapper'; import { Provider } from 'ethereum-types'; @@ -280,42 +273,24 @@ export class FillScenarios { amount: BigNumber, ): Promise { const decodedAssetData = assetDataUtils.decodeAssetDataOrThrow(assetData); - switch (decodedAssetData.assetProxyId) { - case AssetProxyId.ERC20: - await this._increaseERC20BalanceAndAllowanceAsync( - // tslint:disable-next-line:no-unnecessary-type-assertion - (decodedAssetData as ERC20AssetData).tokenAddress, - userAddress, - amount, - ); - break; - case AssetProxyId.ERC721: - await this._increaseERC721BalanceAndAllowanceAsync( - // tslint:disable-next-line:no-unnecessary-type-assertion - (decodedAssetData as ERC721AssetData).tokenAddress, + if (assetDataUtils.isERC20AssetData(decodedAssetData)) { + await this._increaseERC20BalanceAndAllowanceAsync(decodedAssetData.tokenAddress, userAddress, amount); + } else if (assetDataUtils.isERC721AssetData(decodedAssetData)) { + await this._increaseERC721BalanceAndAllowanceAsync( + decodedAssetData.tokenAddress, + userAddress, + decodedAssetData.tokenId, + ); + } else if (assetDataUtils.isMultiAssetData(decodedAssetData)) { + for (const [index, nestedAssetDataElement] of decodedAssetData.nestedAssetData.entries()) { + const amountsElement = decodedAssetData.amounts[index]; + const totalAmount = amount.times(amountsElement); + await this._increaseBalanceAndAllowanceWithAssetDataAsync( + nestedAssetDataElement, userAddress, - // tslint:disable-next-line:no-unnecessary-type-assertion - (decodedAssetData as ERC721AssetData).tokenId, + totalAmount, ); - break; - case AssetProxyId.MultiAsset: - for (const [ - index, - nestedAssetDataElement, - // tslint:disable-next-line:no-unnecessary-type-assertion - ] of (decodedAssetData as MultiAssetData).nestedAssetData.entries()) { - // tslint:disable-next-line:no-unnecessary-type-assertion - const amountsElement = (decodedAssetData as MultiAssetData).amounts[index]; - const totalAmount = amount.times(amountsElement); - await this._increaseBalanceAndAllowanceWithAssetDataAsync( - nestedAssetDataElement, - userAddress, - totalAmount, - ); - } - break; - default: - throw new Error(`Proxy with id ${decodedAssetData.assetProxyId} not supported`); + } } } } -- cgit v1.2.3