Skip to content

Commit

Permalink
fix: tests
Browse files Browse the repository at this point in the history
  • Loading branch information
marktoda committed Feb 22, 2024
1 parent be33b2d commit 3b1797c
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 73 deletions.
23 changes: 13 additions & 10 deletions lib/entities/HardQuoteRequest.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
import { TradeType } from '@uniswap/sdk-core';
import { V2DutchOrder } from '@uniswap/uniswapx-sdk';
import { UnsignedV2DutchOrder } from '@uniswap/uniswapx-sdk';
import { BigNumber, utils } from 'ethers';
import { QuoteRequestDataJSON } from '.';

import { HardQuoteRequestBody } from '../handlers/hard-quote';
import { QuoteRequestDataJSON } from '.';

export class HardQuoteRequest {
public order: V2DutchOrder;
public order: UnsignedV2DutchOrder;

public static fromHardRequestBody(_body: HardQuoteRequestBody): HardQuoteRequest {
// TODO: parse hard request into the same V2 request object format
throw new Error('Method not implemented.');
}

constructor(private data: HardQuoteRequestBody) {
this.order = V2DutchOrder.parse(data.encodedInnerOrder, data.tokenInChainId);
this.order = UnsignedV2DutchOrder.parse(data.encodedInnerOrder, data.tokenInChainId);
}

public toCleanJSON(): QuoteRequestDataJSON {
Expand Down Expand Up @@ -68,19 +69,19 @@ export class HardQuoteRequest {
}

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

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

public get amount(): BigNumber {
if (this.type === TradeType.EXACT_INPUT) {
return this.order.info.input.startAmount;
return this.order.info.baseInput.startAmount;
} else {
const amount = BigNumber.from(0);
for (const output of this.order.info.outputs) {
for (const output of this.order.info.baseOutputs) {
amount.add(output.startAmount);
}

Expand All @@ -89,11 +90,13 @@ export class HardQuoteRequest {
}

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

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

public get cosigner(): string {
Expand Down
2 changes: 1 addition & 1 deletion lib/entities/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export * from './analytics-events';
export * from './aws-metrics-logger';
export * from './HardQuoteRequest';
export * from './QuoteRequest';
export * from './QuoteResponse';
export * from './HardQuoteRequest';
5 changes: 2 additions & 3 deletions lib/handlers/hard-quote/handler.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { MetricLoggerUnit } from '@uniswap/smart-order-router';
import Joi from 'joi';

import { Metric } from '../../entities';
import { HardQuoteRequest } from '../../entities';
import { HardQuoteRequest, Metric } from '../../entities';
import { timestampInMstoSeconds } from '../../util/time';
import { APIGLambdaHandler } from '../base';
import { APIHandleRequestParams, ErrorResponse, Response } from '../base/api-handler';
import { HardQuoteRequestBody, HardQuoteRequestBodyJoi } from './schema';
import { ContainerInjected, RequestInjected } from './injector';
import { HardQuoteRequestBody, HardQuoteRequestBodyJoi } from './schema';

export class QuoteHandler extends APIGLambdaHandler<
ContainerInjected,
Expand Down
7 changes: 5 additions & 2 deletions lib/handlers/hard-quote/injector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@ import { APIGatewayProxyEvent, Context } from 'aws-lambda';
import { default as bunyan, default as Logger } from 'bunyan';

import {
BETA_S3_KEY,
FADE_RATE_BUCKET,
FADE_RATE_S3_KEY,
BETA_S3_KEY, PRODUCTION_S3_KEY, WEBHOOK_CONFIG_BUCKET } from '../../constants';
PRODUCTION_S3_KEY,
WEBHOOK_CONFIG_BUCKET,
} from '../../constants';
import { AWSMetricsLogger, UniswapXParamServiceMetricDimension } from '../../entities/aws-metrics-logger';
import { S3CircuitBreakerConfigurationProvider } from '../../providers/circuit-breaker/s3';
import { S3WebhookConfigurationProvider } from '../../providers';
import { FirehoseLogger } from '../../providers/analytics';
import { S3CircuitBreakerConfigurationProvider } from '../../providers/circuit-breaker/s3';
import { MockFillerComplianceConfigurationProvider } from '../../providers/compliance';
import { Quoter, WebhookQuoter } from '../../quoters';
import { STAGE } from '../../util/stage';
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
"@uniswap/sdk-core": "^3.1.0",
"@uniswap/smart-order-router": "^3.3.0",
"@uniswap/token-lists": "^1.0.0-beta.31",
"@uniswap/uniswapx-sdk": "1.5.0-alpha.5",
"@uniswap/uniswapx-sdk": "1.5.0-alpha.7",
"@uniswap/v3-sdk": "^3.9.0",
"aws-cdk-lib": "2.85.0",
"aws-embedded-metrics": "^4.1.0",
Expand Down
73 changes: 44 additions & 29 deletions test/entities/HardQuoteRequest.test.ts
Original file line number Diff line number Diff line change
@@ -1,107 +1,122 @@
import { TradeType } from '@uniswap/sdk-core';
import { V2DutchOrder, V2DutchOrderInfo } from '@uniswap/uniswapx-sdk';
import { UnsignedV2DutchOrder, UnsignedV2DutchOrderInfo } from '@uniswap/uniswapx-sdk';
import { BigNumber, ethers } from 'ethers';

import { HardQuoteRequest } from '../../lib/entities';
import { HardQuoteRequestBody } from '../../lib/handlers/hard-quote';

const NOW = Math.floor(new Date().getTime() / 1000);
const RAW_AMOUNT = BigNumber.from("1000000");
const RAW_AMOUNT = BigNumber.from('1000000');
const REQUEST_ID = 'a83f397c-8ef4-4801-a9b7-6e79155049f6';
const QUOTE_ID = 'a83f397c-8ef4-4801-a9b7-6e79155049f6';
const SWAPPER = '0x0000000000000000000000000000000000000000';
const TOKEN_IN = '0x1f9840a85d5aF5bf1D1762F925BDADdC4201F984';
const TOKEN_OUT = '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2';
const CHAIN_ID = 1;

export const getOrderInfo = (data: Partial<V2DutchOrderInfo>): V2DutchOrderInfo => {
export const getOrderInfo = (data: Partial<UnsignedV2DutchOrderInfo>): UnsignedV2DutchOrderInfo => {
return Object.assign(
{
deadline: NOW + 1000,
reactor: ethers.constants.AddressZero,
swapper: ethers.constants.AddressZero,
nonce: BigNumber.from(10),
additionalValidationContract: ethers.constants.AddressZero,
additionalValidationData: "0x",
additionalValidationData: '0x',
cosigner: ethers.constants.AddressZero,
cosignerData: undefined,
input: {
baseInput: {
token: TOKEN_IN,
startAmount: RAW_AMOUNT,
endAmount: RAW_AMOUNT,
},
outputs: [
baseOutputs: [
{
token: TOKEN_OUT,
startAmount: RAW_AMOUNT.mul(2),
endAmount: RAW_AMOUNT.mul(90).div(100),
recipient: ethers.constants.AddressZero,
},
],
cosignature: undefined,
},
data
);
};

const makeRequest = (data: Partial<HardQuoteRequestBody>): HardQuoteRequest => {
return new HardQuoteRequest(Object.assign({
requestId: '1',
quoteId: '1',
tokenInChainId: CHAIN_ID,
tokenOutChainId: CHAIN_ID,
encodedInnerOrder: '0x',
innerSig: '0x',
}, data));
}
return new HardQuoteRequest(
Object.assign(
{
requestId: REQUEST_ID,
quoteId: QUOTE_ID,
tokenInChainId: CHAIN_ID,
tokenOutChainId: CHAIN_ID,
encodedInnerOrder: '0x',
innerSig: '0x',
},
data
)
);
};

describe('QuoteRequest', () => {
afterEach(() => {
jest.clearAllMocks();
});

it('parses order properly', () => {
const order = new V2DutchOrder(getOrderInfo({
swapper: SWAPPER,
}), CHAIN_ID);
const order = new UnsignedV2DutchOrder(
getOrderInfo({
swapper: SWAPPER,
}),
CHAIN_ID
);
const request = makeRequest({ encodedInnerOrder: order.serialize(), innerSig: '0x' });
expect(request.swapper).toEqual(SWAPPER);
expect(request.tokenIn).toEqual(TOKEN_IN);
expect(request.tokenOut).toEqual(TOKEN_OUT);
expect(request.numOutputs).toEqual(1);
expect(request.amount).toEqual(RAW_AMOUNT.toString());
expect(request.amount).toEqual(RAW_AMOUNT);
expect(request.type).toEqual(TradeType.EXACT_INPUT);
});

it('toCleanJSON', async () => {
const order = new V2DutchOrder(getOrderInfo({
swapper: SWAPPER,
}), CHAIN_ID);
const order = new UnsignedV2DutchOrder(
getOrderInfo({
swapper: SWAPPER,
}),
CHAIN_ID
);
const request = makeRequest({ encodedInnerOrder: order.serialize(), innerSig: '0x' });
expect(request.toCleanJSON()).toEqual({
tokenInChainId: CHAIN_ID,
tokenOutChainId: CHAIN_ID,
requestId: REQUEST_ID,
quoteId: QUOTE_ID,
tokenIn: TOKEN_IN,
tokenOut: TOKEN_OUT,
amount: ethers.utils.parseEther('1').toString(),
amount: RAW_AMOUNT.toString(),
swapper: ethers.constants.AddressZero,
type: 'EXACT_INPUT',
numOutputs: 1,
});
});

it('toOpposingCleanJSON', async () => {
const order = new V2DutchOrder(getOrderInfo({
swapper: SWAPPER,
}), CHAIN_ID);
const order = new UnsignedV2DutchOrder(
getOrderInfo({
swapper: SWAPPER,
}),
CHAIN_ID
);
const request = makeRequest({ encodedInnerOrder: order.serialize(), innerSig: '0x' });
expect(request.toOpposingCleanJSON()).toEqual({
tokenInChainId: CHAIN_ID,
tokenOutChainId: CHAIN_ID,
requestId: REQUEST_ID,
quoteId: QUOTE_ID,
tokenIn: TOKEN_OUT,
tokenOut: TOKEN_IN,
amount: ethers.utils.parseEther('1').toString(),
amount: RAW_AMOUNT.toString(),
swapper: ethers.constants.AddressZero,
type: 'EXACT_OUTPUT',
numOutputs: 1,
Expand Down
54 changes: 31 additions & 23 deletions test/handlers/hard-quote/schema.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { UnsignedV2DutchOrder } from '@uniswap/uniswapx-sdk';
import { BigNumber, utils } from 'ethers';
import { V2DutchOrder } from '@uniswap/uniswapx-sdk';
import { v4 as uuidv4 } from 'uuid';
import {
HardQuoteRequestBodyJoi,
} from '../../../lib/handlers/hard-quote';

import { HardQuoteRequestBodyJoi } from '../../../lib/handlers/hard-quote';
import { getOrderInfo } from '../../entities/HardQuoteRequest.test';

const SWAPPER = '0x0000000000000000000000000000000000000000';
Expand All @@ -18,28 +17,32 @@ const validAmountIn = ['1', '1000', '1234234', utils.parseEther('1').toString(),
const validHardRequestBodyCombos = validTokenIn.flatMap((tokenIn) =>
validTokenOut.flatMap((tokenOut) =>
validAmountIn.flatMap((amount) => {
const order = new V2DutchOrder(getOrderInfo({
input: {
token: tokenIn,
startAmount: BigNumber.from(amount),
endAmount: BigNumber.from(amount),
},
outputs: [
{
token: tokenOut,
const order = new UnsignedV2DutchOrder(
getOrderInfo({
baseInput: {
token: tokenIn,
startAmount: BigNumber.from(amount),
endAmount: BigNumber.from(amount),
recipient: SWAPPER,
},
],
}), 1);
baseOutputs: [
{
token: tokenOut,
startAmount: BigNumber.from(amount),
endAmount: BigNumber.from(amount),
recipient: SWAPPER,
},
],
}),
1
);
return {
requestId: REQUEST_ID,
quoteId: QUOTE_ID,
tokenInChainId: 1,
tokenOutChainId: 1,
encodedInnerOrder: order.serialize(),
innerSig: '0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
innerSig:
'0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
};
})
)
Expand Down Expand Up @@ -79,19 +82,21 @@ describe('hard-quote schemas', () => {

validated = HardQuoteRequestBodyJoi.validate(
Object.assign({}, validHardRequestBodyCombos[0], {
innerSig: '0x0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
innerSig:
'0x0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
})
);
expect(validated.error).toBeUndefined();
});


it('requires tokenInChainId to be defined', () => {
const { tokenOutChainId, requestId, quoteId, encodedInnerOrder, innerSig } = validHardRequestBodyCombos[0];
const validated = HardQuoteRequestBodyJoi.validate({
tokenOutChainId,
requestId,
quoteId, encodedInnerOrder, innerSig
quoteId,
encodedInnerOrder,
innerSig,
});
expect(validated.error?.message).toEqual('"tokenInChainId" is required');
});
Expand All @@ -101,19 +106,22 @@ describe('hard-quote schemas', () => {
const validated = HardQuoteRequestBodyJoi.validate({
tokenInChainId,
requestId,
quoteId, encodedInnerOrder, innerSig
quoteId,
encodedInnerOrder,
innerSig,
});
expect(validated.error?.message).toEqual('"tokenOutChainId" is required');
});


it('requires tokenOutChainId and tokenInChainId to be the same value', () => {
const { tokenInChainId, requestId, quoteId, encodedInnerOrder, innerSig } = validHardRequestBodyCombos[0];
const validated = HardQuoteRequestBodyJoi.validate({
tokenInChainId,
tokenOutChainId: 5,
requestId,
quoteId, encodedInnerOrder, innerSig
quoteId,
encodedInnerOrder,
innerSig,
});
expect(validated.error?.message).toContain('"tokenOutChainId" must be [ref:tokenInChainId]');
});
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4502,10 +4502,10 @@
resolved "https://registry.yarnpkg.com/@uniswap/token-lists/-/token-lists-1.0.0-beta.31.tgz#ff3852bd505ec7b4c276625c762ea79a93a919ec"
integrity sha512-BQVoelKCRf64IToPEs1wxiXOnhr/ukwPOF78XG11PrTAOL4F8umjYKFb8ZPv1/dIJsPaC7GhLSriEqyp94SasQ==

"@uniswap/[email protected].5":
version "1.5.0-alpha.5"
resolved "https://registry.yarnpkg.com/@uniswap/uniswapx-sdk/-/uniswapx-sdk-1.5.0-alpha.5.tgz#5dfb4dcfa35610845587e90014685098ffd3cb02"
integrity sha512-IBmRhDowOc3bKgPKkgRB3qrAyTR3Y4nfll4XkETcnc90DgZ7c4ZjSkyiFXHkwUjaT57FzURXq6C5rKWuVmO7Ag==
"@uniswap/[email protected].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==
dependencies:
"@ethersproject/bytes" "^5.7.0"
"@ethersproject/providers" "^5.7.0"
Expand Down

0 comments on commit 3b1797c

Please sign in to comment.