fix multiple tickers + abi

This commit is contained in:
Ayush Acharjya
2024-10-27 01:03:00 +01:00
parent c5b020d71d
commit 85ad44cc64
3 changed files with 551 additions and 537 deletions

View File

@@ -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',
});
}
};