-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: handle liquidation seize events
- Loading branch information
Showing
6 changed files
with
147 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
subgraphs/isolated-pools/src/operations/recordLiquidatorAsBorrower.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { Address, ByteArray, Bytes, crypto, ethereum } from '@graphprotocol/graph-ts'; | ||
|
||
import { VToken as VTokenContract } from '../../generated/PoolRegistry/VToken'; | ||
import { ProtocolSeize } from '../../generated/PoolRegistry/VToken'; | ||
import { oneBigInt } from '../constants'; | ||
import { getMarket } from '../operations/get'; | ||
import { getOrCreateAccountVToken } from '../operations/getOrCreate'; | ||
|
||
class LiquidatorTransferMarker { | ||
current: i32; | ||
vTokenAddress: Address; | ||
|
||
constructor(vTokenAddress: Address) { | ||
this.vTokenAddress = vTokenAddress; | ||
} | ||
} | ||
|
||
export const recordLiquidatorAsBorrower = (event: ProtocolSeize): void => { | ||
if (event.receipt) { | ||
const reversedLogs = event.receipt!.logs.reverse(); | ||
reversedLogs.reduce<LiquidatorTransferMarker>((acc, curr, idx) => { | ||
const protocolSeizeTopic = Bytes.fromByteArray( | ||
crypto.keccak256(ByteArray.fromUTF8('ProtocolSeize(address,address,uint256)')), | ||
); | ||
|
||
if (curr.topics.includes(protocolSeizeTopic)) { | ||
acc.current = idx - 1; | ||
} | ||
if (idx == acc.current) { | ||
const transferTuple = ethereum.decode('(address,address,uint256)', curr.data)!.toTuple(); | ||
const liquidator = transferTuple[1].toAddress(); | ||
const amount = transferTuple[2].toBigInt(); | ||
// Register a Transfer to liquidator +1 supplier count | ||
const collateralContract = VTokenContract.bind(acc.vTokenAddress); | ||
const collateralBalanceLiquidator = collateralContract.balanceOf(liquidator); | ||
const market = getMarket(acc.vTokenAddress)!; | ||
// Update balance | ||
const borrowerAccountVToken = getOrCreateAccountVToken( | ||
liquidator, | ||
Address.fromBytes(market.pool), | ||
acc.vTokenAddress, | ||
); | ||
const accountFromVToken = borrowerAccountVToken.entity; | ||
|
||
// Creation updates balance | ||
if (!borrowerAccountVToken.created) { | ||
accountFromVToken.accountVTokenSupplyBalanceMantissa = | ||
accountFromVToken.accountVTokenSupplyBalanceMantissa.minus(amount); | ||
} | ||
accountFromVToken.save(); | ||
// If the transfer amount equals balance it was a funding transfer | ||
if (collateralBalanceLiquidator.equals(amount)) { | ||
market.supplierCount = market.supplierCount.plus(oneBigInt); | ||
market.save(); | ||
} | ||
} | ||
return acc; | ||
}, new LiquidatorTransferMarker(event.address)); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters