Skip to content

Commit

Permalink
Coinselect bae64 bias (#226)
Browse files Browse the repository at this point in the history
* coinselect: select base64 first

* fix wallet.ts to new state enum

* refactor base64 selection

* check for amount
  • Loading branch information
callebtc authored Sep 1, 2024
1 parent a2ba1bc commit 463ac2f
Showing 1 changed file with 28 additions and 5 deletions.
33 changes: 28 additions & 5 deletions src/stores/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { useSendTokensStore } from "src/stores/sendTokensStore"
import * as _ from "underscore";
import token from "src/js/token";
import { notifyApiError, notifyError, notifySuccess, notifyWarning, notify } from "src/js/notify";
import { CashuMint, CashuWallet, Proof, MintQuotePayload, CheckStatePayload, MeltQuotePayload, MeltQuoteResponse, generateNewMnemonic, deriveSeedFromMnemonic, AmountPreference, CheckStateEnum, getDecodedToken, Token } from "@cashu/cashu-ts";
import { CashuMint, CashuWallet, Proof, MintQuotePayload, CheckStatePayload, MeltQuotePayload, MeltQuoteResponse, generateNewMnemonic, deriveSeedFromMnemonic, AmountPreference, CheckStateEnum, getDecodedToken, Token, MeltQuoteState, MintQuoteState } from "@cashu/cashu-ts";
import { hashToCurve } from '@cashu/crypto/modules/common';
import * as bolt11Decoder from "light-bolt11-decoder";
import { bech32 } from "bech32";
Expand Down Expand Up @@ -247,13 +247,36 @@ export const useWalletStore = defineStore("wallet", {

return amountsWithCount;
},
coinSelectSpendBase64: function (proofs: WalletProof[], amount: number): WalletProof[] {
const base64Proofs = proofs.filter(p => !p.id.startsWith("00"))
if (base64Proofs.length > 0) {
base64Proofs.sort((a, b) => b.amount - a.amount);
let sum = 0;
let selectedProofs: WalletProof[] = [];
for (let i = 0; i < base64Proofs.length; i++) {
const proof = base64Proofs[i];
sum += proof.amount;
selectedProofs.push(proof);
if (sum >= amount) {
return selectedProofs;
}
}
return [];
}
return [];
},
coinSelect: function (proofs: WalletProof[], amount: number) {

if (proofs.reduce((s, t) => (s += t.amount), 0) < amount) {
// there are not enough proofs to pay the amount
return [];
}

// override: if there are proofs with a base64 id, use them
const base64Proofs = this.coinSelectSpendBase64(proofs, amount);
if (base64Proofs.length > 0 && base64Proofs.reduce((s, t) => (s += t.amount), 0) >= amount) {
return base64Proofs;
}

// sort proofs by amount ascending
proofs = proofs.slice().sort((a, b) => a.amount - b.amount);
// remember next bigger proof as a fallback
Expand Down Expand Up @@ -509,7 +532,7 @@ export const useWalletStore = defineStore("wallet", {
// first we check if the mint quote is paid
const mintQuote = await mintStore.activeMint().api.checkMintQuote(hash);
console.log("### mintQuote", mintQuote);
if (!mintQuote.paid) {
if (mintQuote.state != MintQuoteState.PAID) {
console.log("### mintQuote not paid yet");
if (verbose) {
notify("Invoice still pending");
Expand All @@ -519,7 +542,7 @@ export const useWalletStore = defineStore("wallet", {
const counter = this.keysetCounter(keysetId)
const preference = this.outputAmountSelect(amount);
console.log("### preference", preference);
const { proofs } = await this.wallet.mintTokens(amount, hash, { keysetId, counter, amountPreference: preference })
const { proofs } = await this.wallet.mintTokens(amount, hash, { keysetId, counter, preference: preference })
this.increaseKeysetCounter(keysetId, proofs.length);

// const proofs = await this.mintApi(split, hash, verbose);
Expand Down Expand Up @@ -859,7 +882,7 @@ export const useWalletStore = defineStore("wallet", {
// this is an outgoing invoice, we first do a getMintQuote to check if the invoice is paid
const mintQuote = await mintStore.activeMint().api.checkMeltQuote(quote);
console.log("### mintQuote", mintQuote);
if (!mintQuote.paid) {
if (mintQuote.state != MeltQuoteState.PAID) {
console.log("### mintQuote not paid yet");
if (invoice.token) {
const tokenJson = token.decode(invoice.token);
Expand Down

0 comments on commit 463ac2f

Please sign in to comment.