mirror of
https://github.com/0xShay/ticketchain.git
synced 2026-01-11 21:23:24 +00:00
fix multiple tickers + abi
This commit is contained in:
@@ -33,42 +33,54 @@ export const buyHandler = async (
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
if (typeof window.ethereum === 'undefined') {
|
||||
toast({
|
||||
title:
|
||||
'You might be asked to approve multiple transactions if you buy multiple tickets',
|
||||
});
|
||||
|
||||
while (numTickets > 0) {
|
||||
try {
|
||||
if (typeof window.ethereum === 'undefined') {
|
||||
toast({
|
||||
title: 'Please install MetaMask or another Ethereum wallet',
|
||||
variant: 'destructive',
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
const provider = new ethers.providers.Web3Provider(window.ethereum);
|
||||
const signer = provider.getSigner();
|
||||
const contract = getContract().connect(signer);
|
||||
|
||||
const singleTicketCost = await contract.getEventPriceFlare(eventId);
|
||||
const totalTicketCost = singleTicketCost
|
||||
.mul(numTickets)
|
||||
.mul(105)
|
||||
.div(100);
|
||||
|
||||
const balance = await provider.getBalance(await signer.getAddress());
|
||||
if (balance.lt(totalTicketCost)) {
|
||||
toast({
|
||||
title: 'Insufficient balance to cover ticket cost and gas fees.',
|
||||
variant: 'destructive',
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
const tx = await contract.buyTicket(eventId, { value: totalTicketCost });
|
||||
const receipt = await tx.wait();
|
||||
|
||||
toast({
|
||||
title: 'Please install MetaMask or another Ethereum wallet',
|
||||
title: `Tickets purchased successfully! Transaction Hash: ${receipt.transactionHash}`,
|
||||
});
|
||||
|
||||
numTickets -= 1;
|
||||
} catch (error) {
|
||||
console.error('Error buying tickets:', error);
|
||||
toast({
|
||||
title: 'Transaction failed. Please try again.',
|
||||
variant: 'destructive',
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
const provider = new ethers.providers.Web3Provider(window.ethereum);
|
||||
const signer = provider.getSigner();
|
||||
const contract = getContract().connect(signer);
|
||||
|
||||
const singleTicketCost = await contract.getEventPriceFlare(eventId);
|
||||
const totalTicketCost = singleTicketCost.mul(numTickets).mul(105).div(100);
|
||||
|
||||
const balance = await provider.getBalance(await signer.getAddress());
|
||||
if (balance.lt(totalTicketCost)) {
|
||||
toast({
|
||||
title: 'Insufficient balance to cover ticket cost and gas fees.',
|
||||
variant: 'destructive',
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
const tx = await contract.buyTicket(eventId, { value: totalTicketCost });
|
||||
const receipt = await tx.wait();
|
||||
|
||||
toast({
|
||||
title: `Tickets purchased successfully! Transaction Hash: ${receipt.transactionHash}`,
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('Error buying tickets:', error);
|
||||
toast({
|
||||
title: 'Transaction failed. Please try again.',
|
||||
variant: 'destructive',
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user