From 4c004cde04d37407893c087b8c40891fe1b64837 Mon Sep 17 00:00:00 2001 From: Trevor Scandalios <5877597+scandycuz@users.noreply.github.com> Date: Wed, 20 Nov 2024 17:51:32 -0800 Subject: [PATCH] throws error if purchasing stream tokens without any newm (#830) --- apps/marketplace/src/modules/sale/thunks.ts | 22 +++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/apps/marketplace/src/modules/sale/thunks.ts b/apps/marketplace/src/modules/sale/thunks.ts index e0691e92..a6261512 100644 --- a/apps/marketplace/src/modules/sale/thunks.ts +++ b/apps/marketplace/src/modules/sale/thunks.ts @@ -1,8 +1,9 @@ import { createAsyncThunk } from "@reduxjs/toolkit"; -import { asThunkHook } from "@newm-web/utils"; +import { NEWM_ASSET_NAME, NEWM_POLICY_ID, asThunkHook } from "@newm-web/utils"; import { enableWallet, getWalletChangeAddress, + getWalletTokenBalance, signWalletTransaction, } from "@newm.io/cardano-dapp-wallet-connector"; import { GenerateOrderRequest } from "@newm-web/types"; @@ -13,6 +14,21 @@ export const purchaseStreamTokens = createAsyncThunk( "sale/purchaseStreamTokens", async (body: GenerateOrderRequest, { dispatch }) => { try { + const wallet = await enableWallet(); + + const newmBalance = await getWalletTokenBalance( + wallet, + NEWM_POLICY_ID, + NEWM_ASSET_NAME + ); + + if (!newmBalance) { + throw new Error( + `Insufficient NEWM tokens in wallet. Please add NEWM tokens to + your wallet and try again.` + ); + } + const orderResp = await dispatch( saleApi.endpoints.generateOrder.initiate(body) ); @@ -20,14 +36,12 @@ export const purchaseStreamTokens = createAsyncThunk( if ("error" in orderResp || !orderResp.data) return; const { orderId, amountCborHex } = orderResp.data; - const wallet = await enableWallet(); const changeAddress = await getWalletChangeAddress(wallet); const utxoCborHexList = await wallet.getUtxos(amountCborHex); if (!utxoCborHexList) { throw new Error( - `Insufficient NEWM tokens in wallet. Please add NEWM tokens to - your wallet and try again.` + "Insufficient balance in wallet. Please add funds and try again." ); }