From 35c5de0e0f43085bbe422e0efe0fb585e6ed37f2 Mon Sep 17 00:00:00 2001 From: Ed Noepel <46749157+EdNoepel@users.noreply.github.com> Date: Mon, 18 Sep 2023 10:06:23 -0400 Subject: [PATCH] Fix issue maintaining auction price (#68) * reworked updateLiquidationAuction * update remaining quantities even if auction was settled and they are 0 --- src/mappings/erc-20-pool.ts | 44 +++++++++++++++++------------------ src/mappings/erc-721-pool.ts | 43 +++++++++++++++++----------------- src/utils/pool/liquidation.ts | 21 ++++++++++------- 3 files changed, 54 insertions(+), 54 deletions(-) diff --git a/src/mappings/erc-20-pool.ts b/src/mappings/erc-20-pool.ts index b1f9a0e..b2efd64 100644 --- a/src/mappings/erc-20-pool.ts +++ b/src/mappings/erc-20-pool.ts @@ -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 @@ -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) { @@ -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) @@ -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 @@ -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)! @@ -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 @@ -690,7 +689,6 @@ export function handleSettle(event: SettleEvent): void { account.save() auction.save() pool.save() - settle.save() } diff --git a/src/mappings/erc-721-pool.ts b/src/mappings/erc-721-pool.ts index 383924b..2fce19e 100644 --- a/src/mappings/erc-721-pool.ts +++ b/src/mappings/erc-721-pool.ts @@ -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 @@ -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) @@ -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 @@ -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) { @@ -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 @@ -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)! diff --git a/src/utils/pool/liquidation.ts b/src/utils/pool/liquidation.ts index 4247273..8b6c8d7 100644 --- a/src/utils/pool/liquidation.ts +++ b/src/utils/pool/liquidation.ts @@ -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 {