Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 52 additions & 15 deletions src/mappings/markets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,18 @@ export function createMarket(marketAddress: string): Market {
return market
}

// Only to be used after block 10678764, since it's aimed to fix the change to USD based price oracle.
function getETHinUSD(blockNumber: i32): BigDecimal {
let comptroller = Comptroller.load('1')
let oracleAddress = comptroller.priceOracle as Address
let oracle = PriceOracle2.bind(oracleAddress)
let ethPriceInUSD = oracle
.getUnderlyingPrice(Address.fromString(cETHAddress))
.toBigDecimal()
.div(mantissaFactorBD)
return ethPriceInUSD
}

export function updateMarket(
marketAddress: Address,
blockNumber: i32,
Expand All @@ -178,26 +190,51 @@ export function updateMarket(
if (market.accrualBlockNumber != blockNumber) {
let contractAddress = Address.fromString(market.id)
let contract = CToken.bind(contractAddress)
let usdPriceInEth = getUSDCpriceETH(blockNumber)

// if cETH, we only update USD price
if (market.id == cETHAddress) {
market.underlyingPriceUSD = market.underlyingPrice
.div(usdPriceInEth)
.truncate(market.underlyingDecimals)
// After block 10678764 price is calculated based on USD instead of ETH
if (blockNumber > 10678764) {
let ethPriceInUSD = getETHinUSD(blockNumber)

// if cETH, we only update USD price
if (market.id == cETHAddress) {
market.underlyingPriceUSD = ethPriceInUSD.truncate(market.underlyingDecimals)
} else {
let tokenPriceUSD = getTokenPrice(
blockNumber,
contractAddress,
market.underlyingAddress as Address,
market.underlyingDecimals,
)
market.underlyingPrice = tokenPriceUSD
.div(ethPriceInUSD)
.truncate(market.underlyingDecimals)
// if USDC, we only update ETH price
if (market.id != cUSDCAddress) {
market.underlyingPriceUSD = tokenPriceUSD.truncate(market.underlyingDecimals)
}
}
} else {
let tokenPriceEth = getTokenPrice(
blockNumber,
contractAddress,
market.underlyingAddress as Address,
market.underlyingDecimals,
)
market.underlyingPrice = tokenPriceEth.truncate(market.underlyingDecimals)
// if USDC, we only update ETH price
if (market.id != cUSDCAddress) {
let usdPriceInEth = getUSDCpriceETH(blockNumber)

// if cETH, we only update USD price
if (market.id == cETHAddress) {
market.underlyingPriceUSD = market.underlyingPrice
.div(usdPriceInEth)
.truncate(market.underlyingDecimals)
} else {
let tokenPriceEth = getTokenPrice(
blockNumber,
contractAddress,
market.underlyingAddress as Address,
market.underlyingDecimals,
)
market.underlyingPrice = tokenPriceEth.truncate(market.underlyingDecimals)
// if USDC, we only update ETH price
if (market.id != cUSDCAddress) {
market.underlyingPriceUSD = market.underlyingPrice
.div(usdPriceInEth)
.truncate(market.underlyingDecimals)
}
}
}

Expand Down