Skip to content

Instantly share code, notes, and snippets.

@DonGuillotine
Created December 19, 2024 19:42
Show Gist options
  • Save DonGuillotine/d2477524da70fed5363d108ffef37b25 to your computer and use it in GitHub Desktop.
Save DonGuillotine/d2477524da70fed5363d108ffef37b25 to your computer and use it in GitHub Desktop.
  1. Date Handling You're passing the Date objects directly to createEvent, but they need to be converted to Unix timestamps first. Here's how to fix this:
const handleSubmit = (e: React.FormEvent) => {
  e.preventDefault();
  if (isConnected) {
    setIsSubmitting(true);

    if (validateForm()) {
      // Combine date and time
      const combinedStartDate = startDate && startTime ? 
        new Date(
          startDate.getFullYear(),
          startDate.getMonth(),
          startDate.getDate(),
          startTime.getHours(),
          startTime.getMinutes()
        ) : null;

      const combinedEndDate = isOneTime && endDate && endTime ? 
        new Date(
          endDate.getFullYear(),
          endDate.getMonth(),
          endDate.getDate(),
          endTime.getHours(),
          endTime.getMinutes()
        ) : null;

      // Convert to BigInt timestamps
      const startTimestamp = combinedStartDate ? 
        BigInt(Math.floor(combinedStartDate.getTime() / 1000)) : 
        BigInt(0);

      const endTimestamp = combinedEndDate ? 
        BigInt(Math.floor(combinedEndDate.getTime() / 1000)) : 
        BigInt(0);

      // Convert price to Wei
      const priceInWei = ticketPrice ? 
        parseEther(ticketPrice.toString()) : 
        BigInt(0);

      // Ensure capacity is within uint16 range
      const safeCapacity = capacity ? 
        Math.min(Math.max(1, capacity), 65535) : 
        1;

      try {
        createEvent(
          safeCapacity,
          priceInWei,
          startTimestamp,
          endTimestamp,
          event.name.trim(),
          selectedImage || "",
          event.description.trim(),
          event.location.trim(),
          !isPublic
        );
      } catch (error) {
        console.error('Event creation error:', error);
        toast.error('Failed to create event');
      }
    }
  } else {
    setIsSubmitting(false);
    toast.error("Please connect your wallet first");
  }
};
  1. Input Validation Add stronger validation in your form:
const validateForm = () => {
  const newErrors: { [key: string]: string } = {};

  if (!event.name.trim()) {
    newErrors.name = "Event name is required";
  }

  if (!event.location.trim()) {
    newErrors.location = "Event location is required";
  }

  if (!startDate || !startTime) {
    newErrors.startDate = "Start date and time are required";
  }

  if (isOneTime && (!endDate || !endTime)) {
    newErrors.endDate = "End date and time are required for one-time events";
  }

  if (capacity === null || capacity <= 0 || capacity > 65535) {
    newErrors.capacity = "Capacity must be between 1 and 65535";
  }

  if (ticketPrice && ticketPrice < 0) {
    newErrors.ticketPrice = "Ticket price cannot be negative";
  }

  const now = new Date();
  const startDateTime = startDate && startTime ? new Date(
    startDate.getFullYear(),
    startDate.getMonth(),
    startDate.getDate(),
    startTime.getHours(),
    startTime.getMinutes()
  ) : null;

  if (startDateTime && startDateTime <= now) {
    newErrors.startDate = "Start date must be in the future";
  }

  setErrors(newErrors);
  return Object.keys(newErrors).length === 0;
};
  1. Add Debug Logging Add this before the createEvent call:
console.log('Creating event with parameters:', {
  capacity: safeCapacity,
  priceInWei: priceInWei.toString(),
  startTimestamp: startTimestamp.toString(),
  endTimestamp: endTimestamp.toString(),
  name: event.name.trim(),
  banner: selectedImage || "",
  description: event.description.trim(),
  location: event.location.trim(),
  isPrivate: !isPublic
});
  1. Error Handling in useCreateEvent Update your error handling in the hook:
function parseContractError(error: any): string {
  console.error('Contract error details:', error);
  
  if (error?.shortMessage) return error.shortMessage;
  
  const errorMap: Record<string, string> = {
    'EventDateInPast': 'Event date must be in the future',
    'InvalidEndDate': 'End date must be after start date',
    'execution reverted': 'Transaction failed - check your inputs',
    'insufficient funds': 'Insufficient funds for gas',
    'gas required exceeds allowance': 'Transaction would fail - check your inputs'
  };

  for (const [key, message] of Object.entries(errorMap)) {
    if (error?.message?.toLowerCase().includes(key.toLowerCase())) {
      return message;
    }
  }

  return 'An unexpected error occurred. Please check your inputs and try again.';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment