【发布时间】:2021-10-03 18:04:50
【问题描述】:
我正在尝试存入 Aave V2 合约 Aave's Code Examples
// SPDX-License-Identifier: MIT
pragma solidity >= 0.4.22 < 0.8.7;
import { IERC20, ILendingPool, IProtocolDataProvider, IStableDebtToken } from './Interfaces.sol';
import { SafeERC20 } from './Libraries.sol';
/**
* This is a proof of concept starter contract, showing how uncollaterised loans are possible
* using Aave v2 credit delegation.
* This example supports stable interest rate borrows.
* It is not production ready (!). User permissions and user accounting of loans should be implemented.
* See @dev comments
*/
contract MyV2CreditDelegation {
using SafeERC20 for IERC20;
ILendingPool constant lendingPool = ILendingPool(address(0x9FE532197ad76c5a68961439604C037EB79681F0)); // Kovan
IProtocolDataProvider constant dataProvider = IProtocolDataProvider(address(0x744C1aaA95232EeF8A9994C4E0b3a89659D9AB79)); // Kovan
address owner;
constructor () public {
owner = msg.sender;
}
/**
* Deposits collateral into the Aave, to enable credit delegation
* This would be called by the delegator.
* @param asset The asset to be deposited as collateral
* @param amount The amount to be deposited as collateral
* @param isPull Whether to pull the funds from the caller, or use funds sent to this contract
* User must have approved this contract to pull funds if `isPull` = true
*
*/
function depositCollateral(address asset, uint256 amount, bool isPull) public {
if (isPull) {
IERC20(asset).safeTransferFrom(msg.sender, address(this), amount);
}
IERC20(asset).safeApprove(address(lendingPool), amount);
lendingPool.deposit(asset, amount, address(this), 0);
}
/**
* Approves the borrower to take an uncollaterised loan
* @param borrower The borrower of the funds (i.e. delgatee)
* @param amount The amount the borrower is allowed to borrow (i.e. their line of credit)
* @param asset The asset they are allowed to borrow
*
* Add permissions to this call, e.g. only the owner should be able to approve borrowers!
*/
function approveBorrower(address borrower, uint256 amount, address asset) public {
(, address stableDebtTokenAddress,) = dataProvider.getReserveTokensAddresses(asset);
IStableDebtToken(stableDebtTokenAddress).approveDelegation(borrower, amount);
}
/**
* Repay an uncollaterised loan
* @param amount The amount to repay
* @param asset The asset to be repaid
*
* User calling this function must have approved this contract with an allowance to transfer the tokens
*
* You should keep internal accounting of borrowers, if your contract will have multiple borrowers
*/
function repayBorrower(uint256 amount, address asset) public {
IERC20(asset).safeTransferFrom(msg.sender, address(this), amount);
IERC20(asset).safeApprove(address(lendingPool), amount);
lendingPool.repay(asset, amount, 1, address(this));
}
/**
* Withdraw all of a collateral as the underlying asset, if no outstanding loans delegated
* @param asset The underlying asset to withdraw
*
* Add permissions to this call, e.g. only the owner should be able to withdraw the collateral!
*/
function withdrawCollateral(address asset) public {
(address aTokenAddress,,) = dataProvider.getReserveTokensAddresses(asset);
uint256 assetBalance = IERC20(aTokenAddress).balanceOf(address(this));
lendingPool.withdraw(asset, assetBalance, owner);
}
}
我有这样的代码:
App = {
web3Provider: null,
contracts: {},
init: async function() {
return await App.initWeb3();
},
initWeb3: async function() {
// Modern dapp browsers...
if (window.ethereum) {
App.web3Provider = window.ethereum;
try {
// Request account access
await window.ethereum.enable();
} catch (error) {
// User denied account access...
console.error("User denied account access")
}
}
// Legacy dapp browsers...
else if (window.web3) {
App.web3Provider = window.web3.currentProvider;
}
// If no injected web3 instance is detected, fall back to Ganache
else {
App.web3Provider = new Web3.providers.HttpProvider('http://localhost:8545');
}
web3 = new Web3(App.web3Provider);
return App.initContract();
},
initContract: function() {
$.getJSON('MyV2CreditDelegation.json', function(data) {
// Get the necessary contract artifact file and instantiate it with @truffle/contract
var safeYieldArtifact = data;
App.contracts.MyV2CreditDelegation = TruffleContract(safeYieldArtifact);
// Set the provider for our contract
App.contracts.MyV2CreditDelegation.setProvider(App.web3Provider);
});
return App.bindEvents();
},
bindEvents: function() {
$(document).on('click', '.btn-deposit', App.handleDeposit);
$(document).on('click', '.btn-withdrawl', App.handleWithdrawl);
},
handleDeposit: function(event) {
event.preventDefault();
web3.eth.getAccounts(function(error, accounts) {
if (error) {
console.log(error);
}
var account = accounts[0];
App.contracts.MyV2CreditDelegation.deployed().then(function(instance) {
creditDelegatorInstance = instance;
const mockETHAddress = "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE"
// Execute adopt as a transaction by sending account
return creditDelegatorInstance.depositCollateral(mockETHAddress, 1, true);
}).then(function(result) {
//return App.markAdopted();
}).catch(function(err) {
console.log(err.message);
});
});
},
handleWithdrawl: function(event) {
event.preventDefault();
},
};
$(function() {
$(window).load(function() {
App.init();
});
});
尝试提供时,Metamask 显示错误:
警告:交易错误。合约代码中抛出异常。
只需一个简单的按钮即可在 html 中调用它:
<button class="btn btn-default btn-withdrawl"
type="button"> Withdrawl
</button>
我正在运行甘纳许
ganache-cli --fork https://mainnet.infura.io/v3/{{MyProjectId}}
我在控制台中看到的唯一错误是:
事务:0x9961f8a187c09fd7c9ebf803771fa161c9939268bb01552a1598807bcfdc13ff 用气量:24813 区块编号:12905002 出块时间:2021 年 7 月 26 日星期一 20:38:30 GMT-0400(东部夏令时间) 运行时错误:还原
我的猜测是我没有适当地从 Web3 调用合同
如何以编程方式向 aave 提供 Eth(或任何其他令牌)?
【问题讨论】:
-
您确定是从正确的帐户发送的吗?毕竟,我看到你定义了
account,但从不使用它。 -
好点,我一直在玩一堆不同的合同,试图让这个发送,所以这显然是一个错误。我会调查的
-
我还注意到 MyV2CreditDelegation 指的是 Kovan 上的
ILendingPool和IProtocolDataProvider,但这些地址并没有在主网上使用。由于您通过分叉主网来启动 ganache,因此这些都行不通。你的depositCollateral肯定会失败。
标签: javascript ethereum solidity web3