Skip to content

Commit

Permalink
Merge pull request #397 from Uniswap/fix-supported-version
Browse files Browse the repository at this point in the history
fix: default supportedProtocols to v2
  • Loading branch information
ConjunctiveNormalForm authored Feb 11, 2025
2 parents 260aef7 + ab0d55b commit 0ac3b86
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 17 deletions.
2 changes: 1 addition & 1 deletion lib/quoters/WebhookQuoter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ function isNonQuote(request: QuoteRequest, hookResponse: AxiosResponse, parsedRe

export function getEndpointSupportedProtocols(e: WebhookConfiguration) {
if (!e.supportedVersions || e.supportedVersions.length == 0) {
return [ProtocolVersion.V1];
return [ProtocolVersion.V2];
}
return e.supportedVersions;
}
Expand Down
1 change: 1 addition & 0 deletions test/fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const now = Math.floor(Date.now() / 1000);
export const WEBHOOK_URL = 'https://uniswap.org';
export const WEBHOOK_URL_ONEINCH = 'https://1inch.io';
export const WEBHOOK_URL_SEARCHER = 'https://searcher.com';
export const WEBHOOK_URL_FOO = 'https://foo.com';

export const MOCK_V2_CB_PROVIDER = new MockV2CircuitBreakerConfigurationProvider(
[WEBHOOK_URL, WEBHOOK_URL_ONEINCH, WEBHOOK_URL_SEARCHER],
Expand Down
79 changes: 70 additions & 9 deletions test/handlers/quote/handler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,9 @@ describe('Quote handler', () => {
};

it('Simple request and response', async () => {
const quoters = [new MockQuoter(logger, 1, 1)];
const quoters = [new MockQuoter(logger, 1, 1), new MockQuoter(logger, 1, 2)];
const amountIn = ethers.utils.parseEther('1');
const request = getRequest(amountIn.toString());
const request = getRequest(amountIn.toString(), 'EXACT_INPUT', ProtocolVersion.V2);

const response: APIGatewayProxyResult = await getQuoteHandler(quoters).handler(
getEvent(request),
Expand All @@ -129,9 +129,9 @@ describe('Quote handler', () => {
});

it('Handles hex amount', async () => {
const quoters = [new MockQuoter(logger, 1, 1)];
const quoters = [new MockQuoter(logger, 1, 1), new MockQuoter(logger, 1, 2)];
const amountIn = ethers.utils.parseEther('1');
const request = getRequest(amountIn.toHexString());
const request = getRequest(amountIn.toHexString(), 'EXACT_INPUT', ProtocolVersion.V2);

const response: APIGatewayProxyResult = await getQuoteHandler(quoters).handler(
getEvent(request),
Expand Down Expand Up @@ -235,6 +235,7 @@ describe('Quote handler', () => {
it('Simple request and response', async () => {
const webhookProvider = new MockWebhookConfigurationProvider([
{ endpoint: 'https://uniswap.org', headers: {}, name: 'uniswap', hash: '0xuni' },
{ endpoint: 'https://foo.org', headers: {}, name: 'foo', hash: '0xfoo' },
]);

const quoters = [
Expand All @@ -248,7 +249,7 @@ describe('Quote handler', () => {
),
];
const amountIn = ethers.utils.parseEther('1');
const request = getRequest(amountIn.toString());
const request = getRequest(amountIn.toString(), 'EXACT_INPUT', ProtocolVersion.V2);

mockedAxios.post
.mockImplementationOnce((_endpoint, _req, _options) => {
Expand Down Expand Up @@ -278,8 +279,35 @@ describe('Quote handler', () => {
quoteId: QUOTE_ID,
},
});
})
.mockImplementationOnce((_endpoint, _req, _options) => {
return Promise.resolve({
data: {
amountOut: amountIn.mul(1).toString(),
requestId: request.requestId,
tokenIn: request.tokenIn,
tokenOut: request.tokenOut,
amountIn: request.amount,
swapper: request.swapper,
chainId: request.tokenInChainId,
quoteId: QUOTE_ID,
},
});
})
.mockImplementationOnce((_endpoint, _req, _options) => {
return Promise.resolve({
data: {
amountOut: amountIn.mul(1).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(
getEvent(request),
{} as unknown as Context
Expand Down Expand Up @@ -308,6 +336,12 @@ describe('Quote handler', () => {
},
hash: '0xuni',
},
{
name: 'foo',
endpoint: 'https://foo.org',
headers: {},
hash: '0xfoo',
},
]);
const quoters = [
new WebhookQuoter(
Expand All @@ -320,7 +354,7 @@ describe('Quote handler', () => {
),
];
const amountIn = ethers.utils.parseEther('1');
const request = getRequest(amountIn.toString());
const request = getRequest(amountIn.toString(), 'EXACT_INPUT', ProtocolVersion.V2);

mockedAxios.post
.mockImplementationOnce((_endpoint, _req, options: any) => {
Expand All @@ -341,6 +375,30 @@ describe('Quote handler', () => {
tokenOut: res.tokenIn,
},
});
})
.mockImplementationOnce((_endpoint, _req, _options) => {
return Promise.resolve({
data: {
amountOut: amountIn.mul(1).toString(),
requestId: request.requestId,
tokenIn: request.tokenIn,
tokenOut: request.tokenOut,
amountIn: request.amount,
swapper: request.swapper,
chainId: request.tokenInChainId,
quoteId: QUOTE_ID,
},
});
})
.mockImplementationOnce((_endpoint, _req, _options) => {
const res = responseFromRequest(request, { amountOut: amountIn.mul(1).toString() });
return Promise.resolve({
data: {
...res,
tokenIn: res.tokenOut,
tokenOut: res.tokenIn,
},
});
});

const response: APIGatewayProxyResult = await getQuoteHandler(quoters).handler(
Expand Down Expand Up @@ -427,6 +485,7 @@ describe('Quote handler', () => {
it('uses backup on failure', async () => {
const webhookProvider = new MockWebhookConfigurationProvider([
{ name: 'uniswap', endpoint: 'https://uniswap.org', headers: {}, hash: '0xuni' },
{ name: 'foo', endpoint: 'https://foo.org', headers: {}, hash: '0xfoo' },
]);
const quoters = [
new WebhookQuoter(
Expand All @@ -438,9 +497,10 @@ describe('Quote handler', () => {
repository
),
new MockQuoter(logger, 1, 1),
new MockQuoter(logger, 1, 2),
];
const amountIn = ethers.utils.parseEther('1');
const request = getRequest(amountIn.toString());
const request = getRequest(amountIn.toString(), 'EXACT_INPUT', ProtocolVersion.V2);

mockedAxios.post.mockImplementationOnce((_endpoint, _req, _options) => {
return Promise.resolve({
Expand All @@ -463,6 +523,7 @@ describe('Quote handler', () => {
it('uses if better than backup', async () => {
const webhookProvider = new MockWebhookConfigurationProvider([
{ name: 'uniswap', endpoint: 'https://uniswap.org', headers: {}, hash: '0xuni' },
{ name: 'foo', endpoint: 'https://foo.org', headers: {}, hash: '0xfoo' },
]);
const quoters = [
new WebhookQuoter(
Expand All @@ -476,7 +537,7 @@ describe('Quote handler', () => {
new MockQuoter(logger, 1, 1),
];
const amountIn = ethers.utils.parseEther('1');
const request = getRequest(amountIn.toString());
const request = getRequest(amountIn.toString(), 'EXACT_INPUT', ProtocolVersion.V2);

mockedAxios.post
.mockImplementationOnce((_endpoint, _req, _options) => {
Expand Down
51 changes: 44 additions & 7 deletions test/providers/quoters/WebhookQuoter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,13 @@ import { FirehoseLogger } from '../../../lib/providers/analytics';
import { MockFillerComplianceConfigurationProvider } from '../../../lib/providers/compliance';
import { WebhookQuoter } from '../../../lib/quoters';
import { MockFillerAddressRepository } from '../../../lib/repositories/filler-address-repository';
import { MOCK_V2_CB_PROVIDER, WEBHOOK_URL, WEBHOOK_URL_ONEINCH, WEBHOOK_URL_SEARCHER } from '../../fixtures';
import {
MOCK_V2_CB_PROVIDER,
WEBHOOK_URL,
WEBHOOK_URL_FOO,
WEBHOOK_URL_ONEINCH,
WEBHOOK_URL_SEARCHER,
} from '../../fixtures';

jest.mock('axios');
jest.mock('../../../lib/providers/analytics');
Expand Down Expand Up @@ -38,9 +44,21 @@ describe('WebhookQuoter tests', () => {
});

const webhookProvider = new MockWebhookConfigurationProvider([
{ name: 'uniswap', endpoint: WEBHOOK_URL, headers: {}, hash: '0xuni' },
{
name: 'uniswap',
endpoint: WEBHOOK_URL,
headers: {},
hash: '0xuni',
supportedVersions: [ProtocolVersion.V1, ProtocolVersion.V2],
},
{ name: '1inch', endpoint: WEBHOOK_URL_ONEINCH, headers: {}, hash: '0x1inch' },
{ name: 'searcher', endpoint: WEBHOOK_URL_SEARCHER, headers: {}, hash: '0xsearcher' },
{
name: 'searcher',
endpoint: WEBHOOK_URL_SEARCHER,
headers: {},
hash: '0xsearcher',
supportedVersions: [ProtocolVersion.V1, ProtocolVersion.V2],
},
]);

const logger = { child: () => logger, info: jest.fn(), error: jest.fn(), debug: jest.fn() } as any;
Expand Down Expand Up @@ -271,7 +289,13 @@ describe('WebhookQuoter tests', () => {

describe('Supported protocols tests', () => {
const webhookProvider = new MockWebhookConfigurationProvider([
{ name: 'uniswap', endpoint: WEBHOOK_URL, headers: {}, hash: '0xuni', supportedVersions: [ProtocolVersion.V2] },
{
name: 'uniswap',
endpoint: WEBHOOK_URL,
headers: {},
hash: '0xuni',
supportedVersions: [ProtocolVersion.V1, ProtocolVersion.V2],
},
{ name: '1inch', endpoint: WEBHOOK_URL_ONEINCH, headers: {}, hash: '0x1inch' },
{
name: 'searcher',
Expand All @@ -280,6 +304,12 @@ describe('WebhookQuoter tests', () => {
hash: '0xsearcher',
supportedVersions: [ProtocolVersion.V1, ProtocolVersion.V2],
},
{
name: 'foo',
endpoint: WEBHOOK_URL_FOO,
headers: {},
hash: '0xfoo',
},
]);
const webhookQuoter = new WebhookQuoter(
logger,
Expand Down Expand Up @@ -322,6 +352,11 @@ describe('WebhookQuoter tests', () => {
headers: {},
timeout: 500,
});
// empty supportedVersions defaults to v2 only
expect(mockedAxios.post).not.toBeCalledWith(WEBHOOK_URL_FOO, request.toCleanJSON(), {
headers: {},
timeout: 500,
});
});

it('v2 quote request only sent to fillers supporting v2', async () => {
Expand Down Expand Up @@ -356,9 +391,9 @@ describe('WebhookQuoter tests', () => {
timeout: 500,
}
);
// empty config defaults to v1 only
expect(mockedAxios.post).not.toBeCalledWith(
WEBHOOK_URL_ONEINCH,
// empty config defaults to v2 only
expect(mockedAxios.post).toBeCalledWith(
WEBHOOK_URL_FOO,
{ quoteId: expect.any(String), ...request.toCleanJSON() },
{
headers: {},
Expand Down Expand Up @@ -446,6 +481,7 @@ describe('WebhookQuoter tests', () => {
emptyMockComplianceProvider,
repository
);
const request = makeQuoteRequest({ tokenInChainId: 1, tokenOutChainId: 1, protocol: ProtocolVersion.V2 });
const quote = {
amountOut: ethers.utils.parseEther('2').toString(),
tokenIn: request.tokenIn,
Expand Down Expand Up @@ -480,6 +516,7 @@ describe('WebhookQuoter tests', () => {
});

it('Skips if chainId not configured', async () => {
const request = makeQuoteRequest({ protocol: ProtocolVersion.V2 });
const provider = new MockWebhookConfigurationProvider([
{ name: 'uniswap', endpoint: WEBHOOK_URL, headers: {}, chainIds: [4, 5, 6], hash: '0xuni' },
]);
Expand Down

0 comments on commit 0ac3b86

Please sign in to comment.