Skip to content

Commit

Permalink
update stores to TS
Browse files Browse the repository at this point in the history
  • Loading branch information
Erik Brakke committed May 20, 2023
1 parent 61d2912 commit 2e2204e
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 31 deletions.
39 changes: 29 additions & 10 deletions src/stores/tokens.js → src/stores/tokens.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,57 @@
import { useLocalStorage } from "@vueuse/core";
import { date } from "quasar";
import { defineStore } from "pinia";
import { Proof, Token } from "@cashu/cashu-ts";

/**
* The tokens store handles everything related to tokens and proofs
*/

type HistoryToken = {
status: "paid" | "pending";
amount: number;
date: string;
token: string;
};

export const useTokensStore = defineStore("tokens", {
state: () => ({
historyTokens: useLocalStorage("cashu.historyTokens", []),
historyTokens: useLocalStorage("cashu.historyTokens", [] as HistoryToken[]),
proofs: useLocalStorage("cashu.proofs", [] as Proof[]),
}),
actions: {
/**
* @param {{amount: number, serializedProofs: string}}
*/
addPaidToken({ amount, serializedProofs }) {
addPaidToken({
amount,
serializedProofs,
}: {
amount: number;
serializedProofs: string;
}) {
this.historyTokens.push({
status: "paid",
amount,
date: currentDateStr(),
token: serializedProofs,
});
},
/**
* @param {{amount: number, serializedProofs: string}} param0
*/
addPendingToken({ amount, serializedProofs }) {
addPendingToken({
amount,
serializedProofs,
}: {
amount: number;
serializedProofs: string;
}) {
this.historyTokens.push({
status: "pending",
amount,
date: currentDateStr(),
token: serializedProofs,
});
},
/**
* @param {string} token
*/
setTokenPaid(token) {
setTokenPaid(token: string) {
const index = this.historyTokens.findIndex((t) => t.token === token);
if (index >= 0) {
this.historyTokens[index].status = "paid";
Expand Down
53 changes: 32 additions & 21 deletions src/stores/wallet.js → src/stores/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,34 @@ import { currentDateStr } from "src/js/utils";
import { notifyApiError } from "src/js/notify";
import { useMintsStore } from "./mints";
import { useLocalStorage } from "@vueuse/core";
import { CashuMint, CashuWallet } from "@cashu/cashu-ts";

type Invoice = {
amount: number;
bolt11: string;
hash: string;
memo: string;
};

type InvoiceHistory = Invoice & {
date: string;
status: "pending" | "paid";
mint?: string;
};

export const useWalletStore = defineStore("wallet", {
state: () => {
return {
invoiceHistory: useLocalStorage("cashu.invoiceHistory", []),
invoiceHistory: useLocalStorage(
"cashu.invoiceHistory",
[] as InvoiceHistory[]
),
invoiceData: {
amount: 0,
memo: "",
bolt11: "",
hash: "",
},
} as Invoice,
payInvoiceData: {
blocking: false,
bolt11: "",
Expand All @@ -32,7 +49,8 @@ export const useWalletStore = defineStore("wallet", {
getters: {
wallet() {
const mints = useMintsStore();
const wallet = new CashuWallet(mints.keys, mints.activeMint);
const mint = new CashuMint(mints.activeMintUrl);
const wallet = new CashuWallet(mint);
return wallet;
},
},
Expand All @@ -41,12 +59,10 @@ export const useWalletStore = defineStore("wallet", {
* Ask the mint to generate an invoice for the given amount
* Upon paying the request, the mint will credit the wallet with
* cashu tokens
* @param {number | null} amount
* @returns
*/
requestMint: async function (amount = null) {
requestMint: async function (amount?: number) {
const mints = useMintsStore();
if (amount != null) {
if (amount) {
this.invoiceData.amount = amount;
}
try {
Expand All @@ -55,26 +71,21 @@ export const useWalletStore = defineStore("wallet", {
);
this.invoiceData.bolt11 = data.pr;
this.invoiceData.hash = data.hash;
this.invoiceHistory.push(
// extend dictionary
Object.assign({}, this.invoiceData, {
date: currentDateStr(),
status: "pending",
mint: this.activeMintUrl,
})
);
this.invoiceHistory.push({
...this.invoiceData,
date: currentDateStr(),
status: "pending",
mint: mints.activeMintUrl,
});
return data;
} catch (error) {
} catch (error: any) {
console.error(error);
notifyApiError(error);
}
},
/**
* Sets an invoice status to paid
* @param {string} payment_hash
*/
setInvoicePaid(payment_hash) {
setInvoicePaid(payment_hash: string) {
const invoice = this.invoiceHistory.find((i) => i.hash === payment_hash);
if (!invoice) return;
invoice.status = "paid";
},
},
Expand Down

0 comments on commit 2e2204e

Please sign in to comment.