简体   繁体   English

有没有办法在部署之前预先部署从智能合约中部署的库?

[英]Is there a way pre-deploy a library deployed from within a smart contract before it is deployed?

Sorry if the question did not make sense.对不起,如果这个问题没有意义。 Here is what I am trying to do: I want to deploy this smart contract (LenseHub) that imports a library that requires data passed to its constructor.这是我想做的事情:我想部署这个智能合约(LenseHub),它导入一个需要将数据传递给其构造函数的库。 This is problematic because I need LenseHub to initialize the contract (a function I can call only after the contract is deployed).这是有问题的,因为我需要 LenseHub 来初始化合约(一个 function 我只能在部署合约后调用)。 If I try to deploy LenseHub without pre-deploying the IERC721Enumerable it will fail obviously.如果我尝试在没有预先部署 IERC721Enumerable 的情况下部署 LenseHub,它显然会失败。 If I can't figure this out I will just inherit IERC721Enumerable and initialize it via the constructor, but would really like to keep the original smart contracts integrity (for testing purposes).如果我无法弄清楚这一点,我将只继承 IERC721Enumerable 并通过构造函数对其进行初始化,但我真的很想保持原始智能合约的完整性(用于测试目的)。 Any suggestions on how to do this would be greatly appreciated任何有关如何做到这一点的建议将不胜感激

Here is the relevant part of the smart contract:以下是智能合约的相关部分:

import {IERC721Enumerable} from "@openzeppelin/contracts/token/ERC721/extensions/IERC721Enumerable.sol";

/**
 * @title LensHub
 * @author Lens Protocol
 *
 * @notice This is the main entrypoint of the Lens Protocol. It contains governance functionality as well as
 * publishing and profile interaction functionality.
 *
 * NOTE: The Lens Protocol is unique in that frontend operators need to track a potentially overwhelming
 * number of NFT contracts and interactions at once. For that reason, we've made two quirky design decisions:
 *      1. Both Follow & Collect NFTs invoke an LensHub callback on transfer with the sole purpose of emitting an event.
 *      2. Almost every event in the protocol emits the current block timestamp, reducing the need to fetch it manually.
 *
 * OVERVIEW: The lense protocall is one of the three main solidity contracts compiled. It compiles all of its code imports code into
 * one main "hub" from were you can interact with the protocall.
 *
 *
 */
contract LensHub is
  LensNFTBase,
  VersionedInitializable,
  LensMultiState,
  LensHubStorage,
  ILensHub
{
  uint256 internal constant REVISION = 1;

  address internal immutable FOLLOW_NFT_IMPL;
  address internal immutable COLLECT_NFT_IMPL;

  /**
   * @dev This modifier reverts if the caller is not the configured governance address.
   */
  modifier onlyGov() {
    _validateCallerIsGovernance();
    _;
  }

  /**
   * @dev The constructor sets the immutable follow & collect NFT implementations.
   *
   * @param followNFTImpl The follow NFT implementation address.
   * @param collectNFTImpl The collect NFT implementation address.
   */
  constructor(address followNFTImpl, address collectNFTImpl) {
    if (followNFTImpl == address(0)) revert Errors.InitParamsInvalid();
    if (collectNFTImpl == address(0)) revert Errors.InitParamsInvalid();
    FOLLOW_NFT_IMPL = followNFTImpl;
    COLLECT_NFT_IMPL = collectNFTImpl;
  }

  /// @inheritdoc ILensHub
  function initialize(
    string calldata name,
    string calldata symbol,
    address newGovernance
  ) external override initializer {
    super._initialize(name, symbol);
    _setState(DataTypes.ProtocolState.Paused);
    _setGovernance(newGovernance);
  }

Here is the _function that the initialize function calls:这是初始化 function 调用的 _function:

 function _initialize(string calldata name, string calldata symbol) internal {
        ERC721Time.__ERC721_Init(name, symbol);

        emit Events.BaseInitialized(name, symbol, block.timestamp);
    }

Here is the relevent part of the library:这是图书馆的相关部分:

// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.5.0) (token/ERC721/extensions/IERC721Enumerable.sol)

pragma solidity ^0.8.0;

import "../IERC721.sol";

/**
 * @title ERC-721 Non-Fungible Token Standard, optional enumeration extension
 * @dev See https://eips.ethereum.org/EIPS/eip-721
 */
interface IERC721Enumerable is IERC721 {
    /**
     * @dev Returns the total amount of tokens stored by the contract.
     */
    function totalSupply() external view returns (uint256);

    /**
     * @dev Returns a token ID owned by `owner` at a given `index` of its token list.
     * Use along with {balanceOf} to enumerate all of ``owner``'s tokens.
     */
    function tokenOfOwnerByIndex(address owner, uint256 index) external view returns (uint256);

    /**
     * @dev Returns a token ID at a given `index` of all the tokens stored by the contract.
     * Use along with {totalSupply} to enumerate all tokens.
     */
    function tokenByIndex(uint256 index) external view returns (uint256);
}

Here is the constructor of ERC721 that I am trying to pass variables to:这是我试图将变量传递给的 ERC721 的构造函数:

    constructor(string memory name_, string memory symbol_) {
        _name = name_;
        _symbol = symbol_;
    }

First of all, don't call the IERC721Enumerable a library - it is just an interface you want to implement in your contract.首先,不要将 IERC721Enumerable 称为库 - 它只是您要在合约中实现的接口。 In the context of solidity, a library has a bit different meaning.在solidity 的上下文中, library有一些不同的含义。 The question became misleading in the context of solidity.这个问题在稳固性的背景下变得具有误导性。

Secondly, what you want to achieve may be done in two ways - complicated but more correct and easy but less correct:其次,您想要实现的目标可以通过两种方式完成 - 复杂但更正确和容易但不太正确:

  1. The complicated approach requires you to use IERC721Upgradeable and properly upgrade your contact when it is needed.复杂的方法要求您使用IERC721Upgradeable并在需要时正确升级您的联系人。 I don't think you need to go this far because using a proxy requires a stiff learning curve, and your use case does not require it.我认为您不需要 go 到目前为止,因为使用代理需要严格的学习曲线,而您的用例不需要它。
  2. The easier way is to copy the needed interface methods directly inside your contract(or some other class your contact will depend on, that will be open to the needed modifications).更简单的方法是直接在您的合同中复制所需的接口方法(或您的联系人将依赖的其他一些 class,这将对所需的修改开放)。 This way, you will be able to set your name and symbol whenever you want, and your contract will still be compliant with NFT interface.这样,您将能够随时设置您的名称和符号,并且您的合约仍然符合 NFT 接口。 I know it looks like copying code is a bad idea, but you won't be able to change anything in your contract as soon as it is deployed(if it is not upgradable) thus, copying of the code doesn't matter in the long run.我知道复制代码看起来是个坏主意,但是一旦部署合同(如果不可升级),您将无法更改合同中的任何内容,因此,复制代码在长跑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 通过应用程序访问在Ropsten网络上部署的智能合约 - Access a smart contract that is deployed on the Ropsten network from an App eth-brownie:部署的合约作为交易收据而不是合约本身返回。 我无法从地址或别名中获取合同 - eth-brownie: Deployed contract returning as transaction receipt instead of the contract itself. I cannot grab the contract from the address or alias 自定义库未部署到WLS jDeveloper - Custom library not deployed to WLS jDeveloper 无法通过 de.net 部署 solana 智能合约 - Not able to deploy solana smart contract over devnet 使用1个功能部署3个ContentTypes-仅部署2个 - Deploy 3 ContentTypes using 1 Feature - Only 2 gets deployed 使用MUP进行流星部署,我的集合未部署 - Meteor deploy with MUP, my collections are not deployed 如何在已经部署的应用程序中部署Portlet? - How to deploy a portlet in an already deployed Application? 如何在Tomcat中部署的Alfresco中部署模块 - How to deploy a module in an Alfresco deployed in Tomcat 找不到作为WSP解决方案部署的工作流(当我从VS部署时可以正常工作) - Can't find workflow deployed as WSP solution (when I deploy it from VS it works) 无法使用 Truffle Infura 将智能合约部署到测试网 (Ropsten) - Unable to deploy smart contract to testnet (Ropsten) using Truffle Infura
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM