From 609b7fce4792a9d28e8ef2245915f37f8ab70a5a Mon Sep 17 00:00:00 2001 From: Serkan Reis Date: Wed, 5 Jul 2023 09:01:10 +0300 Subject: [PATCH] Check wallet balance in real time during collection creation --- pages/collections/create.tsx | 85 +++++++++++++++++++++--------------- 1 file changed, 50 insertions(+), 35 deletions(-) diff --git a/pages/collections/create.tsx b/pages/collections/create.tsx index c732ef3..082fe2b 100644 --- a/pages/collections/create.tsx +++ b/pages/collections/create.tsx @@ -147,8 +147,15 @@ const CollectionCreationPage: NextPage = () => { .then(() => { checkWhitelistDetails() .then(() => { - checkwalletBalance() - setReadyToCreateVm(true) + void checkwalletBalance() + .then(() => { + setReadyToCreateVm(true) + }) + .catch((error) => { + toast.error(`${error.message}`, { style: { maxWidth: 'none' } }) + addLogItem({ id: uid(), message: error.message, type: 'Error', timestamp: new Date() }) + setReadyToCreateVm(false) + }) }) .catch((error) => { if (String(error.message).includes('Insufficient wallet balance')) { @@ -191,8 +198,15 @@ const CollectionCreationPage: NextPage = () => { .then(() => { checkWhitelistDetails() .then(() => { - checkwalletBalance() - setReadyToCreateBm(true) + void checkwalletBalance() + .then(() => { + setReadyToCreateBm(true) + }) + .catch((error) => { + toast.error(`${error.message}`, { style: { maxWidth: 'none' } }) + addLogItem({ id: uid(), message: error.message, type: 'Error', timestamp: new Date() }) + setReadyToCreateVm(false) + }) }) .catch((error) => { toast.error(`${error.message}`, { style: { maxWidth: 'none' } }) @@ -1104,40 +1118,41 @@ const CollectionCreationPage: NextPage = () => { } } - const checkwalletBalance = () => { - if (!wallet.initialized) throw new Error('Wallet not connected.') - if (minterType === 'vending' && whitelistDetails?.whitelistState === 'new' && whitelistDetails.memberLimit) { - const amountNeeded = - Math.ceil(Number(whitelistDetails.memberLimit) / 1000) * 100000000 + - (whitelistDetails.whitelistType === 'flex' - ? Number(vendingMinterFlexCreationFee) - : collectionDetails?.updatable - ? Number(vendingMinterUpdatableCreationFee) - : Number(vendingMinterCreationFee)) - if (amountNeeded >= Number(wallet.balance[0].amount)) - throw new Error( - `Insufficient wallet balance to instantiate the required contracts. Needed amount: ${( - amountNeeded / 1000000 - ).toString()} STARS`, - ) - } else { - const amountNeeded = - minterType === 'vending' - ? whitelistDetails?.whitelistState === 'existing' && whitelistDetails.whitelistType === 'flex' + const checkwalletBalance = async () => { + const walletBalance = await wallet.client?.getBalance(wallet.address, 'ustars').then((balance) => { + if (minterType === 'vending' && whitelistDetails?.whitelistState === 'new' && whitelistDetails.memberLimit) { + const amountNeeded = + Math.ceil(Number(whitelistDetails.memberLimit) / 1000) * 100000000 + + (whitelistDetails.whitelistType === 'flex' ? Number(vendingMinterFlexCreationFee) : collectionDetails?.updatable ? Number(vendingMinterUpdatableCreationFee) - : Number(vendingMinterCreationFee) - : collectionDetails?.updatable - ? Number(baseMinterUpdatableCreationFee) - : Number(baseMinterCreationFee) - if (amountNeeded >= Number(wallet.balance[0].amount)) - throw new Error( - `Insufficient wallet balance to instantiate the required contracts. Needed amount: ${( - amountNeeded / 1000000 - ).toString()} STARS`, - ) - } + : Number(vendingMinterCreationFee)) + if (amountNeeded >= Number(balance.amount)) + throw new Error( + `Insufficient wallet balance to instantiate the required contracts. Needed amount: ${( + amountNeeded / 1000000 + ).toString()} STARS`, + ) + } else { + const amountNeeded = + minterType === 'vending' + ? whitelistDetails?.whitelistState === 'existing' && whitelistDetails.whitelistType === 'flex' + ? Number(vendingMinterFlexCreationFee) + : collectionDetails?.updatable + ? Number(vendingMinterUpdatableCreationFee) + : Number(vendingMinterCreationFee) + : collectionDetails?.updatable + ? Number(baseMinterUpdatableCreationFee) + : Number(baseMinterCreationFee) + if (amountNeeded >= Number(balance.amount)) + throw new Error( + `Insufficient wallet balance to instantiate the required contracts. Needed amount: ${( + amountNeeded / 1000000 + ).toString()} STARS`, + ) + } + }) } const syncCollections = useCallback(async () => {