我正在尝试借给我正在构建的应用程序中的复合协议。
还按照这里的复合文档和指南:复合博客文章
在本地分叉主网并模拟一个带有大量 USDT 的地址后,我现在正试图借给复合协议。
在我的测试中,这条线underlying.transferFrom
总是因为这个错误而失败:ProviderError: Error: Transaction reverted without a reason string
任何关于可能原因的建议。
我问过复合discord,没有得到答复。
也做了很多研究,找不到类似的问题......不知道还能做什么。
下面是我的代码示例
开发环境:安全帽
操作系统:Windows 10
pragma solidity ^0.8.0;
import "hardhat/console.sol";
import "../interfaces/ICompound.sol";
contract CompoundController {
mapping(address => mapping(address => UserInvestedTokenDetails))
public UserInvestments;
struct UserInvestedTokenDetails {
uint256 tokenAmount;
bool isExists;
}
function _setUserInvestments(
address userAddress,
address tokenAddress,
uint256 tokenAmount
) internal {
if (UserInvestments[userAddress][tokenAddress].isExists) {
UserInvestments[userAddress][tokenAddress]
.tokenAmount += tokenAmount;
} else {
UserInvestments[userAddress][
tokenAddress
] = UserInvestedTokenDetails(tokenAmount, true);
}
}
function supplyErc20ToCompound(
address _erc20,
address _cErc20,
uint256 tokenAmount
) public returns (bool) {
console.log("contract!");
// Token being supplied to compound
Erc20 underlying = Erc20(_erc20);
// Token sent from compound in return
CErc20 cToken = CErc20(_cErc20);
require(
underlying.transferFrom(msg.sender, address(this), tokenAmount),
"compound: transferring tokens from user failed!"
);
underlying.approve(_cErc20, tokenAmount);
require(cToken.mint(tokenAmount) == 0, "compound: mint failed!");
_setUserInvestments(msg.sender, _erc20, tokenAmount);
return true;
}
}
还应要求提供测试文件:
const { expect } = require("chai");
const { BigNumber } = require("ethers");
const { ethers } = require("hardhat");
const DAI = process.env.DAI;
const USDT = process.env.USDT;
//
const cDAI = process.env.DAI;
const cUSDT = process.env.CUSDT;
const USDT_WHALE = process.env.USDT_WHALE;
describe("CompoundController", function () {
before(async () => {
[deployer, user1, user2] = await ethers.getSigners();
CompoundController = await ethers.getContractFactory("CompoundController");
compoundController = await CompoundController.deploy();
// FUND USERS ACCOUNT WITH PAYMENT OPTIONs TOKEN
Usdt = await ethers.getContractAt("IERC20", USDT);
Dai = await ethers.getContractAt("IERC20", DAI);
});
describe("sendErc20", function () {
it("should supply tokens to compound", async () => {
let depositAmount = BigNumber.from("100000000000000000000"); // 100
await hre.network.provider.request({
method: "hardhat_impersonateAccount",
params: [USDT_WHALE],
});
const signer = await ethers.getSigner(USDT_WHALE);
await Usdt.connect(signer).approve(
compoundController.address,
depositAmount
);
let tx = await compoundController
.connect(signer)
.supplyErc20ToCompound(USDT, cUSDT, depositAmount);
console.log(tx);
});
});
});