diff options
author | Greg Hysen <greg.hysen@gmail.com> | 2018-05-17 08:55:50 +0800 |
---|---|---|
committer | Greg Hysen <greg.hysen@gmail.com> | 2018-05-19 08:01:06 +0800 |
commit | 2eb5819851252eb90207765ed64a119ad138c0ad (patch) | |
tree | bbc49596d4d60d0a5b40cff207e527f71ab99e41 /packages/contracts/src/utils | |
parent | e748e1891b6af5c5f60f5c1fc45482ace6a3b2ad (diff) | |
download | dexon-sol-tools-2eb5819851252eb90207765ed64a119ad138c0ad.tar dexon-sol-tools-2eb5819851252eb90207765ed64a119ad138c0ad.tar.gz dexon-sol-tools-2eb5819851252eb90207765ed64a119ad138c0ad.tar.bz2 dexon-sol-tools-2eb5819851252eb90207765ed64a119ad138c0ad.tar.lz dexon-sol-tools-2eb5819851252eb90207765ed64a119ad138c0ad.tar.xz dexon-sol-tools-2eb5819851252eb90207765ed64a119ad138c0ad.tar.zst dexon-sol-tools-2eb5819851252eb90207765ed64a119ad138c0ad.zip |
Cleaned up interface for decoding proxy data. Added a general decoder, which should be useful for the forwarding contract code.
Diffstat (limited to 'packages/contracts/src/utils')
-rw-r--r-- | packages/contracts/src/utils/asset_proxy_utils.ts | 41 | ||||
-rw-r--r-- | packages/contracts/src/utils/types.ts | 17 |
2 files changed, 53 insertions, 5 deletions
diff --git a/packages/contracts/src/utils/asset_proxy_utils.ts b/packages/contracts/src/utils/asset_proxy_utils.ts index 9a26a9ca7..c042da5d0 100644 --- a/packages/contracts/src/utils/asset_proxy_utils.ts +++ b/packages/contracts/src/utils/asset_proxy_utils.ts @@ -2,7 +2,7 @@ import { BigNumber } from '@0xproject/utils'; import BN = require('bn.js'); import ethUtil = require('ethereumjs-util'); -import { AssetProxyId } from './types'; +import { AssetProxyId, ERC20ProxyData, ERC721ProxyData, ProxyData } from './types'; export const assetProxyUtils = { encodeAssetProxyId(assetProxyId: AssetProxyId): Buffer { @@ -43,7 +43,7 @@ export const assetProxyUtils = { const encodedMetadataHex = ethUtil.bufferToHex(encodedMetadata); return encodedMetadataHex; }, - decodeERC20ProxyData(proxyData: string): string /* tokenAddress */ { + decodeERC20ProxyData(proxyData: string): ERC20ProxyData { const encodedProxyMetadata = ethUtil.toBuffer(proxyData); if (encodedProxyMetadata.byteLength !== 21) { throw new Error( @@ -63,7 +63,11 @@ export const assetProxyUtils = { } const encodedTokenAddress = encodedProxyMetadata.slice(1, 21); const tokenAddress = assetProxyUtils.decodeAddress(encodedTokenAddress); - return tokenAddress; + const erc20ProxyData = { + assetProxyId, + tokenAddress, + }; + return erc20ProxyData; }, encodeERC721ProxyData(tokenAddress: string, tokenId: BigNumber): string { const encodedAssetProxyId = assetProxyUtils.encodeAssetProxyId(AssetProxyId.ERC721); @@ -73,7 +77,7 @@ export const assetProxyUtils = { const encodedMetadataHex = ethUtil.bufferToHex(encodedMetadata); return encodedMetadataHex; }, - decodeERC721ProxyData(proxyData: string): [string /* tokenAddress */, BigNumber /* tokenId */] { + decodeERC721ProxyData(proxyData: string): ERC721ProxyData { const encodedProxyMetadata = ethUtil.toBuffer(proxyData); if (encodedProxyMetadata.byteLength !== 53) { throw new Error( @@ -95,7 +99,12 @@ export const assetProxyUtils = { const tokenAddress = assetProxyUtils.decodeAddress(encodedTokenAddress); const encodedTokenId = encodedProxyMetadata.slice(21, 53); const tokenId = assetProxyUtils.decodeUint256(encodedTokenId); - return [tokenAddress, tokenId]; + const erc721ProxyData = { + assetProxyId, + tokenAddress, + tokenId, + }; + return erc721ProxyData; }, decodeProxyDataId(proxyData: string): AssetProxyId { const encodedProxyMetadata = ethUtil.toBuffer(proxyData); @@ -110,4 +119,26 @@ export const assetProxyUtils = { const assetProxyId = assetProxyUtils.decodeAssetProxyId(encodedAssetProxyId); return assetProxyId; }, + decodeProxyData(proxyData: string): ProxyData { + const assetProxyId = assetProxyUtils.decodeProxyDataId(proxyData); + switch (assetProxyId) { + case AssetProxyId.ERC20: + const erc20ProxyData = assetProxyUtils.decodeERC20ProxyData(proxyData); + const generalizedERC20ProxyData = { + assetProxyId, + tokenAddress: erc20ProxyData.tokenAddress, + }; + return generalizedERC20ProxyData; + case AssetProxyId.ERC721: + const erc721ProxyData = assetProxyUtils.decodeERC721ProxyData(proxyData); + const generaliedERC721ProxyData = { + assetProxyId, + tokenAddress: erc721ProxyData.tokenAddress, + data: erc721ProxyData.tokenId, + }; + return generaliedERC721ProxyData; + default: + throw new Error(`Unrecognized asset proxy id: ${assetProxyId}`); + } + }, }; diff --git a/packages/contracts/src/utils/types.ts b/packages/contracts/src/utils/types.ts index 518776214..ba1243672 100644 --- a/packages/contracts/src/utils/types.ts +++ b/packages/contracts/src/utils/types.ts @@ -180,3 +180,20 @@ export interface OrderInfo { orderHash: string; orderFilledAmount: BigNumber; } + +export interface ERC20ProxyData { + assetProxyId: AssetProxyId; + tokenAddress: string; +} + +export interface ERC721ProxyData { + assetProxyId: AssetProxyId; + tokenAddress: string; + tokenId: BigNumber; +} + +export interface ProxyData { + assetProxyId: AssetProxyId; + tokenAddress?: string; + data?: any; +} |