diff options
author | Amir Bandeali <abandeali1@gmail.com> | 2018-12-03 05:16:07 +0800 |
---|---|---|
committer | Amir Bandeali <abandeali1@gmail.com> | 2019-01-08 02:31:11 +0800 |
commit | 8549252a2c0d12ec736eea72b702924353a9daaf (patch) | |
tree | 163a36fe5a13c06372a19f831ebe8bf73fdc2c62 | |
parent | aae32037da737e77b3ffaf0115494a1d7966a972 (diff) | |
download | dexon-sol-tools-8549252a2c0d12ec736eea72b702924353a9daaf.tar.gz dexon-sol-tools-8549252a2c0d12ec736eea72b702924353a9daaf.tar.zst dexon-sol-tools-8549252a2c0d12ec736eea72b702924353a9daaf.zip |
Fix decoding and add tests
-rw-r--r-- | packages/order-utils/src/asset_data_utils.ts | 17 | ||||
-rw-r--r-- | packages/order-utils/test/asset_data_utils_test.ts | 72 |
2 files changed, 55 insertions, 34 deletions
diff --git a/packages/order-utils/src/asset_data_utils.ts b/packages/order-utils/src/asset_data_utils.ts index 0b32cf216..1b416abd1 100644 --- a/packages/order-utils/src/asset_data_utils.ts +++ b/packages/order-utils/src/asset_data_utils.ts @@ -30,10 +30,11 @@ export const assetDataUtils = { assetDataUtils.validateERC20AssetDataThrow(assetData); const assetProxyId = assetDataUtils.decodeAssetProxyId(assetData); const abiEncoder = new AbiEncoder.Method(constants.ERC20_METHOD_ABI as MethodAbi); - const [tokenAddress] = abiEncoder.decode(assetData, decodingRules); + const decodedAssetData = abiEncoder.decode(assetData, decodingRules); return { assetProxyId, - tokenAddress, + // TODO(abandeali1): fix decode return types + tokenAddress: (decodedAssetData as any).tokenContract, }; }, /** @@ -58,11 +59,12 @@ export const assetDataUtils = { assetDataUtils.validateERC721AssetDataOrThrow(assetData); const assetProxyId = assetDataUtils.decodeAssetProxyId(assetData); const abiEncoder = new AbiEncoder.Method(constants.ERC721_METHOD_ABI as MethodAbi); - const [tokenAddress, tokenId] = abiEncoder.decode(assetData, decodingRules); + const decodedAssetData = abiEncoder.decode(assetData, decodingRules); return { assetProxyId, - tokenAddress, - tokenId, + // TODO(abandeali1): fix decode return types + tokenAddress: (decodedAssetData as any).tokenContract, + tokenId: (decodedAssetData as any).tokenId, }; }, /** @@ -95,7 +97,10 @@ export const assetDataUtils = { assetDataUtils.validateMultiAssetDataOrThrow(assetData); const assetProxyId = assetDataUtils.decodeAssetProxyId(assetData); const abiEncoder = new AbiEncoder.Method(constants.MULTI_ASSET_METHOD_ABI as MethodAbi); - const [amounts, nestedAssetData] = abiEncoder.decode(assetData, decodingRules); + const decodedAssetData = abiEncoder.decode(assetData, decodingRules); + // TODO(abandeali1): fix decode return types + const amounts = (decodedAssetData as any).amounts; + const nestedAssetData = (decodedAssetData as any).nestedAssetData; if (amounts.length !== nestedAssetData.length) { throw new Error( `Invalid MultiAsset assetData. Expected length of 'amounts' (${ diff --git a/packages/order-utils/test/asset_data_utils_test.ts b/packages/order-utils/test/asset_data_utils_test.ts index f175b7a38..a070aaf16 100644 --- a/packages/order-utils/test/asset_data_utils_test.ts +++ b/packages/order-utils/test/asset_data_utils_test.ts @@ -1,6 +1,6 @@ import * as chai from 'chai'; -import { ERC20AssetData, ERC721AssetData } from '@0x/types'; +import { AssetProxyId } from '@0x/types'; import { BigNumber } from '@0x/utils'; import { assetDataUtils } from '../src/asset_data_utils'; @@ -10,41 +10,57 @@ import { chaiSetup } from './utils/chai_setup'; chaiSetup.configure(); const expect = chai.expect; -const KNOWN_ENCODINGS = [ - { - address: '0x1dc4c1cefef38a777b15aa20260a54e584b16c48', - assetData: '0xf47261b00000000000000000000000001dc4c1cefef38a777b15aa20260a54e584b16c48', - }, - { - address: '0x1dc4c1cefef38a777b15aa20260a54e584b16c48', - tokenId: new BigNumber(1), - assetData: - '0x025717920000000000000000000000001dc4c1cefef38a777b15aa20260a54e584b16c480000000000000000000000000000000000000000000000000000000000000001', - }, -]; - -const ERC20_ASSET_PROXY_ID = '0xf47261b0'; -const ERC721_ASSET_PROXY_ID = '0x02571792'; +const KNOWN_ERC20_ENCODING = { + address: '0x1dc4c1cefef38a777b15aa20260a54e584b16c48', + assetData: '0xf47261b00000000000000000000000001dc4c1cefef38a777b15aa20260a54e584b16c48', +}; +const KNOWN_ERC721_ENCODING = { + address: '0x1dc4c1cefef38a777b15aa20260a54e584b16c48', + tokenId: new BigNumber(1), + assetData: + '0x025717920000000000000000000000001dc4c1cefef38a777b15aa20260a54e584b16c480000000000000000000000000000000000000000000000000000000000000001', +}; +const KNOWN_MULTI_ASSET_ENCODING = { + amounts: [new BigNumber(1), new BigNumber(1)], + nestedAssetData: [KNOWN_ERC20_ENCODING.assetData, KNOWN_ERC721_ENCODING.assetData], + assetData: + '0x94cfcdd7000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000024f47261b00000000000000000000000001dc4c1cefef38a777b15aa20260a54e584b16c48000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000044025717920000000000000000000000001dc4c1cefef38a777b15aa20260a54e584b16c48000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000', +}; describe('assetDataUtils', () => { it('should encode ERC20', () => { - const assetData = assetDataUtils.encodeERC20AssetData(KNOWN_ENCODINGS[0].address); - expect(assetData).to.equal(KNOWN_ENCODINGS[0].assetData); + const assetData = assetDataUtils.encodeERC20AssetData(KNOWN_ERC20_ENCODING.address); + expect(assetData).to.equal(KNOWN_ERC20_ENCODING.assetData); }); it('should decode ERC20', () => { - const assetData: ERC20AssetData = assetDataUtils.decodeERC20AssetData(KNOWN_ENCODINGS[0].assetData); - expect(assetData.tokenAddress).to.equal(KNOWN_ENCODINGS[0].address); - expect(assetData.assetProxyId).to.equal(ERC20_ASSET_PROXY_ID); + const decodedAssetData = assetDataUtils.decodeERC20AssetData(KNOWN_ERC20_ENCODING.assetData); + expect(decodedAssetData.tokenAddress).to.equal(KNOWN_ERC20_ENCODING.address); + expect(decodedAssetData.assetProxyId).to.equal(AssetProxyId.ERC20); }); it('should encode ERC721', () => { - const assetData = assetDataUtils.encodeERC721AssetData(KNOWN_ENCODINGS[1].address, KNOWN_ENCODINGS[1] - .tokenId as BigNumber); - expect(assetData).to.equal(KNOWN_ENCODINGS[1].assetData); + const assetData = assetDataUtils.encodeERC721AssetData( + KNOWN_ERC721_ENCODING.address, + KNOWN_ERC721_ENCODING.tokenId, + ); + expect(assetData).to.equal(KNOWN_ERC721_ENCODING.assetData); }); it('should decode ERC721', () => { - const assetData: ERC721AssetData = assetDataUtils.decodeERC721AssetData(KNOWN_ENCODINGS[1].assetData); - expect(assetData.tokenAddress).to.equal(KNOWN_ENCODINGS[1].address); - expect(assetData.assetProxyId).to.equal(ERC721_ASSET_PROXY_ID); - expect(assetData.tokenId).to.be.bignumber.equal(KNOWN_ENCODINGS[1].tokenId); + const decodedAssetData = assetDataUtils.decodeERC721AssetData(KNOWN_ERC721_ENCODING.assetData); + expect(decodedAssetData.tokenAddress).to.equal(KNOWN_ERC721_ENCODING.address); + expect(decodedAssetData.assetProxyId).to.equal(AssetProxyId.ERC721); + expect(decodedAssetData.tokenId).to.be.bignumber.equal(KNOWN_ERC721_ENCODING.tokenId); + }); + it('should encode ERC20 and ERC721', () => { + const assetData = assetDataUtils.encodeMultiAssetData( + KNOWN_MULTI_ASSET_ENCODING.amounts, + KNOWN_MULTI_ASSET_ENCODING.nestedAssetData, + ); + expect(assetData).to.equal(KNOWN_MULTI_ASSET_ENCODING.assetData); + }); + it('should decode ERC20 and ERC21', () => { + const decodedAssetData = assetDataUtils.decodeMultiAssetData(KNOWN_MULTI_ASSET_ENCODING.assetData); + expect(decodedAssetData.assetProxyId).to.equal(AssetProxyId.MultiAsset); + expect(decodedAssetData.amounts).to.deep.equal(KNOWN_MULTI_ASSET_ENCODING.amounts); + expect(decodedAssetData.nestedAssetData).to.deep.equal(KNOWN_MULTI_ASSET_ENCODING.nestedAssetData); }); }); |