WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Commit 36e6e1c

Browse files
authored
Merge pull request #66 from OriginProtocol/fix/prices
fix: normalize exchange rates to target decimals in price routing
2 parents 342437e + 18273c1 commit 36e6e1c

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

src/shared/post-processors/exchange-rates/price-routing-mainnet.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,16 @@ export const derived = <Base extends MainnetCurrencySymbol, Quote extends Mainne
223223
async (ctx: Context, block: Block['header']) => {
224224
const baseExponent = 10n ** BigInt(decimals)
225225
const rates = await Promise.all(connections.map(({ base, quote }) => getMainnetPrice(ctx, block, base, quote)))
226-
return rates.reduce((acc, [rate]) => (acc * rate) / baseExponent, baseExponent)
226+
// Normalize all rates to target decimals before multiplying
227+
const normalizedRates = rates.map(([rate, rateDecimals]) => {
228+
if (rateDecimals === decimals) return rate
229+
if (rateDecimals < decimals) {
230+
return rate * 10n ** BigInt(decimals - rateDecimals)
231+
} else {
232+
return rate / 10n ** BigInt(rateDecimals - decimals)
233+
}
234+
})
235+
return normalizedRates.reduce((acc, rate) => (acc * rate) / baseExponent, baseExponent)
227236
},
228237
decimals,
229238
)
@@ -277,7 +286,8 @@ export const priceMap: Partial<
277286
{ base: 'USD', quote: 'ETH' },
278287
],
279288
18,
280-
), ...derived(
289+
),
290+
...derived(
281291
'USDe',
282292
'ETH',
283293
[

0 commit comments

Comments
 (0)