|
1 | 1 | import { assetDataUtils } from '@0x/order-utils'; |
| 2 | +import { ERC1155AssetData } from '@0x/types'; |
2 | 3 | import { AbiEncoder, BigNumber } from '@0x/utils'; |
3 | 4 |
|
4 | | -export const godsUnchainedUtils = { |
5 | | - /** |
6 | | - * Encodes the given proto and quality into the bytes format expected by the GodsUnchainedValidator. |
7 | | - */ |
8 | | - encodePropertyData(proto: BigNumber, quality: BigNumber): string { |
9 | | - return AbiEncoder.create([{ name: 'proto', type: 'uint16' }, { name: 'quality', type: 'uint8' }]).encode({ |
10 | | - proto, |
11 | | - quality, |
12 | | - }); |
13 | | - }, |
14 | | - /** |
15 | | - * Encodes the given proto and quality into ERC1155 asset data to be used as the takerAssetData |
16 | | - * of a property-based GodsUnchained order. Must also provide the addresses of the Broker, |
17 | | - * GodsUnchained, and GodsUnchainedValidator contracts. The optional bundleSize parameter specifies |
18 | | - * how many cards are expected for each "unit" of the takerAssetAmount. For example, If the |
19 | | - * takerAssetAmount is 3 and the bundleSize is 2, the taker must provide 2, 4, or 6 cards |
20 | | - * with the given proto and quality to fill the order. If an odd number is provided, the fill fails. |
21 | | - */ |
22 | | - encodeBrokerAssetData( |
23 | | - brokerAddress: string, |
24 | | - godsUnchainedAddress: string, |
25 | | - validatorAddress: string, |
26 | | - proto: BigNumber, |
27 | | - quality: BigNumber, |
28 | | - bundleSize: number = 1, |
29 | | - ): string { |
30 | | - const dataEncoder = AbiEncoder.create([ |
31 | | - { name: 'godsUnchainedAddress', type: 'address' }, |
32 | | - { name: 'validatorAddress', type: 'address' }, |
33 | | - { name: 'propertyData', type: 'bytes' }, |
34 | | - ]); |
35 | | - const propertyData = AbiEncoder.create([ |
36 | | - { name: 'proto', type: 'uint16' }, |
37 | | - { name: 'quality', type: 'uint8' }, |
38 | | - ]).encode({ proto, quality }); |
39 | | - const data = dataEncoder.encode({ godsUnchainedAddress, validatorAddress, propertyData }); |
40 | | - return assetDataUtils.encodeERC1155AssetData(brokerAddress, [], [new BigNumber(bundleSize)], data); |
41 | | - }, |
42 | | -}; |
| 5 | +export interface GodsUnchainedProperties { |
| 6 | + proto: BigNumber | number; |
| 7 | + quality: BigNumber | number; |
| 8 | +} |
| 9 | + |
| 10 | +const propertyDataEncoder = AbiEncoder.create([{ name: 'proto', type: 'uint16' }, { name: 'quality', type: 'uint8' }]); |
| 11 | +const brokerDataEncoder = AbiEncoder.create([ |
| 12 | + { name: 'godsUnchainedAddress', type: 'address' }, |
| 13 | + { name: 'validatorAddress', type: 'address' }, |
| 14 | + { name: 'propertyData', type: 'bytes' }, |
| 15 | +]); |
| 16 | + |
| 17 | +/** |
| 18 | + * Encodes the given proto and quality into the bytes format expected by the GodsUnchainedValidator. |
| 19 | + */ |
| 20 | +export function encodePropertyData(properties: GodsUnchainedProperties): string { |
| 21 | + return propertyDataEncoder.encode(properties); |
| 22 | +} |
| 23 | + |
| 24 | +/** |
| 25 | + * Encodes the given proto and quality into ERC1155 asset data to be used as the takerAssetData |
| 26 | + * of a property-based GodsUnchained order. Must also provide the addresses of the Broker, |
| 27 | + * GodsUnchained, and GodsUnchainedValidator contracts. The optional bundleSize parameter specifies |
| 28 | + * how many cards are expected for each "unit" of the takerAssetAmount. For example, If the |
| 29 | + * takerAssetAmount is 3 and the bundleSize is 2, the taker must provide 2, 4, or 6 cards |
| 30 | + * with the given proto and quality to fill the order. If an odd number is provided, the fill fails. |
| 31 | + */ |
| 32 | +export function encodeBrokerAssetData( |
| 33 | + brokerAddress: string, |
| 34 | + godsUnchainedAddress: string, |
| 35 | + validatorAddress: string, |
| 36 | + properties: GodsUnchainedProperties, |
| 37 | + bundleSize: number = 1, |
| 38 | +): string { |
| 39 | + const propertyData = propertyDataEncoder.encode(properties); |
| 40 | + const brokerData = brokerDataEncoder.encode({ godsUnchainedAddress, validatorAddress, propertyData }); |
| 41 | + return assetDataUtils.encodeERC1155AssetData(brokerAddress, [], [new BigNumber(bundleSize)], brokerData); |
| 42 | +} |
| 43 | + |
| 44 | +/** |
| 45 | + * Decodes proto and quality from the bytes format expected by the GodsUnchainedValidator. |
| 46 | + */ |
| 47 | +export function decodePropertyData(propertyData: string): GodsUnchainedProperties { |
| 48 | + return propertyDataEncoder.decode(propertyData); |
| 49 | +} |
| 50 | + |
| 51 | +/** |
| 52 | + * Decodes proto and quality from the ERC1155 takerAssetData of a property-based GodsUnchained order. |
| 53 | + */ |
| 54 | +export function decodeBrokerAssetData(brokerAssetData: string): GodsUnchainedProperties { |
| 55 | + // tslint:disable-next-line:no-unnecessary-type-assertion |
| 56 | + const { callbackData: brokerData } = assetDataUtils.decodeAssetDataOrThrow(brokerAssetData) as ERC1155AssetData; |
| 57 | + const { propertyData } = brokerDataEncoder.decode(brokerData); |
| 58 | + return decodePropertyData(propertyData); |
| 59 | +} |
0 commit comments