From 045483f93255ccef2583be61777d06aaeaca9cfc Mon Sep 17 00:00:00 2001 From: Shuhui Luo <107524008+shuhuiluo@users.noreply.github.com> Date: Thu, 24 Oct 2024 00:27:29 -0400 Subject: [PATCH] refactor(v4-sdk): simplify `midPrice` in `Route` entity Simplify the `midPrice` calculation by removing complex logic and utilizing `priceOf` method directly. This change improves code readability and maintainability, reducing potential points of failure. --- sdks/v4-sdk/src/entities/route.ts | 25 +++---------------------- 1 file changed, 3 insertions(+), 22 deletions(-) diff --git a/sdks/v4-sdk/src/entities/route.ts b/sdks/v4-sdk/src/entities/route.ts index 7e4156e2b..e5ce73679 100644 --- a/sdks/v4-sdk/src/entities/route.ts +++ b/sdks/v4-sdk/src/entities/route.ts @@ -65,28 +65,9 @@ export class Route { public get midPrice(): Price { if (this._midPrice !== null) return this._midPrice - const price = this.pools.slice(1).reduce( - ({ nextInput, price }, pool) => { - return nextInput.equals(pool.currency0) - ? { - nextInput: pool.currency1, - price: price.multiply(pool.currency0Price), - } - : { - nextInput: pool.currency0, - price: price.multiply(pool.currency1Price), - } - }, - this.pools[0].currency0.equals(this.input) - ? { - nextInput: this.pools[0].currency1, - price: this.pools[0].currency0Price, - } - : { - nextInput: this.pools[0].currency0, - price: this.pools[0].currency1Price, - } - ).price + const price = this.pools.slice(1).reduce((price, pool) => { + return price.multiply(pool.priceOf(price.quoteCurrency)) + }, this.pools[0].priceOf(this.pathInput)) return (this._midPrice = new Price(this.input, this.output, price.denominator, price.numerator)) }