I'm trying to build a simple ticketing application in which buyers are able to buy a ticket (i.e. NFT) from a seller. Note: I don't have a lot of experience in coding, but starting to understand the concepts within Solidity decently.
For me it makes most sense if the seller creates a contract with max tickets, price etc. from which the buyer is able to mint if the correct amount is transferred (saves on gas fees compared to first minting and then selling). See below code. As you notice, only the deployer is able to change the arguments to list multiple events (i.e. occasions) with different parameters.
My questions relates to the deployment of the contracts as I want different sellers being able to deploy the contract from the frond-end with a simple button and some inputs, after which the buyers are able to mint the tickets/NFTs from these contracts. Is this possible and if yes, how? If someone has a better solution, also fine! :)
Cheers,
import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
contract TicketBlock is ERC721 {
address public owner;
uint public numOccasions;
uint public totalSupply;
struct Occasion {
uint id;
string name;
uint cost;
uint tickets;
uint maxTickets;
}
mapping(uint => Occasion) occasions;
mapping(uint => mapping(address => bool)) public hasBought;
modifier onlyOwner() {
require(msg.sender == owner);
_;
}
constructor(string memory _name, string memory _symbol) ERC721 (_name, _symbol) {
owner = msg.sender;
}
function list(string memory _name, uint _cost, uint _maxTickets) public onlyOwner {
numOccasions++;
occasions[numOccasions] = Occasion(
numOccasions,
_name,
_cost,
_maxTickets,
_maxTickets
);
}
function mint (uint _id, uint _seat) public payable {
require(_id != 0);
require(_id <= numOccasions);
require(msg.value >= occasions[_id].cost);
occasions[_id].tickets -= 1; // <-- Update ticket account
hasBought[_id][msg.sender] = true; // <-- Update buying status
totalSupply++;
_safeMint(msg.sender,totalSupply);
}
}
you have to implement factory contract logic:
an example of factory contract would be:
in
createTicketfunction you seenewkeyword. I explained here What is the difference between creating a new solidity contract with and without the `new` keyword?To integrate this with a front-end, you would interact with the TicketFactory contract, you import the deployedfactory contract
you should have a form to create a Ticket contract, so after user enters the inputs for the
createTicketfunction parameters, you submit the form. so you should have anonSubmitmethod and inside