Skip to content

Commit

Permalink
Merge pull request #292 from Uniswap/bump-sdk
Browse files Browse the repository at this point in the history
  • Loading branch information
ConjunctiveNormalForm authored Mar 27, 2024
2 parents 700a2a1 + 20ad874 commit 9190365
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 65 deletions.
12 changes: 6 additions & 6 deletions lib/entities/HardQuoteRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,24 +73,24 @@ export class HardQuoteRequest {
}

public get tokenIn(): string {
return utils.getAddress(this.order.info.baseInput.token);
return utils.getAddress(this.order.info.input.token);
}

public get tokenOut(): string {
return utils.getAddress(this.order.info.baseOutputs[0].token);
return utils.getAddress(this.order.info.outputs[0].token);
}

public get totalOutputAmountStart(): BigNumber {
let amount = BigNumber.from(0);
for (const output of this.order.info.baseOutputs) {
for (const output of this.order.info.outputs) {
amount = amount.add(output.startAmount);
}

return amount;
}

public get totalInputAmountStart(): BigNumber {
return this.order.info.baseInput.startAmount;
return this.order.info.input.startAmount;
}

public get amount(): BigNumber {
Expand All @@ -102,13 +102,13 @@ export class HardQuoteRequest {
}

public get type(): TradeType {
return this.order.info.baseInput.startAmount.eq(this.order.info.baseInput.endAmount)
return this.order.info.input.startAmount.eq(this.order.info.input.endAmount)
? TradeType.EXACT_INPUT
: TradeType.EXACT_OUTPUT;
}

public get numOutputs(): number {
return this.order.info.baseOutputs.length;
return this.order.info.outputs.length;
}

public get cosigner(): string {
Expand Down
14 changes: 7 additions & 7 deletions lib/handlers/hard-quote/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
HardQuoteResponseDataJoi,
} from './schema';

const DEFAULT_EXCLUSIVITY_OVERRIDE_BPS = 100; // non-exclusive fillers must override price by this much
const DEFAULT_EXCLUSIVITY_OVERRIDE_BPS = BigNumber.from(100); // non-exclusive fillers must override price by this much

export class QuoteHandler extends APIGLambdaHandler<
ContainerInjected,
Expand Down Expand Up @@ -115,22 +115,22 @@ export function getCosignerData(request: HardQuoteRequest, quote: QuoteResponse)
const decayStartTime = getDecayStartTime(request.tokenInChainId);
// default to open order with the original prices
let filler = ethers.constants.AddressZero;
let inputAmount = BigNumber.from(0);
const outputAmounts = request.order.info.baseOutputs.map(() => BigNumber.from(0));
let inputOverride = BigNumber.from(0);
const outputOverrides = request.order.info.outputs.map(() => BigNumber.from(0));

// if the quote is better, then increase amounts by the difference
if (request.type === TradeType.EXACT_INPUT) {
if (quote.amountOut.gt(request.totalOutputAmountStart)) {
const increase = quote.amountOut.sub(request.totalOutputAmountStart);
// give all the increase to the first (swapper) output
outputAmounts[0] = request.order.info.baseOutputs[0].startAmount.add(increase);
outputOverrides[0] = request.order.info.outputs[0].startAmount.add(increase);
if (quote.filler) {
filler = quote.filler;
}
}
} else {
if (quote.amountIn.lt(request.totalInputAmountStart)) {
inputAmount = quote.amountIn;
inputOverride = quote.amountIn;
if (quote.filler) {
filler = quote.filler;
}
Expand All @@ -142,8 +142,8 @@ export function getCosignerData(request: HardQuoteRequest, quote: QuoteResponse)
decayEndTime: getDecayEndTime(request.tokenInChainId, decayStartTime),
exclusiveFiller: filler,
exclusivityOverrideBps: DEFAULT_EXCLUSIVITY_OVERRIDE_BPS,
inputAmount: inputAmount,
outputAmounts: outputAmounts,
inputOverride: inputOverride,
outputOverrides: outputOverrides,
};
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
"@uniswap/signer": "0.0.3-beta.4",
"@uniswap/smart-order-router": "^3.3.0",
"@uniswap/token-lists": "^1.0.0-beta.31",
"@uniswap/uniswapx-sdk": "1.5.0-alpha.7",
"@uniswap/uniswapx-sdk": "2.0.1-alpha.3",
"@uniswap/v3-sdk": "^3.9.0",
"aws-cdk-lib": "2.85.0",
"aws-embedded-metrics": "^4.1.0",
Expand Down
4 changes: 2 additions & 2 deletions test/entities/HardQuoteRequest.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ export const getOrderInfo = (data: Partial<UnsignedV2DutchOrderInfo>): UnsignedV
additionalValidationContract: ethers.constants.AddressZero,
additionalValidationData: '0x',
cosigner: ethers.constants.AddressZero,
baseInput: {
input: {
token: TOKEN_IN,
startAmount: RAW_AMOUNT,
endAmount: RAW_AMOUNT,
},
baseOutputs: [
outputs: [
{
token: TOKEN_OUT,
startAmount: RAW_AMOUNT.mul(2),
Expand Down
30 changes: 16 additions & 14 deletions test/entities/HardQuoteResponse.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ const CHAIN_ID = 1;
const fixedTime = 4206969;
jest.spyOn(Date, 'now').mockImplementation(() => fixedTime);

const DEFAULT_EXCLUSIVITY_OVERRIDE_BPS = ethers.BigNumber.from(100);

describe('HardQuoteResponse', () => {
const swapperWallet = Wallet.createRandom();
const cosignerWallet = Wallet.createRandom();
Expand Down Expand Up @@ -60,9 +62,9 @@ describe('HardQuoteResponse', () => {
decayStartTime: now + 100,
decayEndTime: now + 200,
exclusiveFiller: FILLER,
exclusivityOverrideBps: 100,
inputAmount: parseEther('1'),
outputAmounts: [parseEther('1')],
exclusivityOverrideBps: DEFAULT_EXCLUSIVITY_OVERRIDE_BPS,
inputOverride: parseEther('1'),
outputOverrides: [parseEther('1')],
}
);
expect(quoteResponse.toResponseJSON()).toEqual({
Expand All @@ -83,9 +85,9 @@ describe('HardQuoteResponse', () => {
decayStartTime: now + 100,
decayEndTime: now + 200,
exclusiveFiller: FILLER,
exclusivityOverrideBps: 100,
inputAmount: ethers.utils.parseEther('1'),
outputAmounts: [ethers.utils.parseEther('1')],
exclusivityOverrideBps: DEFAULT_EXCLUSIVITY_OVERRIDE_BPS,
inputOverride: ethers.utils.parseEther('1'),
outputOverrides: [ethers.utils.parseEther('1')],
}
);
expect(quoteResponse.toLog()).toEqual({
Expand All @@ -111,9 +113,9 @@ describe('HardQuoteResponse', () => {
decayStartTime: now + 100,
decayEndTime: now + 200,
exclusiveFiller: FILLER,
exclusivityOverrideBps: 100,
inputAmount: parseEther('1'),
outputAmounts: [parseEther('2')],
exclusivityOverrideBps: DEFAULT_EXCLUSIVITY_OVERRIDE_BPS,
inputOverride: parseEther('1'),
outputOverrides: [parseEther('2')],
}
);
expect(quoteResponse.amountOut).toEqual(parseEther('2'));
Expand All @@ -124,12 +126,12 @@ describe('HardQuoteResponse', () => {
const quoteResponse = await getResponse(
{
cosigner: cosignerWallet.address,
baseInput: {
input: {
token: TOKEN_IN,
startAmount: parseEther('1'),
endAmount: parseEther('1.1'),
},
baseOutputs: [
outputs: [
{
token: TOKEN_OUT,
startAmount: parseEther('1'),
Expand All @@ -142,9 +144,9 @@ describe('HardQuoteResponse', () => {
decayStartTime: now + 100,
decayEndTime: now + 200,
exclusiveFiller: FILLER,
exclusivityOverrideBps: 100,
inputAmount: parseEther('0.8'),
outputAmounts: [parseEther('1')],
exclusivityOverrideBps: DEFAULT_EXCLUSIVITY_OVERRIDE_BPS,
inputOverride: parseEther('0.8'),
outputOverrides: [parseEther('1')],
}
);
expect(quoteResponse.amountIn).toEqual(parseEther('0.8'));
Expand Down
58 changes: 29 additions & 29 deletions test/handlers/hard-quote/handler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@ export const getOrder = (data: Partial<UnsignedV2DutchOrderInfo>): UnsignedV2Dut
additionalValidationData: '0x',
cosigner: ethers.constants.AddressZero,
cosignerData: undefined,
baseInput: {
input: {
token: TOKEN_IN,
startAmount: RAW_AMOUNT,
endAmount: RAW_AMOUNT,
},
baseOutputs: [
outputs: [
{
token: TOKEN_OUT,
startAmount: RAW_AMOUNT,
Expand Down Expand Up @@ -139,9 +139,9 @@ describe('Quote handler', () => {

// no overrides since quote was same as request
expect(cosignedOrder.info.cosignerData.exclusiveFiller).toEqual(ethers.constants.AddressZero);
expect(cosignedOrder.info.cosignerData.inputAmount).toEqual(BigNumber.from(0));
expect(cosignedOrder.info.cosignerData.outputAmounts.length).toEqual(1);
expect(cosignedOrder.info.cosignerData.outputAmounts[0]).toEqual(BigNumber.from(0));
expect(cosignedOrder.info.cosignerData.inputOverride).toEqual(BigNumber.from(0));
expect(cosignedOrder.info.cosignerData.outputOverrides.length).toEqual(1);
expect(cosignedOrder.info.cosignerData.outputOverrides[0]).toEqual(BigNumber.from(0));
});

it('Pick the greater of two quotes - EXACT_IN', async () => {
Expand All @@ -162,21 +162,21 @@ describe('Quote handler', () => {
expect(cosignedOrder.info.cosignerData.exclusiveFiller).toEqual(MOCK_FILLER_ADDRESS);

// overridden output amount to 2x
expect(cosignedOrder.info.cosignerData.inputAmount).toEqual(BigNumber.from(0));
expect(cosignedOrder.info.cosignerData.outputAmounts.length).toEqual(1);
expect(cosignedOrder.info.cosignerData.outputAmounts[0]).toEqual(RAW_AMOUNT.mul(2));
expect(cosignedOrder.info.cosignerData.inputOverride).toEqual(BigNumber.from(0));
expect(cosignedOrder.info.cosignerData.outputOverrides.length).toEqual(1);
expect(cosignedOrder.info.cosignerData.outputOverrides[0]).toEqual(RAW_AMOUNT.mul(2));
});

it('Pick the lesser of two quotes - EXACT_OUT', async () => {
const quoters = [new MockQuoter(logger, 9, 10), new MockQuoter(logger, 8, 10)];
const order = getOrder({
cosigner: cosignerWallet.address,
baseInput: {
input: {
token: TOKEN_IN,
startAmount: RAW_AMOUNT,
endAmount: RAW_AMOUNT.mul(110).div(100),
},
baseOutputs: [
outputs: [
{
token: TOKEN_OUT,
startAmount: RAW_AMOUNT,
Expand All @@ -201,9 +201,9 @@ describe('Quote handler', () => {
expect(cosignedOrder.info.cosignerData.exclusiveFiller).toEqual(MOCK_FILLER_ADDRESS);

// overridden output amount to 2x
expect(cosignedOrder.info.cosignerData.inputAmount).toEqual(RAW_AMOUNT.mul(8).div(10));
expect(cosignedOrder.info.cosignerData.outputAmounts.length).toEqual(1);
expect(cosignedOrder.info.cosignerData.outputAmounts[0]).toEqual(BigNumber.from(0));
expect(cosignedOrder.info.cosignerData.inputOverride).toEqual(RAW_AMOUNT.mul(8).div(10));
expect(cosignedOrder.info.cosignerData.outputOverrides.length).toEqual(1);
expect(cosignedOrder.info.cosignerData.outputOverrides[0]).toEqual(BigNumber.from(0));
});

it('Two quoters returning the same result', async () => {
Expand All @@ -224,9 +224,9 @@ describe('Quote handler', () => {
expect(cosignedOrder.info.cosignerData.exclusiveFiller).toEqual(ethers.constants.AddressZero);

// overridden output amount to 2x
expect(cosignedOrder.info.cosignerData.inputAmount).toEqual(BigNumber.from(0));
expect(cosignedOrder.info.cosignerData.outputAmounts.length).toEqual(1);
expect(cosignedOrder.info.cosignerData.outputAmounts[0]).toEqual(BigNumber.from(0));
expect(cosignedOrder.info.cosignerData.inputOverride).toEqual(BigNumber.from(0));
expect(cosignedOrder.info.cosignerData.outputOverrides.length).toEqual(1);
expect(cosignedOrder.info.cosignerData.outputOverrides[0]).toEqual(BigNumber.from(0));
});

it('Unknown cosigner', async () => {
Expand Down Expand Up @@ -301,9 +301,9 @@ describe('Quote handler', () => {
getQuoteResponse({ amountOut: ethers.utils.parseEther('0.8') })
);
expect(cosignerData.exclusiveFiller).toEqual(ethers.constants.AddressZero);
expect(cosignerData.inputAmount).toEqual(BigNumber.from(0));
expect(cosignerData.outputAmounts.length).toEqual(1);
expect(cosignerData.outputAmounts[0]).toEqual(BigNumber.from(0));
expect(cosignerData.inputOverride).toEqual(BigNumber.from(0));
expect(cosignerData.outputOverrides.length).toEqual(1);
expect(cosignerData.outputOverrides[0]).toEqual(BigNumber.from(0));
});

it('exact input quote better, sets exclusivity and updates amounts', async () => {
Expand All @@ -314,9 +314,9 @@ describe('Quote handler', () => {
getQuoteResponse({ amountOut: outputAmount })
);
expect(cosignerData.exclusiveFiller).toEqual(MOCK_FILLER_ADDRESS);
expect(cosignerData.inputAmount).toEqual(BigNumber.from(0));
expect(cosignerData.outputAmounts.length).toEqual(1);
expect(cosignerData.outputAmounts[0]).toEqual(outputAmount);
expect(cosignerData.inputOverride).toEqual(BigNumber.from(0));
expect(cosignerData.outputOverrides.length).toEqual(1);
expect(cosignerData.outputOverrides[0]).toEqual(outputAmount);
});

it('exact output quote worse, no exclusivity', async () => {
Expand All @@ -326,22 +326,22 @@ describe('Quote handler', () => {
getQuoteResponse({ amountIn: ethers.utils.parseEther('1.2') }, TradeType.EXACT_OUTPUT)
);
expect(cosignerData.exclusiveFiller).toEqual(ethers.constants.AddressZero);
expect(cosignerData.inputAmount).toEqual(BigNumber.from(0));
expect(cosignerData.outputAmounts.length).toEqual(1);
expect(cosignerData.outputAmounts[0]).toEqual(BigNumber.from(0));
expect(cosignerData.inputOverride).toEqual(BigNumber.from(0));
expect(cosignerData.outputOverrides.length).toEqual(1);
expect(cosignerData.outputOverrides[0]).toEqual(BigNumber.from(0));
});

it('exact input quote better, sets exclusivity and updates amounts', async () => {
const request = await getRequest(getOrder({ cosigner: cosignerWallet.address }));
const inputAmount = ethers.utils.parseEther('0.8');
const inputOverride = ethers.utils.parseEther('0.8');
const cosignerData = getCosignerData(
new HardQuoteRequest(request),
getQuoteResponse({ amountIn: ethers.utils.parseEther('1.2') }, TradeType.EXACT_OUTPUT)
);
expect(cosignerData.exclusiveFiller).toEqual(MOCK_FILLER_ADDRESS);
expect(cosignerData.inputAmount).toEqual(inputAmount);
expect(cosignerData.outputAmounts.length).toEqual(1);
expect(cosignerData.outputAmounts[0]).toEqual(BigNumber.from(0));
expect(cosignerData.inputOverride).toEqual(inputOverride);
expect(cosignerData.outputOverrides.length).toEqual(1);
expect(cosignerData.outputOverrides[0]).toEqual(BigNumber.from(0));
});
});
});
4 changes: 2 additions & 2 deletions test/handlers/hard-quote/schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ const validHardRequestBodyCombos = validTokenIn.flatMap((tokenIn) =>
validAmountIn.flatMap((amount) => {
const order = new UnsignedV2DutchOrder(
getOrderInfo({
baseInput: {
input: {
token: tokenIn,
startAmount: BigNumber.from(amount),
endAmount: BigNumber.from(amount),
},
baseOutputs: [
outputs: [
{
token: tokenOut,
startAmount: BigNumber.from(amount),
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5304,10 +5304,10 @@
resolved "https://registry.yarnpkg.com/@uniswap/token-lists/-/token-lists-1.0.0-beta.33.tgz#966ba96c9ccc8f0e9e09809890b438203f2b1911"
integrity sha512-JQkXcpRI3jFG8y3/CGC4TS8NkDgcxXaOQuYW8Qdvd6DcDiIyg2vVYCG9igFEzF0G6UvxgHkBKC7cWCgzZNYvQg==

"@uniswap/uniswapx-sdk@1.5.0-alpha.7":
version "1.5.0-alpha.7"
resolved "https://registry.yarnpkg.com/@uniswap/uniswapx-sdk/-/uniswapx-sdk-1.5.0-alpha.7.tgz#6c16e589b2a5492d574a008d5bbfaf3d2fd94f5f"
integrity sha512-G9+KFi++NorgZlbRclQQArynjFr0uWDAW9l/qJoiQJfvQYhqIfNxRkdgp4rWsQldVJf0C1hjyiJbPPfB/sfZoQ==
"@uniswap/uniswapx-sdk@2.0.1-alpha.3":
version "2.0.1-alpha.3"
resolved "https://registry.npmjs.org/@uniswap/uniswapx-sdk/-/uniswapx-sdk-2.0.1-alpha.3.tgz#09992069e7de258504185a85512fc2a5b763713b"
integrity sha512-0KUqmscaRPOL2QVguvZZq/573sQmNoohp7pRyxxuEqz0BSr91jxWe5j6UiSCL4SkACuI0Ti/hvyZAFQgue46YA==
dependencies:
"@ethersproject/bytes" "^5.7.0"
"@ethersproject/providers" "^5.7.0"
Expand Down

0 comments on commit 9190365

Please sign in to comment.