Skip to content

Commit eb5d246

Browse files
authored
Merge pull request #1500 from mars-protocol/twap-oracle-fix
TWAP price fetching
2 parents 2a74524 + a7500b9 commit eb5d246

File tree

1 file changed

+45
-17
lines changed

1 file changed

+45
-17
lines changed

src/api/prices/getOraclePrices.ts

Lines changed: 45 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -33,26 +33,54 @@ export default async function getOraclePrices(
3333
priceResults = await iterateContractQuery(osmosisOracleQueryClient.prices)
3434
} else {
3535
const neutronOracleQueryClient = await getOracleQueryClientNeutron(chainConfig)
36-
const denoms = assets.map((asset) => asset.denom)
3736

38-
const denomChunks = chunkArray(denoms, 3)
37+
// Filter assets: separate those with 'share' in denom from others
38+
const shareAssets = assets.filter((asset) => asset.denom.endsWith('share'))
39+
const nonShareAssets = assets.filter((asset) => !asset.denom.endsWith('share'))
3940

40-
const chunkResults = await Promise.all(
41-
denomChunks.map(async (chunk) => {
42-
return await neutronOracleQueryClient.pricesByDenoms({ denoms: chunk, kind: 'default' })
43-
}),
44-
)
41+
let batchPriceResults: PriceResponse[] = []
42+
let individualPriceResults: PriceResponse[] = []
43+
44+
// Handle non-share assets with batching
45+
if (nonShareAssets.length > 0) {
46+
const denoms = nonShareAssets.map((asset) => asset.denom)
47+
const denomChunks = chunkArray(denoms, 3)
48+
49+
const chunkResults = await Promise.all(
50+
denomChunks.map(async (chunk) => {
51+
return await neutronOracleQueryClient.pricesByDenoms({ denoms: chunk, kind: 'default' })
52+
}),
53+
)
54+
55+
batchPriceResults = chunkResults.reduce((acc: PriceResponse[], result) => {
56+
if (result && typeof result === 'object') {
57+
const priceEntries = Object.entries(result).map(([denom, price]) => ({
58+
denom,
59+
price: String(price),
60+
})) as PriceResponse[]
61+
return [...acc, ...priceEntries]
62+
}
63+
return acc
64+
}, [])
65+
}
66+
67+
// Handle share assets individually to prevent twap errors breaking the app
68+
if (shareAssets.length > 0) {
69+
const shareResults = await Promise.all(
70+
shareAssets.map(async (asset) => {
71+
try {
72+
return await neutronOracleQueryClient.price({ denom: asset.denom })
73+
} catch (error) {
74+
console.warn(`Failed to fetch price for share asset ${asset.denom}:`, error)
75+
return { denom: asset.denom, price: '0' } as PriceResponse
76+
}
77+
}),
78+
)
79+
individualPriceResults = shareResults as PriceResponse[]
80+
}
4581

46-
priceResults = chunkResults.reduce((acc: PriceResponse[], result) => {
47-
if (result && typeof result === 'object') {
48-
const priceEntries = Object.entries(result).map(([denom, price]) => ({
49-
denom,
50-
price: String(price),
51-
})) as PriceResponse[]
52-
return [...acc, ...priceEntries]
53-
}
54-
return acc
55-
}, [])
82+
// Combine all price results
83+
priceResults = [...batchPriceResults, ...individualPriceResults]
5684
}
5785

5886
return assets.map((asset) => {

0 commit comments

Comments
 (0)