Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: log opposing quote iff real quote is valid #226

Merged
merged 3 commits into from
Nov 6, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions lib/entities/QuoteRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,15 @@ export class QuoteRequest {
...(this.quoteId && { quoteId: this.quoteId }),
};
}

public toOpposingRequest(): QuoteRequest {
const opposingJSON = this.toOpposingCleanJSON();
return new QuoteRequest({
...opposingJSON,
amount: BigNumber.from(opposingJSON.amount),
type: TradeType[opposingJSON.type as keyof typeof TradeType],
});
}

public get requestId(): string {
return this.data.requestId;
Expand Down
11 changes: 10 additions & 1 deletion lib/quoters/WebhookQuoter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ export class WebhookQuoter implements Quoter {
...(!!headers && { headers }),
};

const [hookResponse] = await Promise.all([
const [hookResponse, opposite] = await Promise.all([
axios.post(endpoint, cleanRequest, axiosConfig),
axios.post(endpoint, opposingCleanRequest, axiosConfig),
]);
Expand Down Expand Up @@ -170,6 +170,15 @@ export class WebhookQuoter implements Quoter {
request.requestId
} for endpoint ${endpoint} successful quote: ${request.amount.toString()} -> ${quote.toString()}}`
);

//iff valid quote, log the opposing side as well
const opposingRequest = request.toOpposingRequest();
const opposingResponse = QuoteResponse.fromRFQ(opposingRequest, opposite.data, opposingRequest.type).response;
this.log.info({
ConjunctiveNormalForm marked this conversation as resolved.
Show resolved Hide resolved
eventType: 'QuoteResponse',
body: { ...opposingResponse.toLog(), offerer: opposingResponse.swapper },
});

return response;
} catch (e) {
metric.putMetric(Metric.RFQ_FAIL_ERROR, 1, MetricLoggerUnit.Count);
Expand Down
15 changes: 15 additions & 0 deletions test/entities/QuoteRequest.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,19 @@ describe('QuoteRequest', () => {
numOutputs: 1,
});
});

it('toOpposingRequest', async () => {
const opposingRequest = request.toOpposingRequest();
expect(opposingRequest.toCleanJSON()).toEqual({
tokenInChainId: CHAIN_ID,
tokenOutChainId: CHAIN_ID,
requestId: REQUEST_ID,
tokenIn: TOKEN_OUT,
tokenOut: TOKEN_IN,
amount: ethers.utils.parseEther('1').toString(),
swapper: SWAPPER,
type: 'EXACT_OUTPUT',
numOutputs: 1,
});
});
});
36 changes: 36 additions & 0 deletions test/handlers/quote/handler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,19 @@ describe('Quote handler', () => {
quoteId: QUOTE_ID,
},
});
}).mockImplementationOnce((_endpoint, _req, _options) => {
return Promise.resolve({
data: {
amountOut: amountIn.mul(3).toString(),
requestId: request.requestId,
tokenIn: request.tokenOut,
tokenOut: request.tokenIn,
amountIn: request.amount,
swapper: request.swapper,
chainId: request.tokenInChainId,
quoteId: QUOTE_ID,
},
});
});

const response: APIGatewayProxyResult = await getQuoteHandler(quoters).handler(
Expand Down Expand Up @@ -274,6 +287,16 @@ describe('Quote handler', () => {
...responseFromRequest(request, { amountOut: amountIn.mul(2).toString() }),
},
});
}).mockImplementationOnce((_endpoint, _req, options: any) => {
expect(options.headers['X-Authentication']).toEqual('1234');
const res = responseFromRequest(request, { amountOut: amountIn.mul(3).toString() });
return Promise.resolve({
data: {
...res,
tokenIn: res.tokenOut,
tokenOut: res.tokenIn,
},
});
});

const response: APIGatewayProxyResult = await getQuoteHandler(quoters).handler(
Expand Down Expand Up @@ -404,6 +427,19 @@ describe('Quote handler', () => {
quoteId: QUOTE_ID,
},
});
}).mockImplementationOnce((_endpoint, _req, _options) => {
return Promise.resolve({
data: {
amountOut: amountIn.div(2).toString(),
tokenIn: request.tokenOut,
tokenOut: request.tokenIn,
amountIn: request.amount,
swapper: request.swapper,
chainId: request.tokenInChainId,
requestId: request.requestId,
quoteId: QUOTE_ID,
},
});
});

const response: APIGatewayProxyResult = await getQuoteHandler(quoters).handler(
Expand Down
40 changes: 40 additions & 0 deletions test/providers/quoters/WebhookQuoter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,14 @@ describe('WebhookQuoter tests', () => {
return Promise.resolve({
data: quote,
});
}).mockImplementationOnce((_endpoint, _req, _options) => {
return Promise.resolve({
data: {
...quote,
tokenIn: request.tokenOut,
tokenOut: request.tokenIn,
}
});
});
const response = await webhookQuoter.quote(request);

Expand All @@ -82,6 +90,14 @@ describe('WebhookQuoter tests', () => {
return Promise.resolve({
data: quote,
});
}).mockImplementationOnce((_endpoint, _req, _options) => {
return Promise.resolve({
data: {
...quote,
tokenIn: request.tokenOut,
tokenOut: request.tokenIn,
}
});
});
await webhookQuoter.quote(request);

Expand Down Expand Up @@ -168,6 +184,14 @@ describe('WebhookQuoter tests', () => {
return Promise.resolve({
data: quote,
});
}).mockImplementationOnce((_endpoint, _req, _options) => {
return Promise.resolve({
data: {
...quote,
tokenIn: request.tokenOut,
tokenOut: request.tokenIn,
}
});
});
const response = await webhookQuoter.quote(request);

Expand Down Expand Up @@ -202,6 +226,14 @@ describe('WebhookQuoter tests', () => {
return Promise.resolve({
data: quote,
});
}).mockImplementationOnce((_endpoint, _req, _options) => {
return Promise.resolve({
data: {
...quote,
tokenIn: request.tokenOut,
tokenOut: request.tokenIn,
}
});
});
const response = await webhookQuoter.quote(request);

Expand Down Expand Up @@ -230,6 +262,14 @@ describe('WebhookQuoter tests', () => {
return Promise.resolve({
data: quote,
});
}).mockImplementationOnce((_endpoint, _req, _options) => {
return Promise.resolve({
data: {
...quote,
tokenIn: request.tokenOut,
tokenOut: request.tokenIn,
}
});
});
const response = await quoter.quote(request);

Expand Down
Loading