diff options
Diffstat (limited to 'packages')
-rw-r--r-- | packages/contract-wrappers/src/contract_wrappers/dutch_auction_wrapper.ts | 94 | ||||
-rw-r--r-- | packages/contract-wrappers/test/dutch_auction_wrapper_test.ts | 90 |
2 files changed, 76 insertions, 108 deletions
diff --git a/packages/contract-wrappers/src/contract_wrappers/dutch_auction_wrapper.ts b/packages/contract-wrappers/src/contract_wrappers/dutch_auction_wrapper.ts index fee543c3b..5896617fc 100644 --- a/packages/contract-wrappers/src/contract_wrappers/dutch_auction_wrapper.ts +++ b/packages/contract-wrappers/src/contract_wrappers/dutch_auction_wrapper.ts @@ -7,7 +7,7 @@ import { _getDefaultContractAddresses } from '../utils/contract_addresses'; import { DutchAuctionDetails, SignedOrder } from '@0x/types'; import { ContractAbi } from 'ethereum-types'; import { Web3Wrapper } from '@0x/web3-wrapper'; -import { BigNumber } from '@0x/utils'; +import { BigNumber, abiUtils } from '@0x/utils'; import { Provider, TransactionReceiptWithDecodedLogs } from 'ethereum-types'; import * as _ from 'lodash'; import ethAbi = require('ethereumjs-abi'); @@ -21,8 +21,7 @@ import { OrderTransactionOpts } from '../types'; import { ContractWrapper } from './contract_wrapper'; import { ExchangeWrapperError } from '../types'; -import { orderFactory } from '@0x/order-utils/lib/src/order_factory'; -import { constants } from 'zlib'; +import { assetDataUtils, AssetData } from '@0x/order-utils'; export class DutchAuctionWrapper extends ContractWrapper { public abi: ContractAbi = DutchAuction.compilerOutput.abi; @@ -74,6 +73,10 @@ export class DutchAuctionWrapper extends ContractWrapper { sellOrder.takerAssetData !== buyOrder.makerAssetData ) { throw new Error(ExchangeWrapperError.AssetDataMismatch); + } else { + // Smart contracts assigns the asset data from the left order to the right one so we can save gas on reducing the size of call data + //rightSignedOrder.makerAssetData = '0x'; + // rightSignedOrder.takerAssetData = '0x'; } // get contract const dutchAuctionInstance = await this._getDutchAuctionContractAsync(); @@ -136,66 +139,6 @@ export class DutchAuctionWrapper extends ContractWrapper { this._dutchAuctionContractIfExists = contractInstance; return this._dutchAuctionContractIfExists; } - - public async createSignedSellOrderAsync( - auctionBeginTimeSections: BigNumber, - auctionBeginAmount: BigNumber, - auctionEndAmount: BigNumber, - acutionEndTime: BigNumber, - makerAssetData: string, - takerAssetData: string, - makerAddress: string, - takerAddress: string, - takerFillableAmount: BigNumber, - senderAddress?: string, - makerFee?: BigNumber, - takerFee?: BigNumber, - feeRecipientAddress?: string, - ): Promise<SignedOrder> { - console.log(`asdasd`); - const makerAssetAmount = auctionEndAmount; - const makerAssetDataWithAuctionDetails = DutchAuctionWrapper.encodeDutchAuctionAssetData(makerAssetData, auctionBeginTimeSections, auctionBeginAmount); - const signedOrder = await orderFactory.createSignedOrderAsync( - this._web3Wrapper.getProvider(), - makerAddress, - makerAssetAmount, - makerAssetDataWithAuctionDetails, - takerFillableAmount, - takerAssetData, - this._exchangeAddress, - { - takerAddress, - senderAddress, - makerFee, - takerFee, - feeRecipientAddress, - expirationTimeSeconds: acutionEndTime, - }, - ); - //console.log(signedOrder); - return signedOrder; - } - - public async createSignedBuyOrderAsync(sellOrder: SignedOrder, buyerAddress: string, senderAddress?: string, makerFee?: BigNumber, takerFee?: BigNumber, feeRecipientAddress?: string): Promise<SignedOrder> { - const signedOrder = await orderFactory.createSignedOrderAsync( - this._web3Wrapper.getProvider(), - buyerAddress, - sellOrder.takerAssetAmount.times(2), // change this to decode value from auction @TODO -- add decode above for this. - sellOrder.takerAssetData, - sellOrder.makerAssetAmount, - sellOrder.makerAssetData, - sellOrder.exchangeAddress, - { - senderAddress, - makerFee, - takerFee, - feeRecipientAddress, - expirationTimeSeconds: sellOrder.expirationTimeSeconds, - }, - ); - // console.log(signedOrder); - return signedOrder; - } /** * Dutch auction details are encoded with the asset data for a 0x order. This function produces a hex * encoded assetData string, containing information both about the asset being traded and the @@ -206,7 +149,6 @@ export class DutchAuctionWrapper extends ContractWrapper { * @return The hex encoded assetData string. */ public static encodeDutchAuctionAssetData(assetData: string, beginTimeSeconds: BigNumber, beginAmount: BigNumber): string { - // console.log(`yoooo`, assetData); const assetDataBuffer = ethUtil.toBuffer(assetData); const abiEncodedAuctionData = (ethAbi as any).rawEncode( ['uint256', 'uint256'], @@ -218,4 +160,28 @@ export class DutchAuctionWrapper extends ContractWrapper { const dutchAuctionData = ethUtil.bufferToHex(dutchAuctionDataBuffer); return dutchAuctionData; }; + /** + * Dutch auction details are encoded with the asset data for a 0x order. This function produces a hex + * encoded assetData string, containing information both about the asset being traded and the + * dutch auction; which is usable in the makerAssetData or takerAssetData fields in a 0x order. + * @param dutchAuctionData Hex encoded assetData string for the asset being auctioned. + * @return + */ + public static decodeDutchAuctionData(dutchAuctionData: string): [AssetData, BigNumber, BigNumber] { + const dutchAuctionDataBuffer = ethUtil.toBuffer(dutchAuctionData); + // Decode asset data + const assetDataBuffer = dutchAuctionDataBuffer.slice(0, dutchAuctionDataBuffer.byteLength - 64); + const assetDataHex = ethUtil.bufferToHex(assetDataBuffer); + const assetData = assetDataUtils.decodeAssetDataOrThrow(assetDataHex); + // Decode auction details + const dutchAuctionDetailsBuffer = dutchAuctionDataBuffer.slice(dutchAuctionDataBuffer.byteLength - 64); + const [beginTimeSecondsAsBN, beginAmountAsBN] = ethAbi.rawDecode( + ['uint256', 'uint256'], + dutchAuctionDetailsBuffer + ); + const beginTimeSeconds = new BigNumber(`0x${beginTimeSecondsAsBN.toString()}`); + const beginAmount = new BigNumber(`0x${beginAmountAsBN.toString()}`); + console.log(beginAmount); + return [assetData, beginTimeSeconds, beginAmount]; + }; } diff --git a/packages/contract-wrappers/test/dutch_auction_wrapper_test.ts b/packages/contract-wrappers/test/dutch_auction_wrapper_test.ts index bfc4e0a58..f548e3ff8 100644 --- a/packages/contract-wrappers/test/dutch_auction_wrapper_test.ts +++ b/packages/contract-wrappers/test/dutch_auction_wrapper_test.ts @@ -1,7 +1,7 @@ import { BlockchainLifecycle } from '@0x/dev-utils'; import { FillScenarios } from '@0x/fill-scenarios'; import { assetDataUtils } from '@0x/order-utils'; -import { SignedOrder } from '@0x/types'; +import { RevertReason, SignedOrder } from '@0x/types'; import { BigNumber } from '@0x/utils'; import * as chai from 'chai'; import 'mocha'; @@ -18,13 +18,19 @@ import { DutchAuction } from '@0x/contract-artifacts'; import { DutchAuctionWrapper } from '../src/contract_wrappers/dutch_auction_wrapper'; import { Web3Wrapper } from '@0x/web3-wrapper'; +import { DutchAuctionUtils } from './utils/dutch_auction_utils'; + +import { + expectTransactionFailedAsync, +} from '@0x/contracts-test-utils'; + chaiSetup.configure(); const expect = chai.expect; const blockchainLifecycle = new BlockchainLifecycle(web3Wrapper); // tslint:disable:custom-no-magic-numbers describe.only('DutchAuctionWrapper', () => { - const fillableAmount = Web3Wrapper.toBaseUnitAmount(new BigNumber(50), 18); + const fillableAmount = new BigNumber(2);//Web3Wrapper.toBaseUnitAmount(new BigNumber(50), 18); const tenMinutesInSeconds = 10 * 60; let contractWrappers: ContractWrappers; let fillScenarios: FillScenarios; @@ -33,13 +39,16 @@ describe.only('DutchAuctionWrapper', () => { let userAddresses: string[]; let makerAddress: string; let takerAddress: string; - let buyerAddress: string; let makerTokenAddress: string; let takerTokenAddress: string; let buyOrder: SignedOrder; let sellOrder: SignedOrder; let makerTokenAssetData: string; let takerTokenAssetData: string; + let auctionBeginTimeSeconds: BigNumber; + let auctionBeginAmount: BigNumber; + let auctionEndTimeSeconds: BigNumber; + let auctionEndAmount: BigNumber; before(async () => { console.log(`BEOGIN DEPLOYINH`); const contractAddresses = await migrateOnceAsync(); @@ -63,9 +72,8 @@ describe.only('DutchAuctionWrapper', () => { contractWrappers.erc20Proxy.address, contractWrappers.erc721Proxy.address, ); - [, makerAddress, takerAddress, buyerAddress] = userAddresses; - [makerTokenAddress] = tokenUtils.getDummyERC20TokenAddresses(); - takerTokenAddress = contractWrappers.forwarder.etherTokenAddress; + [, makerAddress, takerAddress] = userAddresses; + [makerTokenAddress, takerTokenAddress] = tokenUtils.getDummyERC20TokenAddresses(); console.log(`B`); // construct asset data for tokens being swapped [makerTokenAssetData, takerTokenAssetData] = [ @@ -74,31 +82,40 @@ describe.only('DutchAuctionWrapper', () => { ]; console.log(`C`); // encode auction details in maker asset data - const auctionBeginAmount = fillableAmount; + auctionEndAmount = fillableAmount; + auctionBeginAmount = auctionEndAmount.times(2); const currentBlockTimestamp = await getLatestBlockTimestampAsync(); - const auctionBeginTimeSeconds = new BigNumber(currentBlockTimestamp - tenMinutesInSeconds); + auctionBeginTimeSeconds = new BigNumber(currentBlockTimestamp - tenMinutesInSeconds); + auctionEndTimeSeconds = new BigNumber(currentBlockTimestamp + tenMinutesInSeconds); /* makerAssetData = DutchAuctionWrapper.encodeDutchAuctionAssetData( makerTokenAssetData, auctionBeginTimeSeconds, auctionBeginAmount );*/ console.log(`C2`); + // Create template orders from + + // create sell / buy orders for auction // note that the maker/taker asset datas are swapped in the `buyOrder` - sellOrder = await contractWrappers.dutchAuction.createSignedSellOrderAsync( + + const coinbase = userAddresses[0]; + const dutchAuctionUtils = new DutchAuctionUtils(web3Wrapper, coinbase, exchangeContractAddress, contractWrappers.erc20Proxy.address); + sellOrder = await dutchAuctionUtils.createSignedSellOrderAsync( auctionBeginTimeSeconds, - fillableAmount.times(2), - fillableAmount, - new BigNumber(currentBlockTimestamp + tenMinutesInSeconds), + auctionBeginAmount, + auctionEndAmount, + auctionEndTimeSeconds, makerTokenAssetData, takerTokenAssetData, makerAddress, constants.NULL_ADDRESS, - fillableAmount, + auctionEndAmount, ); - buyOrder = await contractWrappers.dutchAuction.createSignedBuyOrderAsync( + console.log(`ASDS`); + buyOrder = await dutchAuctionUtils.createSignedBuyOrderAsync( sellOrder, - buyerAddress, + takerAddress, ); console.log(`CD`); }); @@ -113,17 +130,15 @@ describe.only('DutchAuctionWrapper', () => { }); describe('#matchOrdersAsync', () => { it('should match two orders', async () => { - console.log(await contractWrappers.dutchAuction.getAuctionDetailsAsync(sellOrder)); - - // const txHash = await contractWrappers.dutchAuction.matchOrdersAsync(buyOrder, sellOrder, takerAddress, {gasLimit: 1000000}); - //await web3Wrapper.awaitTransactionSuccessAsync(txHash, constants.AWAIT_TRANSACTION_MINED_MS); + const txHash = await contractWrappers.dutchAuction.matchOrdersAsync(buyOrder, sellOrder, takerAddress); + await web3Wrapper.awaitTransactionSuccessAsync(txHash, constants.AWAIT_TRANSACTION_MINED_MS); }); it('should throw when invalid transaction and shouldValidate is true', async () => { // request match with bad buy/sell orders const badSellOrder = buyOrder; const badBuyOrder = sellOrder; - return expect( - await contractWrappers.dutchAuction.matchOrdersAsync( + return expectTransactionFailedAsync( + contractWrappers.dutchAuction.matchOrdersAsync( badBuyOrder, badSellOrder, takerAddress, @@ -131,32 +146,19 @@ describe.only('DutchAuctionWrapper', () => { shouldValidate: true, }, ), - ).to.be.rejectedWith('COMPLETE_FILL_FAILED'); + RevertReason.InvalidAssetData + ); }); }); describe('#getAuctionDetailsAsync', () => { - /*it('should be worth the begin price at the begining of the auction', async () => { - // setup auction details - const auctionBeginAmount = fillableAmount; - const currentBlockTimestamp = await getLatestBlockTimestampAsync(); - const auctionBeginTimeSeconds = new BigNumber(currentBlockTimestamp + tenMinutesInSeconds); - const makerAssetData = DutchAuctionWrapper.encodeDutchAuctionAssetData( - makerTokenAssetData, - auctionBeginTimeSeconds, - auctionBeginAmount - ); - const order = await fillScenarios.createFillableSignedOrderAsync( - makerAssetData, - takerAssetData, - makerAddress, - constants.NULL_ADDRESS, - fillableAmount, - ); - const auctionDetails = await contractWrappers.dutchAuction.getAuctionDetailsAsync(order); - expect(auctionDetails.currentTimeSeconds).to.be.bignumber.lte(auctionBeginTimeSeconds); - expect(auctionDetails.currentAmount).to.be.bignumber.equal(auctionBeginAmount); - expect(auctionDetails.beginAmount).to.be.bignumber.equal(auctionBeginAmount); - });*/ + it('should be worth the begin price at the begining of the auction', async () => { + // get auction details + const auctionDetails = await contractWrappers.dutchAuction.getAuctionDetailsAsync(sellOrder); + // run some basic sanity checks on the return value + expect(auctionDetails.beginTimeSeconds, 'auctionDetails.beginTimeSeconds').to.be.bignumber.equal(auctionBeginTimeSeconds); + expect(auctionDetails.beginAmount, 'auctionDetails.beginAmount').to.be.bignumber.equal(auctionBeginAmount); + expect(auctionDetails.endTimeSeconds, 'auctionDetails.endTimeSeconds').to.be.bignumber.equal(auctionEndTimeSeconds); + }); }); }); |