Check wallet balance in real time during collection creation

This commit is contained in:
Serkan Reis 2023-07-05 09:01:10 +03:00
parent 8a7f093d39
commit 609b7fce47

View File

@ -147,9 +147,16 @@ const CollectionCreationPage: NextPage = () => {
.then(() => { .then(() => {
checkWhitelistDetails() checkWhitelistDetails()
.then(() => { .then(() => {
checkwalletBalance() void checkwalletBalance()
.then(() => {
setReadyToCreateVm(true) 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) => { .catch((error) => {
if (String(error.message).includes('Insufficient wallet balance')) { if (String(error.message).includes('Insufficient wallet balance')) {
toast.error(`${error.message}`, { style: { maxWidth: 'none' } }) toast.error(`${error.message}`, { style: { maxWidth: 'none' } })
@ -191,9 +198,16 @@ const CollectionCreationPage: NextPage = () => {
.then(() => { .then(() => {
checkWhitelistDetails() checkWhitelistDetails()
.then(() => { .then(() => {
checkwalletBalance() void checkwalletBalance()
.then(() => {
setReadyToCreateBm(true) 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) => { .catch((error) => {
toast.error(`${error.message}`, { style: { maxWidth: 'none' } }) toast.error(`${error.message}`, { style: { maxWidth: 'none' } })
addLogItem({ id: uid(), message: error.message, type: 'Error', timestamp: new Date() }) addLogItem({ id: uid(), message: error.message, type: 'Error', timestamp: new Date() })
@ -1104,8 +1118,8 @@ const CollectionCreationPage: NextPage = () => {
} }
} }
const checkwalletBalance = () => { const checkwalletBalance = async () => {
if (!wallet.initialized) throw new Error('Wallet not connected.') const walletBalance = await wallet.client?.getBalance(wallet.address, 'ustars').then((balance) => {
if (minterType === 'vending' && whitelistDetails?.whitelistState === 'new' && whitelistDetails.memberLimit) { if (minterType === 'vending' && whitelistDetails?.whitelistState === 'new' && whitelistDetails.memberLimit) {
const amountNeeded = const amountNeeded =
Math.ceil(Number(whitelistDetails.memberLimit) / 1000) * 100000000 + Math.ceil(Number(whitelistDetails.memberLimit) / 1000) * 100000000 +
@ -1114,7 +1128,7 @@ const CollectionCreationPage: NextPage = () => {
: collectionDetails?.updatable : collectionDetails?.updatable
? Number(vendingMinterUpdatableCreationFee) ? Number(vendingMinterUpdatableCreationFee)
: Number(vendingMinterCreationFee)) : Number(vendingMinterCreationFee))
if (amountNeeded >= Number(wallet.balance[0].amount)) if (amountNeeded >= Number(balance.amount))
throw new Error( throw new Error(
`Insufficient wallet balance to instantiate the required contracts. Needed amount: ${( `Insufficient wallet balance to instantiate the required contracts. Needed amount: ${(
amountNeeded / 1000000 amountNeeded / 1000000
@ -1131,13 +1145,14 @@ const CollectionCreationPage: NextPage = () => {
: collectionDetails?.updatable : collectionDetails?.updatable
? Number(baseMinterUpdatableCreationFee) ? Number(baseMinterUpdatableCreationFee)
: Number(baseMinterCreationFee) : Number(baseMinterCreationFee)
if (amountNeeded >= Number(wallet.balance[0].amount)) if (amountNeeded >= Number(balance.amount))
throw new Error( throw new Error(
`Insufficient wallet balance to instantiate the required contracts. Needed amount: ${( `Insufficient wallet balance to instantiate the required contracts. Needed amount: ${(
amountNeeded / 1000000 amountNeeded / 1000000
).toString()} STARS`, ).toString()} STARS`,
) )
} }
})
} }
const syncCollections = useCallback(async () => { const syncCollections = useCallback(async () => {