all dutchie wrapper tests passing

This commit is contained in:
Greg Hysen 2018-12-21 16:11:59 -08:00
parent 5da748a062
commit 7203ca90cf
2 changed files with 76 additions and 108 deletions

View File

@ -7,7 +7,7 @@ import { _getDefaultContractAddresses } from '../utils/contract_addresses';
import { DutchAuctionDetails, SignedOrder } from '@0x/types'; import { DutchAuctionDetails, SignedOrder } from '@0x/types';
import { ContractAbi } from 'ethereum-types'; import { ContractAbi } from 'ethereum-types';
import { Web3Wrapper } from '@0x/web3-wrapper'; import { Web3Wrapper } from '@0x/web3-wrapper';
import { BigNumber } from '@0x/utils'; import { BigNumber, abiUtils } from '@0x/utils';
import { Provider, TransactionReceiptWithDecodedLogs } from 'ethereum-types'; import { Provider, TransactionReceiptWithDecodedLogs } from 'ethereum-types';
import * as _ from 'lodash'; import * as _ from 'lodash';
import ethAbi = require('ethereumjs-abi'); import ethAbi = require('ethereumjs-abi');
@ -21,8 +21,7 @@ import { OrderTransactionOpts } from '../types';
import { ContractWrapper } from './contract_wrapper'; import { ContractWrapper } from './contract_wrapper';
import { ExchangeWrapperError } from '../types'; import { ExchangeWrapperError } from '../types';
import { orderFactory } from '@0x/order-utils/lib/src/order_factory'; import { assetDataUtils, AssetData } from '@0x/order-utils';
import { constants } from 'zlib';
export class DutchAuctionWrapper extends ContractWrapper { export class DutchAuctionWrapper extends ContractWrapper {
public abi: ContractAbi = DutchAuction.compilerOutput.abi; public abi: ContractAbi = DutchAuction.compilerOutput.abi;
@ -74,6 +73,10 @@ export class DutchAuctionWrapper extends ContractWrapper {
sellOrder.takerAssetData !== buyOrder.makerAssetData sellOrder.takerAssetData !== buyOrder.makerAssetData
) { ) {
throw new Error(ExchangeWrapperError.AssetDataMismatch); 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 // get contract
const dutchAuctionInstance = await this._getDutchAuctionContractAsync(); const dutchAuctionInstance = await this._getDutchAuctionContractAsync();
@ -136,66 +139,6 @@ export class DutchAuctionWrapper extends ContractWrapper {
this._dutchAuctionContractIfExists = contractInstance; this._dutchAuctionContractIfExists = contractInstance;
return this._dutchAuctionContractIfExists; 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 * 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 * 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. * @return The hex encoded assetData string.
*/ */
public static encodeDutchAuctionAssetData(assetData: string, beginTimeSeconds: BigNumber, beginAmount: BigNumber): string { public static encodeDutchAuctionAssetData(assetData: string, beginTimeSeconds: BigNumber, beginAmount: BigNumber): string {
// console.log(`yoooo`, assetData);
const assetDataBuffer = ethUtil.toBuffer(assetData); const assetDataBuffer = ethUtil.toBuffer(assetData);
const abiEncodedAuctionData = (ethAbi as any).rawEncode( const abiEncodedAuctionData = (ethAbi as any).rawEncode(
['uint256', 'uint256'], ['uint256', 'uint256'],
@ -218,4 +160,28 @@ export class DutchAuctionWrapper extends ContractWrapper {
const dutchAuctionData = ethUtil.bufferToHex(dutchAuctionDataBuffer); const dutchAuctionData = ethUtil.bufferToHex(dutchAuctionDataBuffer);
return dutchAuctionData; 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];
};
} }

View File

@ -1,7 +1,7 @@
import { BlockchainLifecycle } from '@0x/dev-utils'; import { BlockchainLifecycle } from '@0x/dev-utils';
import { FillScenarios } from '@0x/fill-scenarios'; import { FillScenarios } from '@0x/fill-scenarios';
import { assetDataUtils } from '@0x/order-utils'; import { assetDataUtils } from '@0x/order-utils';
import { SignedOrder } from '@0x/types'; import { RevertReason, SignedOrder } from '@0x/types';
import { BigNumber } from '@0x/utils'; import { BigNumber } from '@0x/utils';
import * as chai from 'chai'; import * as chai from 'chai';
import 'mocha'; import 'mocha';
@ -18,13 +18,19 @@ import { DutchAuction } from '@0x/contract-artifacts';
import { DutchAuctionWrapper } from '../src/contract_wrappers/dutch_auction_wrapper'; import { DutchAuctionWrapper } from '../src/contract_wrappers/dutch_auction_wrapper';
import { Web3Wrapper } from '@0x/web3-wrapper'; import { Web3Wrapper } from '@0x/web3-wrapper';
import { DutchAuctionUtils } from './utils/dutch_auction_utils';
import {
expectTransactionFailedAsync,
} from '@0x/contracts-test-utils';
chaiSetup.configure(); chaiSetup.configure();
const expect = chai.expect; const expect = chai.expect;
const blockchainLifecycle = new BlockchainLifecycle(web3Wrapper); const blockchainLifecycle = new BlockchainLifecycle(web3Wrapper);
// tslint:disable:custom-no-magic-numbers // tslint:disable:custom-no-magic-numbers
describe.only('DutchAuctionWrapper', () => { 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; const tenMinutesInSeconds = 10 * 60;
let contractWrappers: ContractWrappers; let contractWrappers: ContractWrappers;
let fillScenarios: FillScenarios; let fillScenarios: FillScenarios;
@ -33,13 +39,16 @@ describe.only('DutchAuctionWrapper', () => {
let userAddresses: string[]; let userAddresses: string[];
let makerAddress: string; let makerAddress: string;
let takerAddress: string; let takerAddress: string;
let buyerAddress: string;
let makerTokenAddress: string; let makerTokenAddress: string;
let takerTokenAddress: string; let takerTokenAddress: string;
let buyOrder: SignedOrder; let buyOrder: SignedOrder;
let sellOrder: SignedOrder; let sellOrder: SignedOrder;
let makerTokenAssetData: string; let makerTokenAssetData: string;
let takerTokenAssetData: string; let takerTokenAssetData: string;
let auctionBeginTimeSeconds: BigNumber;
let auctionBeginAmount: BigNumber;
let auctionEndTimeSeconds: BigNumber;
let auctionEndAmount: BigNumber;
before(async () => { before(async () => {
console.log(`BEOGIN DEPLOYINH`); console.log(`BEOGIN DEPLOYINH`);
const contractAddresses = await migrateOnceAsync(); const contractAddresses = await migrateOnceAsync();
@ -63,9 +72,8 @@ describe.only('DutchAuctionWrapper', () => {
contractWrappers.erc20Proxy.address, contractWrappers.erc20Proxy.address,
contractWrappers.erc721Proxy.address, contractWrappers.erc721Proxy.address,
); );
[, makerAddress, takerAddress, buyerAddress] = userAddresses; [, makerAddress, takerAddress] = userAddresses;
[makerTokenAddress] = tokenUtils.getDummyERC20TokenAddresses(); [makerTokenAddress, takerTokenAddress] = tokenUtils.getDummyERC20TokenAddresses();
takerTokenAddress = contractWrappers.forwarder.etherTokenAddress;
console.log(`B`); console.log(`B`);
// construct asset data for tokens being swapped // construct asset data for tokens being swapped
[makerTokenAssetData, takerTokenAssetData] = [ [makerTokenAssetData, takerTokenAssetData] = [
@ -74,31 +82,40 @@ describe.only('DutchAuctionWrapper', () => {
]; ];
console.log(`C`); console.log(`C`);
// encode auction details in maker asset data // encode auction details in maker asset data
const auctionBeginAmount = fillableAmount; auctionEndAmount = fillableAmount;
auctionBeginAmount = auctionEndAmount.times(2);
const currentBlockTimestamp = await getLatestBlockTimestampAsync(); const currentBlockTimestamp = await getLatestBlockTimestampAsync();
const auctionBeginTimeSeconds = new BigNumber(currentBlockTimestamp - tenMinutesInSeconds); auctionBeginTimeSeconds = new BigNumber(currentBlockTimestamp - tenMinutesInSeconds);
auctionEndTimeSeconds = new BigNumber(currentBlockTimestamp + tenMinutesInSeconds);
/* makerAssetData = DutchAuctionWrapper.encodeDutchAuctionAssetData( /* makerAssetData = DutchAuctionWrapper.encodeDutchAuctionAssetData(
makerTokenAssetData, makerTokenAssetData,
auctionBeginTimeSeconds, auctionBeginTimeSeconds,
auctionBeginAmount auctionBeginAmount
);*/ );*/
console.log(`C2`); console.log(`C2`);
// Create template orders from
// create sell / buy orders for auction // create sell / buy orders for auction
// note that the maker/taker asset datas are swapped in the `buyOrder` // 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, auctionBeginTimeSeconds,
fillableAmount.times(2), auctionBeginAmount,
fillableAmount, auctionEndAmount,
new BigNumber(currentBlockTimestamp + tenMinutesInSeconds), auctionEndTimeSeconds,
makerTokenAssetData, makerTokenAssetData,
takerTokenAssetData, takerTokenAssetData,
makerAddress, makerAddress,
constants.NULL_ADDRESS, constants.NULL_ADDRESS,
fillableAmount, auctionEndAmount,
); );
buyOrder = await contractWrappers.dutchAuction.createSignedBuyOrderAsync( console.log(`ASDS`);
buyOrder = await dutchAuctionUtils.createSignedBuyOrderAsync(
sellOrder, sellOrder,
buyerAddress, takerAddress,
); );
console.log(`CD`); console.log(`CD`);
}); });
@ -113,17 +130,15 @@ describe.only('DutchAuctionWrapper', () => {
}); });
describe('#matchOrdersAsync', () => { describe('#matchOrdersAsync', () => {
it('should match two orders', async () => { it('should match two orders', async () => {
console.log(await contractWrappers.dutchAuction.getAuctionDetailsAsync(sellOrder)); const txHash = await contractWrappers.dutchAuction.matchOrdersAsync(buyOrder, sellOrder, takerAddress);
await web3Wrapper.awaitTransactionSuccessAsync(txHash, constants.AWAIT_TRANSACTION_MINED_MS);
// const txHash = await contractWrappers.dutchAuction.matchOrdersAsync(buyOrder, sellOrder, takerAddress, {gasLimit: 1000000});
//await web3Wrapper.awaitTransactionSuccessAsync(txHash, constants.AWAIT_TRANSACTION_MINED_MS);
}); });
it('should throw when invalid transaction and shouldValidate is true', async () => { it('should throw when invalid transaction and shouldValidate is true', async () => {
// request match with bad buy/sell orders // request match with bad buy/sell orders
const badSellOrder = buyOrder; const badSellOrder = buyOrder;
const badBuyOrder = sellOrder; const badBuyOrder = sellOrder;
return expect( return expectTransactionFailedAsync(
await contractWrappers.dutchAuction.matchOrdersAsync( contractWrappers.dutchAuction.matchOrdersAsync(
badBuyOrder, badBuyOrder,
badSellOrder, badSellOrder,
takerAddress, takerAddress,
@ -131,32 +146,19 @@ describe.only('DutchAuctionWrapper', () => {
shouldValidate: true, shouldValidate: true,
}, },
), ),
).to.be.rejectedWith('COMPLETE_FILL_FAILED'); RevertReason.InvalidAssetData
);
}); });
}); });
describe('#getAuctionDetailsAsync', () => { describe('#getAuctionDetailsAsync', () => {
/*it('should be worth the begin price at the begining of the auction', async () => { it('should be worth the begin price at the begining of the auction', async () => {
// setup auction details // get auction details
const auctionBeginAmount = fillableAmount; const auctionDetails = await contractWrappers.dutchAuction.getAuctionDetailsAsync(sellOrder);
const currentBlockTimestamp = await getLatestBlockTimestampAsync(); // run some basic sanity checks on the return value
const auctionBeginTimeSeconds = new BigNumber(currentBlockTimestamp + tenMinutesInSeconds); expect(auctionDetails.beginTimeSeconds, 'auctionDetails.beginTimeSeconds').to.be.bignumber.equal(auctionBeginTimeSeconds);
const makerAssetData = DutchAuctionWrapper.encodeDutchAuctionAssetData( expect(auctionDetails.beginAmount, 'auctionDetails.beginAmount').to.be.bignumber.equal(auctionBeginAmount);
makerTokenAssetData, expect(auctionDetails.endTimeSeconds, 'auctionDetails.endTimeSeconds').to.be.bignumber.equal(auctionEndTimeSeconds);
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);
});*/
}); });
}); });