Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make use-mint-quote generic #299

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions app/features/accounts/account-selector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ import { type Currency, Money } from '~/lib/money';

export type AccountType = 'cashu' | 'nwc';

export type Account = {
export type Account<C extends Currency = Currency> = {
id: string;
name: string;
currency: Currency;
currency: C;
type: AccountType;
balance: Big;
} & (
Expand Down
41 changes: 27 additions & 14 deletions app/features/receive/receive-cashu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { Skeleton } from '~/components/ui/skeleton';
import { getDefaultUnit } from '~/features/shared/currencies';
import { useExchangeRate } from '~/hooks/use-exchange-rate';
import { useToast } from '~/hooks/use-toast';
import type { Money } from '~/lib/money';
import type { Currency, CurrencyUnit, Money } from '~/lib/money';
import { cn } from '~/lib/utils';
import type { Account } from '../accounts/account-selector';
import { getCashuRequest } from './reusable-payment-request';
Expand Down Expand Up @@ -84,13 +84,19 @@ function QRCarouselItem({
);
}

type CashuRequestQRProps = {
account: Account & { type: 'cashu' };
amount: Money;
type CashuRequestQRProps<C extends Currency> = {
account: Account<C> & { type: 'cashu' };
amount: Money<C>;
};

function CashuRequestQRItem({ account, amount }: CashuRequestQRProps) {
const cashuUnit = account.currency === 'USD' ? 'usd' : 'sat';
function CashuRequestQRItem<C extends Currency>({
account,
amount,
}: CashuRequestQRProps<C>) {
const cashuUnit = (account.currency === 'USD' ? 'usd' : 'sat') as Extract<
CurrencyUnit<C>,
'usd' | 'sat'
>;
// TODO: this should come from some hook that does a similar thing to the mint quote hook
const cashuRequest = getCashuRequest(account, {
amount,
Expand All @@ -106,13 +112,17 @@ function CashuRequestQRItem({ account, amount }: CashuRequestQRProps) {
);
}

type MintQuoteProps = {
account: Account & { type: 'cashu' };
amount: Money;
type MintQuoteProps<C extends Currency> = {
account: Account<C> & { type: 'cashu' };
amount: Money<C>;
isVisible: boolean;
};

function MintQuoteItem({ account, amount, isVisible }: MintQuoteProps) {
function MintQuoteItem<C extends Currency>({
account,
amount,
isVisible,
}: MintQuoteProps<C>) {
const { mintQuote, createQuote, fetchError, checkError, isLoading } =
useMintQuote({
account,
Expand Down Expand Up @@ -203,12 +213,15 @@ function CarouselControls({
);
}

type Props = {
amount: Money;
account: Account & { type: 'cashu' };
type Props<C extends Currency> = {
amount: Money<C>;
account: Account<C> & { type: 'cashu' };
};

export default function ReceiveCashu({ amount, account }: Props) {
export default function ReceiveCashu<C extends Currency>({
amount,
account,
}: Props<C>) {
const { current, scrollToIndex, setApi } = useCarousel();
const { data: rate, error: exchangeRateError } = useExchangeRate(
`${amount.currency}-${amount.currency === 'BTC' ? 'USD' : 'BTC'}`,
Expand Down
10 changes: 5 additions & 5 deletions app/features/receive/reusable-payment-request.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ import {
PageHeader,
PageHeaderTitle,
} from '~/components/page';
import type { Money } from '~/lib/money';
import type { Currency, CurrencyUnit, Money } from '~/lib/money';
import type { Account } from '../accounts/account-selector';

export const getCashuRequest = (
account: Account & { type: 'cashu' },
export const getCashuRequest = <C extends Currency>(
account: Account<C> & { type: 'cashu' },
opts?: {
amount?: Money;
amount?: Money<C>;
description?: string;
unit?: 'sat' | 'usd';
unit?: Extract<CurrencyUnit<C>, 'sat' | 'usd'>;
singleUse?: boolean;
},
): PaymentRequest => {
Expand Down
23 changes: 12 additions & 11 deletions app/features/receive/use-mint-quote.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,34 @@
import { CashuMint, CashuWallet, MintQuoteState } from '@cashu/cashu-ts';
import { useMutation, useQuery } from '@tanstack/react-query';
import type { Money } from '~/lib/money';
import type { Currency, CurrencyUnit, Money } from '~/lib/money';
import type { Account } from '../accounts/account-selector';

type UseMintQuoteProps = {
type UseMintQuoteProps<C extends Currency> = {
/** The Cashu account to create a mint quote for. */
account: Account & { type: 'cashu' };
account: Account<C> & { type: 'cashu' };
/**
* The amount to create a mint quote for.
* The amount's currency must match the account's currency.
*/
amount: Money;
amount: Money<C>;
};

/**
* A hook to create a mint quote for a Cashu account and then poll the
* status of the quote
*/
export function useMintQuote({ account, amount }: UseMintQuoteProps) {
const cashuUnit = account.currency === 'USD' ? 'usd' : 'sat';
const moneyUnit = cashuUnit === 'usd' ? 'cent' : 'sat';
export function useMintQuote<C extends Currency>({
account,
amount,
}: UseMintQuoteProps<C>) {
const cashuUnit = (
account.currency === 'USD' ? 'usd' : 'sat'
) as CurrencyUnit<C>;
const moneyUnit = (cashuUnit === 'usd' ? 'cent' : 'sat') as CurrencyUnit<C>;
const wallet = new CashuWallet(new CashuMint(account.mintUrl), {
unit: cashuUnit,
});

if (amount.currency !== account.currency) {
throw new Error('Amount currency must match account currency');
}

const {
status,
error,
Expand Down
2 changes: 1 addition & 1 deletion app/features/shared/currencies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ const currencyToDefaultUnit: {
USD: 'usd',
};

export const getDefaultUnit = (currency: Currency) => {
export const getDefaultUnit = <C extends Currency>(currency: C) => {
return currencyToDefaultUnit[currency];
};
Loading