From b4857eeb48513908568747ac2f27afd0a53ebe59 Mon Sep 17 00:00:00 2001 From: Shuhui Luo <107524008+shuhuiluo@users.noreply.github.com> Date: Thu, 24 Oct 2024 02:50:45 -0400 Subject: [PATCH] refactor(v3-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/v3-sdk/src/entities/route.ts | 25 +++---------------------- 1 file changed, 3 insertions(+), 22 deletions(-) diff --git a/sdks/v3-sdk/src/entities/route.ts b/sdks/v3-sdk/src/entities/route.ts index 7b7d6ff8e..4bd490328 100644 --- a/sdks/v3-sdk/src/entities/route.ts +++ b/sdks/v3-sdk/src/entities/route.ts @@ -61,28 +61,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.token0) - ? { - nextInput: pool.token1, - price: price.multiply(pool.token0Price), - } - : { - nextInput: pool.token0, - price: price.multiply(pool.token1Price), - } - }, - this.pools[0].token0.equals(this.input.wrapped) - ? { - nextInput: this.pools[0].token1, - price: this.pools[0].token0Price, - } - : { - nextInput: this.pools[0].token0, - price: this.pools[0].token1Price, - } - ).price + const price = this.pools.slice(1).reduce((price, pool) => { + return price.multiply(pool.priceOf(price.quoteCurrency)) + }, this.pools[0].priceOf(this.input.wrapped)) return (this._midPrice = new Price(this.input, this.output, price.denominator, price.numerator)) }