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

Skip refreshing balances when in anonymous mode #4922

Merged
merged 1 commit into from
Dec 5, 2023
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
export let showTopUp = false;
export let refreshing = false;

$: user = client.user;
$: cryptoLookup = client.cryptoLookup;
$: tokenDetails = $cryptoLookup[ledger];
$: symbol = tokenDetails.symbol;
Expand All @@ -33,7 +32,7 @@
refreshing = true;

return client
.refreshAccountBalance(ledger, $user.userId)
.refreshAccountBalance(ledger)
.then((val) => {
dispatch("refreshed", val);
})
Expand Down
71 changes: 43 additions & 28 deletions frontend/openchat-client/src/openchat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -4159,13 +4159,20 @@ export class OpenChat extends OpenChatAgentWorker {
}
}

refreshAccountBalance(ledger: string, principal: string): Promise<bigint> {
return this.sendRequest({ kind: "refreshAccountBalance", ledger, principal }).then(
(val) => {
cryptoBalance.set(ledger, val);
return val;
},
);
refreshAccountBalance(ledger: string): Promise<bigint> {
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(
Expand Down Expand Up @@ -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);
megrogan marked this conversation as resolved.
Show resolved Hide resolved
}
}

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);
}
}

Expand Down Expand Up @@ -5294,7 +5303,9 @@ export class OpenChat extends OpenChatAgentWorker {
}

getTokenSwaps(inputTokenLedger: string): Promise<Record<string, DexId[]>> {
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",
Expand Down Expand Up @@ -5326,15 +5337,19 @@ export class OpenChat extends OpenChatAgentWorker {
): Promise<SwapTokensResponse> {
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<TokenSwapStatusResponse> {
Expand Down
Loading