Skip to content

Commit

Permalink
STUD-347: Updates create sale logic to ensure a stream token remains …
Browse files Browse the repository at this point in the history
…in wallet (#809)

* updates create sale logic

* minor comment update
  • Loading branch information
scandycuz authored Nov 14, 2024
1 parent a233f6a commit 3820ecf
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 20 deletions.
19 changes: 5 additions & 14 deletions apps/studio/src/modules/sale/hooks.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { useEffect } from "react";
import { useGetSaleCountQuery } from "./api";
import { useGetUserWalletSongsThunk, useHasSongAccess } from "../song";
import { useGetUserWalletSongsThunk } from "../song";

/**
* Determines whether a user is the owner of a song. Checks
Expand All @@ -10,16 +9,9 @@ import { useGetUserWalletSongsThunk, useHasSongAccess } from "../song";
* user doesn't have any of the song's stream tokens in their wallet
* because a sale was created for all of them.
*/
export const useIsStreamTokenOwner = (songId: string) => {
const hasAccess = useHasSongAccess(songId);
const [
getUserWalletSongs,
{ data: walletSongsResponse, isLoading: isWalletSongsLoading },
] = useGetUserWalletSongsThunk();
const { data: countData, isLoading: isSaleCountLoading } =
useGetSaleCountQuery({
songIds: [songId],
});
export const useHasTokens = (songId: string) => {
const [getUserWalletSongs, { data: walletSongsResponse, isLoading }] =
useGetUserWalletSongsThunk();

useEffect(() => {
getUserWalletSongs({
Expand All @@ -28,11 +20,10 @@ export const useIsStreamTokenOwner = (songId: string) => {
});
}, [getUserWalletSongs, songId]);

const isLoading = isWalletSongsLoading || isSaleCountLoading;
const hasTokens = walletSongsResponse?.data?.songs[0]?.song?.id === songId;

return {
hasTokens,
isLoading,
isOwner: hasTokens || (hasAccess && !!countData?.count),
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ export const CreateSale = () => {
bundleAmount: SALE_DEFAULT_BUNDLE_AMOUNT,
bundleAssetName: currentSong.song.nftName,
bundlePolicyId: currentSong.song.nftPolicyId,
costAmount: values.totalSaleValue / values.tokensToSell,
// ensure value is an integer
costAmount: Math.floor(values.totalSaleValue / values.tokensToSell),
email: profileData?.email,
saleCurrency: values.saleCurrency,
songId,
Expand Down Expand Up @@ -93,8 +94,8 @@ export const CreateSale = () => {
.integer("You must sell a whole number of stream tokens")
.min(1, "You must sell at least 1 stream token")
.max(
streamTokensInWallet,
`You only have ${formattedStreamTokensInWallet} stream tokens available to sell.`
streamTokensInWallet - 1,
"It is required to keep at least one stream token in your wallet."
),
totalSaleValue: Yup.number()
.required("This field is required")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import { Sale } from "./Sale";
import { ConnectWallet } from "./ConnectWallet";
import { MarketplaceTabSkeleton } from "../../../../components";
import { SongRouteParams } from "../types";
import { useIsStreamTokenOwner } from "../../../../modules/sale";
import { useHasTokens } from "../../../../modules/sale";

export const MarketplaceTab = () => {
const { songId } = useParams<"songId">() as SongRouteParams;

const { isOwner, isLoading } = useIsStreamTokenOwner(songId);
const { hasTokens, isLoading } = useHasTokens(songId);

if (isLoading) {
return (
Expand All @@ -30,7 +30,7 @@ export const MarketplaceTab = () => {
<Box
maxWidth={ [theme.inputField.maxWidth, theme.inputField.maxWidth, "700px"] }
>
{ isOwner ? <Sale /> : <ConnectWallet /> }
{ hasTokens ? <Sale /> : <ConnectWallet /> }
</Box>
);
};

0 comments on commit 3820ecf

Please sign in to comment.