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: bump sor to 4.7.0 - feat: SOR level implementation to support 1) expanding mixed quoter v1 to L2s 2) support migrate mixed quoter v1 to mixed quoter v2 across chains #746

Merged
merged 5 commits into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@uniswap/smart-order-router",
"version": "4.6.2",
"version": "4.7.0",
"description": "Uniswap Smart Order Router",
"main": "build/main/index.js",
"typings": "build/main/index.d.ts",
Expand Down
107 changes: 84 additions & 23 deletions src/providers/on-chain-quote-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -349,23 +349,35 @@ export class OnChainQuoteProvider implements IOnChainQuoteProvider {
},
protected batchParams: (
optimisticCachedRoutes: boolean,
useMixedRouteQuoter: boolean
) => BatchParams = (_optimisticCachedRoutes, _useMixedRouteQuoter) => {
protocol: Protocol
) => BatchParams = (_optimisticCachedRoutes, _protocol) => {
return {
multicallChunk: 150,
gasLimitPerCall: 1_000_000,
quoteMinSuccessRate: 0.2,
};
},
protected gasErrorFailureOverride: FailureOverrides = {
gasLimitOverride: 1_500_000,
multicallChunk: 100,
protected gasErrorFailureOverride: (
protocol: Protocol
) => FailureOverrides = (_protocol: Protocol) => {
return {
gasLimitOverride: 1_500_000,
multicallChunk: 100,
};
},
// successRateFailureOverrides and blockNumberConfig are not always override in alpha-router.
// So we will extract out below default values into constants.
// In alpha-router default case, we will also define the constants with same values as below.
protected successRateFailureOverrides: FailureOverrides = DEFAULT_SUCCESS_RATE_FAILURE_OVERRIDES,
protected blockNumberConfig: BlockNumberConfig = DEFAULT_BLOCK_NUMBER_CONFIGS,
protected successRateFailureOverrides: (
protocol: Protocol
) => FailureOverrides = (_protocol: Protocol) => {
return DEFAULT_SUCCESS_RATE_FAILURE_OVERRIDES;
},
protected blockNumberConfig: (protocol: Protocol) => BlockNumberConfig = (
_protocol: Protocol
) => {
return DEFAULT_BLOCK_NUMBER_CONFIGS;
},
protected quoterAddressOverride?: (
useMixedRouteQuoter: boolean,
mixedRouteContainsV4Pool: boolean,
Expand All @@ -374,11 +386,21 @@ export class OnChainQuoteProvider implements IOnChainQuoteProvider {
protected metricsPrefix: (
chainId: ChainId,
useMixedRouteQuoter: boolean,
mixedRouteContainsV4Pool: boolean,
protocol: Protocol,
optimisticCachedRoutes: boolean
) => string = (chainId, useMixedRouteQuoter, optimisticCachedRoutes) =>
) => string = (
chainId,
useMixedRouteQuoter,
mixedRouteContainsV4Pool,
protocol,
optimisticCachedRoutes
) =>
useMixedRouteQuoter
? `ChainId_${chainId}_MixedQuoter_OptimisticCachedRoutes${optimisticCachedRoutes}_`
: `ChainId_${chainId}_V3Quoter_OptimisticCachedRoutes${optimisticCachedRoutes}_`
? `ChainId_${chainId}_${protocol}RouteQuoter${
mixedRouteContainsV4Pool ? 'V2' : 'V1'
}_OptimisticCachedRoutes${optimisticCachedRoutes}_`
: `ChainId_${chainId}_${protocol}Quoter_OptimisticCachedRoutes${optimisticCachedRoutes}_`
) {}

private getQuoterAddress(
Expand Down Expand Up @@ -611,6 +633,12 @@ export class OnChainQuoteProvider implements IOnChainQuoteProvider {
(route as MixedRoute).pools.some((pool) => pool instanceof V4Pool)
)
: false;
const protocol = useMixedRouteQuoter
? Protocol.MIXED
jsy1218 marked this conversation as resolved.
Show resolved Hide resolved
: useV4RouteQuoter
? Protocol.V4
: Protocol.V3;

const optimisticCachedRoutes =
_providerConfig?.optimisticCachedRoutes ?? false;

Expand All @@ -619,13 +647,13 @@ export class OnChainQuoteProvider implements IOnChainQuoteProvider {

let multicallChunk = this.batchParams(
optimisticCachedRoutes,
useMixedRouteQuoter
protocol
).multicallChunk;
let gasLimitOverride = this.batchParams(
optimisticCachedRoutes,
useMixedRouteQuoter
protocol
).gasLimitPerCall;
const { baseBlockOffset, rollback } = this.blockNumberConfig;
const { baseBlockOffset, rollback } = this.blockNumberConfig(protocol);

// Apply the base block offset if provided
const originalBlockNumber = await this.provider.getBlockNumber();
Expand Down Expand Up @@ -707,6 +735,8 @@ export class OnChainQuoteProvider implements IOnChainQuoteProvider {
`${this.metricsPrefix(
this.chainId,
useMixedRouteQuoter,
mixedRouteContainsV4Pool,
protocol,
optimisticCachedRoutes
)}QuoteBatchSize`,
inputs.length,
Expand All @@ -716,6 +746,8 @@ export class OnChainQuoteProvider implements IOnChainQuoteProvider {
`${this.metricsPrefix(
this.chainId,
useMixedRouteQuoter,
mixedRouteContainsV4Pool,
protocol,
optimisticCachedRoutes
)}QuoteBatchSize_${ID_TO_NETWORK_NAME(this.chainId)}`,
inputs.length,
Expand Down Expand Up @@ -771,11 +803,6 @@ export class OnChainQuoteProvider implements IOnChainQuoteProvider {
try {
totalCallsMade = totalCallsMade + 1;

const protocol = useMixedRouteQuoter
? Protocol.MIXED
: useV4RouteQuoter
? Protocol.V4
: Protocol.V3;
const results = await this.consolidateResults(
protocol,
useMixedRouteQuoter,
Expand All @@ -790,6 +817,8 @@ export class OnChainQuoteProvider implements IOnChainQuoteProvider {
results.results,
haveRetriedForSuccessRate,
useMixedRouteQuoter,
mixedRouteContainsV4Pool,
protocol,
optimisticCachedRoutes
);

Expand Down Expand Up @@ -896,6 +925,8 @@ export class OnChainQuoteProvider implements IOnChainQuoteProvider {
`${this.metricsPrefix(
this.chainId,
useMixedRouteQuoter,
mixedRouteContainsV4Pool,
protocol,
optimisticCachedRoutes
)}QuoteBlockConflictErrorRetry`,
1,
Expand All @@ -911,6 +942,8 @@ export class OnChainQuoteProvider implements IOnChainQuoteProvider {
`${this.metricsPrefix(
this.chainId,
useMixedRouteQuoter,
mixedRouteContainsV4Pool,
protocol,
optimisticCachedRoutes
)}QuoteBlockHeaderNotFoundRetry`,
1,
Expand Down Expand Up @@ -955,6 +988,8 @@ export class OnChainQuoteProvider implements IOnChainQuoteProvider {
`${this.metricsPrefix(
this.chainId,
useMixedRouteQuoter,
mixedRouteContainsV4Pool,
protocol,
optimisticCachedRoutes
)}QuoteTimeoutRetry`,
1,
Expand All @@ -968,22 +1003,28 @@ export class OnChainQuoteProvider implements IOnChainQuoteProvider {
`${this.metricsPrefix(
this.chainId,
useMixedRouteQuoter,
mixedRouteContainsV4Pool,
protocol,
optimisticCachedRoutes
)}QuoteOutOfGasExceptionRetry`,
1,
MetricLoggerUnit.Count
);
haveRetriedForOutOfGas = true;
}
gasLimitOverride = this.gasErrorFailureOverride.gasLimitOverride;
multicallChunk = this.gasErrorFailureOverride.multicallChunk;
gasLimitOverride =
this.gasErrorFailureOverride(protocol).gasLimitOverride;
multicallChunk =
this.gasErrorFailureOverride(protocol).multicallChunk;
retryAll = true;
} else if (error instanceof SuccessRateError) {
if (!haveRetriedForSuccessRate) {
metric.putMetric(
`${this.metricsPrefix(
this.chainId,
useMixedRouteQuoter,
mixedRouteContainsV4Pool,
protocol,
optimisticCachedRoutes
)}QuoteSuccessRateRetry`,
1,
Expand All @@ -993,9 +1034,9 @@ export class OnChainQuoteProvider implements IOnChainQuoteProvider {

// Low success rate can indicate too little gas given to each call.
gasLimitOverride =
this.successRateFailureOverrides.gasLimitOverride;
this.successRateFailureOverrides(protocol).gasLimitOverride;
multicallChunk =
this.successRateFailureOverrides.multicallChunk;
this.successRateFailureOverrides(protocol).multicallChunk;
retryAll = true;
}
} else {
Expand All @@ -1004,6 +1045,8 @@ export class OnChainQuoteProvider implements IOnChainQuoteProvider {
`${this.metricsPrefix(
this.chainId,
useMixedRouteQuoter,
mixedRouteContainsV4Pool,
protocol,
optimisticCachedRoutes
)}QuoteUnknownReasonRetry`,
1,
Expand Down Expand Up @@ -1099,6 +1142,8 @@ export class OnChainQuoteProvider implements IOnChainQuoteProvider {
`${this.metricsPrefix(
this.chainId,
useMixedRouteQuoter,
mixedRouteContainsV4Pool,
protocol,
optimisticCachedRoutes
)}QuoteLatency`,
endTime - startTime,
Expand All @@ -1109,6 +1154,8 @@ export class OnChainQuoteProvider implements IOnChainQuoteProvider {
`${this.metricsPrefix(
this.chainId,
useMixedRouteQuoter,
mixedRouteContainsV4Pool,
protocol,
optimisticCachedRoutes
)}QuoteApproxGasUsedPerSuccessfulCall`,
approxGasUsedPerSuccessCall,
Expand All @@ -1119,6 +1166,8 @@ export class OnChainQuoteProvider implements IOnChainQuoteProvider {
`${this.metricsPrefix(
this.chainId,
useMixedRouteQuoter,
mixedRouteContainsV4Pool,
protocol,
optimisticCachedRoutes
)}QuoteNumRetryLoops`,
finalAttemptNumber - 1,
Expand All @@ -1129,6 +1178,8 @@ export class OnChainQuoteProvider implements IOnChainQuoteProvider {
`${this.metricsPrefix(
this.chainId,
useMixedRouteQuoter,
mixedRouteContainsV4Pool,
protocol,
optimisticCachedRoutes
)}QuoteTotalCallsToProvider`,
totalCallsMade,
Expand All @@ -1139,6 +1190,8 @@ export class OnChainQuoteProvider implements IOnChainQuoteProvider {
`${this.metricsPrefix(
this.chainId,
useMixedRouteQuoter,
mixedRouteContainsV4Pool,
protocol,
optimisticCachedRoutes
)}QuoteExpectedCallsToProvider`,
expectedCallsMade,
Expand All @@ -1149,6 +1202,8 @@ export class OnChainQuoteProvider implements IOnChainQuoteProvider {
`${this.metricsPrefix(
this.chainId,
useMixedRouteQuoter,
mixedRouteContainsV4Pool,
protocol,
optimisticCachedRoutes
)}QuoteNumRetriedCalls`,
totalCallsMade - expectedCallsMade,
Expand Down Expand Up @@ -1342,6 +1397,8 @@ export class OnChainQuoteProvider implements IOnChainQuoteProvider {
allResults: Result<[BigNumber, BigNumber[], number[], BigNumber]>[],
haveRetriedForSuccessRate: boolean,
useMixedRouteQuoter: boolean,
mixedRouteContainsV4Pool: boolean,
protocol: Protocol,
optimisticCachedRoutes: boolean
): void | SuccessRateError {
const numResults = allResults.length;
Expand All @@ -1353,7 +1410,7 @@ export class OnChainQuoteProvider implements IOnChainQuoteProvider {

const { quoteMinSuccessRate } = this.batchParams(
optimisticCachedRoutes,
useMixedRouteQuoter
protocol
);
if (successRate < quoteMinSuccessRate) {
if (haveRetriedForSuccessRate) {
Expand All @@ -1364,6 +1421,8 @@ export class OnChainQuoteProvider implements IOnChainQuoteProvider {
`${this.metricsPrefix(
this.chainId,
useMixedRouteQuoter,
mixedRouteContainsV4Pool,
protocol,
optimisticCachedRoutes
)}QuoteRetriedSuccessRateLow`,
successRate,
Expand All @@ -1377,6 +1436,8 @@ export class OnChainQuoteProvider implements IOnChainQuoteProvider {
`${this.metricsPrefix(
this.chainId,
useMixedRouteQuoter,
mixedRouteContainsV4Pool,
protocol,
optimisticCachedRoutes
)}QuoteSuccessRateLow`,
successRate,
Expand Down
Loading
Loading