Skip to content

Commit

Permalink
create token store and move history tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
Erik Brakke committed Apr 27, 2023
1 parent 305f9a1 commit bcfd9fa
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 47 deletions.
67 changes: 20 additions & 47 deletions src/pages/WalletPage.vue
Original file line number Diff line number Diff line change
Expand Up @@ -814,6 +814,7 @@ import WelcomeDialog from "components/WelcomeDialog.vue";
import { mapActions, mapState, mapWritableState } from "pinia";
import { useMintsStore } from "src/stores/mints";
import { useWorkersStore } from "src/stores/workers";
import { useTokensStore } from "src/stores/tokens";
var currentDateStr = function () {
return date.formatDate(new Date(), "YYYY-MM-DD HH:mm:ss");
Expand All @@ -840,7 +841,6 @@ export default {
mintName: "",
deferredPWAInstallPrompt: null,
invoiceHistory: [],
historyTokens: [],
invoiceData: {
amount: 0,
memo: "",
Expand Down Expand Up @@ -983,6 +983,8 @@ export default {
"invoiceCheckListener",
"tokensCheckSpendableListener",
]),
...mapState(useTokensStore, ["historyTokens"]),
...mapWritableState(useTokensStore, [""]),
canPay: function () {
if (!this.payInvoiceData.invoice) return false;
return this.payInvoiceData.invoice.sat <= this.balance;
Expand Down Expand Up @@ -1026,6 +1028,11 @@ export default {
"setShowAddMintDialog",
]),
...mapActions(useWorkersStore, ["clearAllWorkers"]),
...mapActions(useTokensStore, [
"addPaidToken",
"addPendingToken",
"setTokenPaid",
]),
// TOKEN METHODS
decodeToken: function (encoded_token) {
return token.decode(encoded_token);
Expand Down Expand Up @@ -1587,14 +1594,10 @@ export default {
// update UI
await this.setInvoicePaid(payment_hash);
this.historyTokens.push({
status: "paid",
amount: amount,
date: currentDateStr(),
token: this.serializeProofs(proofs),
this.addPaidToken({
amount,
serializedProofs: this.serializeProofs(proofs),
});
this.storehistoryTokens();
return proofs;
} catch (error) {
Expand Down Expand Up @@ -1791,13 +1794,10 @@ export default {
this.setActiveProofs(this.activeProofs.concat([]));
this.getBalance();
this.historyTokens.push({
status: "paid",
amount: amount,
date: currentDateStr(),
token: this.receiveData.tokensBase64,
this.addPaidToken({
amount,
serializedProofs: this.receiveData.tokensBase64,
});
this.storehistoryTokens();
if (window.navigator.vibrate) navigator.vibrate(200);
this.notifySuccess("Tokens received.");
Expand Down Expand Up @@ -1828,17 +1828,11 @@ export default {
console.log("### this.sendData.tokens", this.sendData.tokens);
this.sendData.tokensBase64 = this.serializeProofs(scndProofs);
this.historyTokens.push({
status: "pending",
this.addPendingToken({
amount: -this.sendData.amount,
date: currentDateStr(),
token: this.sendData.tokensBase64,
serializedProofs: this.sendData.tokensBase64,
});
// store "pending" outgoing tokens in history table
this.storehistoryTokens();
this.checkTokenSpendableWorker();
} catch (error) {
console.error(error);
Expand Down Expand Up @@ -1912,13 +1906,10 @@ export default {
// update UI
this.historyTokens.push({
status: "paid",
this.addPaidToken({
amount: -amount_paid,
date: currentDateStr(),
token: this.serializeProofs(scndProofs),
serializedProofs: this.serializeProofs(scndProofs),
});
this.storehistoryTokens();
this.invoiceHistory.push({
amount: -amount_paid,
Expand Down Expand Up @@ -1969,13 +1960,10 @@ export default {
// update UI
if (update_history) {
this.historyTokens.push({
status: "paid",
this.addPaidToken({
amount: -this.sumProofs(spentProofs),
date: currentDateStr(),
token: this.serializeProofs(spentProofs),
serializedProofs: this.serializeProofs(spentProofs),
});
this.storehistoryTokens();
}
}
Expand Down Expand Up @@ -2066,11 +2054,6 @@ export default {
i += 1;
}
},
setTokenPaid: async function (token) {
const invoice = this.historyTokens.find((i) => i.token === token);
invoice.status = "paid";
this.storehistoryTokens();
},
checkTokenSpendable: async function (token, verbose = true) {
/*
Expand Down Expand Up @@ -2280,12 +2263,6 @@ export default {
JSON.stringify(this.invoiceHistory)
);
},
storehistoryTokens: function () {
localStorage.setItem(
"cashu.historyTokens",
JSON.stringify(this.historyTokens)
);
},
/*storeProofs: function () {
localStorage.setItem(
"cashu.proofs",
Expand Down Expand Up @@ -2397,10 +2374,6 @@ export default {
localStorage.getItem("cashu.invoiceHistory") || "[]"
);
this.historyTokens = JSON.parse(
localStorage.getItem("cashu.historyTokens") || "[]"
);
// run migrations
await this.migrationLocalstorage();
Expand Down
46 changes: 46 additions & 0 deletions src/stores/tokens.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { useLocalStorage } from "@vueuse/core";
import { date } from "quasar";
import { defineStore } from "pinia";

export const useTokensStore = defineStore("tokens", {
state: () => ({
historyTokens: useLocalStorage("cashu.historyTokens", []),
}),
actions: {
/**
* @param {{amount: number, serializedProofs: string}}
*/
addPaidToken({ amount, serializedProofs }) {
this.historyTokens.push({
status: "paid",
amount,
date: currentDateStr(),
token: serializedProofs,
});
},
/**
* @param {{amount: number, serializedProofs: string}} param0
*/
addPendingToken({ amount, serializedProofs }) {
this.historyTokens.push({
status: "pending",
amount,
date: currentDateStr(),
token: serializedProofs,
});
},
/**
* @param {string} token
*/
setTokenPaid(token) {
const index = this.historyTokens.findIndex((t) => t.token === token);
if (index >= 0) {
this.historyTokens[index].status = "paid";
}
},
},
});

function currentDateStr() {
return date.formatDate(new Date(), "YYYY-MM-DD HH:mm:ss");
}

0 comments on commit bcfd9fa

Please sign in to comment.