Skip to content

Commit e031034

Browse files
committed
add heights to sonic price routing and utilize chainlink OS feeds
1 parent a9eb743 commit e031034

File tree

1 file changed

+42
-12
lines changed

1 file changed

+42
-12
lines changed

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

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,46 @@ const createChainlinkPriceFeed = (address: string, decimals: bigint, from: numbe
1919
}
2020
}
2121

22-
const chainlinkPriceFeeds: Record<string, (ctx: Context, height: number) => Promise<bigint>> = {
23-
ETH_USD: createChainlinkPriceFeed('0x824364077993847f71293B24ccA8567c00c2de11', 8n, 3394229),
24-
S_ETH: async (ctx, height) => {
25-
const sUsd = await chainlinkPriceFeeds.S_USD(ctx, height)
26-
const ethUsd = await chainlinkPriceFeeds.ETH_USD(ctx, height)
27-
return (sUsd * PRECISION) / ethUsd
28-
},
29-
S_USD: createChainlinkPriceFeed('0xc76dfb89ff298145b417d221b2c747d84952e01d', 8n, 4189824),
30-
}
22+
const chainlinkPriceFeeds: Record<string, { height: number; get: (ctx: Context, height: number) => Promise<bigint> }> =
23+
{
24+
ETH_USD: {
25+
height: 3394366,
26+
get: createChainlinkPriceFeed('0x824364077993847f71293B24ccA8567c00c2de11', 8n, 3394229),
27+
},
28+
S_ETH: {
29+
height: 4189824,
30+
get: async (ctx, height) => {
31+
const sUsd = await chainlinkPriceFeeds.S_USD.get(ctx, height)
32+
const ethUsd = await chainlinkPriceFeeds.ETH_USD.get(ctx, height)
33+
return (sUsd * PRECISION) / ethUsd
34+
},
35+
},
36+
S_USD: {
37+
height: 4189824,
38+
get: createChainlinkPriceFeed('0xc76dfb89ff298145b417d221b2c747d84952e01d', 8n, 4189824),
39+
},
40+
OS_S: {
41+
height: 21623950,
42+
get: createChainlinkPriceFeed('0x30caC44b395eB969C9CA0d44dF39e6E0aE8f8D94', 18n, 4189824),
43+
},
44+
OS_ETH: {
45+
height: 21623950,
46+
get: async (ctx, height) => {
47+
const osS = await chainlinkPriceFeeds.OS_S.get(ctx, height)
48+
const sEth = await chainlinkPriceFeeds.S_ETH.get(ctx, height)
49+
return (osS * sEth) / PRECISION
50+
},
51+
},
52+
OS_USD: {
53+
height: 21623950,
54+
get: async (ctx, height) => {
55+
const osS = await chainlinkPriceFeeds.OS_S.get(ctx, height)
56+
const sEth = await chainlinkPriceFeeds.S_ETH.get(ctx, height)
57+
const ethUsd = await chainlinkPriceFeeds.ETH_USD.get(ctx, height)
58+
return (((osS * sEth) / PRECISION) * ethUsd) / PRECISION
59+
},
60+
},
61+
}
3162

3263
export const sonicCurrenciesByAddress = invertMap(sonicAddresses.tokens)
3364

@@ -41,8 +72,8 @@ export const getSonicPrice = async (ctx: Context, height: number, base: SonicCur
4172
quote = translateSonicSymbol(quote)
4273
if (base === quote) return [1_000_000_000_000_000_000n, 18] as const
4374
const feed = chainlinkPriceFeeds[`${base}_${quote}`]
44-
if (feed) {
45-
return [await feed(ctx, height), 18] as const
75+
if (feed && feed.height <= height) {
76+
return [await feed.get(ctx, height), 18] as const
4677
}
4778
return undefined
4879
} catch (err) {
@@ -55,7 +86,6 @@ export const translateSonicSymbol = (symbol: SonicCurrency): SonicCurrencySymbol
5586
symbol = sonicCurrenciesByAddress[symbol as SonicCurrencyAddress] || symbol
5687
if (symbol === 'USDC') return 'USD'
5788
if (symbol === 'wS') return 'S'
58-
if (symbol === 'OS') return 'S'
5989
if (symbol === 'WETH') return 'ETH'
6090
return symbol
6191
}

0 commit comments

Comments
 (0)