diff options
Diffstat (limited to 'packages')
-rw-r--r-- | packages/contracts/test/asset_proxy/decoder.ts | 9 | ||||
-rw-r--r-- | packages/contracts/test/asset_proxy/proxies.ts | 4 | ||||
-rw-r--r-- | packages/contracts/test/libraries/lib_bytes.ts | 1 | ||||
-rw-r--r-- | packages/order-utils/src/asset_proxy_utils.ts | 78 | ||||
-rw-r--r-- | packages/order-utils/src/order_state_utils.ts | 4 | ||||
-rw-r--r-- | packages/types/src/index.ts | 7 |
6 files changed, 54 insertions, 49 deletions
diff --git a/packages/contracts/test/asset_proxy/decoder.ts b/packages/contracts/test/asset_proxy/decoder.ts index 6336f69ae..f2668dfe6 100644 --- a/packages/contracts/test/asset_proxy/decoder.ts +++ b/packages/contracts/test/asset_proxy/decoder.ts @@ -1,5 +1,6 @@ import { BlockchainLifecycle, devConstants, web3Factory } from '@0xproject/dev-utils'; -import { generatePseudoRandomSalt } from '@0xproject/order-utils'; +import { assetProxyUtils, generatePseudoRandomSalt } from '@0xproject/order-utils'; +import { AssetData, AssetProxyId, ERC20AssetData, ERC721AssetData } from '@0xproject/types'; import { BigNumber } from '@0xproject/utils'; import { Web3Wrapper } from '@0xproject/web3-wrapper'; import BN = require('bn.js'); @@ -10,10 +11,8 @@ import * as Web3 from 'web3'; import { TestAssetDataDecodersContract } from '../../src/contract_wrappers/generated/test_asset_data_decoders'; import { artifacts } from '../../src/utils/artifacts'; -import { assetProxyUtils } from '../../src/utils/asset_proxy_utils'; import { chaiSetup } from '../../src/utils/chai_setup'; import { constants } from '../../src/utils/constants'; -import { AssetData, AssetProxyId, ERC20AssetData, ERC721AssetData } from '../../src/utils/types'; import { provider, txDefaults, web3Wrapper } from '../../src/utils/web3_wrapper'; chaiSetup.configure(); @@ -74,7 +73,7 @@ describe('TestAssetDataDecoders', () => { expect(decodedAssetProxyId).to.be.equal(expectedDecodedAssetData.assetProxyId); expect(decodedTokenAddress).to.be.equal(expectedDecodedAssetData.tokenAddress); expect(decodedTokenId).to.be.bignumber.equal(expectedDecodedAssetData.tokenId); - expect(decodedData).to.be.equal(expectedDecodedAssetData.data); + expect(decodedData).to.be.equal(expectedDecodedAssetData.receiverData); }); it('should correctly decode ERC721 asset data with receiver data', async () => { @@ -96,7 +95,7 @@ describe('TestAssetDataDecoders', () => { expect(decodedAssetProxyId).to.be.equal(expectedDecodedAssetData.assetProxyId); expect(decodedTokenAddress).to.be.equal(expectedDecodedAssetData.tokenAddress); expect(decodedTokenId).to.be.bignumber.equal(expectedDecodedAssetData.tokenId); - expect(decodedReceiverData).to.be.equal(expectedDecodedAssetData.data); + expect(decodedReceiverData).to.be.equal(expectedDecodedAssetData.receiverData); }); }); }); diff --git a/packages/contracts/test/asset_proxy/proxies.ts b/packages/contracts/test/asset_proxy/proxies.ts index 6dc652383..52a92718b 100644 --- a/packages/contracts/test/asset_proxy/proxies.ts +++ b/packages/contracts/test/asset_proxy/proxies.ts @@ -1,6 +1,6 @@ import { BlockchainLifecycle } from '@0xproject/dev-utils'; -import { assetProxyUtils } from '@0xproject/order-utils'; import { generatePseudoRandomSalt } from '@0xproject/order-utils'; +import { assetProxyUtils } from '@0xproject/order-utils'; import { AssetProxyId } from '@0xproject/types'; import { BigNumber } from '@0xproject/utils'; import * as chai from 'chai'; @@ -18,13 +18,11 @@ import { ERC20ProxyContract } from '../../src/generated_contract_wrappers/e_r_c2 import { ERC721ProxyContract } from '../../src/generated_contract_wrappers/e_r_c721_proxy'; import { artifacts } from '../../src/utils/artifacts'; import { expectRevertOrAlwaysFailingTransactionAsync } from '../../src/utils/assertions'; -import { assetProxyUtils } from '../../src/utils/asset_proxy_utils'; import { chaiSetup } from '../../src/utils/chai_setup'; import { constants } from '../../src/utils/constants'; import { ERC20Wrapper } from '../../src/utils/erc20_wrapper'; import { ERC721Wrapper } from '../../src/utils/erc721_wrapper'; import { LogDecoder } from '../../src/utils/log_decoder'; -import { AssetProxyId } from '../../src/utils/types'; import { provider, txDefaults, web3Wrapper } from '../../src/utils/web3_wrapper'; chaiSetup.configure(); diff --git a/packages/contracts/test/libraries/lib_bytes.ts b/packages/contracts/test/libraries/lib_bytes.ts index 1a23483ba..9ae95d518 100644 --- a/packages/contracts/test/libraries/lib_bytes.ts +++ b/packages/contracts/test/libraries/lib_bytes.ts @@ -1,4 +1,5 @@ import { BlockchainLifecycle } from '@0xproject/dev-utils'; +import { assetProxyUtils } from '@0xproject/order-utils'; import { generatePseudoRandomSalt } from '@0xproject/order-utils'; import { AssetProxyId } from '@0xproject/types'; import { BigNumber } from '@0xproject/utils'; diff --git a/packages/order-utils/src/asset_proxy_utils.ts b/packages/order-utils/src/asset_proxy_utils.ts index a12be83a3..61a9b12e9 100644 --- a/packages/order-utils/src/asset_proxy_utils.ts +++ b/packages/order-utils/src/asset_proxy_utils.ts @@ -1,11 +1,11 @@ -import { AssetProxyId, ERC20ProxyData, ERC721ProxyData, ProxyData } from '@0xproject/types'; +import { AssetData, AssetProxyId, ERC20AssetData, ERC721AssetData } from '@0xproject/types'; import { BigNumber } from '@0xproject/utils'; import BN = require('bn.js'); import ethUtil = require('ethereumjs-util'); import * as _ from 'lodash'; -const ERC20_PROXY_METADATA_BYTE_LENGTH = 21; -const ERC721_PROXY_METADATA_BYTE_LENGTH = 53; +const ERC20_ASSET_DATA_BYTE_LENGTH = 21; +const ERC721_ASSET_DATA_BYTE_LENGTH = 53; export const assetProxyUtils = { encodeAssetProxyId(assetProxyId: AssetProxyId): Buffer { @@ -44,13 +44,13 @@ export const assetProxyUtils = { encodeERC20AssetData(tokenAddress: string): string { const encodedAssetProxyId = assetProxyUtils.encodeAssetProxyId(AssetProxyId.ERC20); const encodedAddress = assetProxyUtils.encodeAddress(tokenAddress); - const encodedMetadata = Buffer.concat([encodedAddress, encodedAssetProxyId]); - const encodedMetadataHex = ethUtil.bufferToHex(encodedMetadata); - return encodedMetadataHex; + const encodedAssetData = Buffer.concat([encodedAddress, encodedAssetProxyId]); + const encodedAssetDataHex = ethUtil.bufferToHex(encodedAssetData); + return encodedAssetDataHex; }, - decodeERC20ProxyData(proxyData: string): ERC20ProxyData { - const encodedProxyMetadata = ethUtil.toBuffer(proxyData); - if (encodedProxyMetadata.byteLength !== ERC20_PROXY_METADATA_BYTE_LENGTH) { + decodeERC20AssetData(proxyData: string): ERC20AssetData { + const encodedAssetData = ethUtil.toBuffer(proxyData); + if (encodedAssetData.byteLength !== ERC20_ASSET_DATA_BYTE_LENGTH) { throw new Error( `Could not decode ERC20 Proxy Data. Expected length of encoded data to be 21. Got ${ encodedAssetData.byteLength @@ -66,8 +66,8 @@ export const assetProxyUtils = { }), but got ${assetProxyId}`, ); } - const addressOffset = ERC20_PROXY_METADATA_BYTE_LENGTH - 1; - const encodedTokenAddress = encodedProxyMetadata.slice(0, addressOffset); + const addressOffset = ERC20_ASSET_DATA_BYTE_LENGTH - 1; + const encodedTokenAddress = encodedAssetData.slice(0, addressOffset); const tokenAddress = assetProxyUtils.decodeAddress(encodedTokenAddress); const erc20AssetData = { assetProxyId, @@ -75,30 +75,36 @@ export const assetProxyUtils = { }; return erc20AssetData; }, - encodeERC721AssetData(tokenAddress: string, tokenId: BigNumber, data?: string): string { + encodeERC721AssetData(tokenAddress: string, tokenId: BigNumber, receiverData?: string): string { const encodedAssetProxyId = assetProxyUtils.encodeAssetProxyId(AssetProxyId.ERC721); const encodedAddress = assetProxyUtils.encodeAddress(tokenAddress); const encodedTokenId = assetProxyUtils.encodeUint256(tokenId); - let encodedMetadata = Buffer.concat([encodedAddress, encodedTokenId]); - if (!_.isUndefined(data)) { - const encodedData = ethUtil.toBuffer(data); - const dataLength = new BigNumber(encodedData.byteLength); - const encodedDataLength = assetProxyUtils.encodeUint256(dataLength); - encodedMetadata = Buffer.concat([encodedMetadata, encodedDataLength, encodedData]); + let encodedAssetData = Buffer.concat([encodedAddress, encodedTokenId]); + if (!_.isUndefined(receiverData)) { + const encodedReceiverData = ethUtil.toBuffer(receiverData); + const receiverDataLength = new BigNumber(encodedReceiverData.byteLength); + const encodedReceiverDataLength = assetProxyUtils.encodeUint256(receiverDataLength); + encodedAssetData = Buffer.concat([encodedAssetData, encodedReceiverDataLength, encodedReceiverData]); } - encodedMetadata = Buffer.concat([encodedMetadata, encodedAssetProxyId]); - const encodedMetadataHex = ethUtil.bufferToHex(encodedMetadata); - return encodedMetadataHex; + encodedAssetData = Buffer.concat([encodedAssetData, encodedAssetProxyId]); + const encodedAssetDataHex = ethUtil.bufferToHex(encodedAssetData); + return encodedAssetDataHex; }, decodeERC721AssetData(assetData: string): ERC721AssetData { const encodedAssetData = ethUtil.toBuffer(assetData); - if (encodedAssetData.byteLength < ERC721_PROXY_METADATA_BYTE_LENGTH) { + if (encodedAssetData.byteLength < ERC721_ASSET_DATA_BYTE_LENGTH) { throw new Error( `Could not decode ERC20 Proxy Data. Expected length of encoded data to be at least 53. Got ${ encodedAssetData.byteLength }`, ); } + const addressOffset = 0; + const tokenIdOffset = 20; + const receiverDataLengthOffset = 52; + const receiverDataOffset = 84; + const encodedTokenAddress = encodedAssetData.slice(addressOffset, tokenIdOffset); + const proxyIdOffset = encodedAssetData.byteLength - 1; const encodedAssetProxyId = encodedAssetData.slice(-1); const assetProxyId = assetProxyUtils.decodeAssetProxyId(encodedAssetProxyId); if (assetProxyId !== AssetProxyId.ERC721) { @@ -108,31 +114,31 @@ export const assetProxyUtils = { }), but got ${assetProxyId}`, ); } - const addressOffset = ERC20_PROXY_METADATA_BYTE_LENGTH - 1; - const encodedTokenAddress = encodedProxyMetadata.slice(0, addressOffset); const tokenAddress = assetProxyUtils.decodeAddress(encodedTokenAddress); - const tokenIdOffset = ERC721_PROXY_METADATA_BYTE_LENGTH - 1; - const encodedTokenId = encodedProxyMetadata.slice(addressOffset, tokenIdOffset); + const encodedTokenId = encodedAssetData.slice(tokenIdOffset, receiverDataLengthOffset); const tokenId = assetProxyUtils.decodeUint256(encodedTokenId); const nullData = '0x'; - let data = nullData; - if (encodedAssetData.byteLength > 53) { - const encodedDataLength = encodedAssetData.slice(52, 84); - const dataLength = assetProxyUtils.decodeUint256(encodedDataLength); - const expectedDataLength = new BigNumber(encodedAssetData.byteLength - 85); - if (!dataLength.equals(expectedDataLength)) { + let receiverData = nullData; + if (encodedAssetData.byteLength > receiverDataLengthOffset + 1) { + const encodedReceiverDataLength = encodedAssetData.slice(receiverDataLengthOffset, receiverDataOffset); + const receiverDataLength = assetProxyUtils.decodeUint256(encodedReceiverDataLength); + const expectedReceiverDataLength = new BigNumber(encodedAssetData.byteLength - (receiverDataOffset + 1)); + if (!receiverDataLength.equals(expectedReceiverDataLength)) { throw new Error( - `Data length (${dataLength}) does not match actual length of data (${expectedDataLength})`, + `Data length (${receiverDataLength}) does not match actual length of data (${expectedReceiverDataLength})`, ); } - const encodedData = encodedAssetData.slice(84, expectedDataLength.toNumber() + 84); - data = ethUtil.bufferToHex(encodedData); + const encodedReceiverData = encodedAssetData.slice( + receiverDataOffset, + receiverDataOffset + receiverDataLength.toNumber(), + ); + receiverData = ethUtil.bufferToHex(encodedReceiverData); } const erc721AssetData: ERC721AssetData = { assetProxyId, tokenAddress, tokenId, - data, + receiverData, }; return erc721AssetData; }, diff --git a/packages/order-utils/src/order_state_utils.ts b/packages/order-utils/src/order_state_utils.ts index ca18097c9..12f5d8f7e 100644 --- a/packages/order-utils/src/order_state_utils.ts +++ b/packages/order-utils/src/order_state_utils.ts @@ -111,8 +111,8 @@ export class OrderStateUtils { const transferrableMakerAssetAmount = BigNumber.min([makerProxyAllowance, makerBalance]); const transferrableFeeAssetAmount = BigNumber.min([makerFeeProxyAllowance, makerFeeBalance]); - const zrxAssetData = assetProxyUtils.encodeERC20ProxyData(zrxTokenAddress); - const isMakerAssetZRX = signedOrder.makerAssetData === zrxAssetData; + const zrxAssetData = assetProxyUtils.encodeERC20AssetData(zrxTokenAddress); + const isMakerTokenZRX = signedOrder.makerAssetData === zrxAssetData; const remainingFillableCalculator = new RemainingFillableCalculator( signedOrder.makerFee, signedOrder.makerAssetAmount, diff --git a/packages/types/src/index.ts b/packages/types/src/index.ts index 5ba66158e..0f7bb73be 100644 --- a/packages/types/src/index.ts +++ b/packages/types/src/index.ts @@ -156,18 +156,19 @@ export enum AssetProxyId { ERC721, } -export interface ERC20ProxyData { +export interface ERC20AssetData { assetProxyId: AssetProxyId; tokenAddress: string; } -export interface ERC721ProxyData { +export interface ERC721AssetData { assetProxyId: AssetProxyId; tokenAddress: string; tokenId: BigNumber; + receiverData: string; } -export interface ProxyData { +export interface AssetData { assetProxyId: AssetProxyId; tokenAddress?: string; data?: any; |