下面是一个坚固合同的例子,这是一种工厂模式的原生方法,它将船员添加到矩阵中的neb星舰中。
这个合同的克隆工厂版本是什么?
// SPDX-License-Identifier: MIT
pragma solidity >0.4.23 <0.9.0;
contract NebCrewFactory {
//Creating an array of neb crew addresses
NebCrew[] public NebCrewAddresses;
function addNebCrew() public {
//Creating a new crew object, you need to pay //for the deployment of this contract everytime - $$$$
NebCrew nebCrewAddress = new NebCrew();
//Adding the new crew to our list of crew addresses
NebCrewAddresses.push(nebCrewAddress);
}
}
contract NebCrew{
address public crew;
constructor() {
crew = msg.sender;
}
function welcomeCrew() public pure returns (string memory _greeting) {
return "Welcome to the truth...";
}
}发布于 2022-03-09 14:54:16
我展示的克隆工厂版本使用了OpenZeppelin库这里。
// SPDX-License-Identifier: MIT
pragma solidity >0.4.23 <0.9.0;
import { Clones } from "@openzeppelin/contracts/proxy/Clones.sol";
contract NebCrewFactory {
//Creating an array of neb crew addresses
NebCrew[] public NebCrewAddresses;
address public implementationAddress;
function addNebCrew() public {
//Creating a new crew object, you need to pay //for the deployment of this contract everytime - $$$$
NebCrew nebCrewAddress = NewCrew(Clones.clone(implementationAddress));
// since the clone create a proxy, the constructor is redundant and you have to use the initialize function
nebCrewAddress.initialize();
//Adding the new crew to our list of crew addresses
NebCrewAddresses.push(nebCrewAddress);
}
}
contract NebCrew{
address public crew;
initialize() {
require(crew == address(0), "already initialized");
crew = msg.sender;
}
function welcomeCrew() public pure returns (string memory _greeting) {
return "Welcome to the truth...";
}
}也不相关,但我想提一下,如果可以的话,最好是在工厂中使用一个映射,而不是一个数组,因为它可能会在将来造成问题。
https://stackoverflow.com/questions/71405290
复制相似问题