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/asset_proxy_utils.ts | |
parent | b9e0cd4512e6c7cd7584961df6ba106541826836 (diff) | |
download | dexon-0x-contracts-78d81f193f3b9358ab86819f83c76b8bcd52a9c9.tar dexon-0x-contracts-78d81f193f3b9358ab86819f83c76b8bcd52a9c9.tar.gz dexon-0x-contracts-78d81f193f3b9358ab86819f83c76b8bcd52a9c9.tar.bz2 dexon-0x-contracts-78d81f193f3b9358ab86819f83c76b8bcd52a9c9.tar.lz dexon-0x-contracts-78d81f193f3b9358ab86819f83c76b8bcd52a9c9.tar.xz dexon-0x-contracts-78d81f193f3b9358ab86819f83c76b8bcd52a9c9.tar.zst dexon-0x-contracts-78d81f193f3b9358ab86819f83c76b8bcd52a9c9.zip |
Asset Proxy Dispatcher
Diffstat (limited to 'packages/contracts/src/utils/asset_proxy_utils.ts')
-rw-r--r-- | packages/contracts/src/utils/asset_proxy_utils.ts | 67 |
1 files changed, 67 insertions, 0 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; +} |