当子合约没有构造函数时如何执行工厂模式?
How to execute a factory pattern when the child contract does not have a constructor?
// SPDX-License-Identifier: MIT
pragma solidity >=0.7.0 <0.9.0;
contract ConcertTicketReservation {
struct reservation {
address Id;
string name;
string seat;
Class choice;
}
uint256 reservationsCount = 0;
mapping (uint => reservation) public reservations; //to store reservations
enum Class{VIP, NON_VIP } //two types of seats
Class choice = Class.NON_VIP;
function setVIP() public { //set to vip
choice = Class.VIP;
}
function setNON_VIP() public {
choice = Class.NON_VIP;
}
function getChoice() public view returns (string memory) { //for my ease, to see what value has been chosen
if(choice == Class.NON_VIP) {
return "NON_VIP";
} else if(choice == Class.VIP) {
return "VIP";
}
return "NON_VIP";
}
uint non_vip_price = 0.005 ether; //price
uint vip_price = 0.01 ether;
event Received(address, uint);
function pay() internal { //pay function
uint moneyToReturn;
if(reservations[reservationsCount].choice == Class.NON_VIP) {
require (msg.value >= non_vip_price);
emit Received(msg.sender, msg.value);
moneyToReturn = msg.value - non_vip_price;
} else if(reservations[reservationsCount].choice == Class.VIP) {
require (msg.value >= vip_price);
emit Received(msg.sender, msg.value);
moneyToReturn = msg.value - vip_price;
}
if(moneyToReturn > 0) //return amount left
payable(msg.sender).transfer(moneyToReturn);
}
//user enters details and pays
function makeReservation (string memory _name, string memory _seat) public payable{
reservationsCount += 1;
reservations[reservationsCount] = reservation(msg.sender, _name, _seat, choice);
pay();
}
/*------------------------------------------------------------*/
address owner = 0x5B38Da6a701c568545dCfcB03FcB875f56beddC4;
modifier Owner {
require(msg.sender == owner, "not eligible");
_;
}
}
我是 solidity 的新手,这是一项分配的任务。
我已经完成了所有任务,除了一个是在此基础上实现工厂模式,我不明白的是当子合约没有构造函数时如何实现工厂模式我们被明确告知不要使用更新结构值的构造函数我们必须使用函数。
此外,我们还必须使用在父合同的子合同中创建的自定义修饰符,有没有办法做到这一点?
-- 我想到的一个解决方案是在工厂合同中声明修饰符,但我可以在子合同中使用它吗?
即使没有构造函数,您也可以使用工厂模式部署新合约。
contract ConcertTicketReservationFactory {
function create() public {
// deploys the `ConcertTicketReservation` contract to a new address
ConcertTicketReservation reservationContract = new ConcertTicketReservation();
// get the newly deployed contract address
address reservationContractAddress = address(reservationContract);
// or execute its function
string memory choice = reservationContract.getChoice();
}
}
工厂合约无法读取实例的修改器或直接使用它们。如果你也想在那里使用它,你需要将修改器源代码复制到工厂合约。
// SPDX-License-Identifier: MIT
pragma solidity >=0.7.0 <0.9.0;
contract ConcertTicketReservation {
struct reservation {
address Id;
string name;
string seat;
Class choice;
}
uint256 reservationsCount = 0;
mapping (uint => reservation) public reservations; //to store reservations
enum Class{VIP, NON_VIP } //two types of seats
Class choice = Class.NON_VIP;
function setVIP() public { //set to vip
choice = Class.VIP;
}
function setNON_VIP() public {
choice = Class.NON_VIP;
}
function getChoice() public view returns (string memory) { //for my ease, to see what value has been chosen
if(choice == Class.NON_VIP) {
return "NON_VIP";
} else if(choice == Class.VIP) {
return "VIP";
}
return "NON_VIP";
}
uint non_vip_price = 0.005 ether; //price
uint vip_price = 0.01 ether;
event Received(address, uint);
function pay() internal { //pay function
uint moneyToReturn;
if(reservations[reservationsCount].choice == Class.NON_VIP) {
require (msg.value >= non_vip_price);
emit Received(msg.sender, msg.value);
moneyToReturn = msg.value - non_vip_price;
} else if(reservations[reservationsCount].choice == Class.VIP) {
require (msg.value >= vip_price);
emit Received(msg.sender, msg.value);
moneyToReturn = msg.value - vip_price;
}
if(moneyToReturn > 0) //return amount left
payable(msg.sender).transfer(moneyToReturn);
}
//user enters details and pays
function makeReservation (string memory _name, string memory _seat) public payable{
reservationsCount += 1;
reservations[reservationsCount] = reservation(msg.sender, _name, _seat, choice);
pay();
}
/*------------------------------------------------------------*/
address owner = 0x5B38Da6a701c568545dCfcB03FcB875f56beddC4;
modifier Owner {
require(msg.sender == owner, "not eligible");
_;
}
}
我是 solidity 的新手,这是一项分配的任务。 我已经完成了所有任务,除了一个是在此基础上实现工厂模式,我不明白的是当子合约没有构造函数时如何实现工厂模式我们被明确告知不要使用更新结构值的构造函数我们必须使用函数。 此外,我们还必须使用在父合同的子合同中创建的自定义修饰符,有没有办法做到这一点? -- 我想到的一个解决方案是在工厂合同中声明修饰符,但我可以在子合同中使用它吗?
即使没有构造函数,您也可以使用工厂模式部署新合约。
contract ConcertTicketReservationFactory {
function create() public {
// deploys the `ConcertTicketReservation` contract to a new address
ConcertTicketReservation reservationContract = new ConcertTicketReservation();
// get the newly deployed contract address
address reservationContractAddress = address(reservationContract);
// or execute its function
string memory choice = reservationContract.getChoice();
}
}
工厂合约无法读取实例的修改器或直接使用它们。如果你也想在那里使用它,你需要将修改器源代码复制到工厂合约。