diff options
author | Greg Hysen <greg.hysen@gmail.com> | 2018-04-11 08:53:34 +0800 |
---|---|---|
committer | Amir Bandeali <abandeali1@gmail.com> | 2018-04-21 04:56:17 +0800 |
commit | 78d81f193f3b9358ab86819f83c76b8bcd52a9c9 (patch) | |
tree | 6f3532ebbf6ed2bc3d07747ba442ec77d4d9ebe3 /packages/contracts/src/utils | |
parent | b9e0cd4512e6c7cd7584961df6ba106541826836 (diff) | |
download | dexon-0x-contracts-78d81f193f3b9358ab86819f83c76b8bcd52a9c9.tar.gz dexon-0x-contracts-78d81f193f3b9358ab86819f83c76b8bcd52a9c9.tar.zst dexon-0x-contracts-78d81f193f3b9358ab86819f83c76b8bcd52a9c9.zip |
Asset Proxy Dispatcher
Diffstat (limited to 'packages/contracts/src/utils')
-rw-r--r-- | packages/contracts/src/utils/asset_proxy_utils.ts | 67 | ||||
-rw-r--r-- | packages/contracts/src/utils/constants.ts | 1 | ||||
-rw-r--r-- | packages/contracts/src/utils/crypto.ts | 2 | ||||
-rw-r--r-- | packages/contracts/src/utils/order_utils.ts | 6 | ||||
-rw-r--r-- | packages/contracts/src/utils/types.ts | 20 |
5 files changed, 94 insertions, 2 deletions
diff --git a/packages/contracts/src/utils/asset_proxy_utils.ts b/packages/contracts/src/utils/asset_proxy_utils.ts new file mode 100644 index 000000000..938110a75 --- /dev/null +++ b/packages/contracts/src/utils/asset_proxy_utils.ts @@ -0,0 +1,67 @@ +import { BigNumber } from '@0xproject/utils'; +import * as Web3 from 'web3'; + +import { AssetProxyId } from './types'; +const ethersUtils = require('ethers-utils'); + +export function zeroPad(value: string, width: number): string { + return '0'.repeat(width - value.length) + value; +} + +export function encodeAssetProxyId(assetProxyId: AssetProxyId, encoded_metadata: { value: string }) { + encoded_metadata.value += zeroPad(new BigNumber(assetProxyId).toString(16), 2); +} + +export function encodeAddress(address: string, encoded_metadata: { value: string }) { + encoded_metadata.value += zeroPad(address.replace('0x', ''), 40); +} + +export function encodeUint256(value: BigNumber, encoded_metadata: { value: string }) { + encoded_metadata.value += zeroPad(value.toString(16), 64); +} + +export function encodeERC20ProxyMetadata_V1(tokenAddress: string) { + // Encode metadata + const encoded_metadata = { value: '0x' }; + encodeAssetProxyId(AssetProxyId.ERC20_V1, encoded_metadata); + encodeAddress(tokenAddress, encoded_metadata); + + // Verify encoding length - '0x' plus 21 bytes of encoded data + if (encoded_metadata.value.length != 44) { + throw Error('Bad encoding length. Expected 44, got ' + encoded_metadata.value.length); + } + + // Return encoded metadata + return encoded_metadata.value; +} + +export function encodeERC20ProxyMetadata(tokenAddress: string) { + // Encode metadata + const encoded_metadata = { value: '0x' }; + encodeAssetProxyId(AssetProxyId.ERC20, encoded_metadata); + encodeAddress(tokenAddress, encoded_metadata); + + // Verify encoding length - '0x' plus 21 bytes of encoded data + if (encoded_metadata.value.length != 44) { + throw Error('Bad encoding length. Expected 44, got ' + encoded_metadata.value.length); + } + + // Return encoded metadata + return encoded_metadata.value; +} + +export function encodeERC721ProxyMetadata(tokenAddress: string, tokenId: BigNumber) { + // Encode metadata + const encoded_metadata = { value: '0x' }; + encodeAssetProxyId(AssetProxyId.ERC721, encoded_metadata); + encodeAddress(tokenAddress, encoded_metadata); + encodeUint256(tokenId, encoded_metadata); + + // Verify encoding length - '0x' plus 53 bytes of encoded data + if (encoded_metadata.value.length != 108) { + throw Error('Bad encoding length. Expected 108, got ' + encoded_metadata.value.length); + } + + // Return encoded metadata + return encoded_metadata.value; +} diff --git a/packages/contracts/src/utils/constants.ts b/packages/contracts/src/utils/constants.ts index d31e1e285..49872fc59 100644 --- a/packages/contracts/src/utils/constants.ts +++ b/packages/contracts/src/utils/constants.ts @@ -26,5 +26,6 @@ export const constants = { MAX_TOKEN_TRANSFERFROM_GAS: 80000, MAX_TOKEN_APPROVE_GAS: 60000, DUMMY_TOKEN_ARGS: [DUMMY_TOKEN_NAME, DUMMY_TOKEN_SYMBOL, DUMMY_TOKEN_DECIMALS, DUMMY_TOKEN_TOTAL_SUPPLY], + DUMMY_ERC721TOKEN_ARGS: [DUMMY_TOKEN_NAME, DUMMY_TOKEN_SYMBOL], TESTRPC_PRIVATE_KEYS: _.map(TESTRPC_PRIVATE_KEYS_STRINGS, privateKeyString => ethUtil.toBuffer(privateKeyString)), }; diff --git a/packages/contracts/src/utils/crypto.ts b/packages/contracts/src/utils/crypto.ts index 810072d2f..5bc678cdf 100644 --- a/packages/contracts/src/utils/crypto.ts +++ b/packages/contracts/src/utils/crypto.ts @@ -31,6 +31,8 @@ export const crypto = { argTypes.push('address'); } else if (_.isString(arg)) { argTypes.push('string'); + } else if (arg instanceof Buffer) { + argTypes.push('bytes'); } else if (_.isBoolean(arg)) { argTypes.push('bool'); } else { diff --git a/packages/contracts/src/utils/order_utils.ts b/packages/contracts/src/utils/order_utils.ts index 26336c81d..8eb4da35a 100644 --- a/packages/contracts/src/utils/order_utils.ts +++ b/packages/contracts/src/utils/order_utils.ts @@ -35,6 +35,8 @@ export const orderUtils = { takerFee: signedOrder.takerFee, expirationTimeSeconds: signedOrder.expirationTimeSeconds, salt: signedOrder.salt, + makerAssetProxyData: signedOrder.makerAssetProxyData, + takerAssetProxyData: signedOrder.takerAssetProxyData, }; return orderStruct; }, @@ -52,6 +54,8 @@ export const orderUtils = { 'uint256 takerFee', 'uint256 expirationTimeSeconds', 'uint256 salt', + 'bytes makerAssetProxyData', + 'bytes takerAssetProxyData', ]); const orderParamsHashBuff = crypto.solSHA3([ order.exchangeAddress, @@ -66,6 +70,8 @@ export const orderUtils = { order.takerFee, order.expirationTimeSeconds, order.salt, + ethUtil.toBuffer(order.makerAssetProxyData), + ethUtil.toBuffer(order.takerAssetProxyData), ]); const orderSchemaHashHex = `0x${orderSchemaHashBuff.toString('hex')}`; const orderParamsHashHex = `0x${orderParamsHashBuff.toString('hex')}`; diff --git a/packages/contracts/src/utils/types.ts b/packages/contracts/src/utils/types.ts index ed0ebeee9..f1636929b 100644 --- a/packages/contracts/src/utils/types.ts +++ b/packages/contracts/src/utils/types.ts @@ -37,6 +37,13 @@ export interface CancelOrdersBefore { salt: BigNumber; } +export enum AssetProxyId { + INVALID, + ERC20_V1, + ERC20, + ERC721, +} + export interface DefaultOrderParams { exchangeAddress: string; makerAddress: string; @@ -45,8 +52,10 @@ export interface DefaultOrderParams { takerTokenAddress: string; makerTokenAmount: BigNumber; takerTokenAmount: BigNumber; - makerFee: BigNumber; - takerFee: BigNumber; + makerFeeAmount: BigNumber; + takerFeeAmount: BigNumber; + makerAssetProxyData: string; + takerAssetProxyData: string; } export interface TransactionDataParams { @@ -100,6 +109,11 @@ export enum ContractName { AccountLevels = 'AccountLevels', EtherDelta = 'EtherDelta', Arbitrage = 'Arbitrage', + AssetProxyDispatcher = 'AssetProxyDispatcher', + ERC20TransferProxy = 'ERC20TransferProxy', + ERC20TransferProxy_V1 = 'ERC20TransferProxy_v1', + ERC721TransferProxy = 'ERC721TransferProxy', + DummyERC721Token = 'DummyERC721Token', } export interface Artifact { @@ -134,6 +148,8 @@ export interface OrderStruct { takerFee: BigNumber; expirationTimeSeconds: BigNumber; salt: BigNumber; + makerAssetProxyData: string; + takerAssetProxyData: string; } export interface UnsignedOrder extends OrderStruct { |