File tree Expand file tree Collapse file tree 1 file changed +28
-11
lines changed
src/common/blockchains/near-protocol/utils Expand file tree Collapse file tree 1 file changed +28
-11
lines changed Original file line number Diff line number Diff line change @@ -4,14 +4,31 @@ import type { AccountId } from "@/common/types";
44
55import { 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+ } ;
You can’t perform that action at this time.
0 commit comments