Skip to content

Commit

Permalink
Fix issue maintaining auction price (#68)
Browse files Browse the repository at this point in the history
* reworked updateLiquidationAuction

* update remaining quantities even if auction was settled and they are 0
  • Loading branch information
EdNoepel authored Sep 18, 2023
1 parent 615d00b commit 35c5de0
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 54 deletions.
44 changes: 21 additions & 23 deletions src/mappings/erc-20-pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,15 +187,16 @@ export function handleBucketBankruptcy(event: BucketBankruptcyEvent): void {
}

export function handleBucketTake(event: BucketTakeEvent): void {
const bucketTakeId = event.transaction.hash.concatI32(event.logIndex.toI32());
const bucketTake = BucketTake.load(bucketTakeId)!
bucketTake.borrower = event.params.borrower
bucketTake.taker = event.transaction.from
bucketTake.index = event.params.index.toU32()
bucketTake.amount = wadToDecimal(event.params.amount)
bucketTake.collateral = wadToDecimal(event.params.collateral)
bucketTake.bondChange = wadToDecimal(event.params.bondChange)
bucketTake.isReward = event.params.isReward
const bucketTakeId = event.transaction.hash.concatI32(event.logIndex.toI32());
const bucketTake = BucketTake.load(bucketTakeId)!
bucketTake.borrower = event.params.borrower
bucketTake.taker = event.transaction.from
bucketTake.index = event.params.index.toU32()
bucketTake.amount = wadToDecimal(event.params.amount)
bucketTake.collateral = wadToDecimal(event.params.collateral)
bucketTake.auctionPrice = bucketTake.amount.div(bucketTake.collateral)
bucketTake.bondChange = wadToDecimal(event.params.bondChange)
bucketTake.isReward = event.params.isReward

bucketTake.blockNumber = event.block.number
bucketTake.blockTimestamp = event.block.timestamp
Expand Down Expand Up @@ -229,11 +230,9 @@ export function handleBucketTake(event: BucketTakeEvent): void {
// update liquidation auction state
const auctionId = loan.liquidationAuction!
const auction = LiquidationAuction.load(auctionId)!
updateLiquidationAuction(auction, auctionInfo, auctionStatus)
updateLiquidationAuction(auction, auctionInfo, auctionStatus, bucketTake.auctionPrice)
auction.bucketTakes = auction.bucketTakes.concat([bucketTake.id])

bucketTake.auctionPrice = wadToDecimal(auctionStatus.price)

// update kick and pool for the change in bond as a result of the take
const kick = Kick.load(auction.kick)!
if (bucketTake.isReward) {
Expand Down Expand Up @@ -420,7 +419,7 @@ export function handleKick(event: KickEvent): void {
// update liquidation auction state
const auctionId = getLiquidationAuctionId(pool.id, loan.id, kick.blockNumber)
const auction = loadOrCreateLiquidationAuction(pool.id, auctionId, kick, loan)
updateLiquidationAuction(auction, auctionInfo, auctionStatus, false)
updateLiquidationAuction(auction, auctionInfo, auctionStatus)

kick.kickMomp = wadToDecimal(auctionInfo.kickMomp)
kick.startingPrice = wadToDecimal(auctionStatus.price)
Expand Down Expand Up @@ -579,12 +578,13 @@ export function handleTake(event: TakeEvent): void {
const take = new Take(
event.transaction.hash.concatI32(event.logIndex.toI32())
)
take.borrower = event.params.borrower
take.taker = event.transaction.from
take.amount = wadToDecimal(event.params.amount)
take.collateral = wadToDecimal(event.params.collateral)
take.bondChange = wadToDecimal(event.params.bondChange)
take.isReward = event.params.isReward
take.borrower = event.params.borrower
take.taker = event.transaction.from
take.amount = wadToDecimal(event.params.amount)
take.collateral = wadToDecimal(event.params.collateral)
take.auctionPrice = take.amount.div(take.collateral)
take.bondChange = wadToDecimal(event.params.bondChange)
take.isReward = event.params.isReward

take.blockNumber = event.block.number
take.blockTimestamp = event.block.timestamp
Expand Down Expand Up @@ -615,11 +615,10 @@ export function handleTake(event: TakeEvent): void {
auction.takes = auction.takes.concat([take.id])
const auctionInfo = getAuctionInfoERC20Pool(take.borrower, pool)
const auctionStatus = getAuctionStatus(pool, event.params.borrower)
updateLiquidationAuction(auction, auctionInfo, auctionStatus)
updateLiquidationAuction(auction, auctionInfo, auctionStatus, take.auctionPrice)

const collateralPurchased = wadToDecimal(event.params.collateral)
pool.pledgedCollateral = pool.pledgedCollateral.minus(collateralPurchased)
take.auctionPrice = wadToDecimal(auctionStatus.price)

// update kick and pool for the change in bond as a result of the take
const kick = Kick.load(auction.kick)!
Expand Down Expand Up @@ -678,7 +677,7 @@ export function handleSettle(event: SettleEvent): void {
const auction = LiquidationAuction.load(auctionId)!
const auctionInfo = getAuctionInfoERC20Pool(settle.borrower, pool)
const auctionStatus = getAuctionStatus(pool, event.params.borrower)
updateLiquidationAuction(auction, auctionInfo, auctionStatus, false, true)
updateLiquidationAuction(auction, auctionInfo, auctionStatus)
auction.settles = auction.settles.concat([settle.id])

// update settle pointers
Expand All @@ -690,7 +689,6 @@ export function handleSettle(event: SettleEvent): void {
account.save()
auction.save()
pool.save()

settle.save()
}

Expand Down
43 changes: 21 additions & 22 deletions src/mappings/erc-721-pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,7 @@ export function handleSettle(event: SettleEvent): void {
const auction = LiquidationAuction.load(auctionId)!
const auctionInfo = getAuctionInfoERC721Pool(settle.borrower, pool)
const auctionStatus = getAuctionStatus(pool, event.params.borrower)
updateLiquidationAuction(auction, auctionInfo, auctionStatus, false, true)
updateLiquidationAuction(auction, auctionInfo, auctionStatus)
auction.settles = auction.settles.concat([settle.id])

// update settle pointers
Expand Down Expand Up @@ -584,7 +584,7 @@ export function handleKick(event: KickEvent): void {
// update liquidation auction state
const auctionId = getLiquidationAuctionId(pool.id, loan.id, kick.blockNumber)
const auction = loadOrCreateLiquidationAuction(pool.id, auctionId, kick, loan)
updateLiquidationAuction(auction, auctionInfo, auctionStatus, false)
updateLiquidationAuction(auction, auctionInfo, auctionStatus)

kick.kickMomp = wadToDecimal(auctionInfo.kickMomp)
kick.startingPrice = wadToDecimal(auctionStatus.price)
Expand All @@ -605,15 +605,16 @@ export function handleKick(event: KickEvent): void {
}

export function handleBucketTake(event: BucketTakeEvent): void {
const bucketTakeId = event.transaction.hash.concatI32(event.logIndex.toI32());
const bucketTake = BucketTake.load(bucketTakeId)!
bucketTake.borrower = event.params.borrower
bucketTake.taker = event.transaction.from
bucketTake.index = event.params.index.toU32()
bucketTake.amount = wadToDecimal(event.params.amount)
bucketTake.collateral = wadToDecimal(event.params.collateral)
bucketTake.bondChange = wadToDecimal(event.params.bondChange)
bucketTake.isReward = event.params.isReward
const bucketTakeId = event.transaction.hash.concatI32(event.logIndex.toI32());
const bucketTake = BucketTake.load(bucketTakeId)!
bucketTake.borrower = event.params.borrower
bucketTake.taker = event.transaction.from
bucketTake.index = event.params.index.toU32()
bucketTake.amount = wadToDecimal(event.params.amount)
bucketTake.collateral = wadToDecimal(event.params.collateral)
bucketTake.auctionPrice = bucketTake.amount.div(bucketTake.collateral)
bucketTake.bondChange = wadToDecimal(event.params.bondChange)
bucketTake.isReward = event.params.isReward

bucketTake.blockNumber = event.block.number
bucketTake.blockTimestamp = event.block.timestamp
Expand Down Expand Up @@ -662,11 +663,9 @@ export function handleBucketTake(event: BucketTakeEvent): void {
// update liquidation auction state
const auctionId = loan.liquidationAuction!
const auction = LiquidationAuction.load(auctionId)!
updateLiquidationAuction(auction, auctionInfo, auctionStatus)
updateLiquidationAuction(auction, auctionInfo, auctionStatus, bucketTake.auctionPrice)
auction.bucketTakes = auction.bucketTakes.concat([bucketTake.id])

bucketTake.auctionPrice = wadToDecimal(auctionStatus.price)

// update kick and pool for the change in bond as a result of the take
const kick = Kick.load(auction.kick)!
if (bucketTake.isReward) {
Expand Down Expand Up @@ -755,12 +754,13 @@ export function handleTake(event: TakeEvent): void {
const take = new Take(
event.transaction.hash.concatI32(event.logIndex.toI32())
)
take.borrower = event.params.borrower
take.taker = event.transaction.from
take.amount = wadToDecimal(event.params.amount)
take.collateral = wadToDecimal(event.params.collateral)
take.bondChange = wadToDecimal(event.params.bondChange)
take.isReward = event.params.isReward
take.borrower = event.params.borrower
take.taker = event.transaction.from
take.amount = wadToDecimal(event.params.amount)
take.collateral = wadToDecimal(event.params.collateral)
take.auctionPrice = take.amount.div(take.collateral)
take.bondChange = wadToDecimal(event.params.bondChange)
take.isReward = event.params.isReward

take.blockNumber = event.block.number
take.blockTimestamp = event.block.timestamp
Expand Down Expand Up @@ -804,11 +804,10 @@ export function handleTake(event: TakeEvent): void {
auction.takes = auction.takes.concat([take.id])
const auctionInfo = getAuctionInfoERC721Pool(take.borrower, pool)
const auctionStatus = getAuctionStatus(pool, event.params.borrower)
updateLiquidationAuction(auction, auctionInfo, auctionStatus)
updateLiquidationAuction(auction, auctionInfo, auctionStatus, take.auctionPrice)

const collateralPurchased = wadToDecimal(event.params.collateral)
pool.pledgedCollateral = pool.pledgedCollateral.minus(collateralPurchased)
take.auctionPrice = wadToDecimal(auctionStatus.price)

// update kick and pool for the change in bond as a result of the take
const kick = Kick.load(auction.kick)!
Expand Down
21 changes: 12 additions & 9 deletions src/utils/pool/liquidation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,18 +74,21 @@ export function updateLiquidationAuction(
liquidationAuction: LiquidationAuction,
auctionInfo: AuctionInfo,
auctionStatus: AuctionStatus,
isTake: bool = true,
isSettle: bool = false): void {
if (!isSettle) {
if (isTake) liquidationAuction.lastTakePrice = wadToDecimal(auctionStatus.price)
liquidationAuction.bondFactor = wadToDecimal(auctionInfo.bondFactor)
liquidationAuction.bondSize = wadToDecimal(auctionInfo.bondSize)
liquidationAuction.kickTime = auctionInfo.kickTime
liquidationAuction.neutralPrice = wadToDecimal(auctionInfo.neutralPrice)
lastTakePrice: BigDecimal | null = null): void {
// kickTime of 0 indicates auction was settled and auctionInfo/auctionStatus have no useful information
if (auctionInfo.kickTime != ZERO_BI) {
liquidationAuction.bondFactor = wadToDecimal(auctionInfo.bondFactor)
liquidationAuction.bondSize = wadToDecimal(auctionInfo.bondSize)
liquidationAuction.kickTime = auctionInfo.kickTime
liquidationAuction.neutralPrice = wadToDecimal(auctionInfo.neutralPrice)
}

// update remaining quantities even if auction was settled and they are 0
liquidationAuction.collateralRemaining = wadToDecimal(auctionStatus.collateral)
liquidationAuction.debtRemaining = wadToDecimal(auctionStatus.debtToCover)
liquidationAuction.debtRemaining = wadToDecimal(auctionStatus.debtToCover)

if (lastTakePrice)
liquidationAuction.lastTakePrice = lastTakePrice
}

export class AuctionInfo {
Expand Down

0 comments on commit 35c5de0

Please sign in to comment.