Skip to content

Commit

Permalink
refactor(v4-sdk): simplify midPrice in Route entity (#180)
Browse files Browse the repository at this point in the history
Co-authored-by: Emily Williams <[email protected]>
  • Loading branch information
shuhuiluo and ewilz authored Nov 27, 2024
1 parent a3fce42 commit b24c41f
Showing 1 changed file with 3 additions and 22 deletions.
25 changes: 3 additions & 22 deletions sdks/v4-sdk/src/entities/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,28 +65,9 @@ export class Route<TInput extends Currency, TOutput extends Currency> {
public get midPrice(): Price<TInput, TOutput> {
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))
}
Expand Down

0 comments on commit b24c41f

Please sign in to comment.