在Aave借贷操作中触发Gas估算失败的ValueError问题求助
我正在学习DeFi与Aave相关内容,在执行借贷操作时遇到以下错误:
ValueError: Gas estimation failed: 'The execution failed due to an exception.'. This transaction will likely revert. If you wish to broadcast, you must set the gas limit manually.
最初以为是资金不足,但增加账户资金并将借贷金额设为极低值后,问题仍未解决。其中deposit功能正常,仅borrow操作触发上述错误。
相关代码
def main(): account = get_account() erc20_address = config["networks"][network.show_active()]["weth_token"] if network.show_active() in ["mainnet-fork"]: get_weth() lending_pool = get_lending_pool() print(lending_pool) # 授权ERC20代币转账 approve_erc20(amount, lending_pool.address, erc20_address, account) tx = lending_pool.deposit( erc20_address, amount, account.address, 0, {"from": account} ) tx.wait(1) print("存款成功!!!") borrowable_eth, total_debt = get_borrowable_data(lending_pool, account) print("开始借贷!!") dai_eth_price_feed = config["networks"][network.show_active()]["dai_eth_price_feed"] dai_eth_price = get_asset_price(dai_eth_price_feed) amount_dai_to_borrow = (borrowable_eth * 0.05) / dai_eth_price print( f"准备借贷 {amount_dai_to_borrow} DAI ({Web3.toWei(borrowable_eth*0.05, 'ether')} wei)" ) dai_address = config["networks"][network.show_active()]["dai_token"] borrow_tx = lending_pool.borrow( dai_address, Web3.toWei(amount_dai_to_borrow, "ether"), 1, 0, account.address, {"from": account}, ) borrow_tx.wait(1) print("已借到DAI")
brownie-config.yaml配置
dependencies: - aave/protocol-v2@1.0.1 compiler: solc: remappings: - '@aave=aave/protocol-v2@1.0.1' networks: default: mainnet-fork kovan: weth_token: '0xd0A1E359811322d97991E03f863a0C30C2cF029C' lending_pool_addresses_provider: '0x88757f2f99175387ab4c6a4b3067c77a695b0349' dai_eth_price_feed: '0x22B58f1EbEDfCA50feF632bD73368b2FdA96D541' dai_token: '0x4F96Fe3b7A6Cf9725f59d353F723c1bDb64CA6Aa' mainnet-fork: weth_token: '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2' lending_pool_addresses_provider: '0xB53C1a33016B2DC2fF3653530bfF1848a515c8c5' dai_eth_price_feed: '0x773616E4d11A78F511299002da57A0a94577F1f4' dai_token: '0x6B175474E89094C44Da98b954EedeAC495271d0F' dotenv: .env wallets: from_key: ${PRIVATE_KEY}
排查方向与解决建议
利率模式参数错误
borrow函数第三个参数为利率模式,你当前传的是1(变量利率模式)。部分资产在特定网络可能不支持该模式,可尝试改为2(稳定利率模式)测试,或查阅Aave官方文档确认DAI支持的利率模式。借贷金额精度问题
计算amount_dai_to_borrow时可能出现精度丢失,导致实际金额不符合Aave最小借贷要求。建议先固定小额借贷(比如Web3.toWei(1, "ether")),排除计算逻辑问题。可借贷储备不足
即使是fork环境,也可能出现DAI可借贷储备为0的情况。可调用lending_pool.getReserveData(dai_address)查看availableLiquidity字段,确认是否有可借额度。合约实例正确性
检查get_lending_pool()函数是否正确通过配置中的lending_pool_addresses_provider调用getLendingPool()方法,确保返回的是Aave V2 Lending Pool的有效合约实例。
修改示例代码
# 调整borrow参数,固定借贷金额+切换利率模式 borrow_tx = lending_pool.borrow( dai_address, Web3.toWei(1, "ether"), # 固定借1DAI测试 2, # 使用稳定利率模式 0, account.address, {"from": account}, )
内容的提问来源于stack exchange,提问作者Irfan wani

