0

我正在尝试借给我正在构建的应用程序中的复合协议。
还按照这里的复合文档和指南:复合博客文章
在本地分叉主网并模拟一个带有大量 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);
    });
  });
});
4

2 回答 2

1

因此,经过几天的努力,我找到了这些指针。

  • USDT 合约似乎出于某种原因不符合 eip20 标准。它不返回布尔值,所以我的所有 erc20 合约的接口都与它们的功能不匹配。我立即用符合 eip20 的 DAI 进行了测试,它工作正常。
  • 同样在某个时候,在分叉主网时,我的签名者账户(我正在测试的账户)没有我试图提供给复合的令牌(足够的 USDT)。

我的意思是,USDT 合约问题令人沮丧,希望其他人觉得这很有用。

于 2022-01-26T03:00:50.810 回答
0

我认为在您尝试之前,用户需要批准合约地址才能代表他们进行转账transferFrom

于 2022-01-25T02:14:30.323 回答