PatrickAlphaC / aave_brownie_py_freecode

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

incorrect calculation when repaying debt in aave_borrow.py (In repay_all() function)

kavish6 opened this issue · comments

https://github.com/PatrickAlphaC/aave_brownie_py_freecode/blob/main/scripts/aave_borrow.py
amount = Web3.toWei(0.1, "ether")

we never modify amount so when we call repay_all() function in main with amount as parameter why do we have to convert it to wei again?

repay_all(amount,lending_pool,account) **function call in main **

def repay_all(amount, lending_pool, account):
approve_erc20(
Web3.toWei(amount, "ether"), #here we don't need to convert amount to wei as we have already defined amount in wei
lending_pool,
config["networks"][network.show_active()]["dai_token"],
account,
)
repay_tx = lending_pool.repay(
config["networks"][network.show_active()]["dai_token"],
amount,
1,
account.address,
{"from": account},
)
repay_tx.wait(1)
print("Repaid!")

I think you're right!

Could you make a PR to fix?

Anyways, shouldn't we repay "amount_dai_to_borrow", i.e. repay the DAI tokens that we borrowed, not the "amount" which is 0.1 Ether.

Anyways, shouldn't we repay "amount_dai_to_borrow", i.e. repay the DAI tokens that we borrowed, not the "amount" which is 0.1 Ether.

i think so

commented

I found a solution. I have noticed a little bit of Gas Fee is required for this transaction to occur. Since we are paying 100% of our debt. This doesnt work because there is no gwei left for our gas which will be our transaction fee. Hence If we multiply our debt by 0.99. We have 0.01% which is more than enough gwei for gas to be paid.

Code:

  • This to be called from main(). Remember dai_borrowed is the amount of DAI not ETH.
    repay_all(lending_pool, account,Web3.toWei(dai_borrowed, "ether"),1)

  • This will be called in repay_all() function. Do no interfere with the value of amount. Since we already passed a converted value. repay_tx = lending_pool.repay(asset,(debt*0.99),r,account.address,{"from": account})

Update: Did more digging and this is exactly whats happening. Warning! Error encountered during contract execution [Out of gas]. So my assumption was right.

Update #2: Alternatively, is it better to set a priority_fee to 3 gwei (2.5 gwei was fast as shown in AAVE). Everything better work's out if you use this. {"from": account, "priority_fee":"3 gwei"} in your tranasctions.

Thanks @is-it-ayush for clarifying!

I noticed this essentially means we can't repay the entire debt through code? There will always be some small amount left, and once that amount is smaller than the gas fee required to process it, then it can no longer be repaid.

In the video, Patrick repays the remaining amount using collateral. But I can't see anything in the ILendingPool interface that allows us to repay the debt using collateral. And the updated testnet Aave interface (staging.aave.com) doesn't allow paying with collateral. Is there any way to pay off these small amounts left over? I can't imagine I would ever do this on a mainnet with real money if I couldn't be confident that I can drive the debt down to zero.

commented

Welcome @youss-b! Dont worry, you can be confident doing on mainnet with real money since the last time I worked on this project was about 24 days ago. The total debt since then has amounted to about $0.07. Looking at this it's relatively safe to say the amount is negligible. I still have about 0.34 WETH deposited in the collateral. In conclusion, its safe for you to practice on mainnet. This isn't a financial advise since I am in no way a financial advisor due it on your own risks. Just be cautious about your health factor so you dont end up getting liquidated.

Thanks Ayush!

But not being able to repay the whole debt means you can never withdraw the entire collateral either, right? Or you risk being liquidated?

I've found that I can't withdraw any of the collateral in the Aave UI either (there's always an error). And I've tried interacting directly with the LendingPool contract through Etherscan; there, I've confirmed that I can borrow and repay (although not the entire amount), but I can't withdraw at all.

With these limitations, I'll likely steer clear of the Aave protocol until I know more. Maybe I'll try the Rinkeby network and the v3 protocol and see if I have better luck there, although based on the interface it seems you still can't pay with collateral through code. Maybe you can through the UI though? It seems strange though--I thought the front-end was calling the same smart contracts on the backend that we are calling through code, so I'd assume they should have equivalent functionality. It seems weird that you can pay down the whole debt with collateral in an earlier Aave version that Patrick was using, but you can no longer do that.

commented

It's true. This is definately since the value of our collateral constantly keeps on changing in very small scale due to our APY. It would'nt be possible for them to calculate the health factor within seconds or even process the transaction. A simple approach to solve this limitation would be to update collateral amount for a address after a delay of few mins say 5 for their transaction to resolve incase they want to pull out. The same might be the case with debt. Therefore, making both of them unable to withdraw in their entirety for an address. Thanks @youss-b for raising this.