From 774eb10512e14545a7369c0303e07ab9fb4b8f16 Mon Sep 17 00:00:00 2001 From: callebtc <93376500+callebtc@users.noreply.github.com> Date: Mon, 22 Jul 2024 14:24:19 +0200 Subject: [PATCH 1/2] Add melt example --- README.md | 33 +++++++++++++++++++++++++++++---- 1 file changed, 29 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 75244c600..3e7b5f708 100644 --- a/README.md +++ b/README.md @@ -53,15 +53,40 @@ Go to the [docs](https://cashubtc.github.io/cashu-ts/docs) for detailed usage, o npm i @cashu/cashu-ts ``` -### Example +### Examples +#### Mint tokens ```typescript -import { CashuMint, CashuWallet, getEncodedToken } from '@cashu/cashu-ts'; +import { CashuMint, CashuWallet, MintQuoteState } from '@cashu/cashu-ts'; +const mintUrl = 'http://localhost:3338' // the mint URL +const mint = new CashuMint(mintUrl); +const wallet = new CashuWallet(mint); +const mintQuote = await wallet.createMintQuote(64); +// pay the invoice here before you continue... +const mintQuoteChecked = await wallet.checkMintQuote(mintQuote.quote) +if (mintQuoteChecked.state == MintQuoteState.PAID) { + const {proofs} = await wallet.mintTokens(64, mintQuote.quote); +} +``` +#### Melt tokens +```typescript +import { CashuMint, CashuWallet } from '@cashu/cashu-ts'; +const mintUrl = 'http://localhost:3338' // the mint URL const mint = new CashuMint(mintUrl); const wallet = new CashuWallet(mint); -const mintQuote = await wallet.mintQuote(64); -const tokens = await wallet.mintTokens(64, mintQuote.quote); + +const invoice = 'lnbc......' // Lightning invoice to pay +const meltQuote = await wallet.createMeltQuote(invoice); +const amountToSend = meltQuote.amount + meltQuote.fee_reserve; + +// in a real wallet, we would coin select the correct amount of proofs from the wallet's storage +// instead of that, here we swap `proofs` with the mint to get the correct amount of proofs +const { returnChange: proofsToKeep, send: proofsToSend } = await wallet.send(amountToSend, proofs) +// store proofsToKeep in wallet .. + +const meltResponse = await wallet.meltTokens(meltQuote, proofsToSend) +// store meltResponse.change in wallet .. ``` ## Contribute From 35d6b0fd77e2747bcf9bdf1f9e1ffcff648d3047 Mon Sep 17 00:00:00 2001 From: callebtc <93376500+callebtc@users.noreply.github.com> Date: Mon, 22 Jul 2024 14:26:07 +0200 Subject: [PATCH 2/2] npm run format --- README.md | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 3e7b5f708..8c4137966 100644 --- a/README.md +++ b/README.md @@ -56,36 +56,38 @@ npm i @cashu/cashu-ts ### Examples #### Mint tokens + ```typescript import { CashuMint, CashuWallet, MintQuoteState } from '@cashu/cashu-ts'; -const mintUrl = 'http://localhost:3338' // the mint URL +const mintUrl = 'http://localhost:3338'; // the mint URL const mint = new CashuMint(mintUrl); const wallet = new CashuWallet(mint); const mintQuote = await wallet.createMintQuote(64); // pay the invoice here before you continue... -const mintQuoteChecked = await wallet.checkMintQuote(mintQuote.quote) +const mintQuoteChecked = await wallet.checkMintQuote(mintQuote.quote); if (mintQuoteChecked.state == MintQuoteState.PAID) { - const {proofs} = await wallet.mintTokens(64, mintQuote.quote); + const { proofs } = await wallet.mintTokens(64, mintQuote.quote); } ``` #### Melt tokens + ```typescript import { CashuMint, CashuWallet } from '@cashu/cashu-ts'; -const mintUrl = 'http://localhost:3338' // the mint URL +const mintUrl = 'http://localhost:3338'; // the mint URL const mint = new CashuMint(mintUrl); const wallet = new CashuWallet(mint); -const invoice = 'lnbc......' // Lightning invoice to pay +const invoice = 'lnbc......'; // Lightning invoice to pay const meltQuote = await wallet.createMeltQuote(invoice); const amountToSend = meltQuote.amount + meltQuote.fee_reserve; // in a real wallet, we would coin select the correct amount of proofs from the wallet's storage // instead of that, here we swap `proofs` with the mint to get the correct amount of proofs -const { returnChange: proofsToKeep, send: proofsToSend } = await wallet.send(amountToSend, proofs) +const { returnChange: proofsToKeep, send: proofsToSend } = await wallet.send(amountToSend, proofs); // store proofsToKeep in wallet .. -const meltResponse = await wallet.meltTokens(meltQuote, proofsToSend) +const meltResponse = await wallet.meltTokens(meltQuote, proofsToSend); // store meltResponse.change in wallet .. ```