Skip to content

Commit

Permalink
log opposing quote iff real quote is valid
Browse files Browse the repository at this point in the history
  • Loading branch information
ConjunctiveNormalForm committed Nov 6, 2023
1 parent e880162 commit 604fcb1
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 1 deletion.
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({
eventType: 'QuoteResponse',
body: { ...opposingResponse.toLog(), offerer: opposingResponse.swapper },
});

return response;
} catch (e) {
metric.putMetric(Metric.RFQ_FAIL_ERROR, 1, MetricLoggerUnit.Count);
Expand Down
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

0 comments on commit 604fcb1

Please sign in to comment.