Skip to content

Commit

Permalink
update to cashu-ts@v1.0.0-rc.12 (#82)
Browse files Browse the repository at this point in the history
  • Loading branch information
gudnuf authored Jul 23, 2024
1 parent 9619d3f commit 1ca80e4
Show file tree
Hide file tree
Showing 10 changed files with 58 additions and 36 deletions.
7 changes: 4 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"lint-fix": "eslint . --ext .js,.jsx,.ts,.tsx --fix"
},
"dependencies": {
"@cashu/cashu-ts": "github:gudnuf/cashu-ts#compiled",
"@cashu/cashu-ts": "^1.0.0-rc.12",
"@cashu/crypto": "^0.2.6",
"@gandlaf21/bc-ur": "^1.1.12",
"@heroicons/react": "^2.1.1",
Expand Down
2 changes: 1 addition & 1 deletion public/sw.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/components/sidebar/Reserve/ConnectReserveSetting.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { TxStatus, addTransaction } from '@/redux/slices/HistorySlice';
import { useAppDispatch } from '@/redux/store';
import { createBlindedMessages } from '@/utils/crypto';
import { CashuMint, getEncodedToken } from '@cashu/cashu-ts';
import { constructProofs } from '@cashu/cashu-ts/dist/lib/es5/DHKE';
import { constructProofs } from '@/utils/crypto';
import EyeIcon from '@heroicons/react/20/solid/EyeIcon';
import EyeSlashIcon from '@heroicons/react/20/solid/EyeSlashIcon';
import { Badge, Button, Spinner } from 'flowbite-react';
Expand Down
9 changes: 1 addition & 8 deletions src/components/transactionHistory/HistoryTableRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -91,15 +91,8 @@ const HistoryTableRow: React.FC<{ tx: Transaction }> = ({ tx }) => {
}),
);
} else {
const { token, tokensWithErrors } = await wallet.receive(transaction.token);
const newProofs = await wallet.receive(transaction.token);

if (tokensWithErrors !== undefined) {
addToast('Failed to reclaim tokens', 'error');
setReclaiming(false);
return;
}

const newProofs = token.token[0].proofs;
addProofs(newProofs);
addToast(
`Reclaimed $${(newProofs.reduce((a, b) => (a += b.amount), 0) / 100).toFixed(2)}`,
Expand Down
29 changes: 13 additions & 16 deletions src/hooks/cashu/useCashu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
Proof,
getEncodedToken,
getDecodedToken,
MintQuoteState,
} from '@cashu/cashu-ts';
import { useProofStorage } from './useProofStorage';
import { useNostrMintConnect } from '../nostr/useNostrMintConnect';
Expand Down Expand Up @@ -43,9 +44,14 @@ export const useCashu = () => {
const getMintQuote = async (wallet: CashuWallet, amount: number): Promise<MintQuoteResponse> => {
if (reserveWallet?.keys.id === wallet.keys.id) {
const invoice = await requestDeposit(getReserveUri(), amount);
return { request: invoice.invoice, quote: 'reserve' };
return {
request: invoice.invoice,
quote: 'reserve',
state: MintQuoteState.UNPAID,
expiry: 0,
};
} else {
return wallet.getMintQuote(amount);
return wallet.createMintQuote(amount);
}
};

Expand Down Expand Up @@ -92,7 +98,7 @@ export const useCashu = () => {
attempts++;

const mintQuote = await getMintQuote(toWallet, amountToMint);
const meltQuote = await fromWallet.getMeltQuote(mintQuote.request);
const meltQuote = await fromWallet.createMeltQuote(mintQuote.request);
if (meltQuote.amount + meltQuote.fee_reserve <= totalProofsAmount) {
console.log('Found valid quotes');
return { mintQuote, meltQuote, amountToMint };
Expand Down Expand Up @@ -208,22 +214,12 @@ export const useCashu = () => {
lockBalance();

try {
const swapRes = await wallet.receiveTokenEntry({
const newProofs = await wallet.receiveTokenEntry({
proofs: proofs,
mint: wallet.mint.mintUrl,
});

if (swapRes.proofsWithError) {
if (swapRes.proofs) {
addProofs(swapRes.proofs);
}
console.error('proofs came back with error', swapRes.proofsWithError);
throw new Error(
`Error claiming proofs. ${swapRes.proofsWithError.length} of ${swapRes.proofs?.length} failed`,
);
}

addProofs(swapRes.proofs);
addProofs(newProofs);

const amountUsd = proofs.reduce((a, b) => a + b.amount, 0);
toastSwapSuccess(wallet, activeWallet, amountUsd);
Expand Down Expand Up @@ -296,7 +292,7 @@ export const useCashu = () => {
wallet = activeWallet;
}
try {
const quote = await wallet.getMeltQuote(invoice);
const quote = await wallet.createMeltQuote(invoice);

const { amount, fee_reserve } = await quote;

Expand Down Expand Up @@ -349,6 +345,7 @@ export const useCashu = () => {

// TODO: should validate preimage, but sometimes invoice is truly paid but preimage is null
if (!isPaid) {
addProofs([...change, ...proofsToSend]);
throw new TransactionError('Melt failed');
}

Expand Down
2 changes: 1 addition & 1 deletion src/hooks/nostr/useNostrMintConnect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
SerializedBlindedMessage,
SerializedBlindedSignature,
} from '@cashu/cashu-ts';
import { constructProofs } from '@cashu/cashu-ts/dist/lib/es5/DHKE';
import { constructProofs } from '@/utils/crypto';
import { NDKEvent, NDKPrivateKeySigner, NostrEvent } from '@nostr-dev-kit/ndk';
import { nip04 } from 'nostr-tools';

Expand Down
7 changes: 5 additions & 2 deletions src/lib/nip47Processors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { NIP47Method, NIP47Response, decryptEventContent } from '@/utils/nip47';
import { NWA } from '@/hooks/nostr/useNwc';
import NDK, { NDKEvent } from '@nostr-dev-kit/ndk';
import { getAmountFromInvoice } from '@/utils/bolt11';
import { CashuWallet, MeltTokensResponse, Proof } from '@cashu/cashu-ts';
import { CashuWallet, MeltQuoteState, MeltTokensResponse, Proof } from '@cashu/cashu-ts';
import { addBalance } from '@/utils/cashu';

export class NIP47RequestProcessor {
Expand Down Expand Up @@ -63,6 +63,9 @@ export class NIP47RequestProcessor {
quote: this.quoteId!,
amount: this.amountCents,
fee_reserve: fee,
state: MeltQuoteState.UNPAID,
expiry: 0,
payment_preimage: null,
});
} catch (e) {
addBalance(this.proofs);
Expand Down Expand Up @@ -152,7 +155,7 @@ export class NIP47RequestProcessor {
}

try {
const meltQuote = await this.wallet.getMeltQuote(this.params.invoice);
const meltQuote = await this.wallet.createMeltQuote(this.params.invoice);
this.quoteId = meltQuote.quote;
this.fee = meltQuote.fee_reserve;
} catch {
Expand Down
Loading

0 comments on commit 1ca80e4

Please sign in to comment.