From 33815c49f5303ae06e3b7f8df4216bc9e3c00173 Mon Sep 17 00:00:00 2001 From: Ayush Acharjya Date: Sun, 27 Oct 2024 05:29:11 +0000 Subject: [PATCH] add transfer ticket functionality --- app/transferTicket/page.tsx | 3 ++ components/ui/alert.tsx | 59 +++++++++++++++++++++++++++++++++++++ lib/transferTicket.ts | 36 ++++++++++++++++++++++ 3 files changed, 98 insertions(+) create mode 100644 app/transferTicket/page.tsx create mode 100644 components/ui/alert.tsx create mode 100644 lib/transferTicket.ts diff --git a/app/transferTicket/page.tsx b/app/transferTicket/page.tsx new file mode 100644 index 0000000..03c6677 --- /dev/null +++ b/app/transferTicket/page.tsx @@ -0,0 +1,3 @@ +export default function Page() { + return
Transfer Ticket
; +} diff --git a/components/ui/alert.tsx b/components/ui/alert.tsx new file mode 100644 index 0000000..2a99f82 --- /dev/null +++ b/components/ui/alert.tsx @@ -0,0 +1,59 @@ +import * as React from 'react'; +import { cva, type VariantProps } from 'class-variance-authority'; + +import { cn } from '@/lib/utils'; + +const alertVariants = cva( + 'relative w-full rounded-lg border px-4 py-3 text-sm [&>svg+div]:translate-y-[-3px] [&>svg]:absolute [&>svg]:left-4 [&>svg]:top-4 [&>svg]:text-foreground [&>svg~*]:pl-7', + { + variants: { + variant: { + default: 'bg-background text-foreground', + destructive: + 'border-destructive/50 text-destructive dark:border-destructive [&>svg]:text-destructive', + }, + }, + defaultVariants: { + variant: 'default', + }, + } +); + +const Alert = React.forwardRef< + HTMLDivElement, + React.HTMLAttributes & VariantProps +>(({ className, variant, ...props }, ref) => ( +
+)); +Alert.displayName = 'Alert'; + +const AlertTitle = React.forwardRef< + HTMLParagraphElement, + React.HTMLAttributes +>(({ className, ...props }, ref) => ( +
+)); +AlertTitle.displayName = 'AlertTitle'; + +const AlertDescription = React.forwardRef< + HTMLParagraphElement, + React.HTMLAttributes +>(({ className, ...props }, ref) => ( +
+)); +AlertDescription.displayName = 'AlertDescription'; + +export { Alert, AlertTitle, AlertDescription }; diff --git a/lib/transferTicket.ts b/lib/transferTicket.ts new file mode 100644 index 0000000..dcb340c --- /dev/null +++ b/lib/transferTicket.ts @@ -0,0 +1,36 @@ +import { ethers } from 'ethers'; +import { getContract } from './ethers'; + +declare global { + interface Window { + ethereumProvider?: ethers.providers.ExternalProvider & { + isMetaMask?: boolean; + request?: (method: string, params?: unknown[]) => Promise; + }; + } +} + +export const transferTicket = async ( + ticketId: number, + receiverAddress: string +): Promise => { + try { + if (typeof window.ethereumProvider === 'undefined') { + throw new Error('Please install MetaMask or another Ethereum wallet'); + } + await window.ethereumProvider.request?.({ method: 'eth_requestAccounts' }); + const provider = new ethers.providers.Web3Provider(window.ethereumProvider); + const signer = provider.getSigner(); + const contract = getContract().connect(signer); + if (!ethers.utils.isAddress(receiverAddress)) { + throw new Error('Invalid receiver address'); + } + + const tx = await contract.transferTicket(ticketId, receiverAddress); + const receipt = await tx.wait(); + return receipt.transactionHash; + } catch (error) { + console.error('Error transferring ticket:', error); + throw error; + } +};