Skip to content

Commit

Permalink
throws error if purchasing stream tokens without any newm (#830)
Browse files Browse the repository at this point in the history
  • Loading branch information
scandycuz authored Nov 21, 2024
1 parent b4902d4 commit 4c004cd
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions apps/marketplace/src/modules/sale/thunks.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -13,21 +14,34 @@ 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)
);

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."
);
}

Expand Down

0 comments on commit 4c004cd

Please sign in to comment.