From 7fa7911b88320abe451527161161611be8f6c6e7 Mon Sep 17 00:00:00 2001 From: ayomaska18 Date: Sat, 26 Oct 2024 11:44:47 +0100 Subject: [PATCH] updated sc and ethers.ts --- contracts/EventManager.sol | 21 +++++- lib/ethers.ts | 130 ++++++++++++++++++++++++++++++++++++- 2 files changed, 145 insertions(+), 6 deletions(-) diff --git a/contracts/EventManager.sol b/contracts/EventManager.sol index f68a7a7..d59c3f7 100644 --- a/contracts/EventManager.sol +++ b/contracts/EventManager.sol @@ -38,6 +38,11 @@ contract EventManager { uint256 eventId; } + event EventCreated(uint256 eventId, string name, uint256 eventDate); + event TicketPurchased(uint256 ticketId, uint256 eventId, address buyer, uint256 price); + event TicketTransferred(uint256 ticketId, address from, address to); + event TicketTransferApproved(uint256 ticketId, address owner, address trustee); + mapping(uint256 => Event) public events; mapping(uint256 => Ticket) public tickets; @@ -88,6 +93,7 @@ contract EventManager { function createEvent(string memory _name, string memory _description, uint256 _capacity, uint256 _ticketPrice, uint256 _eventDate, string[] memory _images) public returns (uint256 _eventId) { events[eventCounter] = Event(_name, _description, _capacity, 0, _ticketPrice, _eventDate, _images, new uint256[](0), payable(msg.sender)); eventCounter++; + emit EventCreated(eventCounter - 1, _name, _eventDate); return eventCounter - 1; } @@ -129,6 +135,7 @@ contract EventManager { (bool sentToHost, ) = events[_eventId].eventHost.call{value: ticketCost}(""); require(sentToHost, "Failed to send FLR to event host"); + emit TicketPurchased(ticketCounter - 1, _eventId, msg.sender, ticketCost); return ticketCounter - 1; } @@ -159,20 +166,28 @@ contract EventManager { userTickets[_to].push(_ticketId); tickets[_ticketId].holder = _to; + + emit TicketTransferred(_ticketId, prevHolder, _to); } function approveTicket(uint256 _ticketId, address _to, bool _allowed) public { require(_ticketId < ticketCounter, "Invalid ticket ID"); require(tickets[_ticketId].holder == msg.sender, "You do not own this ticket"); ticketAllowance[_ticketId][_to] = _allowed; + + emit TicketTransferApproved(_ticketId, msg.sender, _to); } - function transferTicket(uint256 _ticketId, address _to) public { - require(_ticketId < ticketCounter, "Invalid ticket ID"); - require(tickets[_ticketId].holder == msg.sender || tickets[_ticketId].holder == msg.sender, "You do not own this ticket"); + function transferTicketFrom(uint256 _ticketId, address _to) public { require(ticketAllowance[_ticketId][msg.sender], "You are not allowed to transfer this ticket"); ticketAllowance[_ticketId][msg.sender] = false; transferTicketForce(_ticketId, _to); } + function transferTicket(uint256 _ticketId, address _to) public { + require(_ticketId < ticketCounter, "Invalid ticket ID"); + require(tickets[_ticketId].holder == msg.sender, "You do not own this ticket"); + transferTicketForce(_ticketId, _to); + } + } \ No newline at end of file diff --git a/lib/ethers.ts b/lib/ethers.ts index 304b2e5..3c4c788 100644 --- a/lib/ethers.ts +++ b/lib/ethers.ts @@ -2,7 +2,7 @@ import { ethers } from 'ethers'; const FLARE_TESTNET_RPC_URL = 'https://coston2.enosys.global/ext/C/rpc'; -const CONTRACT_ADDRESS = '0xa67f64937a0a4daf3b5f5Eea7903d1E81d375b7b'; +const CONTRACT_ADDRESS = '0xA4E90EF8f5846C568515e9120635edb1f2776842'; export function getFlareProvider() { const flareRpcUrl = FLARE_TESTNET_RPC_URL; @@ -100,6 +100,117 @@ export function getContract() { stateMutability: 'nonpayable', type: 'function', }, + { + inputs: [], + stateMutability: 'nonpayable', + type: 'constructor', + }, + { + anonymous: false, + inputs: [ + { + indexed: false, + internalType: 'uint256', + name: 'eventId', + type: 'uint256', + }, + { + indexed: false, + internalType: 'string', + name: 'name', + type: 'string', + }, + { + indexed: false, + internalType: 'uint256', + name: 'eventDate', + type: 'uint256', + }, + ], + name: 'EventCreated', + type: 'event', + }, + { + anonymous: false, + inputs: [ + { + indexed: false, + internalType: 'uint256', + name: 'ticketId', + type: 'uint256', + }, + { + indexed: false, + internalType: 'uint256', + name: 'eventId', + type: 'uint256', + }, + { + indexed: false, + internalType: 'address', + name: 'buyer', + type: 'address', + }, + { + indexed: false, + internalType: 'uint256', + name: 'price', + type: 'uint256', + }, + ], + name: 'TicketPurchased', + type: 'event', + }, + { + anonymous: false, + inputs: [ + { + indexed: false, + internalType: 'uint256', + name: 'ticketId', + type: 'uint256', + }, + { + indexed: false, + internalType: 'address', + name: 'owner', + type: 'address', + }, + { + indexed: false, + internalType: 'address', + name: 'trustee', + type: 'address', + }, + ], + name: 'TicketTransferApproved', + type: 'event', + }, + { + anonymous: false, + inputs: [ + { + indexed: false, + internalType: 'uint256', + name: 'ticketId', + type: 'uint256', + }, + { + indexed: false, + internalType: 'address', + name: 'from', + type: 'address', + }, + { + indexed: false, + internalType: 'address', + name: 'to', + type: 'address', + }, + ], + name: 'TicketTransferred', + type: 'event', + }, { inputs: [ { @@ -119,9 +230,22 @@ export function getContract() { type: 'function', }, { - inputs: [], + inputs: [ + { + internalType: 'uint256', + name: '_ticketId', + type: 'uint256', + }, + { + internalType: 'address', + name: '_to', + type: 'address', + }, + ], + name: 'transferTicketFrom', + outputs: [], stateMutability: 'nonpayable', - type: 'constructor', + type: 'function', }, { inputs: [