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

Add fees and coin selection #159

Merged
merged 79 commits into from
Oct 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
79 commits
Select commit Hold shift + click to select a range
54dac3a
Merge branch 'cashubtc:main' into amt_preference_fixes
lollerfirst Jun 18, 2024
ec76cc3
getPreferences checks against keyset amounts, not necessarily powers …
lollerfirst Jun 18, 2024
a5e4cd2
remove splitAmount fix for "filling up the rest" behaviour
lollerfirst Jun 18, 2024
4d51bf9
sendPreference, keepPreference
lollerfirst Jun 18, 2024
bd00ff6
splitAmount: split is now based on key amounts, not powers of 2.
lollerfirst Jun 18, 2024
f7f37a6
fix for integration test
lollerfirst Jun 19, 2024
63b2c1e
primary order is ascending amounts
lollerfirst Jun 20, 2024
4766e90
npm run format
lollerfirst Jun 20, 2024
b858878
Merge remote-tracking branch 'upstream/staging' into amt_preference_f…
lollerfirst Jun 26, 2024
9a0e589
updated tests
Egge21M Jun 28, 2024
6ed41a9
added cbor-x
Egge21M Jun 28, 2024
1060428
added tokenv4 parsing / removed depracated token
Egge21M Jun 28, 2024
b5ec29b
remove cbor dependency
Egge21M Jun 30, 2024
a34c439
added byte id and C
Egge21M Jul 1, 2024
7f23bf2
added testcases and multi token
Egge21M Jul 1, 2024
da1c603
specified return type
Egge21M Jul 1, 2024
c68de1a
cleanup rebase
Egge21M Jul 4, 2024
c91c27e
added cbor test cases
Egge21M Jul 18, 2024
79cefe5
fixed 16 bit float parsing
Egge21M Jul 18, 2024
b2f70a5
wip
callebtc Jul 22, 2024
0197df4
tests working again
callebtc Jul 22, 2024
13dfedd
Merge remote-tracking branch 'upstream/development' into amt_preferen…
lollerfirst Jul 25, 2024
64236c3
wip: working
callebtc Jul 25, 2024
003c5fd
cleanup
callebtc Jul 25, 2024
59417ad
refactor
callebtc Jul 26, 2024
65f5c8c
wip
callebtc Jul 26, 2024
07db2ef
owrks
callebtc Jul 26, 2024
865d93c
npm run format
callebtc Jul 26, 2024
9bf559a
Merge branch 'amt_preference_fixes-patch' into fees-and-coinselection
callebtc Jul 26, 2024
69e1fcb
merge with preferences branch
callebtc Jul 28, 2024
b8e90a3
use latest nutshell docker image for integration tests
callebtc Jul 28, 2024
8414ae2
npm run format
callebtc Jul 28, 2024
97686ce
Merge branch 'ci-nutshell-latest' into fees-and-coinselection
callebtc Jul 28, 2024
6aa0553
fix test
callebtc Jul 28, 2024
29a2915
fix test script
callebtc Jul 28, 2024
5be7715
wip
callebtc Jul 29, 2024
175373a
wip: output selection
callebtc Jul 30, 2024
d32e697
tmp
callebtc Aug 27, 2024
013590c
working agian
callebtc Oct 1, 2024
cbb98a1
Merge branch 'development' into fees-and-coinselection
callebtc Oct 1, 2024
2a5d834
npm run format
callebtc Oct 1, 2024
7e72950
remove empty file
callebtc Oct 1, 2024
cbca9b0
send works with fees
callebtc Oct 1, 2024
5d3563d
receive works with fees
callebtc Oct 1, 2024
1a11182
mintTokens and meltTokens are renamed to mintProofs and meltProofs
callebtc Oct 14, 2024
568f07d
returnChange -> keep
callebtc Oct 14, 2024
55e6acc
create blank outputs for entire overpaid amount, not only for fee_res…
callebtc Oct 14, 2024
cb54e70
rename CashuMint.split to CashuMint.swap
callebtc Oct 14, 2024
fd4ef99
remove amountPreference and replace with outputAmounts
callebtc Oct 14, 2024
2b6e0bd
fix tests
callebtc Oct 15, 2024
29a4a93
fix
callebtc Oct 15, 2024
87d66c8
Merge branch 'development' into fees-and-coinselection
callebtc Oct 19, 2024
2cad1ad
format
callebtc Oct 19, 2024
bd95a09
Merge branch 'fees-and-coinselection' of https://github.com/cashubtc/…
callebtc Oct 19, 2024
efe541b
fix import
callebtc Oct 19, 2024
814a0a0
uppercase
callebtc Oct 19, 2024
c3dfc43
test with fees
callebtc Oct 19, 2024
1240357
optimizations
callebtc Oct 19, 2024
57d6d02
works, still with comments
callebtc Oct 22, 2024
2f4df3c
remove logging
callebtc Oct 22, 2024
1351966
npm run format
callebtc Oct 22, 2024
3f55dba
remove integration tests from unit test pipeline
callebtc Oct 22, 2024
e2247e0
removed payLnInvoice handlers
Egge21M Oct 23, 2024
bfeade5
remove payLn tests
Egge21M Oct 23, 2024
c6efe40
added removal to migration doc
Egge21M Oct 23, 2024
398787b
Merge pull request #192 from cashubtc/remove-ln-handler
gandlafbtc Oct 24, 2024
c6a7ab7
clean up version migration files
gandlafbtc Oct 24, 2024
cfad657
union type for order
gandlafbtc Oct 24, 2024
a06ada1
some review fixes & docs completion
gandlafbtc Oct 24, 2024
3c92c21
add missing param description
gandlafbtc Oct 24, 2024
3f9d4a8
mark optional params
gandlafbtc Oct 24, 2024
e3e26b0
format
gandlafbtc Oct 24, 2024
dd0af82
add getKeepAmount tests
callebtc Oct 25, 2024
21bd131
add example of send and receive
callebtc Oct 25, 2024
d4ef495
format
callebtc Oct 25, 2024
08e62ff
remove loadMint from constructor
callebtc Oct 26, 2024
691accb
clean up
callebtc Oct 26, 2024
c0bfb76
filter base64 implicitly using keyset ID version prefix
callebtc Oct 26, 2024
1107562
expose selectProofsToSend
callebtc Oct 26, 2024
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
2 changes: 1 addition & 1 deletion .github/workflows/nutshell-integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jobs:
steps:
- name: Pull and start mint
run: |
docker run -d -p 3338:3338 --name nutshell -e MINT_LIGHTNING_BACKEND=FakeWallet -e MINT_LISTEN_HOST=0.0.0.0 -e MINT_LISTEN_PORT=3338 -e MINT_PRIVATE_KEY=TEST_PRIVATE_KEY cashubtc/nutshell:0.16.0 poetry run mint
docker run -d -p 3338:3338 --name nutshell -e MINT_LIGHTNING_BACKEND=FakeWallet -e MINT_INPUT_FEE_PPK=100 -e MINT_LISTEN_HOST=0.0.0.0 -e MINT_LISTEN_PORT=3338 -e MINT_PRIVATE_KEY=TEST_PRIVATE_KEY cashubtc/nutshell:0.16.0 poetry run mint

- name: Check running containers
run: docker ps
Expand Down
28 changes: 22 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,12 @@ 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);
await wallet.loadMint(); // persist wallet.keys and wallet.keysets to avoid calling loadMint() in the future
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);
const { proofs } = await wallet.mintProofs(64, mintQuote.quote);
}
```

Expand All @@ -77,21 +78,36 @@ if (mintQuoteChecked.state == MintQuoteState.PAID) {
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 wallet = new CashuWallet(mint); // load the keysets of the mint

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);
// CashuWallet.send performs coin selection and swaps the proofs with the mint
// if no appropriate amount can be selected offline. We must include potential
// ecash fees that the mint might require to melt the resulting proofsToSend later.
const { keep: proofsToKeep, send: proofsToSend } = await wallet.send(amountToSend, proofs, {
includeFees: true
});
// store proofsToKeep in wallet ..

const meltResponse = await wallet.meltTokens(meltQuote, proofsToSend);
const meltResponse = await wallet.meltProofs(meltQuote, proofsToSend);
// store meltResponse.change in wallet ..
```

#### Create a token and receive it

```typescript
gandlafbtc marked this conversation as resolved.
Show resolved Hide resolved
// we assume that `wallet` already minted `proofs`, as above
const { keep, send } = await wallet.send(32, proofs);
const token = getEncodedTokenV4({ token: [{ mint: mintUrl, proofs: send }] });
console.log(token);

const wallet2 = new CashuWallet(mint); // receiving wallet
const receiveProofs = await wallet2.receive(token);
```

## Contribute

Contributions are very welcome.
Expand Down
42 changes: 42 additions & 0 deletions migration-2.0.0.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Version 2.0.0 Migration guide

⚠️ Upgrading to version 2.0.0 will come with breaking changes! Please follow the migration guide for a smooth transition to the new version.

## Breaking changes

### `CashuWallet` interface changes

#### removed `payLnInvoice` helper

The helper function was removed. Instead users will have to manage a melt quote manually:

```ts
const quote = await wallet.createMeltQuote(invoice);
const totalAmount = quote.fee_reserve + invoiceAmount;
const { keep, send } = await wallet.send(totalAmount, proofs);
const payRes = await wallet.meltProofs(quote, send);
```

---

#### Preference for outputs are now passed as a object of simple arrays

**`AmountPreference`** is not used anymore.

`preference?: Array<AmountPreference>;` -> `outputAmounts?: OutputAmounts;`

where

```typescript
export type OutputAmounts = {
sendAmounts: Array<number>;
keepAmounts?: Array<number>;
};
```

#### renamed functions

- in `SendResponse`, `returnChange` is now called `keep`
- `CashuWallet.mintTokens()` is now called `CashuWallet.mintProofs()`
- `CashuWallet.meltTokens()` is now called `CashuWallet.meltProofs()`
- `CashuMint.split()` is now called `CashuMint.swap()`
6 changes: 3 additions & 3 deletions src/CashuMint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class CashuMint {
* @param customRequest
* @returns signed outputs
*/
public static async split(
public static async swap(
mintUrl: string,
swapPayload: SwapPayload,
customRequest?: typeof request
Expand All @@ -99,8 +99,8 @@ class CashuMint {
* @param swapPayload payload containing inputs and outputs
* @returns signed outputs
*/
async split(swapPayload: SwapPayload): Promise<SwapResponse> {
return CashuMint.split(this._mintUrl, swapPayload, this._customRequest);
async swap(swapPayload: SwapPayload): Promise<SwapResponse> {
return CashuMint.swap(this._mintUrl, swapPayload, this._customRequest);
}

/**
Expand Down
Loading
Loading