diff options
author | Greg Hysen <greg.hysen@gmail.com> | 2018-12-21 07:39:19 +0800 |
---|---|---|
committer | Greg Hysen <greg.hysen@gmail.com> | 2019-01-08 07:50:48 +0800 |
commit | 43b648e7dc1ea49aff3ab1e6883aa6e069fae72f (patch) | |
tree | 7db4b13135cc91a1c217c78766667e85cfc62e14 /contracts/extensions | |
parent | 89fcbec43b04a49c45786067f61c539128e1c507 (diff) | |
download | dexon-sol-tools-43b648e7dc1ea49aff3ab1e6883aa6e069fae72f.tar dexon-sol-tools-43b648e7dc1ea49aff3ab1e6883aa6e069fae72f.tar.gz dexon-sol-tools-43b648e7dc1ea49aff3ab1e6883aa6e069fae72f.tar.bz2 dexon-sol-tools-43b648e7dc1ea49aff3ab1e6883aa6e069fae72f.tar.lz dexon-sol-tools-43b648e7dc1ea49aff3ab1e6883aa6e069fae72f.tar.xz dexon-sol-tools-43b648e7dc1ea49aff3ab1e6883aa6e069fae72f.tar.zst dexon-sol-tools-43b648e7dc1ea49aff3ab1e6883aa6e069fae72f.zip |
Dutch wrapper
Diffstat (limited to 'contracts/extensions')
-rw-r--r-- | contracts/extensions/test/extensions/dutch_auction.ts | 50 | ||||
-rw-r--r-- | contracts/extensions/test/utils/dutch_auction_test_wrapper.ts (renamed from contracts/extensions/test/utils/dutch_auction_wrapper.ts) | 16 |
2 files changed, 40 insertions, 26 deletions
diff --git a/contracts/extensions/test/extensions/dutch_auction.ts b/contracts/extensions/test/extensions/dutch_auction.ts index 5e1534594..cd5816810 100644 --- a/contracts/extensions/test/extensions/dutch_auction.ts +++ b/contracts/extensions/test/extensions/dutch_auction.ts @@ -34,7 +34,7 @@ import * as _ from 'lodash'; import { DutchAuctionContract } from '../../generated-wrappers/dutch_auction'; import { artifacts } from '../../src/artifacts'; -import { DutchAuctionWrapper } from '../utils/dutch_auction_wrapper'; +import { DutchAuctionTestWrapper } from '../utils/dutch_auction_test_wrapper'; chaiSetup.configure(); const expect = chai.expect; @@ -68,7 +68,7 @@ describe(ContractName.DutchAuction, () => { let erc721MakerAssetIds: BigNumber[]; const tenMinutesInSeconds = 10 * 60; - let dutchAuctionWrapper: DutchAuctionWrapper; + let dutchAuctionTestWrapper: DutchAuctionTestWrapper; let defaultERC20MakerAssetData: string; before(async () => { @@ -125,7 +125,7 @@ describe(ContractName.DutchAuction, () => { dutchAuctionInstance.address, provider, ); - dutchAuctionWrapper = new DutchAuctionWrapper(dutchAuctionInstance, provider); + dutchAuctionTestWrapper = new DutchAuctionTestWrapper(dutchAuctionInstance, provider); defaultMakerAssetAddress = erc20TokenA.address; const defaultTakerAssetAddress = wethContract.address; @@ -164,7 +164,7 @@ describe(ContractName.DutchAuction, () => { feeRecipientAddress, // taker address or sender address should be set to the ducth auction contract takerAddress: dutchAuctionContract.address, - makerAssetData: assetDataUtils.encodeDutchAuctionAssetData( + makerAssetData: DutchAuctionTestWrapper.encodeDutchAuctionAssetData( assetDataUtils.encodeERC20AssetData(defaultMakerAssetAddress), auctionBeginTimeSeconds, auctionBeginAmount, @@ -206,13 +206,13 @@ describe(ContractName.DutchAuction, () => { describe('matchOrders', () => { it('should be worth the begin price at the begining of the auction', async () => { auctionBeginTimeSeconds = new BigNumber(currentBlockTimestamp + 2); - const makerAssetData = assetDataUtils.encodeDutchAuctionAssetData( + const makerAssetData = DutchAuctionTestWrapper.encodeDutchAuctionAssetData( defaultERC20MakerAssetData, auctionBeginTimeSeconds, auctionBeginAmount, ); sellOrder = await sellerOrderFactory.newSignedOrderAsync({ makerAssetData }); - const auctionDetails = await dutchAuctionWrapper.getAuctionDetailsAsync(sellOrder); + const auctionDetails = await dutchAuctionTestWrapper.getAuctionDetailsAsync(sellOrder); expect(auctionDetails.currentTimeSeconds).to.be.bignumber.lte(auctionBeginTimeSeconds); expect(auctionDetails.currentAmount).to.be.bignumber.equal(auctionBeginAmount); expect(auctionDetails.beginAmount).to.be.bignumber.equal(auctionBeginAmount); @@ -220,7 +220,7 @@ describe(ContractName.DutchAuction, () => { it('should be be worth the end price at the end of the auction', async () => { auctionBeginTimeSeconds = new BigNumber(currentBlockTimestamp - tenMinutesInSeconds * 2); auctionEndTimeSeconds = new BigNumber(currentBlockTimestamp - tenMinutesInSeconds); - const makerAssetData = assetDataUtils.encodeDutchAuctionAssetData( + const makerAssetData = DutchAuctionTestWrapper.encodeDutchAuctionAssetData( defaultERC20MakerAssetData, auctionBeginTimeSeconds, auctionBeginAmount, @@ -229,18 +229,18 @@ describe(ContractName.DutchAuction, () => { makerAssetData, expirationTimeSeconds: auctionEndTimeSeconds, }); - const auctionDetails = await dutchAuctionWrapper.getAuctionDetailsAsync(sellOrder); + const auctionDetails = await dutchAuctionTestWrapper.getAuctionDetailsAsync(sellOrder); expect(auctionDetails.currentTimeSeconds).to.be.bignumber.gte(auctionEndTimeSeconds); expect(auctionDetails.currentAmount).to.be.bignumber.equal(auctionEndAmount); expect(auctionDetails.beginAmount).to.be.bignumber.equal(auctionBeginAmount); }); it('should match orders at current amount and send excess to buyer', async () => { - const beforeAuctionDetails = await dutchAuctionWrapper.getAuctionDetailsAsync(sellOrder); + const beforeAuctionDetails = await dutchAuctionTestWrapper.getAuctionDetailsAsync(sellOrder); buyOrder = await buyerOrderFactory.newSignedOrderAsync({ makerAssetAmount: beforeAuctionDetails.currentAmount.times(2), }); - await dutchAuctionWrapper.matchOrdersAsync(buyOrder, sellOrder, takerAddress); - const afterAuctionDetails = await dutchAuctionWrapper.getAuctionDetailsAsync(sellOrder); + await dutchAuctionTestWrapper.matchOrdersAsync(buyOrder, sellOrder, takerAddress); + const afterAuctionDetails = await dutchAuctionTestWrapper.getAuctionDetailsAsync(sellOrder); const newBalances = await erc20Wrapper.getBalancesAsync(); expect(newBalances[dutchAuctionContract.address][wethContract.address]).to.be.bignumber.equal( constants.ZERO_AMOUNT, @@ -259,8 +259,8 @@ describe(ContractName.DutchAuction, () => { sellOrder = await sellerOrderFactory.newSignedOrderAsync({ makerFee: new BigNumber(1), }); - await dutchAuctionWrapper.matchOrdersAsync(buyOrder, sellOrder, takerAddress); - const afterAuctionDetails = await dutchAuctionWrapper.getAuctionDetailsAsync(sellOrder); + await dutchAuctionTestWrapper.matchOrdersAsync(buyOrder, sellOrder, takerAddress); + const afterAuctionDetails = await dutchAuctionTestWrapper.getAuctionDetailsAsync(sellOrder); const newBalances = await erc20Wrapper.getBalancesAsync(); expect(newBalances[makerAddress][wethContract.address]).to.be.bignumber.gte( erc20Balances[makerAddress][wethContract.address].plus(afterAuctionDetails.currentAmount), @@ -273,9 +273,9 @@ describe(ContractName.DutchAuction, () => { buyOrder = await buyerOrderFactory.newSignedOrderAsync({ makerFee: new BigNumber(1), }); - await dutchAuctionWrapper.matchOrdersAsync(buyOrder, sellOrder, takerAddress); + await dutchAuctionTestWrapper.matchOrdersAsync(buyOrder, sellOrder, takerAddress); const newBalances = await erc20Wrapper.getBalancesAsync(); - const afterAuctionDetails = await dutchAuctionWrapper.getAuctionDetailsAsync(sellOrder); + const afterAuctionDetails = await dutchAuctionTestWrapper.getAuctionDetailsAsync(sellOrder); expect(newBalances[makerAddress][wethContract.address]).to.be.bignumber.gte( erc20Balances[makerAddress][wethContract.address].plus(afterAuctionDetails.currentAmount), ); @@ -286,7 +286,7 @@ describe(ContractName.DutchAuction, () => { it('should revert when auction expires', async () => { auctionBeginTimeSeconds = new BigNumber(currentBlockTimestamp - tenMinutesInSeconds * 2); auctionEndTimeSeconds = new BigNumber(currentBlockTimestamp - tenMinutesInSeconds); - const makerAssetData = assetDataUtils.encodeDutchAuctionAssetData( + const makerAssetData = DutchAuctionTestWrapper.encodeDutchAuctionAssetData( defaultERC20MakerAssetData, auctionBeginTimeSeconds, auctionBeginAmount, @@ -296,7 +296,7 @@ describe(ContractName.DutchAuction, () => { makerAssetData, }); return expectTransactionFailedAsync( - dutchAuctionWrapper.matchOrdersAsync(buyOrder, sellOrder, takerAddress), + dutchAuctionTestWrapper.matchOrdersAsync(buyOrder, sellOrder, takerAddress), RevertReason.AuctionExpired, ); }); @@ -305,7 +305,7 @@ describe(ContractName.DutchAuction, () => { makerAssetAmount: sellOrder.takerAssetAmount, }); return expectTransactionFailedAsync( - dutchAuctionWrapper.matchOrdersAsync(buyOrder, sellOrder, takerAddress), + dutchAuctionTestWrapper.matchOrdersAsync(buyOrder, sellOrder, takerAddress), RevertReason.AuctionInvalidAmount, ); }); @@ -314,13 +314,13 @@ describe(ContractName.DutchAuction, () => { takerAssetAmount: auctionBeginAmount.plus(1), }); return expectTransactionFailedAsync( - dutchAuctionWrapper.matchOrdersAsync(buyOrder, sellOrder, takerAddress), + dutchAuctionTestWrapper.matchOrdersAsync(buyOrder, sellOrder, takerAddress), RevertReason.AuctionInvalidAmount, ); }); it('begin time is less than end time', async () => { auctionBeginTimeSeconds = new BigNumber(auctionEndTimeSeconds).plus(tenMinutesInSeconds); - const makerAssetData = assetDataUtils.encodeDutchAuctionAssetData( + const makerAssetData = DutchAuctionTestWrapper.encodeDutchAuctionAssetData( defaultERC20MakerAssetData, auctionBeginTimeSeconds, auctionBeginAmount, @@ -330,7 +330,7 @@ describe(ContractName.DutchAuction, () => { makerAssetData, }); return expectTransactionFailedAsync( - dutchAuctionWrapper.matchOrdersAsync(buyOrder, sellOrder, takerAddress), + dutchAuctionTestWrapper.matchOrdersAsync(buyOrder, sellOrder, takerAddress), RevertReason.AuctionInvalidBeginTime, ); }); @@ -339,7 +339,7 @@ describe(ContractName.DutchAuction, () => { makerAssetData: assetDataUtils.encodeERC20AssetData(defaultMakerAssetAddress), }); return expectTransactionFailedAsync( - dutchAuctionWrapper.matchOrdersAsync(buyOrder, sellOrder, takerAddress), + dutchAuctionTestWrapper.matchOrdersAsync(buyOrder, sellOrder, takerAddress), RevertReason.InvalidAssetData, ); }); @@ -348,7 +348,7 @@ describe(ContractName.DutchAuction, () => { it('should match orders when ERC721', async () => { const makerAssetId = erc721MakerAssetIds[0]; const erc721MakerAssetData = assetDataUtils.encodeERC721AssetData(erc721Token.address, makerAssetId); - const makerAssetData = assetDataUtils.encodeDutchAuctionAssetData( + const makerAssetData = DutchAuctionTestWrapper.encodeDutchAuctionAssetData( erc721MakerAssetData, auctionBeginTimeSeconds, auctionBeginAmount, @@ -361,8 +361,8 @@ describe(ContractName.DutchAuction, () => { takerAssetAmount: new BigNumber(1), takerAssetData: sellOrder.makerAssetData, }); - await dutchAuctionWrapper.matchOrdersAsync(buyOrder, sellOrder, takerAddress); - const afterAuctionDetails = await dutchAuctionWrapper.getAuctionDetailsAsync(sellOrder); + await dutchAuctionTestWrapper.matchOrdersAsync(buyOrder, sellOrder, takerAddress); + const afterAuctionDetails = await dutchAuctionTestWrapper.getAuctionDetailsAsync(sellOrder); const newBalances = await erc20Wrapper.getBalancesAsync(); // HACK gte used here due to a bug in ganache where the timestamp can change // between multiple calls to the same block. Which can move the amount in our case diff --git a/contracts/extensions/test/utils/dutch_auction_wrapper.ts b/contracts/extensions/test/utils/dutch_auction_test_wrapper.ts index 138b40ca9..653b9136c 100644 --- a/contracts/extensions/test/utils/dutch_auction_wrapper.ts +++ b/contracts/extensions/test/utils/dutch_auction_test_wrapper.ts @@ -5,11 +5,13 @@ import { DutchAuctionDetails, SignedOrder } from '@0x/types'; import { Web3Wrapper } from '@0x/web3-wrapper'; import { Provider, TransactionReceiptWithDecodedLogs } from 'ethereum-types'; import * as _ from 'lodash'; +import { BigNumber } from '@0x/utils'; import { DutchAuctionContract } from '../../generated-wrappers/dutch_auction'; import { artifacts } from '../../src/artifacts'; +import { DutchAuctionWrapper } from '@0x/contract-wrappers'; -export class DutchAuctionWrapper { +export class DutchAuctionTestWrapper { private readonly _dutchAuctionContract: DutchAuctionContract; private readonly _web3Wrapper: Web3Wrapper; private readonly _logDecoder: LogDecoder; @@ -59,4 +61,16 @@ export class DutchAuctionWrapper { const afterAuctionDetails = await this._dutchAuctionContract.getAuctionDetails.callAsync(sellOrder); return afterAuctionDetails; } + /** + * 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 assetData Hex encoded assetData string for the asset being auctioned. + * @param beginTimeSeconds Begin time of the dutch auction. + * @param beginAmount Starting amount being sold in the dutch auction. + * @return The hex encoded assetData string. + */ + public static encodeDutchAuctionAssetData(assetData: string, beginTimeSeconds: BigNumber, beginAmount: BigNumber): string { + return DutchAuctionWrapper.encodeDutchAuctionAssetData(assetData, beginTimeSeconds, beginAmount); + }; } |