Skip to content

Commit

Permalink
Create a routeId in CachedRoute (Uniswap#376)
Browse files Browse the repository at this point in the history
* Create a routeId in CachedRoute

* review feedback
  • Loading branch information
mikeki authored Aug 28, 2023
1 parent ff2ee09 commit f3a6dea
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 2 deletions.
27 changes: 27 additions & 0 deletions src/providers/caching/route/model/cached-route.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Protocol } from '@uniswap/router-sdk';
import { Token } from '@uniswap/sdk-core';
import { Pool } from '@uniswap/v3-sdk';

import { MixedRoute, V2Route, V3Route } from '../../../../routers';

Expand All @@ -17,6 +18,9 @@ interface CachedRouteParams<Route extends V3Route | V2Route | MixedRoute> {
export class CachedRoute<Route extends V3Route | V2Route | MixedRoute> {
public readonly route: Route;
public readonly percent: number;
// Hashing function copying the same implementation as Java's `hashCode`
// Sourced from: https://gist.github.com/hyamamoto/fd435505d29ebfa3d9716fd2be8d42f0?permalink_comment_id=4613539#gistcomment-4613539
private hashCode = (str: string) => [...str].reduce((s, c) => Math.imul(31, s) + c.charCodeAt(0) | 0, 0);

/**
* @param route
Expand All @@ -38,4 +42,27 @@ export class CachedRoute<Route extends V3Route | V2Route | MixedRoute> {
public get tokenOut(): Token {
return this.route.output;
}

public get routePath(): string {
if (this.protocol == Protocol.V3) {
const route = this.route as V3Route;
return route.pools.map(pool => `[V3]${pool.token0.address}/${pool.token1.address}/${pool.fee}`).join('->');
} else if (this.protocol == Protocol.V2) {
const route = this.route as V2Route;
return route.pairs.map(pair => `[V2]${pair.token0.address}/${pair.token1.address}`).join('->');
} else {
const route = this.route as MixedRoute;
return route.pools.map(pool => {
if (pool instanceof Pool) {
return `[V3]${pool.token0.address}/${pool.token1.address}/${pool.fee}`;
} else {
return `[V2]${pool.token0.address}/${pool.token1.address}`;
}
}).join('->');
}
}

public get routeId(): number {
return this.hashCode(this.routePath);
}
}
54 changes: 52 additions & 2 deletions test/unit/providers/caching/route/model/cached-route.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Protocol } from '@uniswap/router-sdk';
import { DAI_MAINNET, MixedRoute, USDC_MAINNET, V2Route, V3Route } from '../../../../../../build/main';
import { CachedRoute } from '../../../../../../src';
import { USDC_DAI, USDC_DAI_MEDIUM } from '../../../../../test-util/mock-data';
import { USDC_DAI, USDC_DAI_MEDIUM, WETH_DAI } from '../../../../../test-util/mock-data';

describe('CachedRoute', () => {
it('creates an instance given a route object and percent', () => {
Expand All @@ -27,10 +27,60 @@ describe('CachedRoute', () => {
});

it('is correctly MIXED when using MixedRoute', () => {
const route = new MixedRoute([USDC_DAI_MEDIUM], USDC_MAINNET, DAI_MAINNET);
const route = new MixedRoute([USDC_DAI_MEDIUM, WETH_DAI], USDC_MAINNET, DAI_MAINNET);
const cachedRoute = new CachedRoute({ route: route, percent: 100 });

expect(cachedRoute.protocol).toEqual(Protocol.MIXED);
});
});

describe('#routePath', () => {
it('is correctly returned when using V3Route', () => {
const route = new V3Route([USDC_DAI_MEDIUM], USDC_MAINNET, DAI_MAINNET);
const cachedRoute = new CachedRoute({ route: route, percent: 100 });

expect(cachedRoute.routePath)
.toEqual('[V3]0x6B175474E89094C44Da98b954EedeAC495271d0F/0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48/3000');
});

it('is correctly returned when using V2Route', () => {
const route = new V2Route([USDC_DAI], USDC_MAINNET, DAI_MAINNET);
const cachedRoute = new CachedRoute({ route: route, percent: 100 });

expect(cachedRoute.routePath)
.toEqual('[V2]0x6B175474E89094C44Da98b954EedeAC495271d0F/0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48');
});

it('is correctly returned when using MixedRoute', () => {
const route = new MixedRoute([USDC_DAI_MEDIUM, WETH_DAI], USDC_MAINNET, DAI_MAINNET);
const cachedRoute = new CachedRoute({ route: route, percent: 100 });

expect(cachedRoute.routePath)
.toEqual(
'[V3]0x6B175474E89094C44Da98b954EedeAC495271d0F/0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48/3000->[V2]0x6B175474E89094C44Da98b954EedeAC495271d0F/0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2');
});
});

describe('#routeId', () => {
it('is correctly returned when using V3Route', () => {
const route = new V3Route([USDC_DAI_MEDIUM], USDC_MAINNET, DAI_MAINNET);
const cachedRoute = new CachedRoute({ route: route, percent: 100 });

expect(cachedRoute.routeId).toEqual(610157808);
});

it('is correctly returned when using V2Route', () => {
const route = new V2Route([USDC_DAI], USDC_MAINNET, DAI_MAINNET);
const cachedRoute = new CachedRoute({ route: route, percent: 100 });

expect(cachedRoute.routeId).toEqual(783252763);
});

it('is correctly returned when using MixedRoute', () => {
const route = new MixedRoute([USDC_DAI_MEDIUM, WETH_DAI], USDC_MAINNET, DAI_MAINNET);
const cachedRoute = new CachedRoute({ route: route, percent: 100 });

expect(cachedRoute.routeId).toEqual(-882458629);
});
});
});

0 comments on commit f3a6dea

Please sign in to comment.