在此協議中,使用“健康係數”指標衡量使用者的健康狀況,該指標顯示使用者相對於所請求貸款的抵押狀態。該資料的計算如下:
如果您不喜歡數學,我們可以以更加圖形化的方式檢視它,就像是一個比例表一樣,其中ETH抵押品的總和與所要求的貸款價格之間的清算保證金相平衡。
這樣必須確保抵押部分比貸款部分重,以免使餘額不平衡。
什麼會導致平衡變得不平衡?
· 抵押品貶值:如果我們把一種失去價值的貨幣作為擔保,它將使我們陷入債務。
· 本金升值:這可能有兩個原因,一個是收取利息,導致本金升值。除此之外,貨幣對以太坊的價格可能會上漲,而且由於貸款以以以太坊計價,我們的本金價值可能會超過抵押品的價值。
當債務大於剩餘的抵押物準備金時,使用者的健康係數的值將降至1以下,此時清算人可以償還部分債務,並從中扣除抵押品,並對其進行折價,這鼓勵清算人採取以下行動:清算。
使用協議的實際案例
為了得到一個小於1的健康係數,我將在Aave testnet上申請一筆貸款,風險可能最高,使用的加密資產利息最高,這樣我就能儘快負債。
我要做的第一件事是存入我將用作抵押品的加密資產,在這種情況下是ETH。
然後,我要申請一筆貸款,看看哪一個利息最高,我發現這是TUSD
因此,我申請貸款,但只用我已存入抵押品的50%。
這意味著,由於我存入的資金非常多,因此仍然保留了很高的健康係數。
為了不必長時間等待,我將增加我的風險,提取賬戶中剩餘的所有可能的抵押品:
這讓我加快負債
現在,您所要做的就是等待將健康係數降至1以下。
準備清算
雖然貸款利息使我的健康因數降低了1,但我們將建立指令碼來進行付款。在Aave網頁上有一個基本模型,以及該過程的概述,但是在我的測試中,啟動事務時,我做了一些“還原”操作,這使我建立了一個整合測試,可以評估這些條件以瞭解指令碼 是正確的。
要執行測試,我們需要以下輸入資料:
const collateralAddress = "0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee" //Collateral address of the user that we want to liquidate
const reserveAddress = "0xf80a32a835f79d7787e8a8ee5721d0feafd78108" //Reserve address that we are going to pay for ERC20 token
const userLiquidated = "0xe4509F2ce63DED82e74e9e7E648c1A69e90cD2C4" //User that we are going to liquidate
const purchaseAmount = 10 //Amount of the reserve that we are going to liquidate
· 我想在還清貸款時想要得到的抵押地址,在這種情況下,我想得到ETH,可以在這裡找到這些地址。
· 儲備金地址:我將要支付以清算債務的加密資產的地址是您的ERC20合約的地址。
· 我要清算的使用者。
· 要清算的金額,以貸款令牌表示。
有了這些資料,將執行測試,該測試將直接呼叫Ropsten測試網的合約並驗證條件是否滿足,就像智慧合約在呼叫LiquidationCall函式時所做的方式一樣,但是當僅進行呼叫時,我們避免還原,我們知道錯誤的原因。實現它們的程式碼是:
it('should get the latest LendingPoolCore address', async () => {
lpCoreAddress = await getLpCoreAddress(lpAddressProviderContract)
expect(lpCoreAddress).contains('0x')
});
it('should Get the latest LendingPool contract address', async () => {
lpAddress = await getLendingPoolAddress(lpAddressProviderContract)
expect(lpAddress).contains('0x')
});
it('Should Get the lpContract', async () => {
lpContract = await new web3.eth.Contract(LendingPoolABI, lpAddress)
expect(lpContract._address).contains('0x')
});
it('Should Get the lpCoreContract', async () => {
lpCoreContract = await new web3.eth.Contract(LendingPoolCoreABI, lpCoreAddress)
expect(lpCoreContract._address).contains('0x')
});
it('Should have health factor below 1', async () => {
const userData = await getUserAccountData(lpContract, userLiquidated);
expect(convertUnits(userData.healthFactor)).lessThan(1)
})
it('Should have collateral enabled', async () => {
const userReserveData = await getUserReserveData(lpContract, collateralAddress, userLiquidated);
expect(userReserveData.usageAsCollateralEnabled).to.be.true
})
it('Should have collateral deposited', async () => {
const userCollateralBalance = await getUserUnderlyingAssetBalance(
lpCoreContract,
collateralAddress,
userLiquidated);
expect(Number(userCollateralBalance)).to.be.greaterThan(0)
})
it('Should have enough reseve borrowed', async () => {
const LIQUIDATION_CLOSE_FACTOR_PERCENT = 0.5
const userBorrows = await getUserBorrowBalances(
lpCoreContract,
reserveAddress,
userLiquidated);
const userCompoundedBorrowBalance = convertUnits(userBorrows[1])
expect(userCompoundedBorrowBalance * LIQUIDATION_CLOSE_FACTOR_PERCENT)
.to.be.greaterThan(Number(purchaseAmount))
})
如果我們現在嘗試執行它們,請執行
npm test
由於不滿足至少一個條件,它應該返回一個錯誤:
如我們所見,驗證我帳戶的健康因子小於1的測試失敗,表明該交易將無法正確執行,如果不滿足任何其他條件,則同樣會發生。
還清部分貸款
過了一會兒,從貸款中扣除的利息使我的債務變得越來越多,而我的抵押品所承擔的債務卻越來越少,直到我的健康係數低於這一水平為止,現在清算人可以清算。
如果現在執行測試,我們將看到:
它告訴我們條件已經滿足,所以我們執行應用程式,這樣,現在是的,呼叫LiquidationCall函式並清算貸款的一部分
npm start
當執行交易時,我們看到貸款減少了我們呼叫該函式的金額,這可以由監視所有使用者健康因素並以折價購買抵押品的任何清算人來完成清算。