aboutsummaryrefslogtreecommitdiffstats
path: root/packages/contracts/src/utils/asset_proxy_utils.ts
blob: 7fcf3dd2637255bbf2e0ddcef1eeb60b2823df7d (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
import { BigNumber } from '@0xproject/utils';
import * as Web3 from 'web3';

import { AssetProxyId } from './types';

export function zeroPad(value: string, width: number): string {
    return '0'.repeat(width - value.length) + value;
}

export function encodeAssetProxyId(assetProxyId: AssetProxyId, encodedMetadata: { value: string }) {
    encodedMetadata.value += zeroPad(new BigNumber(assetProxyId).toString(16), 2);
}

export function encodeAddress(address: string, encodedMetadata: { value: string }) {
    encodedMetadata.value += zeroPad(address.replace('0x', ''), 40);
}

export function encodeUint256(value: BigNumber, encodedMetadata: { value: string }) {
    encodedMetadata.value += zeroPad(value.toString(16), 64);
}

export function encodeERC20ProxyMetadata_V1(tokenAddress: string) {
    // Encode metadata
    const encodedMetadata = { value: '0x' };
    encodeAssetProxyId(AssetProxyId.ERC20_V1, encodedMetadata);
    encodeAddress(tokenAddress, encodedMetadata);

    // Verify encoding length - '0x' plus 21 bytes of encoded data
    if (encodedMetadata.value.length != 44) {
        throw Error('Bad encoding length. Expected 44, got ' + encodedMetadata.value.length);
    }

    // Return encoded metadata
    return encodedMetadata.value;
}

export function encodeERC20ProxyMetadata(tokenAddress: string) {
    // Encode metadata
    const encodedMetadata = { value: '0x' };
    encodeAssetProxyId(AssetProxyId.ERC20, encodedMetadata);
    encodeAddress(tokenAddress, encodedMetadata);

    // Verify encoding length - '0x' plus 21 bytes of encoded data
    if (encodedMetadata.value.length != 44) {
        throw Error('Bad encoding length. Expected 44, got ' + encodedMetadata.value.length);
    }

    // Return encoded metadata
    return encodedMetadata.value;
}

export function encodeERC721ProxyMetadata(tokenAddress: string, tokenId: BigNumber) {
    // Encode metadata
    const encodedMetadata = { value: '0x' };
    encodeAssetProxyId(AssetProxyId.ERC721, encodedMetadata);
    encodeAddress(tokenAddress, encodedMetadata);
    encodeUint256(tokenId, encodedMetadata);

    // Verify encoding length - '0x' plus 53 bytes of encoded data
    if (encodedMetadata.value.length != 108) {
        throw Error('Bad encoding length. Expected 108, got ' + encodedMetadata.value.length);
    }

    // Return encoded metadata
    return encodedMetadata.value;
}