aboutsummaryrefslogtreecommitdiffstats
path: root/packages/contracts/src/utils/asset_proxy_utils.ts
blob: 938110a75ceb76da6732c2289df77f3105c361f1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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;
}