Progress on dutch auction wrapper. Need to add auction data decoding to it.

This commit is contained in:
Greg Hysen 2018-12-20 19:40:16 -08:00
parent 09afee55ed
commit 5da748a062
3 changed files with 99 additions and 23 deletions

View File

@ -149,7 +149,7 @@ export class ContractWrappers {
this.dutchAuction = new DutchAuctionWrapper( this.dutchAuction = new DutchAuctionWrapper(
this._web3Wrapper, this._web3Wrapper,
config.networkId, config.networkId,
contractAddresses.orderValidator, contractAddresses.dutchAuction,
); );
} }
/** /**

View File

@ -21,9 +21,13 @@ 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 { 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;
public address: string; public address: string;
private _exchangeAddress: string;
private _dutchAuctionContractIfExists?: DutchAuctionContract; private _dutchAuctionContractIfExists?: DutchAuctionContract;
/** /**
* Instantiate DutchAuctionWrapper * Instantiate DutchAuctionWrapper
@ -35,10 +39,12 @@ export class DutchAuctionWrapper extends ContractWrapper {
constructor( constructor(
web3Wrapper: Web3Wrapper, web3Wrapper: Web3Wrapper,
networkId: number, networkId: number,
address: string, address?: string,
exchangeAddress?: string,
) { ) {
super(web3Wrapper, networkId); super(web3Wrapper, networkId);
this.address = address; this.address = this.address = _.isUndefined(address) ? _getDefaultContractAddresses(networkId).dutchAuction : address;
this._exchangeAddress = _.isUndefined(exchangeAddress) ? _getDefaultContractAddresses(networkId).exchange : exchangeAddress;
} }
/** /**
* Matches the buy and sell orders at an amount given the following: the current block time, the auction * Matches the buy and sell orders at an amount given the following: the current block time, the auction
@ -110,6 +116,8 @@ export class DutchAuctionWrapper extends ContractWrapper {
// type assertions // type assertions
assert.doesConformToSchema('sellOrder', sellOrder, schemas.signedOrderSchema); assert.doesConformToSchema('sellOrder', sellOrder, schemas.signedOrderSchema);
// get contract // get contract
console.log(sellOrder);
console.log(await this._getDutchAuctionContractAsync());
const dutchAuctionInstance = await this._getDutchAuctionContractAsync(); const dutchAuctionInstance = await this._getDutchAuctionContractAsync();
// call contract // call contract
const afterAuctionDetails = await dutchAuctionInstance.getAuctionDetails.callAsync(sellOrder); const afterAuctionDetails = await dutchAuctionInstance.getAuctionDetails.callAsync(sellOrder);
@ -128,6 +136,66 @@ 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
@ -138,6 +206,7 @@ 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'],
@ -145,6 +214,7 @@ export class DutchAuctionWrapper extends ContractWrapper {
); );
const abiEncodedAuctionDataBuffer = ethUtil.toBuffer(abiEncodedAuctionData); const abiEncodedAuctionDataBuffer = ethUtil.toBuffer(abiEncodedAuctionData);
const dutchAuctionDataBuffer = Buffer.concat([assetDataBuffer, abiEncodedAuctionDataBuffer]); const dutchAuctionDataBuffer = Buffer.concat([assetDataBuffer, abiEncodedAuctionDataBuffer]);
// console.log(`GREFG --- `, abiEncodedAuctionData);
const dutchAuctionData = ethUtil.bufferToHex(dutchAuctionDataBuffer); const dutchAuctionData = ethUtil.bufferToHex(dutchAuctionDataBuffer);
return dutchAuctionData; return dutchAuctionData;
}; };

View File

@ -16,6 +16,7 @@ import { provider, web3Wrapper } from './utils/web3_wrapper';
import { getLatestBlockTimestampAsync } from '@0x/contracts-test-utils'; import { getLatestBlockTimestampAsync } from '@0x/contracts-test-utils';
import { DutchAuction } from '@0x/contract-artifacts'; 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';
chaiSetup.configure(); chaiSetup.configure();
const expect = chai.expect; const expect = chai.expect;
@ -23,7 +24,7 @@ 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 = new BigNumber(5); const fillableAmount = 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;
@ -32,10 +33,9 @@ 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 makerAssetData: string;
let takerAssetData: string;
let buyOrder: SignedOrder; let buyOrder: SignedOrder;
let sellOrder: SignedOrder; let sellOrder: SignedOrder;
let makerTokenAssetData: string; let makerTokenAssetData: string;
@ -63,40 +63,44 @@ describe.only('DutchAuctionWrapper', () => {
contractWrappers.erc20Proxy.address, contractWrappers.erc20Proxy.address,
contractWrappers.erc721Proxy.address, contractWrappers.erc721Proxy.address,
); );
[, makerAddress, takerAddress] = userAddresses; [, makerAddress, takerAddress, buyerAddress] = userAddresses;
[makerTokenAddress] = tokenUtils.getDummyERC20TokenAddresses(); [makerTokenAddress] = tokenUtils.getDummyERC20TokenAddresses();
takerTokenAddress = contractWrappers.forwarder.etherTokenAddress; takerTokenAddress = contractWrappers.forwarder.etherTokenAddress;
console.log(`B`);
// construct asset data for tokens being swapped // construct asset data for tokens being swapped
[makerTokenAssetData, takerTokenAssetData] = [ [makerTokenAssetData, takerTokenAssetData] = [
assetDataUtils.encodeERC20AssetData(makerTokenAddress), assetDataUtils.encodeERC20AssetData(makerTokenAddress),
assetDataUtils.encodeERC20AssetData(takerTokenAddress), assetDataUtils.encodeERC20AssetData(takerTokenAddress),
]; ];
console.log(`C`);
// encode auction details in maker asset data // encode auction details in maker asset data
const auctionBeginAmount = fillableAmount; const auctionBeginAmount = fillableAmount;
const currentBlockTimestamp = await getLatestBlockTimestampAsync(); const currentBlockTimestamp = await getLatestBlockTimestampAsync();
const auctionBeginTimeSeconds = new BigNumber(currentBlockTimestamp - tenMinutesInSeconds); const auctionBeginTimeSeconds = new BigNumber(currentBlockTimestamp - tenMinutesInSeconds);
makerAssetData = DutchAuctionWrapper.encodeDutchAuctionAssetData( /* makerAssetData = DutchAuctionWrapper.encodeDutchAuctionAssetData(
makerTokenAssetData, makerTokenAssetData,
auctionBeginTimeSeconds, auctionBeginTimeSeconds,
auctionBeginAmount auctionBeginAmount
); );*/
takerAssetData = takerTokenAssetData; console.log(`C2`);
// 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 fillScenarios.createFillableSignedOrderAsync( sellOrder = await contractWrappers.dutchAuction.createSignedSellOrderAsync(
makerAssetData, auctionBeginTimeSeconds,
takerAssetData, fillableAmount.times(2),
fillableAmount,
new BigNumber(currentBlockTimestamp + tenMinutesInSeconds),
makerTokenAssetData,
takerTokenAssetData,
makerAddress, makerAddress,
constants.NULL_ADDRESS, constants.NULL_ADDRESS,
fillableAmount, fillableAmount,
); );
buyOrder = await fillScenarios.createFillableSignedOrderAsync( buyOrder = await contractWrappers.dutchAuction.createSignedBuyOrderAsync(
takerAssetData, sellOrder,
makerAssetData, buyerAddress,
makerAddress,
constants.NULL_ADDRESS,
fillableAmount,
); );
console.log(`CD`);
}); });
after(async () => { after(async () => {
await blockchainLifecycle.revertAsync(); await blockchainLifecycle.revertAsync();
@ -109,8 +113,10 @@ describe.only('DutchAuctionWrapper', () => {
}); });
describe('#matchOrdersAsync', () => { describe('#matchOrdersAsync', () => {
it('should match two orders', async () => { it('should match two orders', async () => {
const txHash = await contractWrappers.dutchAuction.matchOrdersAsync(buyOrder, sellOrder, takerAddress); console.log(await contractWrappers.dutchAuction.getAuctionDetailsAsync(sellOrder));
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
@ -130,7 +136,7 @@ describe.only('DutchAuctionWrapper', () => {
}); });
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 // setup auction details
const auctionBeginAmount = fillableAmount; const auctionBeginAmount = fillableAmount;
const currentBlockTimestamp = await getLatestBlockTimestampAsync(); const currentBlockTimestamp = await getLatestBlockTimestampAsync();
@ -151,6 +157,6 @@ describe.only('DutchAuctionWrapper', () => {
expect(auctionDetails.currentTimeSeconds).to.be.bignumber.lte(auctionBeginTimeSeconds); expect(auctionDetails.currentTimeSeconds).to.be.bignumber.lte(auctionBeginTimeSeconds);
expect(auctionDetails.currentAmount).to.be.bignumber.equal(auctionBeginAmount); expect(auctionDetails.currentAmount).to.be.bignumber.equal(auctionBeginAmount);
expect(auctionDetails.beginAmount).to.be.bignumber.equal(auctionBeginAmount); expect(auctionDetails.beginAmount).to.be.bignumber.equal(auctionBeginAmount);
}); });*/
}); });
}); });