@shadow-corp/nearconnect 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +546 -0
- package/build/InjectedWallet.d.ts +22 -0
- package/build/InjectedWallet.js +58 -0
- package/build/InjectedWallet.js.map +1 -0
- package/build/NearConnector.d.ts +151 -0
- package/build/NearConnector.js +536 -0
- package/build/NearConnector.js.map +1 -0
- package/build/ParentFrameWallet.d.ts +22 -0
- package/build/ParentFrameWallet.js +66 -0
- package/build/ParentFrameWallet.js.map +1 -0
- package/build/SandboxedWallet/code.d.ts +7 -0
- package/build/SandboxedWallet/code.js +324 -0
- package/build/SandboxedWallet/code.js.map +1 -0
- package/build/SandboxedWallet/executor.d.ts +23 -0
- package/build/SandboxedWallet/executor.js +338 -0
- package/build/SandboxedWallet/executor.js.map +1 -0
- package/build/SandboxedWallet/iframe.d.ts +18 -0
- package/build/SandboxedWallet/iframe.js +78 -0
- package/build/SandboxedWallet/iframe.js.map +1 -0
- package/build/SandboxedWallet/index.d.ts +24 -0
- package/build/SandboxedWallet/index.js +54 -0
- package/build/SandboxedWallet/index.js.map +1 -0
- package/build/actions/index.d.ts +3 -0
- package/build/actions/index.js +105 -0
- package/build/actions/index.js.map +1 -0
- package/build/actions/types.d.ts +76 -0
- package/build/actions/types.js +3 -0
- package/build/actions/types.js.map +1 -0
- package/build/connection/health.d.ts +213 -0
- package/build/connection/health.js +391 -0
- package/build/connection/health.js.map +1 -0
- package/build/connection/index.d.ts +4 -0
- package/build/connection/index.js +48 -0
- package/build/connection/index.js.map +1 -0
- package/build/connection/reconnect.d.ts +261 -0
- package/build/connection/reconnect.js +454 -0
- package/build/connection/reconnect.js.map +1 -0
- package/build/connection/retry.d.ts +187 -0
- package/build/connection/retry.js +427 -0
- package/build/connection/retry.js.map +1 -0
- package/build/connection/state.d.ts +222 -0
- package/build/connection/state.js +431 -0
- package/build/connection/state.js.map +1 -0
- package/build/errors.d.ts +177 -0
- package/build/errors.js +546 -0
- package/build/errors.js.map +1 -0
- package/build/hardware/errors.d.ts +36 -0
- package/build/hardware/errors.js +127 -0
- package/build/hardware/errors.js.map +1 -0
- package/build/hardware/index.d.ts +7 -0
- package/build/hardware/index.js +39 -0
- package/build/hardware/index.js.map +1 -0
- package/build/hardware/near-app.d.ts +95 -0
- package/build/hardware/near-app.js +291 -0
- package/build/hardware/near-app.js.map +1 -0
- package/build/hardware/transport.d.ts +94 -0
- package/build/hardware/transport.js +267 -0
- package/build/hardware/transport.js.map +1 -0
- package/build/hardware/types.d.ts +98 -0
- package/build/hardware/types.js +72 -0
- package/build/hardware/types.js.map +1 -0
- package/build/helpers/analytics.d.ts +191 -0
- package/build/helpers/analytics.js +304 -0
- package/build/helpers/analytics.js.map +1 -0
- package/build/helpers/base58.d.ts +6 -0
- package/build/helpers/base58.js +47 -0
- package/build/helpers/base58.js.map +1 -0
- package/build/helpers/events.d.ts +42 -0
- package/build/helpers/events.js +68 -0
- package/build/helpers/events.js.map +1 -0
- package/build/helpers/html.d.ts +8 -0
- package/build/helpers/html.js +30 -0
- package/build/helpers/html.js.map +1 -0
- package/build/helpers/indexdb.d.ts +14 -0
- package/build/helpers/indexdb.js +166 -0
- package/build/helpers/indexdb.js.map +1 -0
- package/build/helpers/manifest.d.ts +147 -0
- package/build/helpers/manifest.js +329 -0
- package/build/helpers/manifest.js.map +1 -0
- package/build/helpers/queue.d.ts +11 -0
- package/build/helpers/queue.js +48 -0
- package/build/helpers/queue.js.map +1 -0
- package/build/helpers/session.d.ts +119 -0
- package/build/helpers/session.js +289 -0
- package/build/helpers/session.js.map +1 -0
- package/build/helpers/simulation.d.ts +128 -0
- package/build/helpers/simulation.js +441 -0
- package/build/helpers/simulation.js.map +1 -0
- package/build/helpers/storage.d.ts +58 -0
- package/build/helpers/storage.js +190 -0
- package/build/helpers/storage.js.map +1 -0
- package/build/helpers/trust.d.ts +157 -0
- package/build/helpers/trust.js +340 -0
- package/build/helpers/trust.js.map +1 -0
- package/build/helpers/url.d.ts +1 -0
- package/build/helpers/url.js +13 -0
- package/build/helpers/url.js.map +1 -0
- package/build/helpers/uuid.d.ts +1 -0
- package/build/helpers/uuid.js +14 -0
- package/build/helpers/uuid.js.map +1 -0
- package/build/index.d.ts +21 -0
- package/build/index.js +167 -0
- package/build/index.js.map +1 -0
- package/build/popups/IframeWalletPopup.d.ts +16 -0
- package/build/popups/IframeWalletPopup.js +38 -0
- package/build/popups/IframeWalletPopup.js.map +1 -0
- package/build/popups/NearWalletsPopup.d.ts +25 -0
- package/build/popups/NearWalletsPopup.js +153 -0
- package/build/popups/NearWalletsPopup.js.map +1 -0
- package/build/popups/Popup.d.ts +22 -0
- package/build/popups/Popup.js +94 -0
- package/build/popups/Popup.js.map +1 -0
- package/build/popups/styles.d.ts +1 -0
- package/build/popups/styles.js +257 -0
- package/build/popups/styles.js.map +1 -0
- package/build/security/audit-log.d.ts +123 -0
- package/build/security/audit-log.js +268 -0
- package/build/security/audit-log.js.map +1 -0
- package/build/security/csp.d.ts +68 -0
- package/build/security/csp.js +328 -0
- package/build/security/csp.js.map +1 -0
- package/build/security/index.d.ts +10 -0
- package/build/security/index.js +42 -0
- package/build/security/index.js.map +1 -0
- package/build/security/origin-guard.d.ts +90 -0
- package/build/security/origin-guard.js +244 -0
- package/build/security/origin-guard.js.map +1 -0
- package/build/security/rate-limiter.d.ts +84 -0
- package/build/security/rate-limiter.js +212 -0
- package/build/security/rate-limiter.js.map +1 -0
- package/build/security/secure-storage.d.ts +77 -0
- package/build/security/secure-storage.js +242 -0
- package/build/security/secure-storage.js.map +1 -0
- package/build/security/transaction-guard.d.ts +71 -0
- package/build/security/transaction-guard.js +239 -0
- package/build/security/transaction-guard.js.map +1 -0
- package/build/types.d.ts +508 -0
- package/build/types.js +3 -0
- package/build/types.js.map +1 -0
- package/build/ui/AccountSwitcherModal.d.ts +53 -0
- package/build/ui/AccountSwitcherModal.js +239 -0
- package/build/ui/AccountSwitcherModal.js.map +1 -0
- package/build/ui/Modal.d.ts +84 -0
- package/build/ui/Modal.js +278 -0
- package/build/ui/Modal.js.map +1 -0
- package/build/ui/TransactionModal.d.ts +84 -0
- package/build/ui/TransactionModal.js +406 -0
- package/build/ui/TransactionModal.js.map +1 -0
- package/build/ui/WalletSelectorModal.d.ts +97 -0
- package/build/ui/WalletSelectorModal.js +481 -0
- package/build/ui/WalletSelectorModal.js.map +1 -0
- package/build/ui/icons.d.ts +19 -0
- package/build/ui/icons.js +65 -0
- package/build/ui/icons.js.map +1 -0
- package/build/ui/index.d.ts +10 -0
- package/build/ui/index.js +31 -0
- package/build/ui/index.js.map +1 -0
- package/build/ui/styles.d.ts +5 -0
- package/build/ui/styles.js +973 -0
- package/build/ui/styles.js.map +1 -0
- package/build/ui/theme.d.ts +133 -0
- package/build/ui/theme.js +204 -0
- package/build/ui/theme.js.map +1 -0
- package/build/wallets/external/index.d.ts +4 -0
- package/build/wallets/external/index.js +9 -0
- package/build/wallets/external/index.js.map +1 -0
- package/build/wallets/external/manager.d.ts +152 -0
- package/build/wallets/external/manager.js +586 -0
- package/build/wallets/external/manager.js.map +1 -0
- package/build/wallets/privileged/index.d.ts +5 -0
- package/build/wallets/privileged/index.js +12 -0
- package/build/wallets/privileged/index.js.map +1 -0
- package/build/wallets/privileged/ledger.d.ts +132 -0
- package/build/wallets/privileged/ledger.js +563 -0
- package/build/wallets/privileged/ledger.js.map +1 -0
- package/build/wallets/privileged/manager.d.ts +54 -0
- package/build/wallets/privileged/manager.js +174 -0
- package/build/wallets/privileged/manager.js.map +1 -0
- package/package.json +33 -0
|
@@ -0,0 +1,289 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SessionManager = void 0;
|
|
4
|
+
const storage_1 = require("./storage");
|
|
5
|
+
const DEFAULT_MAX_AGE = 7 * 24 * 60 * 60 * 1000; // 7 days
|
|
6
|
+
const DEFAULT_STORAGE_KEY = "near-connect-session";
|
|
7
|
+
const BROADCAST_CHANNEL_NAME = "near-connect-session-sync";
|
|
8
|
+
class SessionManager {
|
|
9
|
+
storage;
|
|
10
|
+
storageKey;
|
|
11
|
+
maxAge;
|
|
12
|
+
idleTimeout;
|
|
13
|
+
autoReconnect;
|
|
14
|
+
enableMultiTabSync;
|
|
15
|
+
cachedSession = null;
|
|
16
|
+
broadcastChannel = null;
|
|
17
|
+
idleTimer = null;
|
|
18
|
+
activityHandler = null;
|
|
19
|
+
onSessionRestored;
|
|
20
|
+
onSessionExpired;
|
|
21
|
+
onSessionInvalid;
|
|
22
|
+
onSessionCleared;
|
|
23
|
+
onSessionSynced;
|
|
24
|
+
onIdleTimeout;
|
|
25
|
+
constructor(options = {}) {
|
|
26
|
+
this.storage = options.storage ?? new storage_1.LocalStorage();
|
|
27
|
+
this.storageKey = options.storageKey ?? DEFAULT_STORAGE_KEY;
|
|
28
|
+
this.maxAge = options.maxAge ?? DEFAULT_MAX_AGE;
|
|
29
|
+
this.idleTimeout = options.idleTimeout ?? null;
|
|
30
|
+
this.autoReconnect = options.autoReconnect ?? true;
|
|
31
|
+
this.enableMultiTabSync = options.enableMultiTabSync ?? true;
|
|
32
|
+
this.onSessionRestored = options.onSessionRestored;
|
|
33
|
+
this.onSessionExpired = options.onSessionExpired;
|
|
34
|
+
this.onSessionInvalid = options.onSessionInvalid;
|
|
35
|
+
this.onSessionCleared = options.onSessionCleared;
|
|
36
|
+
this.onSessionSynced = options.onSessionSynced;
|
|
37
|
+
this.onIdleTimeout = options.onIdleTimeout;
|
|
38
|
+
this.initMultiTabSync();
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Initialize multi-tab synchronization via BroadcastChannel
|
|
42
|
+
*/
|
|
43
|
+
initMultiTabSync() {
|
|
44
|
+
if (!this.enableMultiTabSync)
|
|
45
|
+
return;
|
|
46
|
+
if (typeof BroadcastChannel === "undefined")
|
|
47
|
+
return;
|
|
48
|
+
try {
|
|
49
|
+
this.broadcastChannel = new BroadcastChannel(BROADCAST_CHANNEL_NAME);
|
|
50
|
+
this.broadcastChannel.onmessage = (event) => {
|
|
51
|
+
this.handleSyncMessage(event.data);
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
catch {
|
|
55
|
+
// BroadcastChannel not supported
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Handle incoming sync messages from other tabs
|
|
60
|
+
*/
|
|
61
|
+
handleSyncMessage(message) {
|
|
62
|
+
switch (message.type) {
|
|
63
|
+
case "session:updated":
|
|
64
|
+
if (message.session) {
|
|
65
|
+
this.cachedSession = message.session;
|
|
66
|
+
this.onSessionSynced?.(message.session);
|
|
67
|
+
}
|
|
68
|
+
break;
|
|
69
|
+
case "session:cleared":
|
|
70
|
+
case "session:disconnected":
|
|
71
|
+
this.cachedSession = null;
|
|
72
|
+
this.onSessionCleared?.();
|
|
73
|
+
break;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Broadcast session change to other tabs
|
|
78
|
+
*/
|
|
79
|
+
broadcast(message) {
|
|
80
|
+
this.broadcastChannel?.postMessage(message);
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Start idle timeout tracking
|
|
84
|
+
*/
|
|
85
|
+
startIdleTracking() {
|
|
86
|
+
if (!this.idleTimeout || typeof window === "undefined")
|
|
87
|
+
return;
|
|
88
|
+
this.stopIdleTracking();
|
|
89
|
+
const resetTimer = () => {
|
|
90
|
+
if (this.idleTimer)
|
|
91
|
+
clearTimeout(this.idleTimer);
|
|
92
|
+
this.idleTimer = setTimeout(async () => {
|
|
93
|
+
const session = await this.get();
|
|
94
|
+
if (session) {
|
|
95
|
+
this.onIdleTimeout?.(session);
|
|
96
|
+
await this.clear();
|
|
97
|
+
}
|
|
98
|
+
}, this.idleTimeout);
|
|
99
|
+
// Touch session on activity
|
|
100
|
+
this.touch().catch(() => { });
|
|
101
|
+
};
|
|
102
|
+
this.activityHandler = resetTimer;
|
|
103
|
+
const events = ["mousedown", "keydown", "touchstart", "scroll"];
|
|
104
|
+
events.forEach((event) => {
|
|
105
|
+
window.addEventListener(event, resetTimer, { passive: true });
|
|
106
|
+
});
|
|
107
|
+
resetTimer();
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Stop idle timeout tracking
|
|
111
|
+
*/
|
|
112
|
+
stopIdleTracking() {
|
|
113
|
+
if (this.idleTimer) {
|
|
114
|
+
clearTimeout(this.idleTimer);
|
|
115
|
+
this.idleTimer = null;
|
|
116
|
+
}
|
|
117
|
+
if (this.activityHandler && typeof window !== "undefined") {
|
|
118
|
+
const events = ["mousedown", "keydown", "touchstart", "scroll"];
|
|
119
|
+
events.forEach((event) => {
|
|
120
|
+
window.removeEventListener(event, this.activityHandler);
|
|
121
|
+
});
|
|
122
|
+
this.activityHandler = null;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Cleanup resources
|
|
127
|
+
*/
|
|
128
|
+
destroy() {
|
|
129
|
+
this.stopIdleTracking();
|
|
130
|
+
this.broadcastChannel?.close();
|
|
131
|
+
this.broadcastChannel = null;
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* Save a new session
|
|
135
|
+
*/
|
|
136
|
+
async save(session) {
|
|
137
|
+
const now = Date.now();
|
|
138
|
+
const fullSession = {
|
|
139
|
+
...session,
|
|
140
|
+
connectedAt: now,
|
|
141
|
+
lastActiveAt: now,
|
|
142
|
+
};
|
|
143
|
+
await this.storage.set(this.storageKey, JSON.stringify(fullSession));
|
|
144
|
+
this.cachedSession = fullSession;
|
|
145
|
+
// Broadcast to other tabs
|
|
146
|
+
this.broadcast({ type: "session:updated", session: fullSession, timestamp: now });
|
|
147
|
+
// Start idle tracking if configured
|
|
148
|
+
this.startIdleTracking();
|
|
149
|
+
return fullSession;
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* Get the current session if valid
|
|
153
|
+
*/
|
|
154
|
+
async get() {
|
|
155
|
+
if (this.cachedSession && this.isValid(this.cachedSession)) {
|
|
156
|
+
return this.cachedSession;
|
|
157
|
+
}
|
|
158
|
+
const raw = await this.storage.get(this.storageKey);
|
|
159
|
+
if (!raw)
|
|
160
|
+
return null;
|
|
161
|
+
try {
|
|
162
|
+
const session = JSON.parse(raw);
|
|
163
|
+
const validation = this.validate(session);
|
|
164
|
+
if (!validation.valid) {
|
|
165
|
+
if (validation.reason === "expired") {
|
|
166
|
+
this.onSessionExpired?.(session);
|
|
167
|
+
}
|
|
168
|
+
else if (validation.reason === "invalid") {
|
|
169
|
+
this.onSessionInvalid?.(session);
|
|
170
|
+
}
|
|
171
|
+
await this.clear();
|
|
172
|
+
return null;
|
|
173
|
+
}
|
|
174
|
+
this.cachedSession = session;
|
|
175
|
+
return session;
|
|
176
|
+
}
|
|
177
|
+
catch {
|
|
178
|
+
await this.clear();
|
|
179
|
+
return null;
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
/**
|
|
183
|
+
* Validate a session and return detailed result
|
|
184
|
+
*/
|
|
185
|
+
validate(session) {
|
|
186
|
+
if (!session.walletId || !session.accounts?.length) {
|
|
187
|
+
return { valid: false, reason: "invalid" };
|
|
188
|
+
}
|
|
189
|
+
const age = Date.now() - session.connectedAt;
|
|
190
|
+
if (age > this.maxAge) {
|
|
191
|
+
return { valid: false, reason: "expired" };
|
|
192
|
+
}
|
|
193
|
+
return { valid: true, session };
|
|
194
|
+
}
|
|
195
|
+
/**
|
|
196
|
+
* Update the last active timestamp
|
|
197
|
+
*/
|
|
198
|
+
async touch() {
|
|
199
|
+
const session = await this.get();
|
|
200
|
+
if (!session)
|
|
201
|
+
return;
|
|
202
|
+
session.lastActiveAt = Date.now();
|
|
203
|
+
await this.storage.set(this.storageKey, JSON.stringify(session));
|
|
204
|
+
this.cachedSession = session;
|
|
205
|
+
}
|
|
206
|
+
/**
|
|
207
|
+
* Update session data (e.g., accounts changed)
|
|
208
|
+
*/
|
|
209
|
+
async update(updates) {
|
|
210
|
+
const session = await this.get();
|
|
211
|
+
if (!session)
|
|
212
|
+
return null;
|
|
213
|
+
const updatedSession = {
|
|
214
|
+
...session,
|
|
215
|
+
...updates,
|
|
216
|
+
lastActiveAt: Date.now(),
|
|
217
|
+
};
|
|
218
|
+
await this.storage.set(this.storageKey, JSON.stringify(updatedSession));
|
|
219
|
+
this.cachedSession = updatedSession;
|
|
220
|
+
return updatedSession;
|
|
221
|
+
}
|
|
222
|
+
/**
|
|
223
|
+
* Clear the session
|
|
224
|
+
*/
|
|
225
|
+
async clear() {
|
|
226
|
+
await this.storage.remove(this.storageKey);
|
|
227
|
+
this.cachedSession = null;
|
|
228
|
+
// Stop idle tracking
|
|
229
|
+
this.stopIdleTracking();
|
|
230
|
+
// Broadcast to other tabs
|
|
231
|
+
this.broadcast({ type: "session:cleared", timestamp: Date.now() });
|
|
232
|
+
this.onSessionCleared?.();
|
|
233
|
+
}
|
|
234
|
+
/**
|
|
235
|
+
* Check if a session is still valid (simple boolean check)
|
|
236
|
+
*/
|
|
237
|
+
isValid(session) {
|
|
238
|
+
return this.validate(session).valid;
|
|
239
|
+
}
|
|
240
|
+
/**
|
|
241
|
+
* Check if we should auto-reconnect
|
|
242
|
+
*/
|
|
243
|
+
shouldAutoReconnect() {
|
|
244
|
+
return this.autoReconnect;
|
|
245
|
+
}
|
|
246
|
+
/**
|
|
247
|
+
* Try to restore a previous session
|
|
248
|
+
* Returns the session if valid and autoReconnect is enabled
|
|
249
|
+
*/
|
|
250
|
+
async tryRestore() {
|
|
251
|
+
if (!this.autoReconnect)
|
|
252
|
+
return null;
|
|
253
|
+
const session = await this.get();
|
|
254
|
+
if (session) {
|
|
255
|
+
this.onSessionRestored?.(session);
|
|
256
|
+
// Start idle tracking for restored session
|
|
257
|
+
this.startIdleTracking();
|
|
258
|
+
return session;
|
|
259
|
+
}
|
|
260
|
+
return null;
|
|
261
|
+
}
|
|
262
|
+
/**
|
|
263
|
+
* Get session age in milliseconds
|
|
264
|
+
*/
|
|
265
|
+
async getAge() {
|
|
266
|
+
const session = await this.get();
|
|
267
|
+
if (!session)
|
|
268
|
+
return null;
|
|
269
|
+
return Date.now() - session.connectedAt;
|
|
270
|
+
}
|
|
271
|
+
/**
|
|
272
|
+
* Get time since last activity
|
|
273
|
+
*/
|
|
274
|
+
async getIdleTime() {
|
|
275
|
+
const session = await this.get();
|
|
276
|
+
if (!session)
|
|
277
|
+
return null;
|
|
278
|
+
return Date.now() - session.lastActiveAt;
|
|
279
|
+
}
|
|
280
|
+
/**
|
|
281
|
+
* Check if session exists (without full validation)
|
|
282
|
+
*/
|
|
283
|
+
async exists() {
|
|
284
|
+
const raw = await this.storage.get(this.storageKey);
|
|
285
|
+
return raw !== null;
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
exports.SessionManager = SessionManager;
|
|
289
|
+
//# sourceMappingURL=session.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"session.js","sourceRoot":"","sources":["../../src/helpers/session.ts"],"names":[],"mappings":";;;AAAA,uCAAsD;AAoCtD,MAAM,eAAe,GAAG,CAAC,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,IAAI,CAAC,CAAC,SAAS;AAC1D,MAAM,mBAAmB,GAAG,sBAAsB,CAAC;AACnD,MAAM,sBAAsB,GAAG,2BAA2B,CAAC;AAE3D,MAAa,cAAc;IACjB,OAAO,CAAc;IACrB,UAAU,CAAS;IACnB,MAAM,CAAS;IACf,WAAW,CAAgB;IAC3B,aAAa,CAAU;IACvB,kBAAkB,CAAU;IAC5B,aAAa,GAAmB,IAAI,CAAC;IAErC,gBAAgB,GAA4B,IAAI,CAAC;IACjD,SAAS,GAAyC,IAAI,CAAC;IACvD,eAAe,GAAwB,IAAI,CAAC;IAE5C,iBAAiB,CAA8B;IAC/C,gBAAgB,CAA8B;IAC9C,gBAAgB,CAA8B;IAC9C,gBAAgB,CAAc;IAC9B,eAAe,CAA8B;IAC7C,aAAa,C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,iBAAiB,EAAE,CAAC,OAAO,CAAC,CAAC;YAElC,2CAA2C;YAC3C,IAAI,CAAC,iBAAiB,EAAE,CAAC;YAEzB,OAAO,OAAO,CAAC;QACjB,CAAC;QAED,OAAO,IAAI,CAAC;IACd,CAAC;IAED;;OAEG;IACH,KAAK,CAAC,MAAM;QACV,MAAM,OAAO,GAAG,MAAM,IAAI,CAAC,GAAG,EAAE,CAAC;QACjC,IAAI,CAAC,OAAO;YAAE,OAAO,IAAI,CAAC;QAC1B,OAAO,IAAI,CAAC,GAAG,EAAE,GAAG,OAAO,CAAC,WAAW,CAAC;IAC1C,CAAC;IAED;;OAEG;IACH,KAAK,CAAC,WAAW;QACf,MAAM,OAAO,GAAG,MAAM,IAAI,CAAC,GAAG,EAAE,CAAC;QACjC,IAAI,CAAC,OAAO;YAAE,OAAO,IAAI,CAAC;QAC1B,OAAO,IAAI,CAAC,GAAG,EAAE,GAAG,OAAO,CAAC,YAAY,CAAC;IAC3C,CAAC;IAED;;OAEG;IACH,KAAK,CAAC,MAAM;QACV,MAAM,GAAG,GAAG,MAAM,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC;QACpD,OAAO,GAAG,KAAK,IAAI,CAAC;IACtB,CAAC;CACF;AA9TD,wCA8TC"}
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
import { Transaction, Network, Account } from "../types";
|
|
2
|
+
/**
|
|
3
|
+
* Gas estimation result
|
|
4
|
+
*/
|
|
5
|
+
export interface GasEstimate {
|
|
6
|
+
/** Estimated gas in gas units */
|
|
7
|
+
gas: string;
|
|
8
|
+
/** Estimated gas in TGas for display */
|
|
9
|
+
tgas: number;
|
|
10
|
+
/** Estimated gas cost in NEAR */
|
|
11
|
+
gasCostNear: string;
|
|
12
|
+
/** Whether the account has enough balance */
|
|
13
|
+
canAfford: boolean;
|
|
14
|
+
/** Breakdown by action */
|
|
15
|
+
breakdown: ActionGasEstimate[];
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Per-action gas estimate
|
|
19
|
+
*/
|
|
20
|
+
export interface ActionGasEstimate {
|
|
21
|
+
type: string;
|
|
22
|
+
methodName?: string;
|
|
23
|
+
gas: string;
|
|
24
|
+
tgas: number;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Simulation result for a transaction
|
|
28
|
+
*/
|
|
29
|
+
export interface SimulationResult {
|
|
30
|
+
/** Whether the simulation succeeded */
|
|
31
|
+
success: boolean;
|
|
32
|
+
/** Gas estimation */
|
|
33
|
+
gasEstimate: GasEstimate;
|
|
34
|
+
/** Total deposit required in yoctoNEAR */
|
|
35
|
+
totalDeposit: string;
|
|
36
|
+
/** Total deposit in NEAR for display */
|
|
37
|
+
totalDepositNear: string;
|
|
38
|
+
/** Storage deposit required */
|
|
39
|
+
storageDeposit?: string;
|
|
40
|
+
/** Logs from the simulation */
|
|
41
|
+
logs: string[];
|
|
42
|
+
/** Return value if any */
|
|
43
|
+
returnValue?: unknown;
|
|
44
|
+
/** Error message if failed */
|
|
45
|
+
error?: string;
|
|
46
|
+
/** Warnings about the transaction */
|
|
47
|
+
warnings: SimulationWarning[];
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Warning types for transaction simulation
|
|
51
|
+
*/
|
|
52
|
+
export type SimulationWarningType = "high_gas" | "high_deposit" | "low_balance" | "contract_not_found" | "method_not_found" | "access_key_limited" | "storage_staking" | "dangerous_action";
|
|
53
|
+
/**
|
|
54
|
+
* Simulation warning
|
|
55
|
+
*/
|
|
56
|
+
export interface SimulationWarning {
|
|
57
|
+
type: SimulationWarningType;
|
|
58
|
+
message: string;
|
|
59
|
+
severity: "info" | "warning" | "error";
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* RPC provider for simulation
|
|
63
|
+
*/
|
|
64
|
+
export interface SimulationRpcProvider {
|
|
65
|
+
query<T>(params: {
|
|
66
|
+
request_type: string;
|
|
67
|
+
finality?: string;
|
|
68
|
+
account_id?: string;
|
|
69
|
+
method_name?: string;
|
|
70
|
+
args_base64?: string;
|
|
71
|
+
}): Promise<T>;
|
|
72
|
+
sendTransaction(signedTx: string): Promise<unknown>;
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Transaction simulator
|
|
76
|
+
*/
|
|
77
|
+
export declare class TransactionSimulator {
|
|
78
|
+
private network;
|
|
79
|
+
private rpcEndpoint;
|
|
80
|
+
constructor(options?: {
|
|
81
|
+
network?: Network;
|
|
82
|
+
rpcEndpoint?: string;
|
|
83
|
+
});
|
|
84
|
+
/**
|
|
85
|
+
* Simulate a transaction and get gas estimates
|
|
86
|
+
*/
|
|
87
|
+
simulate(transaction: Transaction, signerAccount: Account): Promise<SimulationResult>;
|
|
88
|
+
/**
|
|
89
|
+
* Estimate gas for a transaction
|
|
90
|
+
*/
|
|
91
|
+
estimateGas(transaction: Transaction, signerAccount: Account): Promise<GasEstimate>;
|
|
92
|
+
/**
|
|
93
|
+
* Estimate gas for a single action
|
|
94
|
+
*/
|
|
95
|
+
private estimateActionGas;
|
|
96
|
+
/**
|
|
97
|
+
* Calculate total deposit required
|
|
98
|
+
*/
|
|
99
|
+
private calculateTotalDeposit;
|
|
100
|
+
/**
|
|
101
|
+
* Check for dangerous actions
|
|
102
|
+
*/
|
|
103
|
+
private checkDangerousActions;
|
|
104
|
+
/**
|
|
105
|
+
* Simulate a function call via RPC view call
|
|
106
|
+
*/
|
|
107
|
+
private simulateFunctionCall;
|
|
108
|
+
/**
|
|
109
|
+
* Get account balance
|
|
110
|
+
*/
|
|
111
|
+
private getAccountBalance;
|
|
112
|
+
/**
|
|
113
|
+
* Format yoctoNEAR to NEAR
|
|
114
|
+
*/
|
|
115
|
+
private formatNear;
|
|
116
|
+
/**
|
|
117
|
+
* Set network
|
|
118
|
+
*/
|
|
119
|
+
setNetwork(network: Network): void;
|
|
120
|
+
/**
|
|
121
|
+
* Set custom RPC endpoint
|
|
122
|
+
*/
|
|
123
|
+
setRpcEndpoint(endpoint: string): void;
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Quick gas estimate without full simulation
|
|
127
|
+
*/
|
|
128
|
+
export declare function quickGasEstimate(transaction: Transaction): GasEstimate;
|