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

[asset-buyer] Fix order expiration calc bug and expose gas options to executeBuyQuote #1116

Merged
merged 7 commits into from
Oct 10, 2018
6 changes: 5 additions & 1 deletion packages/asset-buyer/src/asset_buyer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ export class AssetBuyer {
buyQuote: BuyQuote,
options: Partial<BuyQuoteExecutionOpts> = {},
): Promise<string> {
const { ethAmount, takerAddress, feeRecipient } = {
const { ethAmount, takerAddress, feeRecipient, gasLimit, gasPrice } = {
...constants.DEFAULT_BUY_QUOTE_EXECUTION_OPTS,
...options,
};
Expand Down Expand Up @@ -219,6 +219,10 @@ export class AssetBuyer {
feeOrders,
feePercentage,
feeRecipient,
{
gasLimit,
gasPrice,
},
);
return txHash;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/asset-buyer/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const MAINNET_NETWORK_ID = 1;
const DEFAULT_ASSET_BUYER_OPTS: AssetBuyerOpts = {
networkId: MAINNET_NETWORK_ID,
orderRefreshIntervalMs: 10000, // 10 seconds
expiryBufferSeconds: 15,
expiryBufferSeconds: 300,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How was this number chosen? Can you leave a comment?

};

const DEFAULT_BUY_QUOTE_REQUEST_OPTS: BuyQuoteRequestOpts = {
Expand Down
4 changes: 4 additions & 0 deletions packages/asset-buyer/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,15 @@ export interface BuyQuoteRequestOpts {
/**
* ethAmount: The desired amount of eth to spend. Defaults to buyQuote.worstCaseQuoteInfo.totalEthAmount.
* takerAddress: The address to perform the buy. Defaults to the first available address from the provider.
* gasLimit: The amount of gas to send with a transaction (in Gwei). Defaults to an eth_estimateGas rpc call.
* gasPrice: Gas price in Wei to use for a transaction
* feeRecipient: The address where affiliate fees are sent. Defaults to null address (0x000...000).
*/
export interface BuyQuoteExecutionOpts {
ethAmount?: BigNumber;
takerAddress?: string;
gasLimit?: number;
gasPrice?: BigNumber;
feeRecipient: string;
}

Expand Down
10 changes: 5 additions & 5 deletions packages/asset-buyer/src/utils/buy_quote_calculator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ export const buyQuoteCalculator = {
assetBuyAmount,
feePercentage,
);

return {
assetData,
orders: resultOrders,
Expand All @@ -98,13 +97,14 @@ function calculateQuoteInfo(
);
// find the total eth needed to buy fees
const ethAmountToBuyFees = findEthAmountNeededToBuyFees(feeOrdersAndFillableAmounts, zrxAmountToBuyAsset);
const ethAmountBeforeAffiliateFee = ethAmountToBuyAsset.plus(ethAmountToBuyFees);
const totalEthAmount = ethAmountBeforeAffiliateFee.mul(feePercentage + 1);
const affiliateFeeEthAmount = ethAmountToBuyAsset.mul(feePercentage);
const totalEthAmountWithoutAffiliateFee = ethAmountToBuyAsset.plus(ethAmountToBuyFees);
const totalEthAmount = totalEthAmountWithoutAffiliateFee.plus(affiliateFeeEthAmount);
// divide into the assetBuyAmount in order to find rate of makerAsset / WETH
const ethPerAssetPrice = ethAmountBeforeAffiliateFee.div(assetBuyAmount);
const ethPerAssetPrice = totalEthAmountWithoutAffiliateFee.div(assetBuyAmount);
return {
totalEthAmount,
feeEthAmount: totalEthAmount.minus(ethAmountBeforeAffiliateFee),
feeEthAmount: affiliateFeeEthAmount,
ethPerAssetPrice,
};
}
Expand Down
2 changes: 1 addition & 1 deletion packages/asset-buyer/src/utils/order_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export const orderUtils = {
willOrderExpire(order: SignedOrder, secondsFromNow: number): boolean {
const millisecondsInSecond = 1000;
const currentUnixTimestampSec = new BigNumber(Date.now() / millisecondsInSecond).round();
return order.expirationTimeSeconds.lessThan(currentUnixTimestampSec.minus(secondsFromNow));
return order.expirationTimeSeconds.lessThan(currentUnixTimestampSec.plus(secondsFromNow));
},
calculateRemainingMakerAssetAmount(order: SignedOrder, remainingTakerAssetAmount: BigNumber): BigNumber {
if (remainingTakerAssetAmount.eq(0)) {
Expand Down
24 changes: 15 additions & 9 deletions packages/asset-buyer/test/buy_quote_calculator_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,11 @@ describe('buyQuoteCalculator', () => {
expect(buyQuote.feeOrders).to.deep.equal([smallFeeOrderAndFillableAmount.orders[0]]);
// test if rates are correct
// 50 eth to fill the first order + 100 eth for fees
const expectedFillEthAmount = new BigNumber(150);
const expectedTotalEthAmount = expectedFillEthAmount.mul(feePercentage + 1);
const expectedFeeEthAmount = expectedTotalEthAmount.minus(expectedFillEthAmount);
const expectedEthAmountForAsset = new BigNumber(50);
const expectedEthAmountForZrxFees = new BigNumber(100);
const expectedFillEthAmount = expectedEthAmountForAsset.plus(expectedEthAmountForZrxFees);
const expectedFeeEthAmount = expectedEthAmountForAsset.mul(feePercentage);
const expectedTotalEthAmount = expectedFillEthAmount.plus(expectedFeeEthAmount);
const expectedEthPerAssetPrice = expectedFillEthAmount.div(assetBuyAmount);
expect(buyQuote.bestCaseQuoteInfo.feeEthAmount).to.bignumber.equal(expectedFeeEthAmount);
expect(buyQuote.bestCaseQuoteInfo.totalEthAmount).to.bignumber.equal(expectedTotalEthAmount);
Expand Down Expand Up @@ -138,17 +140,21 @@ describe('buyQuoteCalculator', () => {
expect(buyQuote.feeOrders).to.deep.equal(allFeeOrdersAndFillableAmounts.orders);
// test if rates are correct
// 50 eth to fill the first order + 100 eth for fees
const expectedFillEthAmount = new BigNumber(150);
const expectedTotalEthAmount = expectedFillEthAmount.mul(feePercentage + 1);
const expectedFeeEthAmount = expectedTotalEthAmount.minus(expectedFillEthAmount);
const expectedEthAmountForAsset = new BigNumber(50);
const expectedEthAmountForZrxFees = new BigNumber(100);
const expectedFillEthAmount = expectedEthAmountForAsset.plus(expectedEthAmountForZrxFees);
const expectedFeeEthAmount = expectedEthAmountForAsset.mul(feePercentage);
const expectedTotalEthAmount = expectedFillEthAmount.plus(expectedFeeEthAmount);
const expectedEthPerAssetPrice = expectedFillEthAmount.div(assetBuyAmount);
expect(buyQuote.bestCaseQuoteInfo.feeEthAmount).to.bignumber.equal(expectedFeeEthAmount);
expect(buyQuote.bestCaseQuoteInfo.totalEthAmount).to.bignumber.equal(expectedTotalEthAmount);
expect(buyQuote.bestCaseQuoteInfo.ethPerAssetPrice).to.bignumber.equal(expectedEthPerAssetPrice);
// 100 eth to fill the first order + 200 eth for fees
const expectedWorstFillEthAmount = new BigNumber(300);
const expectedWorstTotalEthAmount = expectedWorstFillEthAmount.mul(feePercentage + 1);
const expectedWorstFeeEthAmount = expectedWorstTotalEthAmount.minus(expectedWorstFillEthAmount);
const expectedWorstEthAmountForAsset = new BigNumber(100);
const expectedWorstEthAmountForZrxFees = new BigNumber(200);
const expectedWorstFillEthAmount = expectedWorstEthAmountForAsset.plus(expectedWorstEthAmountForZrxFees);
const expectedWorstFeeEthAmount = expectedWorstEthAmountForAsset.mul(feePercentage);
const expectedWorstTotalEthAmount = expectedWorstFillEthAmount.plus(expectedWorstFeeEthAmount);
const expectedWorstEthPerAssetPrice = expectedWorstFillEthAmount.div(assetBuyAmount);
expect(buyQuote.worstCaseQuoteInfo.feeEthAmount).to.bignumber.equal(expectedWorstFeeEthAmount);
expect(buyQuote.worstCaseQuoteInfo.totalEthAmount).to.bignumber.equal(expectedWorstTotalEthAmount);
Expand Down