1. Updating index.js
In Chapter 3, you created a JavaScript project with an index.js file that:
• Connected to Sepolia with a provider and signer.
• Used contract instances of Comet, WETH, and USDC.
• Showed how to supply WETH as collateral and borrow USDC.
In this chapter, we’ll extend that same index.js with new functions for repaying what you borrowed and withdrawing your collateral.
Let’s assume your index.js from Chapter 3 has code similar to this structure:
• Environment variable load (dotenv)• Ethers provider and signer setup
• Contract addresses & ABIs (comet, weth, usdc)
• Functions for:
– supplyWethAsCollateral(amount)
– borrowUsdc(amount)
• A main execution block with (async () => { ... })()
Now, we’ll show you how to integrate new repayment and withdraw collage.
Just place the following code chunks below the existing supply/borrow functions (but above your main execution block or in a dedicated helper module if you prefer)
1.1 Adding a Function to Check Debt
First, we add a getUsdcDebt function. This will help you see how much you currently owe in USDC (the base asset).
Explanation:
• If USDC has 6 decimals, and this function returns 50,000,000, that would be 50 USDC in standard decimal format.
1.2 Approving USDC for Repayment
Just like you approved WETH for supplying, you need to approve Comet to handle your USDC when repaying.
Explanation:
• The user is allowing the Comet contract to pull USDC from their account to cover the repayment.
1.3 Repaying Your Borrowed USDC
Once approved, you can repay USDC by “supplying” it back to Comet. This reduces (or eliminates) your debt.
Explanation:
• The supply(...) function works for any token, including the base asset.
• Because you have a negative balance (i.e., a debt) in USDC, supplying USDC reduces that negative balance.
1.4 Withdrawing Collateral (WETH)
After you repay what you owe (or if you simply haven’t borrowed much), you can remove some or all of your WETH collateral:
If you still have a debt, make sure you don’t drop below the required collateral threshold. Otherwise, the transaction may revert.
Last updated