@dynamic-labs/tron 4.40.1 → 4.40.2
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/CHANGELOG.md +7 -0
- package/package.cjs +1 -1
- package/package.js +1 -1
- package/package.json +6 -6
- package/src/connectors/TronWalletAdapterConnector/TronWalletAdapterConnector.cjs +201 -69
- package/src/connectors/TronWalletAdapterConnector/TronWalletAdapterConnector.d.ts +25 -17
- package/src/connectors/TronWalletAdapterConnector/TronWalletAdapterConnector.js +201 -69
- package/src/index.cjs +2 -10
- package/src/index.d.ts +4 -5
- package/src/index.js +2 -10
- package/src/utils/fetchTronWalletAdapterConnectors/fetchTronWalletAdapterConnectors.cjs +80 -0
- package/src/utils/fetchTronWalletAdapterConnectors/fetchTronWalletAdapterConnectors.d.ts +35 -0
- package/src/utils/fetchTronWalletAdapterConnectors/fetchTronWalletAdapterConnectors.js +75 -0
- package/src/utils/fetchTronWalletAdapterConnectors/index.d.ts +1 -0
- package/src/utils/index.d.ts +1 -0
- package/src/connectors/BitgetTronConnector/BitgetTronConnector.cjs +0 -20
- package/src/connectors/BitgetTronConnector/BitgetTronConnector.d.ts +0 -7
- package/src/connectors/BitgetTronConnector/BitgetTronConnector.js +0 -16
- package/src/connectors/BitgetTronConnector/index.d.ts +0 -1
- package/src/connectors/OKXTronConnector/OKXTronConnector.cjs +0 -20
- package/src/connectors/OKXTronConnector/OKXTronConnector.d.ts +0 -7
- package/src/connectors/OKXTronConnector/OKXTronConnector.js +0 -16
- package/src/connectors/OKXTronConnector/index.d.ts +0 -1
- package/src/connectors/TokenPocketTronConnector/TokenPocketTronConnector.cjs +0 -20
- package/src/connectors/TokenPocketTronConnector/TokenPocketTronConnector.d.ts +0 -7
- package/src/connectors/TokenPocketTronConnector/TokenPocketTronConnector.js +0 -16
- package/src/connectors/TokenPocketTronConnector/index.d.ts +0 -1
- package/src/connectors/TrustTronConnector/TrustTronConnector.cjs +0 -20
- package/src/connectors/TrustTronConnector/TrustTronConnector.d.ts +0 -7
- package/src/connectors/TrustTronConnector/TrustTronConnector.js +0 -16
- package/src/connectors/TrustTronConnector/index.d.ts +0 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,4 +1,11 @@
|
|
|
1
1
|
|
|
2
|
+
### [4.40.2](https://github.com/dynamic-labs/dynamic-auth/compare/v4.40.1...v4.40.2) (2025-10-28)
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
### Bug Fixes
|
|
6
|
+
|
|
7
|
+
* upgrade node to 20.20 in sanity-yarn-add-sdk job for compatibility ([#9743](https://github.com/dynamic-labs/dynamic-auth/issues/9743)) ([58d7e32](https://github.com/dynamic-labs/dynamic-auth/commit/58d7e32cd3965fba983c91c6345eae3bfe8dd1d5))
|
|
8
|
+
|
|
2
9
|
### [4.40.1](https://github.com/dynamic-labs/dynamic-auth/compare/v4.40.0...v4.40.1) (2025-10-24)
|
|
3
10
|
|
|
4
11
|
### Bug Fixes
|
package/package.cjs
CHANGED
package/package.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dynamic-labs/tron",
|
|
3
|
-
"version": "4.40.
|
|
3
|
+
"version": "4.40.2",
|
|
4
4
|
"description": "A React SDK for implementing wallet web3 authentication and authorization to your website.",
|
|
5
5
|
"author": "Dynamic Labs, Inc.",
|
|
6
6
|
"license": "MIT",
|
|
@@ -18,14 +18,14 @@
|
|
|
18
18
|
},
|
|
19
19
|
"homepage": "https://www.dynamic.xyz/",
|
|
20
20
|
"dependencies": {
|
|
21
|
-
"@dynamic-labs/assert-package-version": "4.40.
|
|
21
|
+
"@dynamic-labs/assert-package-version": "4.40.2",
|
|
22
22
|
"@tronweb3/tronwallet-abstract-adapter": "1.1.9",
|
|
23
23
|
"@tronweb3/tronwallet-adapters": "1.2.14",
|
|
24
24
|
"tronweb": "6.0.4",
|
|
25
|
-
"@dynamic-labs/types": "4.40.
|
|
26
|
-
"@dynamic-labs/utils": "4.40.
|
|
27
|
-
"@dynamic-labs/wallet-book": "4.40.
|
|
28
|
-
"@dynamic-labs/wallet-connector-core": "4.40.
|
|
25
|
+
"@dynamic-labs/types": "4.40.2",
|
|
26
|
+
"@dynamic-labs/utils": "4.40.2",
|
|
27
|
+
"@dynamic-labs/wallet-book": "4.40.2",
|
|
28
|
+
"@dynamic-labs/wallet-connector-core": "4.40.2"
|
|
29
29
|
},
|
|
30
30
|
"peerDependencies": {}
|
|
31
31
|
}
|
|
@@ -4,59 +4,124 @@
|
|
|
4
4
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
5
5
|
|
|
6
6
|
var _tslib = require('../../../_virtual/_tslib.cjs');
|
|
7
|
+
var tronwalletAdapters = require('@tronweb3/tronwallet-adapters');
|
|
7
8
|
var walletConnectorCore = require('@dynamic-labs/wallet-connector-core');
|
|
8
9
|
var utils = require('@dynamic-labs/utils');
|
|
9
10
|
var TronWallet = require('../../wallet/TronWallet/TronWallet.cjs');
|
|
10
11
|
var TronUiTransaction = require('../../utils/TronUiTransaction/TronUiTransaction.cjs');
|
|
11
12
|
var convertChainIdToDecimal = require('../../utils/convertChainIdToDecimal/convertChainIdToDecimal.cjs');
|
|
12
13
|
|
|
14
|
+
/**
|
|
15
|
+
* Registry mapping wallet keys to their adapter classes
|
|
16
|
+
*/
|
|
17
|
+
const ADAPTER_REGISTRY = {
|
|
18
|
+
bitgettron: tronwalletAdapters.BitKeepAdapter,
|
|
19
|
+
okxtron: tronwalletAdapters.OkxWalletAdapter,
|
|
20
|
+
tokenpockettron: tronwalletAdapters.TokenPocketAdapter,
|
|
21
|
+
tronlinkwallet: tronwalletAdapters.TronLinkAdapter,
|
|
22
|
+
trusttron: tronwalletAdapters.TrustAdapter,
|
|
23
|
+
};
|
|
13
24
|
class TronWalletAdapterConnector extends walletConnectorCore.WalletConnectorBase {
|
|
25
|
+
/**
|
|
26
|
+
* Create the TronWallet Adapter instance for this connector
|
|
27
|
+
* Uses the adapter registry to create the appropriate adapter
|
|
28
|
+
*/
|
|
29
|
+
createAdapter() {
|
|
30
|
+
const AdapterClass = ADAPTER_REGISTRY[this.overrideKey];
|
|
31
|
+
if (!AdapterClass) {
|
|
32
|
+
walletConnectorCore.logger.warn(`[${this.name}] Unknown adapter for overrideKey: ${this.overrideKey}. Falling back to TronLink.`);
|
|
33
|
+
return new tronwalletAdapters.TronLinkAdapter();
|
|
34
|
+
}
|
|
35
|
+
const adapter = new AdapterClass();
|
|
36
|
+
return adapter;
|
|
37
|
+
}
|
|
14
38
|
constructor(opts) {
|
|
15
39
|
super(opts);
|
|
16
40
|
this.ChainWallet = TronWallet.TronWallet;
|
|
17
|
-
this.name = 'Tron';
|
|
18
|
-
this.overrideKey = '
|
|
41
|
+
this.name = 'Tron Wallet';
|
|
42
|
+
this.overrideKey = 'tronwallet';
|
|
19
43
|
this.connectedChain = 'TRON';
|
|
20
44
|
this.supportedChains = ['TRON'];
|
|
21
45
|
this.switchNetworkOnlyFromWallet = true;
|
|
46
|
+
this.adapter = undefined;
|
|
47
|
+
this.balanceCache = new Map();
|
|
48
|
+
this.networkCache = null;
|
|
22
49
|
this.tronNetworks = opts.tronNetworks;
|
|
23
|
-
|
|
24
|
-
this.adapter = this.createAdapter();
|
|
50
|
+
this.overrideKey = opts.overrideKey || 'tronwallet';
|
|
25
51
|
}
|
|
26
52
|
confirmTransactionStatus() {
|
|
27
53
|
throw new Error('Method not implemented.');
|
|
28
54
|
}
|
|
29
55
|
/**
|
|
30
56
|
* Get the TronWallet Adapter instance
|
|
57
|
+
* Adapter is created lazily only when connect() is called to avoid multiple adapters
|
|
58
|
+
* polling simultaneously at startup.
|
|
31
59
|
*/
|
|
32
60
|
getAdapter() {
|
|
61
|
+
if (!this.adapter) {
|
|
62
|
+
throw new Error('Adapter not initialized. Call connect() first.');
|
|
63
|
+
}
|
|
33
64
|
return this.adapter;
|
|
34
65
|
}
|
|
35
66
|
/**
|
|
36
|
-
* Check if the wallet is installed
|
|
67
|
+
* Check if the specific wallet is installed on the browser
|
|
68
|
+
* Each wallet adapter checks for its own specific browser injection
|
|
37
69
|
*/
|
|
38
70
|
isInstalledOnBrowser() {
|
|
39
|
-
|
|
40
|
-
|
|
71
|
+
try {
|
|
72
|
+
// Create the adapter to check if the wallet is installed
|
|
73
|
+
if (!this.adapter) {
|
|
74
|
+
const AdapterClass = ADAPTER_REGISTRY[this.overrideKey];
|
|
75
|
+
if (!AdapterClass) {
|
|
76
|
+
return false;
|
|
77
|
+
}
|
|
78
|
+
this.adapter = new AdapterClass();
|
|
79
|
+
}
|
|
80
|
+
// The adapter's readyState indicates if the wallet is installed:
|
|
81
|
+
// NotFound = wallet not installed
|
|
82
|
+
// Loading/Found = wallet is installed
|
|
83
|
+
return this.adapter.readyState !== 'NotFound';
|
|
84
|
+
}
|
|
85
|
+
catch (_a) {
|
|
86
|
+
return false;
|
|
87
|
+
}
|
|
41
88
|
}
|
|
42
89
|
/**
|
|
43
90
|
* Connect to the wallet
|
|
44
91
|
*/
|
|
45
92
|
connect() {
|
|
46
93
|
return _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
47
|
-
|
|
94
|
+
// Create adapter NOW (first time connect is called)
|
|
95
|
+
if (!this.adapter) {
|
|
96
|
+
this.adapter = this.createAdapter();
|
|
97
|
+
}
|
|
98
|
+
const { adapter } = { adapter: this.adapter };
|
|
99
|
+
// If already connected, return early
|
|
48
100
|
if (adapter.connected) {
|
|
49
101
|
return;
|
|
50
102
|
}
|
|
51
|
-
|
|
52
|
-
throw new utils.DynamicError(`${this.name} wallet not found. Please install the extension.`);
|
|
53
|
-
}
|
|
103
|
+
// Connect to the adapter
|
|
54
104
|
try {
|
|
55
105
|
yield adapter.connect();
|
|
56
106
|
}
|
|
57
107
|
catch (error) {
|
|
58
108
|
throw new utils.DynamicError(`Failed to connect to ${this.name}: ${error}`);
|
|
59
109
|
}
|
|
110
|
+
// Some wallets (Trust, Bitget) populate the address asynchronously after connect()
|
|
111
|
+
// Wait briefly for the address to be populated by the wallet
|
|
112
|
+
const initialAddress = adapter.address;
|
|
113
|
+
const hasNoAddress = !initialAddress ||
|
|
114
|
+
(typeof initialAddress === 'string' && initialAddress.length === 0);
|
|
115
|
+
if (hasNoAddress) {
|
|
116
|
+
// Give the wallet 1000ms to populate the address
|
|
117
|
+
const startTime = Date.now();
|
|
118
|
+
while ((!adapter.address ||
|
|
119
|
+
(typeof adapter.address === 'string' &&
|
|
120
|
+
adapter.address.length === 0)) &&
|
|
121
|
+
Date.now() - startTime < 1000) {
|
|
122
|
+
yield new Promise((resolve) => setTimeout(resolve, 50));
|
|
123
|
+
}
|
|
124
|
+
}
|
|
60
125
|
});
|
|
61
126
|
}
|
|
62
127
|
/**
|
|
@@ -64,12 +129,41 @@ class TronWalletAdapterConnector extends walletConnectorCore.WalletConnectorBase
|
|
|
64
129
|
*/
|
|
65
130
|
getAddress() {
|
|
66
131
|
return _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
67
|
-
|
|
68
|
-
|
|
132
|
+
// Create adapter if it doesn't exist yet (defensive against SDK calling getAddress before connect)
|
|
133
|
+
if (!this.adapter) {
|
|
134
|
+
this.adapter = this.createAdapter();
|
|
135
|
+
}
|
|
136
|
+
const { adapter } = { adapter: this.adapter };
|
|
137
|
+
// If adapter exists but isn't connected yet, connect it first
|
|
69
138
|
if (!adapter.connected) {
|
|
70
|
-
|
|
139
|
+
try {
|
|
140
|
+
yield adapter.connect();
|
|
141
|
+
// Some wallets (Trust, Bitget) populate the address asynchronously after connect()
|
|
142
|
+
// Wait briefly for the address to be populated by the wallet
|
|
143
|
+
const initialAddress = adapter.address;
|
|
144
|
+
const hasNoAddress = !initialAddress ||
|
|
145
|
+
(typeof initialAddress === 'string' && initialAddress.length === 0);
|
|
146
|
+
if (hasNoAddress) {
|
|
147
|
+
// Give the wallet 1000ms to populate the address
|
|
148
|
+
const startTime = Date.now();
|
|
149
|
+
while ((!adapter.address ||
|
|
150
|
+
(typeof adapter.address === 'string' &&
|
|
151
|
+
adapter.address.length === 0)) &&
|
|
152
|
+
Date.now() - startTime < 1000) {
|
|
153
|
+
yield new Promise((resolve) => setTimeout(resolve, 50));
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
catch (error) {
|
|
158
|
+
throw new utils.DynamicError(`Failed to connect to ${this.name}: ${error}`);
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
const { address } = adapter;
|
|
162
|
+
// Handle null, undefined, and empty string
|
|
163
|
+
if (!address || address.length === 0) {
|
|
164
|
+
return undefined;
|
|
71
165
|
}
|
|
72
|
-
return
|
|
166
|
+
return address;
|
|
73
167
|
});
|
|
74
168
|
}
|
|
75
169
|
/**
|
|
@@ -82,15 +176,35 @@ class TronWalletAdapterConnector extends walletConnectorCore.WalletConnectorBase
|
|
|
82
176
|
});
|
|
83
177
|
}
|
|
84
178
|
/**
|
|
85
|
-
* Get the current network
|
|
179
|
+
* Get the current network with caching to prevent rate limiting
|
|
86
180
|
*/
|
|
87
181
|
getNetwork() {
|
|
88
182
|
return _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
183
|
+
var _a;
|
|
184
|
+
// Don't create adapter if it doesn't exist yet (prevents initialization crashes)
|
|
185
|
+
if (!this.adapter) {
|
|
186
|
+
return '728126428'; // Mainnet fallback
|
|
187
|
+
}
|
|
188
|
+
// Check cache first (10 second cache for network info)
|
|
189
|
+
if (this.networkCache && Date.now() - this.networkCache.timestamp < 10000) {
|
|
190
|
+
return this.networkCache.network;
|
|
191
|
+
}
|
|
89
192
|
try {
|
|
90
193
|
const networkInfo = yield this.adapter.network();
|
|
91
|
-
|
|
194
|
+
const networkStr = convertChainIdToDecimal.convertChainIdToDecimal(networkInfo.chainId);
|
|
195
|
+
// Cache the result
|
|
196
|
+
this.networkCache = {
|
|
197
|
+
network: networkStr,
|
|
198
|
+
timestamp: Date.now(),
|
|
199
|
+
};
|
|
200
|
+
return networkStr;
|
|
92
201
|
}
|
|
93
202
|
catch (error) {
|
|
203
|
+
// Handle rate limiting gracefully
|
|
204
|
+
if (error instanceof Error && error.message.includes('429')) {
|
|
205
|
+
walletConnectorCore.logger.warn(`[${this.name}] Rate limited, returning cached network or mainnet`);
|
|
206
|
+
return ((_a = this.networkCache) === null || _a === void 0 ? void 0 : _a.network) || '728126428'; // Mainnet in decimal format
|
|
207
|
+
}
|
|
94
208
|
// If adapter is not connected, fall back to mainnet
|
|
95
209
|
return '728126428'; // Mainnet in decimal format
|
|
96
210
|
}
|
|
@@ -101,6 +215,10 @@ class TronWalletAdapterConnector extends walletConnectorCore.WalletConnectorBase
|
|
|
101
215
|
*/
|
|
102
216
|
isTestnet() {
|
|
103
217
|
return _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
218
|
+
// Don't create adapter if it doesn't exist yet (prevents initialization crashes)
|
|
219
|
+
if (!this.adapter) {
|
|
220
|
+
return false; // Assume mainnet if not connected
|
|
221
|
+
}
|
|
104
222
|
try {
|
|
105
223
|
const networkInfo = yield this.adapter.network();
|
|
106
224
|
// Testnet networkTypes: Shasta, Nile
|
|
@@ -108,6 +226,11 @@ class TronWalletAdapterConnector extends walletConnectorCore.WalletConnectorBase
|
|
|
108
226
|
networkInfo.networkType === 'Nile');
|
|
109
227
|
}
|
|
110
228
|
catch (error) {
|
|
229
|
+
// Handle rate limiting gracefully - assume mainnet (not testnet)
|
|
230
|
+
if (error instanceof Error && error.message.includes('429')) {
|
|
231
|
+
walletConnectorCore.logger.warn(`[${this.name}] Rate limited, assuming mainnet (not testnet)`);
|
|
232
|
+
return false;
|
|
233
|
+
}
|
|
111
234
|
// If adapter is not connected, assume mainnet (not testnet)
|
|
112
235
|
return false;
|
|
113
236
|
}
|
|
@@ -118,7 +241,10 @@ class TronWalletAdapterConnector extends walletConnectorCore.WalletConnectorBase
|
|
|
118
241
|
*/
|
|
119
242
|
signMessage(messageToSign) {
|
|
120
243
|
return _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
121
|
-
|
|
244
|
+
if (!this.adapter) {
|
|
245
|
+
throw new utils.DynamicError('Wallet not connected');
|
|
246
|
+
}
|
|
247
|
+
const { adapter } = { adapter: this.adapter };
|
|
122
248
|
if (!adapter.connected) {
|
|
123
249
|
throw new utils.DynamicError('Wallet not connected');
|
|
124
250
|
}
|
|
@@ -134,36 +260,70 @@ class TronWalletAdapterConnector extends walletConnectorCore.WalletConnectorBase
|
|
|
134
260
|
}
|
|
135
261
|
/**
|
|
136
262
|
* Get the TronWeb instance from the adapter
|
|
137
|
-
*
|
|
263
|
+
*
|
|
264
|
+
* CRITICAL: Each adapter maintains its own _wallet.tronWeb reference
|
|
265
|
+
* to avoid collision. We access it through the adapter's internal state,
|
|
266
|
+
* NOT through global window objects.
|
|
267
|
+
*
|
|
268
|
+
* This is the key insight from the tronwallet-adapter implementation:
|
|
269
|
+
* - TronLink stores in _wallet (can be window.tronLink or window.tron.tronWeb)
|
|
270
|
+
* - TokenPocket stores in _wallet (from window.tokenpocket.tronWeb)
|
|
271
|
+
* - BitKeep stores in _wallet (from window.bitkeep.tronWeb)
|
|
272
|
+
* - Each adapter handles its own detection and isolation
|
|
138
273
|
*/
|
|
139
274
|
getWalletTronWeb() {
|
|
140
|
-
var _a;
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
if (adapter.tronWeb) {
|
|
144
|
-
return adapter.tronWeb;
|
|
275
|
+
var _a, _b, _c;
|
|
276
|
+
if (!this.adapter) {
|
|
277
|
+
return undefined;
|
|
145
278
|
}
|
|
146
|
-
|
|
279
|
+
const adapter = this.adapter;
|
|
280
|
+
// The adapter's internal wallet contains the wallet-specific TronWeb
|
|
281
|
+
// This is the safe way to access it without collisions
|
|
147
282
|
if ((_a = adapter._wallet) === null || _a === void 0 ? void 0 : _a.tronWeb) {
|
|
148
283
|
return adapter._wallet.tronWeb;
|
|
149
284
|
}
|
|
285
|
+
// Some adapters nest it under _wallet.tron.tronWeb
|
|
286
|
+
if ((_c = (_b = adapter._wallet) === null || _b === void 0 ? void 0 : _b.tron) === null || _c === void 0 ? void 0 : _c.tronWeb) {
|
|
287
|
+
return adapter._wallet.tron.tronWeb;
|
|
288
|
+
}
|
|
150
289
|
return undefined;
|
|
151
290
|
}
|
|
152
291
|
/**
|
|
153
|
-
* Get balance of an address
|
|
292
|
+
* Get balance of an address with caching to prevent rate limiting
|
|
154
293
|
*/
|
|
155
294
|
getBalance(address) {
|
|
156
295
|
return _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
157
296
|
var _a;
|
|
297
|
+
if (!this.adapter) {
|
|
298
|
+
return '0';
|
|
299
|
+
}
|
|
300
|
+
// Check cache first (5 second cache)
|
|
301
|
+
const cached = this.balanceCache.get(address);
|
|
302
|
+
if (cached && Date.now() - cached.timestamp < 5000) {
|
|
303
|
+
return cached.balance;
|
|
304
|
+
}
|
|
305
|
+
// Get TronWeb from the adapter's internal wallet state
|
|
158
306
|
const tronWeb = this.getWalletTronWeb();
|
|
159
307
|
if (!((_a = tronWeb === null || tronWeb === void 0 ? void 0 : tronWeb.trx) === null || _a === void 0 ? void 0 : _a.getBalance)) {
|
|
308
|
+
walletConnectorCore.logger.debug(`[${this.name}] TronWeb not available for balance check`);
|
|
160
309
|
return '0';
|
|
161
310
|
}
|
|
162
311
|
try {
|
|
163
312
|
const balance = yield tronWeb.trx.getBalance(address);
|
|
164
|
-
|
|
313
|
+
const balanceStr = (balance / 1000000).toFixed(6); // SUN → TRX
|
|
314
|
+
// Cache the result
|
|
315
|
+
this.balanceCache.set(address, {
|
|
316
|
+
balance: balanceStr,
|
|
317
|
+
timestamp: Date.now(),
|
|
318
|
+
});
|
|
319
|
+
return balanceStr;
|
|
165
320
|
}
|
|
166
321
|
catch (error) {
|
|
322
|
+
// Handle rate limiting gracefully
|
|
323
|
+
if (error instanceof Error && error.message.includes('429')) {
|
|
324
|
+
walletConnectorCore.logger.warn(`[${this.name}] Rate limited, returning cached balance or 0`);
|
|
325
|
+
return (cached === null || cached === void 0 ? void 0 : cached.balance) || '0';
|
|
326
|
+
}
|
|
167
327
|
walletConnectorCore.logger.error(`[${this.name}] Failed to get balance:`, error);
|
|
168
328
|
return '0';
|
|
169
329
|
}
|
|
@@ -188,15 +348,19 @@ class TronWalletAdapterConnector extends walletConnectorCore.WalletConnectorBase
|
|
|
188
348
|
*/
|
|
189
349
|
getBlockExplorerUrlsForCurrentNetwork() {
|
|
190
350
|
return _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
191
|
-
var _a, _b, _c, _d;
|
|
351
|
+
var _a, _b, _c, _d, _e, _f;
|
|
352
|
+
if (!this.adapter) {
|
|
353
|
+
// If adapter is not connected, return mainnet block explorer URLs
|
|
354
|
+
return ((_b = (_a = this.tronNetworks.find((n) => String(n.chainId) === '728126428')) === null || _a === void 0 ? void 0 : _a.blockExplorerUrls) !== null && _b !== void 0 ? _b : []);
|
|
355
|
+
}
|
|
192
356
|
try {
|
|
193
357
|
const networkInfo = yield this.adapter.network();
|
|
194
358
|
const decimalChainId = convertChainIdToDecimal.convertChainIdToDecimal(networkInfo.chainId);
|
|
195
|
-
return ((
|
|
359
|
+
return ((_d = (_c = this.tronNetworks.find((n) => String(n.chainId) === decimalChainId)) === null || _c === void 0 ? void 0 : _c.blockExplorerUrls) !== null && _d !== void 0 ? _d : []);
|
|
196
360
|
}
|
|
197
361
|
catch (error) {
|
|
198
362
|
// If adapter is not connected, return mainnet block explorer URLs
|
|
199
|
-
return ((
|
|
363
|
+
return ((_f = (_e = this.tronNetworks.find((n) => String(n.chainId) === '728126428')) === null || _e === void 0 ? void 0 : _e.blockExplorerUrls) !== null && _f !== void 0 ? _f : []);
|
|
200
364
|
}
|
|
201
365
|
});
|
|
202
366
|
}
|
|
@@ -205,52 +369,20 @@ class TronWalletAdapterConnector extends walletConnectorCore.WalletConnectorBase
|
|
|
205
369
|
*/
|
|
206
370
|
endSession() {
|
|
207
371
|
return _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
208
|
-
|
|
209
|
-
if (adapter.connected) {
|
|
372
|
+
var _a;
|
|
373
|
+
if ((_a = this.adapter) === null || _a === void 0 ? void 0 : _a.connected) {
|
|
210
374
|
try {
|
|
211
|
-
yield adapter.disconnect();
|
|
375
|
+
yield this.adapter.disconnect();
|
|
212
376
|
}
|
|
213
377
|
catch (error) {
|
|
214
378
|
walletConnectorCore.logger.debug(`[${this.name}] Error during disconnect:`, error);
|
|
215
379
|
}
|
|
216
380
|
}
|
|
381
|
+
// Clear caches on disconnect
|
|
382
|
+
this.balanceCache.clear();
|
|
383
|
+
this.networkCache = null;
|
|
217
384
|
});
|
|
218
385
|
}
|
|
219
|
-
/**
|
|
220
|
-
* Setup event listeners - called by Dynamic's initEventListener()
|
|
221
|
-
*/
|
|
222
|
-
setupEventListeners() {
|
|
223
|
-
const { adapter } = this;
|
|
224
|
-
// Connect event
|
|
225
|
-
adapter.on('connect', (address) => {
|
|
226
|
-
this.emit('accountChange', {
|
|
227
|
-
accounts: [address],
|
|
228
|
-
});
|
|
229
|
-
});
|
|
230
|
-
// Disconnect event
|
|
231
|
-
adapter.on('disconnect', () => {
|
|
232
|
-
this.emit('disconnect');
|
|
233
|
-
});
|
|
234
|
-
// Account change event
|
|
235
|
-
adapter.on('accountsChanged', (address) => {
|
|
236
|
-
this.emit('accountChange', {
|
|
237
|
-
accounts: [address],
|
|
238
|
-
});
|
|
239
|
-
});
|
|
240
|
-
// Chain change event
|
|
241
|
-
adapter.on('chainChanged', (chainData) => {
|
|
242
|
-
const data = chainData;
|
|
243
|
-
this.emit('chainChange', {
|
|
244
|
-
chain: data.chainId,
|
|
245
|
-
});
|
|
246
|
-
});
|
|
247
|
-
}
|
|
248
|
-
/**
|
|
249
|
-
* Cleanup event listeners
|
|
250
|
-
*/
|
|
251
|
-
teardownEventListeners() {
|
|
252
|
-
this.adapter.removeAllListeners();
|
|
253
|
-
}
|
|
254
386
|
/**
|
|
255
387
|
* Create a UI transaction for the send balance flow
|
|
256
388
|
*
|
|
@@ -260,7 +392,7 @@ class TronWalletAdapterConnector extends walletConnectorCore.WalletConnectorBase
|
|
|
260
392
|
createUiTransaction(from) {
|
|
261
393
|
return _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
262
394
|
yield this.validateActiveWallet(from);
|
|
263
|
-
// Get TronWeb from the wallet
|
|
395
|
+
// Get TronWeb from the adapter's internal wallet state
|
|
264
396
|
const tronWeb = this.getWalletTronWeb();
|
|
265
397
|
if (!tronWeb) {
|
|
266
398
|
throw new utils.DynamicError('TronWeb not available');
|
|
@@ -1,12 +1,14 @@
|
|
|
1
1
|
import type { Adapter, Network, TronWeb } from '@tronweb3/tronwallet-abstract-adapter';
|
|
2
|
-
import type { WalletBookSchema } from '@dynamic-labs/wallet-book';
|
|
3
2
|
import { Chain, ISendBalanceWalletConnector, WalletConnectorBase, WalletMetadata } from '@dynamic-labs/wallet-connector-core';
|
|
3
|
+
import type { WalletBookSchema } from '@dynamic-labs/wallet-book';
|
|
4
4
|
import type { GenericNetwork, IUITransaction, TransactionReceipt } from '@dynamic-labs/types';
|
|
5
5
|
import { TronWallet } from '../../wallet/TronWallet';
|
|
6
6
|
export type TronWalletAdapterConnectorOpts = {
|
|
7
7
|
walletBook: WalletBookSchema;
|
|
8
8
|
tronNetworks: GenericNetwork[];
|
|
9
9
|
metadata?: WalletMetadata;
|
|
10
|
+
overrideKey?: string;
|
|
11
|
+
walletData?: WalletBookSchema['wallets'][string];
|
|
10
12
|
};
|
|
11
13
|
/**
|
|
12
14
|
* TronWallet Adapter interface that includes the network() method
|
|
@@ -23,20 +25,25 @@ export declare abstract class TronWalletAdapterConnector extends WalletConnector
|
|
|
23
25
|
supportedChains: Chain[];
|
|
24
26
|
switchNetworkOnlyFromWallet: boolean;
|
|
25
27
|
protected tronNetworks: GenericNetwork[];
|
|
26
|
-
protected adapter
|
|
28
|
+
protected adapter?: TronAdapterWithNetwork;
|
|
29
|
+
private balanceCache;
|
|
30
|
+
private networkCache;
|
|
27
31
|
/**
|
|
28
32
|
* Create the TronWallet Adapter instance for this connector
|
|
29
|
-
*
|
|
33
|
+
* Uses the adapter registry to create the appropriate adapter
|
|
30
34
|
*/
|
|
31
|
-
protected
|
|
35
|
+
protected createAdapter(): TronAdapterWithNetwork;
|
|
32
36
|
constructor(opts: TronWalletAdapterConnectorOpts);
|
|
33
37
|
confirmTransactionStatus?(): Promise<TransactionReceipt>;
|
|
34
38
|
/**
|
|
35
39
|
* Get the TronWallet Adapter instance
|
|
40
|
+
* Adapter is created lazily only when connect() is called to avoid multiple adapters
|
|
41
|
+
* polling simultaneously at startup.
|
|
36
42
|
*/
|
|
37
|
-
getAdapter():
|
|
43
|
+
getAdapter(): TronAdapterWithNetwork;
|
|
38
44
|
/**
|
|
39
|
-
* Check if the wallet is installed
|
|
45
|
+
* Check if the specific wallet is installed on the browser
|
|
46
|
+
* Each wallet adapter checks for its own specific browser injection
|
|
40
47
|
*/
|
|
41
48
|
isInstalledOnBrowser(): boolean;
|
|
42
49
|
/**
|
|
@@ -52,7 +59,7 @@ export declare abstract class TronWalletAdapterConnector extends WalletConnector
|
|
|
52
59
|
*/
|
|
53
60
|
getConnectedAccounts(): Promise<string[]>;
|
|
54
61
|
/**
|
|
55
|
-
* Get the current network
|
|
62
|
+
* Get the current network with caching to prevent rate limiting
|
|
56
63
|
*/
|
|
57
64
|
getNetwork(): Promise<string | undefined>;
|
|
58
65
|
/**
|
|
@@ -65,11 +72,20 @@ export declare abstract class TronWalletAdapterConnector extends WalletConnector
|
|
|
65
72
|
signMessage(messageToSign: string): Promise<string>;
|
|
66
73
|
/**
|
|
67
74
|
* Get the TronWeb instance from the adapter
|
|
68
|
-
*
|
|
75
|
+
*
|
|
76
|
+
* CRITICAL: Each adapter maintains its own _wallet.tronWeb reference
|
|
77
|
+
* to avoid collision. We access it through the adapter's internal state,
|
|
78
|
+
* NOT through global window objects.
|
|
79
|
+
*
|
|
80
|
+
* This is the key insight from the tronwallet-adapter implementation:
|
|
81
|
+
* - TronLink stores in _wallet (can be window.tronLink or window.tron.tronWeb)
|
|
82
|
+
* - TokenPocket stores in _wallet (from window.tokenpocket.tronWeb)
|
|
83
|
+
* - BitKeep stores in _wallet (from window.bitkeep.tronWeb)
|
|
84
|
+
* - Each adapter handles its own detection and isolation
|
|
69
85
|
*/
|
|
70
86
|
getWalletTronWeb(): TronWeb | undefined;
|
|
71
87
|
/**
|
|
72
|
-
* Get balance of an address
|
|
88
|
+
* Get balance of an address with caching to prevent rate limiting
|
|
73
89
|
*/
|
|
74
90
|
getBalance(address: string): Promise<string | undefined>;
|
|
75
91
|
/**
|
|
@@ -88,14 +104,6 @@ export declare abstract class TronWalletAdapterConnector extends WalletConnector
|
|
|
88
104
|
* Disconnect and cleanup
|
|
89
105
|
*/
|
|
90
106
|
endSession(): Promise<void>;
|
|
91
|
-
/**
|
|
92
|
-
* Setup event listeners - called by Dynamic's initEventListener()
|
|
93
|
-
*/
|
|
94
|
-
setupEventListeners(): void;
|
|
95
|
-
/**
|
|
96
|
-
* Cleanup event listeners
|
|
97
|
-
*/
|
|
98
|
-
teardownEventListeners(): void;
|
|
99
107
|
/**
|
|
100
108
|
* Create a UI transaction for the send balance flow
|
|
101
109
|
*
|