Skip to content
Merged
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
31 changes: 7 additions & 24 deletions apps/web-wallet/app/features/receive/receive-cashu-token-hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import type {
} from '@agicash/wallet-sdk';
import {
DomainError,
ReceiveCashuTokenQuoteService,
ReceiveCashuTokenService,
canSendToLightning,
createSparkWalletStub,
Expand All @@ -21,28 +20,17 @@ import {
useAccounts,
useAddCashuAccount,
} from '~/features/accounts/account-hooks';
import { sdk } from '~/features/shared/sdk.client';
import { useGetExchangeRate } from '~/hooks/use-exchange-rate';
import {
type AccountSelectorOption,
toAccountSelectorOption,
} from '../accounts/account-selector';
import { useUser } from '../user/user-hooks';
import { useCashuReceiveQuoteService } from './cashu-receive-quote-hooks';
import { useSparkReceiveQuoteService } from './spark-receive-quote-hooks';

export function useReceiveCashuTokenService() {
return new ReceiveCashuTokenService();
}

export function useReceiveCashuTokenQuoteService() {
const cashuReceiveQuoteService = useCashuReceiveQuoteService();
const sparkLightningReceiveService = useSparkReceiveQuoteService();
return new ReceiveCashuTokenQuoteService(
cashuReceiveQuoteService,
sparkLightningReceiveService,
);
}

type UseGetClaimableTokenProps = {
token: Token;
cashuPubKey?: string;
Expand Down Expand Up @@ -269,9 +257,7 @@ type CreateCrossAccountReceiveQuotesProps = {
* The actual melting of proofs should be done by the caller.
*/
export function useCreateCrossAccountReceiveQuotes() {
const userId = useUser((user) => user.id);
const getExchangeRate = useGetExchangeRate();
const receiveCashuTokenQuoteService = useReceiveCashuTokenQuoteService();

return useMutation({
mutationFn: async ({
Expand All @@ -285,15 +271,12 @@ export function useCreateCrossAccountReceiveQuotes() {
`${tokenCurrency}-${accountCurrency}`,
);

return await receiveCashuTokenQuoteService.createCrossAccountReceiveQuotes(
{
userId,
token,
sourceAccount,
destinationAccount,
exchangeRate,
},
);
return await sdk.receive.cashuToken.createQuotes({
token,
sourceAccount,
destinationAccount,
exchangeRate,
});
},
retry: (failureCount, error) => {
if (error instanceof DomainError) {
Expand Down
84 changes: 4 additions & 80 deletions apps/web-wallet/app/routes/_protected.receive.cashu_.token.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,7 @@
import { validateCashuToken } from '@agicash/cashu';
import type { Account, User } from '@agicash/wallet-sdk';
import { isDefaultAccount } from '@agicash/wallet-sdk';
import {
AccountRepository,
AccountService,
CashuReceiveQuoteRepository,
CashuReceiveQuoteService,
CashuReceiveSwapRepository,
CashuReceiveSwapService,
ClaimCashuTokenService,
ReceiveCashuTokenQuoteService,
ReceiveCashuTokenService,
SparkReceiveQuoteRepository,
SparkReceiveQuoteService,
decodeCashuToken,
} from '@agicash/wallet-sdk/temporary';
import { decodeCashuToken } from '@agicash/wallet-sdk/temporary';
import * as Sentry from '@sentry/react-router';
import type { QueryClient } from '@tanstack/react-query';
import { Suspense } from 'react';
Expand All @@ -24,78 +11,16 @@ import {
AccountsCache,
accountsQueryOptions,
} from '~/features/accounts/account-hooks';
import { agicashDbClient } from '~/features/agicash-db/database.client';
import { LoadingScreen } from '~/features/loading/LoadingScreen';
import { ReceiveCashuToken } from '~/features/receive';
import { UnsupportedCashuTokenPage } from '~/features/receive/unsupported-cashu-token-page';
import {
getCashuCryptography,
seedQueryOptions,
} from '~/features/shared/cashu-query-options';
import { encryptionQueryOptions } from '~/features/shared/encryption-hooks';
import { getQueryClient } from '~/features/shared/query-client';
import { sdk } from '~/features/shared/sdk.client';
import { sparkMnemonicQueryOptions } from '~/features/shared/spark-query-options';
import { UserCache, getUserFromCacheOrThrow } from '~/features/user/user-hooks';
import { getExchangeRate } from '~/hooks/use-exchange-rate';
import { toast } from '~/hooks/use-toast';
import { breezApiKey } from '~/lib/breez';
import type { Route } from './+types/_protected.receive.cashu_.token';
import { ReceiveCashuTokenSkeleton } from './receive-cashu-token-skeleton';

const getServices = async () => {
const queryClient = getQueryClient();
const getCashuWalletSeed = () => queryClient.fetchQuery(seedQueryOptions());
const getSparkWalletMnemonic = () =>
queryClient.fetchQuery(sparkMnemonicQueryOptions());
const encryption = await queryClient.ensureQueryData(
encryptionQueryOptions(),
);
const accountRepository = new AccountRepository(
agicashDbClient,
encryption,
getCashuWalletSeed,
getSparkWalletMnemonic,
{ storageDir: './.spark-data', apiKey: breezApiKey },
);
const accountService = new AccountService(accountRepository);
const receiveSwapRepository = new CashuReceiveSwapRepository(
agicashDbClient,
encryption,
accountRepository,
);
const receiveSwapService = new CashuReceiveSwapService(receiveSwapRepository);
const cashuReceiveQuoteRepository = new CashuReceiveQuoteRepository(
agicashDbClient,
encryption,
accountRepository,
);
const cashuCryptography = getCashuCryptography(queryClient);
const cashuReceiveQuoteService = new CashuReceiveQuoteService(
cashuCryptography,
cashuReceiveQuoteRepository,
);
const sparkReceiveQuoteService = new SparkReceiveQuoteService(
new SparkReceiveQuoteRepository(agicashDbClient, encryption),
);
const receiveCashuTokenService = new ReceiveCashuTokenService();
const receiveCashuTokenQuoteService = new ReceiveCashuTokenQuoteService(
cashuReceiveQuoteService,
sparkReceiveQuoteService,
);
const claimCashuTokenService = new ClaimCashuTokenService(
accountService,
receiveSwapService,
cashuReceiveQuoteService,
sparkReceiveQuoteService,
receiveCashuTokenService,
receiveCashuTokenQuoteService,
(ticker) => getExchangeRate(queryClient, ticker),
);

return { claimCashuTokenService };
};

/**
* Sets the just-received account as the user's default when it isn't already —
* a UI nicety so a first-time user receiving to a non-default account sees the
Expand Down Expand Up @@ -161,16 +86,15 @@ export async function clientLoader({ request }: Route.ClientLoaderArgs) {

if (claimTo) {
const user = getUserFromCacheOrThrow();
const { claimCashuTokenService } = await getServices();
const queryClient = getQueryClient();
const accounts = await queryClient.fetchQuery(accountsQueryOptions());

const result = await claimCashuTokenService.claimToken(
user,
const result = await sdk.receive.cashuToken.claim({
token,
claimTo,
accounts,
);
user,
});

if (result.success) {
// Apply the claim's account changes to the cache so the destination
Expand Down
Loading
Loading