Skip to content
This repository has been archived by the owner on Sep 4, 2024. It is now read-only.

Commit

Permalink
fix: apply neg buffer to portion (#414)
Browse files Browse the repository at this point in the history
  • Loading branch information
ConjunctiveNormalForm authored May 9, 2024
1 parent df7d6de commit fe168b2
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 7 deletions.
56 changes: 51 additions & 5 deletions lib/entities/quote/DutchV2Quote.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { PermitTransferFromData } from '@uniswap/permit2-sdk';
import { TradeType } from '@uniswap/sdk-core';
import {
DutchInput,
DutchOutput,
UnsignedV2DutchOrder,
UnsignedV2DutchOrderInfoJSON,
V2DutchOrderBuilder,
Expand All @@ -10,7 +12,7 @@ import { BigNumber, ethers } from 'ethers';
import { IQuote, LogJSON, SharedOrderQuoteDataJSON } from '.';
import { DutchV2Request } from '..';
import { ChainConfigManager } from '../../config/ChainConfigManager';
import { DEFAULT_V2_DEADLINE_BUFFER_SECS, frontendAndUraEnablePortion, RoutingType } from '../../constants';
import { BPS, DEFAULT_V2_DEADLINE_BUFFER_SECS, frontendAndUraEnablePortion, RoutingType } from '../../constants';
import { generateRandomNonce } from '../../util/nonce';
import { timestampInMstoSeconds } from '../../util/time';
import { DutchQuote, getPortionAdjustedOutputs } from './DutchQuote';
Expand Down Expand Up @@ -46,7 +48,7 @@ export class DutchV2Quote extends DutchQuote<DutchV2Request> implements IQuote {
// this is FE requirement
...(frontendAndUraEnablePortion(this.request.info.sendPortionEnabled) && {
portionBips: this.portion?.bips ?? 0,
portionAmount: this.portionAmountOutStart.toString() ?? '0',
portionAmount: applyBufferToPortion(this.portionAmountOutStart, this.request.info.type).toString() ?? '0',
portionRecipient: this.portion?.recipient,
}),
};
Expand Down Expand Up @@ -133,12 +135,56 @@ export class DutchV2Quote extends DutchQuote<DutchV2Request> implements IQuote {
createdAtMs: this.createdAtMs,
portionBips: this.portion?.bips,
portionRecipient: this.portion?.recipient,
portionAmountOutStart: this.portionAmountOutStart.toString(),
portionAmountOutEnd: this.portionAmountOutEnd.toString(),
portionAmountOutStart: applyBufferToPortion(this.portionAmountOutStart, this.request.info.type).toString(),
portionAmountOutEnd: applyBufferToPortion(this.portionAmountOutEnd, this.request.info.type).toString(),
};
}

static getLabsCosigner(): string {
return process.env.RFQ_LABS_COSIGNER_ADDRESS || DEFAULT_LABS_COSIGNER;
}
}
}

export function addBufferToV2InputOutput(
input: DutchInput,
output: DutchOutput,
type: TradeType,
bps: number
): {
input: DutchInput;
output: DutchOutput;
} {
if (type === TradeType.EXACT_INPUT) {
return {
input,
output: {
...output,
// subtract buffer from output
startAmount: output.startAmount.mul(BPS - bps).div(BPS),
endAmount: output.endAmount.mul(BPS - bps).div(BPS),
},
};
} else {
return {
input: {
...input,
// add buffer to input
startAmount: input.startAmount.mul(BPS + bps).div(BPS),
endAmount: input.endAmount.mul(BPS + bps).div(BPS),
},
output,
};
}
}

/*
* if exact_input, apply buffer to both user and portion outputs
* if exact_output, do nothing since the buffer is applied to user input
*/
export function applyBufferToPortion(portionAmount: BigNumber, type: TradeType): BigNumber {
if (type === TradeType.EXACT_INPUT) {
return portionAmount.mul(BPS - V2_OUTPUT_AMOUNT_BUFFER_BPS).div(BPS);
} else {
return portionAmount;
}
}
63 changes: 62 additions & 1 deletion test/unit/entities/DutchV2Quote.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ import { it } from '@jest/globals';
import { TradeType } from '@uniswap/sdk-core';
import { BigNumber } from 'ethers';
import { BPS } from '../../../lib/constants';
import { DEFAULT_LABS_COSIGNER, DutchQuote } from '../../../lib/entities';
import { DEFAULT_LABS_COSIGNER, DutchQuote, V2_OUTPUT_AMOUNT_BUFFER_BPS } from '../../../lib/entities';
import { PortionType } from '../../../lib/fetchers/PortionFetcher';
import { AMOUNT, ETH_IN, SWAPPER, TOKEN_IN } from '../../constants';
import { createDutchV2QuoteWithRequestOverrides } from '../../utils/fixtures';

process.env.ENABLE_PORTION = 'true';

describe('DutchV2Quote', () => {
//setChainConfigManager();
// silent logger in tests
Expand Down Expand Up @@ -38,6 +41,64 @@ describe('DutchV2Quote', () => {
expect(orderJson.cosigner).toEqual(DEFAULT_LABS_COSIGNER);
});

it('apply negative buffer to outputs for EXACT_INPUT trades', () => {
const v2Quote = createDutchV2QuoteWithRequestOverrides(
{},
{
tokenIn: ETH_IN,
tokenOut: TOKEN_IN,
type: 'EXACT_INPUT',
portion: { bips: 25, recipient: TOKEN_IN, type: PortionType.Flat },
sendPortionEnabled: true,
},
{}
);
const order = v2Quote.toOrder();
const orderJson = order.toJSON();

console.log(orderJson.outputs);
console.log(v2Quote.amountOutGasAndPortionAdjusted.toString());

expect(orderJson.outputs[0].startAmount).toEqual(
v2Quote.amountOutGasAndPortionAdjusted
.mul(BPS - V2_OUTPUT_AMOUNT_BUFFER_BPS)
.div(BPS)
.toString()
);
expect(orderJson.outputs[1].startAmount).toEqual(
v2Quote.portionAmountOutStart
.mul(BPS - V2_OUTPUT_AMOUNT_BUFFER_BPS)
.div(BPS)
.toString()
);
});

it('does not apply neg buffer to outputs, but to user input for EXACT_OUTPUT trades', () => {
const v2Quote = createDutchV2QuoteWithRequestOverrides(
{},
{
tokenIn: ETH_IN,
tokenOut: TOKEN_IN,
type: 'EXACT_OUTPUT',
portion: { bips: 25, recipient: TOKEN_IN, type: PortionType.Flat },
sendPortionEnabled: true,
},
{}
);
const order = v2Quote.toOrder();
const orderJson = order.toJSON();

expect(orderJson.outputs[0].startAmount).toEqual(v2Quote.amountOutStart.toString());
expect(orderJson.outputs[1].startAmount).toEqual(v2Quote.portionAmountOutStart.toString());

expect(orderJson.input.startAmount).toEqual(
v2Quote.amountInGasAndPortionAdjusted
.mul(BPS + V2_OUTPUT_AMOUNT_BUFFER_BPS)
.div(BPS)
.toString()
);
});

it('should serialize', () => {
const v2Quote = createDutchV2QuoteWithRequestOverrides(
{},
Expand Down
4 changes: 3 additions & 1 deletion test/utils/fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -638,7 +638,9 @@ export function createDutchV2QuoteWithRequest(
Object.assign({}, DUTCH_V2_QUOTE_DATA, {
quote: { ...DUTCH_V2_QUOTE_DATA.quote, type: RoutingType.DUTCH_V2, ...overrides },
}),
request
request,
undefined,
request.info.portion
) as DutchV2Quote;
}

Expand Down

0 comments on commit fe168b2

Please sign in to comment.