Repaying debt
The aaveRepayDebt data slice provides a re-usable Alpine.js data slice for enabling the current user to repay their debt via either the repayWithPermit() or repayWithATokens() contract methods.
Data Slice Structure
status
- Type: String
- Description: Represents the status of the contract write request. Can be
'idle','signaturePending','transactionPending','transactionSuccessful', or'error'. Defaults to'idle'.
amount
- Type: Number
- Description: Amount spent on repaying the debt. Defaults to
0. Set or update it in the markup withx-bind,x-init, orx-data.
interestRateMode
- Type: Enumeration (InterestRate)
- Description: Borrow rate mode. Defaults to variable. Set in the markup.
token
- Type: Object or Undefined
- Description: Reserve asset information the user wishes to borrow. Defaults to
undefined. Set or update it in the markup withx-bind,x-init, orx-data.
txsHashes
- Type: Any
- Description: Hash(es) of the transactions. Defaults to
undefinedand is defined within therepayDebtfunction.
Methods
The aaveRepayDebt data slice provides the following methods:
repayDebt(args: { onBehalfOfAddress?: string }): Promise<void>
This method allows the current user to repay their debt via the repayWithPermit() contract method. The user must have a collateralized position (hold a positive balance of aToken in their wallet), or the method will fail.
Parameters:
args.amount- (Optional) Amount of tokens used to repay the debt.args.reserve- (Optional) Reserve from which the user wishes to borrow.
repayDebtWithAToken(): Promise<void>
This method allows the current user to repay their debt via the repayWithATokens() contract method.
Example
The snippet below showcases a simple implementation of a widget that allows the user to repay their GHO against debt using their GHO balance.
<template x-if="$store.currentUser.account && $store.currentUser.aavePortfolio.summary"> <section x-data="aaveRepayDebt" x-init=` token = $aaveAssetBySymbol('GHO'); amount = 0; userAssetData = $store.currentUser?.aavePortfolio?.summary.userReservesData.filter(reserve => token.UNDERLYING.toLowerCase() === reserve.underlyingAsset.toLowerCase())[0]; interestRateMode = userAssetData?.reserve?.borrowRateMode; ` > <form @submit.prevent="repayDebt()"> <div> <label for="repayWithAmount">Repay this amount using my balance:</label> <input x-model.number="amount" min="0.000001" id="repayWithAmount" type="number" placeholder="1.00" step="0.000001" /> </div> <button>Repay</button> </form> <span x-text="status"></span> </section></template>