diff options
author | Fabio Berger <me@fabioberger.com> | 2018-07-18 21:18:54 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-07-18 21:18:54 +0800 |
commit | e2fb49a8f871fd4b631bb01bb641c632b7c19435 (patch) | |
tree | 6a04c7686159cfa80435e47e08ceb08533c0dd77 /packages/order-utils | |
parent | f3241ff86a0d99f4291c5a5f4eaaa5ebe1736da0 (diff) | |
parent | b9e7973a0f92661f2e469b748a162b0d3bb53624 (diff) | |
download | dexon-0x-contracts-e2fb49a8f871fd4b631bb01bb641c632b7c19435.tar dexon-0x-contracts-e2fb49a8f871fd4b631bb01bb641c632b7c19435.tar.gz dexon-0x-contracts-e2fb49a8f871fd4b631bb01bb641c632b7c19435.tar.bz2 dexon-0x-contracts-e2fb49a8f871fd4b631bb01bb641c632b7c19435.tar.lz dexon-0x-contracts-e2fb49a8f871fd4b631bb01bb641c632b7c19435.tar.xz dexon-0x-contracts-e2fb49a8f871fd4b631bb01bb641c632b7c19435.tar.zst dexon-0x-contracts-e2fb49a8f871fd4b631bb01bb641c632b7c19435.zip |
Merge pull request #884 from 0xProject/export-more-0x.js
Export missing V2 pieces from 0x.js
Diffstat (limited to 'packages/order-utils')
-rw-r--r-- | packages/order-utils/package.json | 1 | ||||
-rw-r--r-- | packages/order-utils/src/asset_data_utils.ts (renamed from packages/order-utils/src/asset_proxy_utils.ts) | 110 | ||||
-rw-r--r-- | packages/order-utils/src/constants.ts | 1 | ||||
-rw-r--r-- | packages/order-utils/src/index.ts | 2 | ||||
-rw-r--r-- | packages/order-utils/test/exchange_transfer_simulator_test.ts | 4 |
5 files changed, 55 insertions, 63 deletions
diff --git a/packages/order-utils/package.json b/packages/order-utils/package.json index a8205f848..cdacf58d0 100644 --- a/packages/order-utils/package.json +++ b/packages/order-utils/package.json @@ -54,7 +54,6 @@ "@0xproject/dev-utils": "^0.4.5", "@0xproject/monorepo-scripts": "^0.2.2", "@0xproject/tslint-config": "^0.4.21", - "@types/ethereumjs-abi": "^0.6.0", "@types/bn.js": "^4.11.0", "@types/lodash": "4.14.104", "chai": "^4.0.1", diff --git a/packages/order-utils/src/asset_proxy_utils.ts b/packages/order-utils/src/asset_data_utils.ts index 8140ad89d..cb02d7d42 100644 --- a/packages/order-utils/src/asset_proxy_utils.ts +++ b/packages/order-utils/src/asset_data_utils.ts @@ -1,64 +1,25 @@ import { AssetProxyId, ERC20AssetData, ERC721AssetData } from '@0xproject/types'; import { BigNumber } from '@0xproject/utils'; -import BN = require('bn.js'); +import ethAbi = require('ethereumjs-abi'); import ethUtil = require('ethereumjs-util'); import { constants } from './constants'; -// TODO: Push upstream to DefinitelyTyped -interface EthAbi { - simpleEncode(signature: string, ...args: any[]): Buffer; - rawDecode(signature: string[], data: Buffer): any[]; -} -// tslint:disable:no-var-requires -const ethAbi = require('ethereumjs-abi') as EthAbi; - -export const assetProxyUtils = { - encodeAssetProxyId(assetProxyId: AssetProxyId): Buffer { - return ethUtil.toBuffer(assetProxyId); - }, - decodeAssetProxyId(encodedAssetProxyId: Buffer): AssetProxyId { - const hexString = ethUtil.bufferToHex(encodedAssetProxyId); - if (hexString === AssetProxyId.ERC20) { - return AssetProxyId.ERC20; - } - if (hexString === AssetProxyId.ERC721) { - return AssetProxyId.ERC721; - } - throw new Error(`Invalid ProxyId: ${hexString}`); - }, - encodeAddress(address: string): Buffer { - if (!ethUtil.isValidAddress(address)) { - throw new Error(`Invalid Address: ${address}`); - } - const encodedAddress = ethUtil.toBuffer(address); - const padded = ethUtil.setLengthLeft(encodedAddress, constants.WORD_LENGTH); - return padded; - }, - decodeAddress(encodedAddress: Buffer): string { - const unpadded = ethUtil.setLengthLeft(encodedAddress, constants.ADDRESS_LENGTH); - const address = ethUtil.bufferToHex(unpadded); - if (!ethUtil.isValidAddress(address)) { - throw new Error(`Invalid Address: ${address}`); - } - return address; - }, - encodeUint256(value: BigNumber): Buffer { - const base = 10; - const formattedValue = new BN(value.toString(base)); - const encodedValue = ethUtil.toBuffer(formattedValue); - // tslint:disable-next-line:custom-no-magic-numbers - const paddedValue = ethUtil.setLengthLeft(encodedValue, constants.WORD_LENGTH); - return paddedValue; - }, - decodeUint256(encodedValue: Buffer): BigNumber { - const formattedValue = ethUtil.bufferToHex(encodedValue); - const value = new BigNumber(formattedValue, constants.BASE_16); - return value; - }, +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 { return ethUtil.bufferToHex(ethAbi.simpleEncode('ERC20Token(address)', tokenAddress)); }, + /** + * 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 { const data = ethUtil.toBuffer(assetData); if (data.byteLength < constants.ERC20_ASSET_DATA_BYTE_LENGTH) { @@ -82,6 +43,13 @@ export const assetProxyUtils = { tokenAddress: ethUtil.addHexPrefix(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, receiverData?: string): string { // TODO: Pass `tokendId` as a BigNumber. return ethUtil.bufferToHex( @@ -93,6 +61,11 @@ export const assetProxyUtils = { ), ); }, + /** + * 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 { const data = ethUtil.toBuffer(assetData); if (data.byteLength < constants.ERC721_ASSET_DATA_MINIMUM_BYTE_LENGTH) { @@ -121,30 +94,51 @@ export const assetProxyUtils = { receiverData: ethUtil.bufferToHex(receiverData), }; }, - decodeAssetDataId(assetData: string): AssetProxyId { + /** + * Decode and return the assetProxyId from the assetData + * @param assetData Hex encoded assetData string to decode + * @return The assetProxyId + */ + decodeAssetProxyId(assetData: string): AssetProxyId { const encodedAssetData = ethUtil.toBuffer(assetData); if (encodedAssetData.byteLength < constants.SELECTOR_LENGTH) { throw new Error( - `Could not decode Proxy Data. Expected length of encoded data to be at least 4. Got ${ + `Could not decode assetData. Expected length of encoded data to be at least 4. Got ${ encodedAssetData.byteLength }`, ); } const encodedAssetProxyId = encodedAssetData.slice(0, constants.SELECTOR_LENGTH); - const assetProxyId = assetProxyUtils.decodeAssetProxyId(encodedAssetProxyId); + const assetProxyId = decodeAssetProxyId(encodedAssetProxyId); return 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 + */ decodeAssetData(assetData: string): ERC20AssetData | ERC721AssetData { - const assetProxyId = assetProxyUtils.decodeAssetDataId(assetData); + const assetProxyId = assetDataUtils.decodeAssetProxyId(assetData); switch (assetProxyId) { case AssetProxyId.ERC20: - const erc20AssetData = assetProxyUtils.decodeERC20AssetData(assetData); + const erc20AssetData = assetDataUtils.decodeERC20AssetData(assetData); return erc20AssetData; case AssetProxyId.ERC721: - const erc721AssetData = assetProxyUtils.decodeERC721AssetData(assetData); + const erc721AssetData = assetDataUtils.decodeERC721AssetData(assetData); return erc721AssetData; default: throw new Error(`Unrecognized asset proxy id: ${assetProxyId}`); } }, }; + +function decodeAssetProxyId(encodedAssetProxyId: Buffer): AssetProxyId { + const hexString = ethUtil.bufferToHex(encodedAssetProxyId); + if (hexString === AssetProxyId.ERC20) { + return AssetProxyId.ERC20; + } + if (hexString === AssetProxyId.ERC721) { + return AssetProxyId.ERC721; + } + throw new Error(`Invalid ProxyId: ${hexString}`); +} diff --git a/packages/order-utils/src/constants.ts b/packages/order-utils/src/constants.ts index 383a657b8..bb7482184 100644 --- a/packages/order-utils/src/constants.ts +++ b/packages/order-utils/src/constants.ts @@ -6,7 +6,6 @@ export const constants = { UNLIMITED_ALLOWANCE_IN_BASE_UNITS: new BigNumber(2).pow(256).minus(1), TESTRPC_NETWORK_ID: 50, ADDRESS_LENGTH: 20, - WORD_LENGTH: 32, ERC20_ASSET_DATA_BYTE_LENGTH: 36, ERC721_ASSET_DATA_MINIMUM_BYTE_LENGTH: 53, SELECTOR_LENGTH: 4, diff --git a/packages/order-utils/src/index.ts b/packages/order-utils/src/index.ts index b4a7a6b67..8d748a2dc 100644 --- a/packages/order-utils/src/index.ts +++ b/packages/order-utils/src/index.ts @@ -19,7 +19,7 @@ export { AbstractOrderFilledCancelledFetcher } from './abstract/abstract_order_f export { BalanceAndProxyAllowanceLazyStore } from './store/balance_and_proxy_allowance_lazy_store'; export { RemainingFillableCalculator } from './remaining_fillable_calculator'; export { OrderStateUtils } from './order_state_utils'; -export { assetProxyUtils } from './asset_proxy_utils'; +export { assetDataUtils } from './asset_data_utils'; export { EIP712Utils } from './eip712_utils'; export { OrderValidationUtils } from './order_validation_utils'; export { ExchangeTransferSimulator } from './exchange_transfer_simulator'; diff --git a/packages/order-utils/test/exchange_transfer_simulator_test.ts b/packages/order-utils/test/exchange_transfer_simulator_test.ts index f86b05e20..f5c18cdb9 100644 --- a/packages/order-utils/test/exchange_transfer_simulator_test.ts +++ b/packages/order-utils/test/exchange_transfer_simulator_test.ts @@ -4,7 +4,7 @@ import { BigNumber } from '@0xproject/utils'; import * as chai from 'chai'; import { artifacts } from '../src/artifacts'; -import { assetProxyUtils } from '../src/asset_proxy_utils'; +import { assetDataUtils } from '../src/asset_data_utils'; import { constants } from '../src/constants'; import { ExchangeTransferSimulator } from '../src/exchange_transfer_simulator'; import { DummyERC20TokenContract } from '../src/generated_contract_wrappers/dummy_erc20_token'; @@ -66,7 +66,7 @@ describe('ExchangeTransferSimulator', async () => { totalSupply, ); - exampleAssetData = assetProxyUtils.encodeERC20AssetData(dummyERC20Token.address); + exampleAssetData = assetDataUtils.encodeERC20AssetData(dummyERC20Token.address); }); beforeEach(async () => { await blockchainLifecycle.startAsync(); |