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

Coinselect bae64 bias #226

Merged
merged 5 commits into from
Sep 1, 2024
Merged
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
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
Loading