Skip to content

Commit 3041f9c

Browse files
authored
Merge pull request #604 from PotLock/staging
Staging
2 parents a790e24 + b7c1c90 commit 3041f9c

File tree

2 files changed

+34
-11
lines changed

2 files changed

+34
-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+
};

src/common/contracts/core/sybil-resistance/hooks.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,10 @@ export const useIsHuman = ({ enabled = true, accountId }: ByAccountId & Conditio
1111

1212
([_queryKeyHead, accountIdKey]) =>
1313
!IS_CLIENT ? undefined : contractClient.is_human({ account_id: accountIdKey }),
14+
15+
{
16+
revalidateIfStale: false,
17+
revalidateOnFocus: false,
18+
revalidateOnReconnect: false,
19+
},
1420
);

0 commit comments

Comments
 (0)