Skip to content

Liquidation

adamant-al edited this page Jun 27, 2023 · 9 revisions

Liquidation DeFi scenario

In this example we will liquidate a borrow based on oXCN (XCN) asset underlying for 0xb5Cd64ba87E6f7FBD356b517BB7a279040C8Ac2a address.

See detailed description on the Onyx blog.

Preparations

Enter markets with borrow-asset:

await Comptroller.enterMarkets([oXCN.address])

Allow oTokens contract and Comptroller to transfer tokens:

await XCN.approve(oXCN.address, bank)
await XCN.approve(Comptroller.address, bank)

Check the balances and liquidity:

await oXCN.callStatic.balanceOfUnderlying(signerAddress) // $0
await Comptroller.getAccountLiquidity(borrowerAddress) // Check borrower account liquidity, it's negative, -> liquidate his borrow
await Comptroller.getAssetsIn(borrowerAddress) // Returns the address of oXCN contract, it means the collateral is supplied by oXCN tokens

Liquidate borrow step-by-step

Get borrow balance in the underlying for oXCN token (XCN):

const borrowedBalance = await oXCN.callStatic.borrowBalanceCurrent(borrowerAddress)

Liquidate a half of borrowed balance:

oXCN.liquidateBorrow(borrowerAddress, borrowedBalance.div(2), oXCN.address)

After emitted event LiquidateBorrow, which means successful borrow liquidation, check balances again:

await XCN.balanceOf(signerAddress) // The liquidator's balance decreased due to spending XCN tokens to liquidate half of the borrow  
await oXCN.callStatic.balanceOfUnderlying(signerAddress) // The liquidator's oXCN balance increased due to seized collateral from the borrower and liquidation bonus

Borrower account liquidity is increased and became positive after the liquidation.

Full example code

To run the liquidation scenario: npm run liquidation

Code

Clone this wiki locally