From 6fcc0a80b48ccc08c2255fe6551f0e5f495665fa Mon Sep 17 00:00:00 2001 From: Hamish Peebles Date: Mon, 4 Dec 2023 19:54:36 +0000 Subject: [PATCH] Skip refreshing balances when in anonymous mode --- .../components/home/BalanceWithRefresh.svelte | 3 +- frontend/openchat-client/src/openchat.ts | 71 +++++++++++-------- 2 files changed, 44 insertions(+), 30 deletions(-) diff --git a/frontend/app/src/components/home/BalanceWithRefresh.svelte b/frontend/app/src/components/home/BalanceWithRefresh.svelte index 2f59dc7cfc..1aeb27a3c4 100644 --- a/frontend/app/src/components/home/BalanceWithRefresh.svelte +++ b/frontend/app/src/components/home/BalanceWithRefresh.svelte @@ -17,7 +17,6 @@ export let showTopUp = false; export let refreshing = false; - $: user = client.user; $: cryptoLookup = client.cryptoLookup; $: tokenDetails = $cryptoLookup[ledger]; $: symbol = tokenDetails.symbol; @@ -33,7 +32,7 @@ refreshing = true; return client - .refreshAccountBalance(ledger, $user.userId) + .refreshAccountBalance(ledger) .then((val) => { dispatch("refreshed", val); }) diff --git a/frontend/openchat-client/src/openchat.ts b/frontend/openchat-client/src/openchat.ts index c5d8e3b055..b3e41a6946 100644 --- a/frontend/openchat-client/src/openchat.ts +++ b/frontend/openchat-client/src/openchat.ts @@ -132,7 +132,13 @@ import { focusThreadMessageIndex, selectedMessageContext, } from "./stores/chat"; -import { cryptoBalance, cryptoLookup, enhancedCryptoLookup, lastCryptoSent, nervousSystemLookup } from "./stores/crypto"; +import { + cryptoBalance, + cryptoLookup, + enhancedCryptoLookup, + lastCryptoSent, + nervousSystemLookup, +} from "./stores/crypto"; import { draftThreadMessages } from "./stores/draftThreadMessages"; import { disableAllProposalFilters, @@ -3075,10 +3081,7 @@ export class OpenChat extends OpenChatAgentWorker { if (resp.kind === "success" || resp.kind === "transfer_success") { this.onSendMessageSuccess(chatId, resp, msg, threadRootMessageIndex); if (msg.kind === "message" && msg.content.kind === "crypto_content") { - this.refreshAccountBalance( - msg.content.transfer.ledger, - this._liveState.user.cryptoAccount, - ); + this.refreshAccountBalance(msg.content.transfer.ledger); } if (threadRootMessageIndex !== undefined) { trackEvent("sent_threaded_message"); @@ -3260,10 +3263,7 @@ export class OpenChat extends OpenChatAgentWorker { if (resp.kind === "success" || resp.kind === "transfer_success") { this.onSendMessageSuccess(chatId, resp, msg, threadRootMessageIndex); if (msg.kind === "message" && msg.content.kind === "crypto_content") { - this.refreshAccountBalance( - msg.content.transfer.ledger, - this._liveState.user.userId, - ); + this.refreshAccountBalance(msg.content.transfer.ledger); } if (threadRootMessageIndex !== undefined) { trackEvent("sent_threaded_message"); @@ -4159,13 +4159,20 @@ export class OpenChat extends OpenChatAgentWorker { } } - refreshAccountBalance(ledger: string, principal: string): Promise { - return this.sendRequest({ kind: "refreshAccountBalance", ledger, principal }).then( - (val) => { - cryptoBalance.set(ledger, val); - return val; - }, - ); + refreshAccountBalance(ledger: string): Promise { + const user = this._liveState.user; + if (user === undefined) { + return Promise.resolve(0n); + } + + return this.sendRequest({ + kind: "refreshAccountBalance", + ledger, + principal: user.userId, + }).then((val) => { + cryptoBalance.set(ledger, val); + return val; + }); } async getAccountTransactions( @@ -5155,12 +5162,14 @@ export class OpenChat extends OpenChatAgentWorker { cryptoLookup.set(cryptoRecord); - window.setTimeout(this.refreshBalancesInSeries, 0); + if (!this._liveState.anonUser) { + window.setTimeout(() => this.refreshBalancesInSeries(), 0); + } } private async refreshBalancesInSeries() { for (const t of Object.values(get(cryptoLookup))) { - await this.refreshAccountBalance(t.ledger, get(this.user).userId); + await this.refreshAccountBalance(t.ledger); } } @@ -5294,7 +5303,9 @@ export class OpenChat extends OpenChatAgentWorker { } getTokenSwaps(inputTokenLedger: string): Promise> { - const outputTokenLedgers = Object.keys(get(cryptoLookup)).filter((t) => t !== inputTokenLedger); + const outputTokenLedgers = Object.keys(get(cryptoLookup)).filter( + (t) => t !== inputTokenLedger, + ); return this.sendRequest({ kind: "getTokenSwaps", @@ -5326,15 +5337,19 @@ export class OpenChat extends OpenChatAgentWorker { ): Promise { const lookup = get(cryptoLookup); - return this.sendRequest({ - kind: "swapTokens", - swapId, - inputTokenDetails: lookup[inputTokenLedger], - outputTokenDetails: lookup[outputTokenLedger], - amountIn, - minAmountOut, - dex, - }, false, 1000 * 60 * 3); + return this.sendRequest( + { + kind: "swapTokens", + swapId, + inputTokenDetails: lookup[inputTokenLedger], + outputTokenDetails: lookup[outputTokenLedger], + amountIn, + minAmountOut, + dex, + }, + false, + 1000 * 60 * 3, + ); } tokenSwapStatus(swapId: bigint): Promise {