Repaying Debt
Type
Name
Description
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;
}Repaying Debt on Behalf of Others
Type
Name
Description
Last updated
Was this helpful?