Skip to content
This repository has been archived by the owner on Jul 9, 2021. It is now read-only.

Commit

Permalink
encode/decode dutch auction asset data using order-utils
Browse files Browse the repository at this point in the history
  • Loading branch information
xianny committed Jul 12, 2019
1 parent fadaa17 commit 8286eb3
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 39 deletions.
2 changes: 1 addition & 1 deletion packages/0x.js/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ export {
OrderAndTraderInfo,
TraderInfo,
ValidateOrderFillableOpts,
DutchAuctionData,
} from '@0x/contract-wrappers';

export {
Expand Down Expand Up @@ -85,6 +84,7 @@ export {
ExchangeContractErrs,
Order,
SignedOrder,
DutchAuctionData,
ECSignature,
OrderStateValid,
OrderStateInvalid,
Expand Down
8 changes: 8 additions & 0 deletions packages/contract-wrappers/CHANGELOG.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
[
{
"version": "9.1.5",
"changes": [
{
"note": "Use assetDataUtils for encoding and decoding DutchAuctionData"
}
]
},
{
"timestamp": 1558712885,
"version": "9.1.4",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,15 @@ import { DutchAuction } from '@0x/contract-artifacts';
import { schemas } from '@0x/json-schemas';
import { assetDataUtils } from '@0x/order-utils';
import { DutchAuctionDetails, SignedOrder } from '@0x/types';
import { DutchAuctionData } from '@0x/types';
import { BigNumber } from '@0x/utils';
import { Web3Wrapper } from '@0x/web3-wrapper';
import { ContractAbi } from 'ethereum-types';
import * as ethAbi from 'ethereumjs-abi';
import * as ethUtil from 'ethereumjs-util';
import * as _ from 'lodash';

import { orderTxOptsSchema } from '../schemas/order_tx_opts_schema';
import { txOptsSchema } from '../schemas/tx_opts_schema';
import { DutchAuctionData, DutchAuctionWrapperError, OrderTransactionOpts } from '../types';
import { DutchAuctionWrapperError, OrderTransactionOpts } from '../types';
import { assert } from '../utils/assert';
import { _getDefaultContractAddresses } from '../utils/contract_addresses';

Expand All @@ -36,14 +35,7 @@ export class DutchAuctionWrapper extends ContractWrapper {
beginTimeSeconds: BigNumber,
beginAmount: BigNumber,
): string {
const assetDataBuffer = ethUtil.toBuffer(assetData);
const abiEncodedAuctionData = (ethAbi as any).rawEncode(
['uint256', 'uint256'],
[beginTimeSeconds.toString(), beginAmount.toString()],
);
const abiEncodedAuctionDataBuffer = ethUtil.toBuffer(abiEncodedAuctionData);
const dutchAuctionDataBuffer = Buffer.concat([assetDataBuffer, abiEncodedAuctionDataBuffer]);
const dutchAuctionData = ethUtil.bufferToHex(dutchAuctionDataBuffer);
const dutchAuctionData = assetDataUtils.encodeDutchAuctionAssetData(assetData, beginTimeSeconds, beginAmount);
return dutchAuctionData;
}
/**
Expand All @@ -54,30 +46,8 @@ export class DutchAuctionWrapper extends ContractWrapper {
* @return An object containing the auction asset, auction begin time and auction begin amount.
*/
public static decodeDutchAuctionData(dutchAuctionData: string): DutchAuctionData {
const dutchAuctionDataBuffer = ethUtil.toBuffer(dutchAuctionData);
// Decode asset data
const dutchAuctionDataLengthInBytes = 64;
const assetDataBuffer = dutchAuctionDataBuffer.slice(
0,
dutchAuctionDataBuffer.byteLength - dutchAuctionDataLengthInBytes,
);
const assetDataHex = ethUtil.bufferToHex(assetDataBuffer);
const assetData = assetDataUtils.decodeAssetDataOrThrow(assetDataHex);
// Decode auction details
const dutchAuctionDetailsBuffer = dutchAuctionDataBuffer.slice(
dutchAuctionDataBuffer.byteLength - dutchAuctionDataLengthInBytes,
);
const [beginTimeSecondsAsBN, beginAmountAsBN] = ethAbi.rawDecode(
['uint256', 'uint256'],
dutchAuctionDetailsBuffer,
);
const beginTimeSeconds = new BigNumber(beginTimeSecondsAsBN.toString());
const beginAmount = new BigNumber(beginAmountAsBN.toString());
return {
assetData,
beginTimeSeconds,
beginAmount,
};
const decoded = assetDataUtils.decodeDutchAuctionData(dutchAuctionData);
return decoded;
}
/**
* Instantiate DutchAuctionWrapper
Expand Down
2 changes: 1 addition & 1 deletion packages/contract-wrappers/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ export {
OrderAndTraderInfo,
TraderInfo,
ValidateOrderFillableOpts,
DutchAuctionData,
CoordinatorServerCancellationResponse,
CoordinatorServerError,
} from './types';
Expand All @@ -73,6 +72,7 @@ export {
StaticCallAssetData,
MultiAssetDataWithRecursiveDecoding,
DutchAuctionDetails,
DutchAuctionData,
Order,
SignedOrder,
AssetProxyId,
Expand Down
2 changes: 0 additions & 2 deletions packages/contract-wrappers/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,8 +220,6 @@ export enum DutchAuctionWrapperError {
AssetDataMismatch = 'ASSET_DATA_MISMATCH',
}

export { DutchAuctionData } from '@0x/types';

export { CoordinatorServerCancellationResponse, CoordinatorServerError } from './utils/coordinator_server_types';

export interface CoordinatorTransaction {
Expand Down
2 changes: 2 additions & 0 deletions packages/order-utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ export {
OrderRelevantState,
OrderState,
ECSignature,
AssetData,
SingleAssetData,
DutchAuctionData,
ERC20AssetData,
ERC721AssetData,
ERC1155AssetData,
Expand Down

0 comments on commit 8286eb3

Please sign in to comment.