import { AssetData, AssetProxyId, ERC20AssetData, ERC721AssetData, MultiAssetData } from '@0x/types'; import { AbiEncoder, BigNumber } from '@0x/utils'; import { MethodAbi } from 'ethereum-types'; import * as _ from 'lodash'; import { constants } from './constants'; const encodingRules: AbiEncoder.EncodingRules = { optimize: true }; const decodingRules: AbiEncoder.DecodingRules = { structsAsObjects: true }; export const assetDataUtils = { /** * Encodes an ERC20 token address into a hex encoded assetData string, usable in the makerAssetData or * takerAssetData fields in a 0x order. * @param tokenAddress The ERC20 token address to encode * @return The hex encoded assetData string */ encodeERC20AssetData(tokenAddress: string): string { const abiEncoder = new AbiEncoder.Method(constants.ERC20_METHOD_ABI as MethodAbi); const args = [tokenAddress]; const assetData = abiEncoder.encode(args, encodingRules); return assetData; }, /** * Decodes an ERC20 assetData hex string into it's corresponding ERC20 tokenAddress & assetProxyId * @param assetData Hex encoded assetData string to decode * @return An object containing the decoded tokenAddress & assetProxyId */ decodeERC20AssetData(assetData: string): ERC20AssetData { 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); return { assetProxyId, tokenAddress, }; }, /** * Encodes an ERC721 token address into a hex encoded assetData string, usable in the makerAssetData or * takerAssetData fields in a 0x order. * @param tokenAddress The ERC721 token address to encode * @param tokenId The ERC721 tokenId to encode * @return The hex encoded assetData string */ encodeERC721AssetData(tokenAddress: string, tokenId: BigNumber): string { const abiEncoder = new AbiEncoder.Method(constants.ERC721_METHOD_ABI as MethodAbi); const args = [tokenAddress, tokenId]; const assetData = abiEncoder.encode(args, encodingRules); return assetData; }, /** * Decodes an ERC721 assetData hex string into it's corresponding ERC721 tokenAddress, tokenId & assetProxyId * @param assetData Hex encoded assetData string to decode * @return An object containing the decoded tokenAddress, tokenId & assetProxyId */ decodeERC721AssetData(assetData: string): ERC721AssetData { 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); return { assetProxyId, tokenAddress, tokenId, }; }, /** * Encodes assetData for multiple AssetProxies into a single hex encoded assetData string, usable in the makerAssetData or * takerAssetData fields in a 0x order. * @param amounts Amounts of each asset that correspond to a ginle unit within an order. * @param nestedAssetData assetData strings that correspond to a valid assetProxyId. * @return The hex encoded assetData string */ encodeMultiAssetData(amounts: BigNumber[], nestedAssetData: string[]): string { if (amounts.length !== nestedAssetData.length) { throw new Error( `Invalid MultiAsset arguments. Expected length of 'amounts' (${ amounts.length }) to equal length of 'nestedAssetData' (${nestedAssetData.length})`, ); } _.forEach(nestedAssetData, assetDataElement => assetDataUtils.validateAssetDataOrThrow(assetDataElement)); const abiEncoder = new AbiEncoder.Method(constants.MULTI_ASSET_METHOD_ABI as MethodAbi); const args = [amounts, nestedAssetData]; const assetData = abiEncoder.encode(args, encodingRules); return assetData; }, /** * Decodes a MultiAsset assetData hex string into it's corresponding amounts and nestedAssetData * @param assetData Hex encoded assetData string to decode * @return An object containing the decoded amounts and nestedAssetData */ decodeMultiAssetData(assetData: string): MultiAssetData { 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); if (amounts.length !== nestedAssetData.length) { throw new Error( `Invalid MultiAsset assetData. Expected length of 'amounts' (${ amounts.length }) to equal length of 'nestedAssetData' (${nestedAssetData.length})`, ); } return { assetProxyId, amounts, nestedAssetData, }; }, /** * Decode and return the assetProxyId from the assetData * @param assetData Hex encoded assetData string to decode * @return The assetProxyId */ decodeAssetProxyId(assetData: string): AssetProxyId { if (assetData.length < constants.SELECTOR_CHAR_LENGTH_WITH_PREFIX) { throw new Error( `Could not decode assetData. Expected length of encoded data to be at least 10. Got ${ assetData.length }`, ); } const assetProxyId = assetData.slice(0, constants.SELECTOR_CHAR_LENGTH_WITH_PREFIX); if ( assetProxyId !== AssetProxyId.ERC20 && assetProxyId !== AssetProxyId.ERC721 && assetProxyId !== AssetProxyId.MultiAsset ) { throw new Error(`Invalid assetProxyId: ${assetProxyId}`); } return assetProxyId; }, /** * Throws if the length or assetProxyId are invalid for the ERC20Proxy. * @param assetData Hex encoded assetData string */ validateERC20AssetDataThrow(assetData: string): void { if (assetData.length < constants.ERC20_ASSET_DATA_MIN_CHAR_LENGTH_WITH_PREFIX) { throw new Error( `Could not decode ERC20 Proxy Data. Expected length of encoded data to be at least ${ constants.ERC20_ASSET_DATA_MIN_CHAR_LENGTH_WITH_PREFIX }. Got ${assetData.length}`, ); } const assetProxyId = assetDataUtils.decodeAssetProxyId(assetData); if (assetProxyId !== AssetProxyId.ERC20) { throw new Error( `Could not decode ERC20 assetData. Expected assetProxyId to be ERC20 (${ AssetProxyId.ERC20 }), but got ${assetProxyId}`, ); } }, /** * Throws if the length or assetProxyId are invalid for the ERC721Proxy. * @param assetData Hex encoded assetData string */ validateERC721AssetDataOrThrow(assetData: string): void { if (assetData.length < constants.ERC721_ASSET_DATA_MIN_CHAR_LENGTH_WITH_PREFIX) { throw new Error( `Could not decode ERC721 assetData. Expected length of encoded data to be at least ${ constants.ERC721_ASSET_DATA_MIN_CHAR_LENGTH_WITH_PREFIX }. Got ${assetData.length}`, ); } const assetProxyId = assetDataUtils.decodeAssetProxyId(assetData); if (assetProxyId !== AssetProxyId.ERC721) { throw new Error( `Could not decode ERC721 assetData. Expected assetProxyId to be ERC721 (${ AssetProxyId.ERC721 }), but got ${assetProxyId}`, ); } }, /** * Throws if the length or assetProxyId are invalid for the MultiAssetProxy. * @param assetData Hex encoded assetData string */ validateMultiAssetDataOrThrow(assetData: string): void { if (assetData.length < constants.MULTI_ASSET_DATA_MIN_CHAR_LENGTH_WITH_PREFIX) { throw new Error( `Could not decode MultiAsset assetData. Expected length of encoded data to be at least ${ constants.MULTI_ASSET_DATA_MIN_CHAR_LENGTH_WITH_PREFIX }. Got ${assetData.length}`, ); } const assetProxyId = assetDataUtils.decodeAssetProxyId(assetData); if (assetProxyId !== AssetProxyId.MultiAsset) { throw new Error( `Could not decode MultiAsset assetData. Expected assetProxyId to be MultiAsset (${ AssetProxyId.MultiAsset }), but got ${assetProxyId}`, ); } }, /** * Throws if the length or assetProxyId are invalid for the corresponding AssetProxy. * @param assetData Hex encoded assetData string */ validateAssetDataOrThrow(assetData: string): void { const assetProxyId = assetDataUtils.decodeAssetProxyId(assetData); switch (assetProxyId) { case AssetProxyId.ERC20: assetDataUtils.validateERC20AssetDataThrow(assetData); break; case AssetProxyId.ERC721: assetDataUtils.validateERC721AssetDataOrThrow(assetData); break; case AssetProxyId.MultiAsset: assetDataUtils.validateMultiAssetDataOrThrow(assetData); break; default: throw new Error(`Unrecognized asset proxy id: ${assetProxyId}`); } }, /** * Decode any assetData into it's corresponding assetData object * @param assetData Hex encoded assetData string to decode * @return Either a ERC20 or ERC721 assetData object */ decodeAssetDataOrThrow(assetData: string): AssetData { const assetProxyId = assetDataUtils.decodeAssetProxyId(assetData); switch (assetProxyId) { case AssetProxyId.ERC20: const erc20AssetData = assetDataUtils.decodeERC20AssetData(assetData); return erc20AssetData; case AssetProxyId.ERC721: const erc721AssetData = assetDataUtils.decodeERC721AssetData(assetData); return erc721AssetData; case AssetProxyId.MultiAsset: const multiAssetData = assetDataUtils.decodeMultiAssetData(assetData); return multiAssetData; default: throw new Error(`Unrecognized asset proxy id: ${assetProxyId}`); } }, };