Remix

每次我嘗試與 Brownie 編譯包含來自網路的導入語句的契約時都會出現編譯錯誤

  • October 15, 2021

我有一份以以下導入語句開頭的契約:

import 'https://github.com/aave/protocol-v2/blob/master/contracts/flashloan/base/FlashLoanReceiverBase.sol';
import 'https://github.com/aave/protocol-v2/blob/master/contracts/interfaces/ILendingPool.sol';
import 'https://github.com/aave/protocol-v2/blob/master/contracts/interfaces/ILendingPoolAddressesProvider.sol';
import 'https://github.com/aave/protocol-v2/blob/master/contracts/dependencies/openzeppelin/contracts/IERC20.sol';

每次我用 brownie 編譯這份契約時,每個導入語句都會出現以下錯誤:

contracts/FlashLoan.sol:5:1: ParserError: Source "https://github.com/aave/protocol-v2/blob/master/contracts/flashloan/base/FlashLoanReceiverBase.sol" not found: File outside of allowed directories.
import 'https://github.com/aave/protocol-v2/blob/master/contracts/flashloan/base/FlashLoanReceiverBase.sol';
^----------------------------------------------------------------------------------------------------------^

我已經用 ganache 和 infura 網路嘗試過這個(我懷疑這很重要,因為它是一個編譯器錯誤,但我想我會提到它)。我試圖將導入語句更改為不包含https://github.com/並將 URL 的那部分替換為 @ 並且仍然得到相同的錯誤。我的機器絕對連接到網際網路,因為我能夠在 ganache 鍊和測試網路上部署合約並與之互動,而無需導入語句。

我檢查了語法,看起來我做得對,我在這裡遺漏了什麼嗎?有沒有更好的導入方式?

當然,我不需要下載我的契約所依賴的每個 .sol 文件並將其放在契約文件夾中,對嗎?因為我的契約的每個依賴項都有自己的依賴項,我也必須追踪這些依賴項。

任何幫助深表感謝。

這是完整的契約:

  // SPDX-License-Identifier: MIT

pragma solidity ^0.6.0;

import 'https://github.com/aave/protocol-v2/blob/master/contracts/flashloan/base/FlashLoanReceiverBase.sol';
import 'https://github.com/aave/protocol-v2/blob/master/contracts/interfaces/ILendingPool.sol';
import 'https://github.com/aave/protocol-v2/blob/master/contracts/interfaces/ILendingPoolAddressesProvider.sol';
import 'https://github.com/aave/protocol-v2/blob/master/contracts/dependencies/openzeppelin/contracts/IERC20.sol';

contract TheFlashLoan is FlashLoanReceiverBase {

   constructor() public {
       address owner = msg.sender; 
   }

   function executeOperation(
       address[] calldata assets,
       uint256[] calldata amounts,
       uint256[] calldata premiums,
       address initiator,
       bytes calldata params) external override returns (bool) {

       //
       // This contract now has the funds requested.
       // Your logic goes here.
       //

       // At the end of your logic above, this contract owes
       // the flashloaned amounts + premiums.
       // Therefore ensure your contract has enough to repay
       // these amounts.

       // Approve the LendingPool contract allowance to *pull* the owed amount
       for (uint i = 0; i < assets.length; i++) {
           // add lending fee to amount owed
           uint amountOwing = amounts[i].add(premiums[i]);
           IERC20(assets[i]).approve(address(LENDING_POOL), amountOwing);
       }

       return true;
   }

   function startFlashLoan() public onlyOwner {
       address receiverAddress = address(this);

       address[] memory assets = new address[];
       //assets[0] = address(0x8f3Cf7ad23Cd3CaDbD9735AFf958023239c6A063); // DAI on Polygon
       assets[0] = address(0xaD6D458402F60fD3Bd25163575031ACDce07538D); // DAI on Ropsten

       uint256[] memory amounts = new uint256[];
       amounts[0] = 100;

       // 0 = no debt, 1 = stable, 2 = variable
       uint256[] memory modes = new uint256[];
       modes[0] = 0;

       address onBehalfOf = address(this);
       bytes memory params = "";
       uint16 referralCode = 0;

       LENDING_POOL.flashLoan(
           receiverAddress,
           assets,
           amounts,
           modes,
           onBehalfOf,
           params,
           referralCode
       );
   }

   modifier onlyOwner {
       assert(owner == msg.sender);
       _;
   }

   function Withdraw(address payable _to, uint256 _amount) external onlyOwner {
       _to.transfer(_amount);
   }

   function ShowBalance() public view onlyOwner returns(uint256) {
       return address(this).balance;
   }
}

我嘗試將契約移動到 Remix 並且導入語句有效,但奇怪的是,Remix 為每一行都拋出瞭如下所示的錯誤:

uint256[] memory amounts = new uint256[];

出於某種原因,它會引發此錯誤,僅在我刪除時才會消失new uint256[]

它拋出的錯誤是:

contracts/Arbitrage/FL.sol:52:9: TypeError: Type function (uint256) pure returns (uint256[] memory) is not implicitly convertible to expected type uint256[] memory.
   uint256[] memory amounts = new uint256[];
   ^--------------------------------------^

如果我刪除new uint256[]程式碼執行的行,但現在它會拋出一個錯誤,說我的契約需要標記為抽象:

contracts/Arbitrage/FL.sol:10:1: TypeError: Contract "TheFlashLoan" should be marked as abstract.
   contract TheFlashLoan is FlashLoanReceiverBase {
   ^ (Relevant source part starts here and spans across multiple lines).
   https://github.com/aave/protocol-v2/blob/master/contracts/flashloan/base/FlashLoanReceiverBase.sol:18:3: Missing implementation: 
     constructor(ILendingPoolAddressesProvider provider) public {
     ^ (Relevant source part starts here and spans across multiple lines).

有人也可以幫助啟發我為什麼這樣做嗎?

為了直接從 github 導入,您需要將依賴項添加到您的brownie-config.yaml文件中。

即,如果您嘗試從中導入,https://github.com/aave/protocol-v2/blob/master/contracts/flashloan/base/FlashLoanReceiverBase.sol則需要brownie-config.yaml

dependencies:
 - aave/protocol-v2@1.0.1

通常,這與重新映射結合使用:

compiler:
 solc:
   remappings:
     - '@aave=aave/protocol-v2@1.0.1'

然後在您的可靠程式碼中,您可以執行以下操作:

import '@aave/contracts/flashloan/base/FlashLoanReceiverBase.sol';

此外,您不希望在導入中包含“blob”和“master”之類的內容。布朗尼導入來自特定版本,而不是來自 URL。 在此處輸入圖像描述

在我們的範例中,我們使用了1.0.1Aave 的發布版本。

引用自:https://ethereum.stackexchange.com/questions/111531