Repaying Debt
When you're ready to reduce or eliminate your debt, (using USDC as an example) you can repay it through the cUSDC contract. Repayment requires you to first approve the cUSDC contract to spend your USDC, then call the repay() function with the following function arguments:
uint256
amount
The amount of the underlying asset to repay, or 0 for the full outstanding amount.
Implementation snippet:
async function repayUSDCDebt(amount) {
const USDCDecimals = await USDC.decimals();
// Use 0 to repay the full outstanding amount
const isFullRepay = amount === 'full';
const amountToRepay = isFullRepay ? '0' : amount;
const amountInSmallestUnit = ethers.utils.parseUnits(amountToRepay, USDCDecimals);
// Approve USDC to be spent by eUSDC contract
let approvalAmount = amountInSmallestUnit;
const timestamp = block.timestamp;
if (isFullRepay) {
// Use the ProtocolReader contract to get a buffered amount that includes
// any interest that needs to be accrued without making a separate transaction.
const block = await provider.getBlock('latest');
approvalAmount = protocolReader.debtBalanceAtTimestamp(
userAddress,
cUSDCAddress,
timestamp);
}
await USDC.approve(cUSDCAddress, approvalAmount);
// Repay the debt
const repayTx = await cUSDC.repay(amountInSmallestUnit);
const receipt = await repayTx.wait();
console.log(`Repaid ${isFullRepay ? 'full debt' : amount + ' USDC'}`);
return receipt;
}Curvance simplifies full repayment by allowing you to input 0 as the amount, which automatically clears your entire outstanding debt, including any accrued interest. This removes the need for manual calculations of the total debt amount.
Repaying Debt on Behalf of Others
A unique feature of Curvance is the ability to repay another users debt. This can be useful in various scenarios, such as:
Helping a friend avoid liquidation.
Managing multiple wallets in a DAO or organization.
Implementing complex DeFi strategies.
The process is similar to regular repayment but uses the repayFor function using the following arguments:
address
account
The account address to repay on behalf of.
uint256
amount
The amount to repay, or 0 for the full outstanding amount.
Implementation snippets:
async function repayForAccount(account, amount) {
const USDCDecimals = await USDC.decimals();
const amountInSmallestUnit = ethers.utils.parseUnits(amount.toString(), USDCDecimals);
// Approve USDC to be spent by cUSDC contract
await USDC.approve(eUSDCAddress, amountInSmallestUnit);
// Repay debt on behalf of another account
const repayTx = await eUSDC.repayFor(account, amountInSmallestUnit);
const receipt = await repayTx.wait();
console.log(`Repaid ${amount} USDC on behalf of ${account}`);
return receipt;
}This function allows anyone to repay debt for any user without requiring permission from the borrower, creating interesting possibilities for social coordination in DeFi.
Last updated