Skip to content

Commit b7c1c90

Browse files
authored
Merge pull request #603 from PotLock/fix/excessive-rpc-calls
fixed calls to rpc when typing in campaign page
2 parents e041fbc + 4414fe0 commit b7c1c90

File tree

1 file changed

+28
-11
lines changed

1 file changed

+28
-11
lines changed

src/common/blockchains/near-protocol/utils/validations.ts

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,31 @@ import type { AccountId } from "@/common/types";
44

55
import { nearRpc } from "../client";
66

7-
export const isNearAccountValid = async (account_id: AccountId) =>
8-
account_id.length > 4
9-
? await nearRpc
10-
.query<AccountView>({
11-
request_type: "view_account",
12-
finality: "final",
13-
account_id,
14-
})
15-
.then(Boolean)
16-
.catch(() => false)
17-
: false;
7+
const accountValidationCache = new Map<string, boolean>();
8+
let lastValidationTimestamp = 0;
9+
const DEBOUNCE_MS = 300;
10+
11+
export const isNearAccountValid = async (account_id: AccountId) => {
12+
if (account_id.length <= 4) return false;
13+
14+
const cached = accountValidationCache.get(account_id);
15+
if (cached !== undefined) return cached;
16+
17+
// Debounce: wait before making RPC call, skip if a newer call arrives
18+
const timestamp = Date.now();
19+
lastValidationTimestamp = timestamp;
20+
await new Promise((resolve) => setTimeout(resolve, DEBOUNCE_MS));
21+
if (lastValidationTimestamp !== timestamp) return false;
22+
23+
const isValid = await nearRpc
24+
.query<AccountView>({
25+
request_type: "view_account",
26+
finality: "final",
27+
account_id,
28+
})
29+
.then(Boolean)
30+
.catch(() => false);
31+
32+
accountValidationCache.set(account_id, isValid);
33+
return isValid;
34+
};

0 commit comments

Comments
 (0)