@dynamic-labs/tron 4.40.0 → 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 +13 -0
- package/README.md +264 -20
- package/package.cjs +1 -1
- package/package.js +1 -1
- package/package.json +8 -6
- package/src/connectors/TronWalletAdapterConnector/TronWalletAdapterConnector.cjs +436 -0
- package/src/connectors/TronWalletAdapterConnector/TronWalletAdapterConnector.d.ts +115 -0
- package/src/connectors/TronWalletAdapterConnector/TronWalletAdapterConnector.js +432 -0
- package/src/connectors/TronWalletAdapterConnector/index.d.ts +1 -0
- package/src/index.cjs +4 -13
- package/src/index.d.ts +5 -5
- package/src/index.js +3 -12
- package/src/types.d.ts +11 -181
- package/src/utils/TronUiTransaction/TronUiTransaction.cjs +67 -26
- package/src/utils/TronUiTransaction/TronUiTransaction.d.ts +38 -5
- package/src/utils/TronUiTransaction/TronUiTransaction.js +67 -26
- package/src/utils/convertChainIdToDecimal/convertChainIdToDecimal.cjs +16 -0
- package/src/utils/convertChainIdToDecimal/convertChainIdToDecimal.d.ts +9 -0
- package/src/utils/convertChainIdToDecimal/convertChainIdToDecimal.js +12 -0
- package/src/utils/convertChainIdToDecimal/index.d.ts +1 -0
- 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/getTronGasEstimation/getTronGasEstimation.cjs +162 -0
- package/src/utils/getTronGasEstimation/getTronGasEstimation.d.ts +49 -0
- package/src/utils/getTronGasEstimation/getTronGasEstimation.js +158 -0
- package/src/utils/getTronGasEstimation/index.d.ts +1 -0
- package/src/utils/index.d.ts +1 -4
- package/src/wallet/TronWallet/TronWallet.cjs +124 -0
- package/src/wallet/TronWallet/TronWallet.d.ts +51 -0
- package/src/wallet/TronWallet/TronWallet.js +120 -0
- package/src/wallet/TronWallet/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/BybitTronConnector/BybitTronConnector.d.ts +0 -7
- package/src/connectors/BybitTronConnector/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 -28
- package/src/connectors/TokenPocketTronConnector/TokenPocketTronConnector.d.ts +0 -7
- package/src/connectors/TokenPocketTronConnector/TokenPocketTronConnector.js +0 -24
- package/src/connectors/TokenPocketTronConnector/index.d.ts +0 -1
- package/src/connectors/TronWalletConnector/TronWalletConnector.cjs +0 -255
- package/src/connectors/TronWalletConnector/TronWalletConnector.d.ts +0 -42
- package/src/connectors/TronWalletConnector/TronWalletConnector.js +0 -251
- package/src/connectors/TronWalletConnector/index.d.ts +0 -1
- package/src/connectors/TrustTronConnector/TrustTronConnector.cjs +0 -31
- package/src/connectors/TrustTronConnector/TrustTronConnector.d.ts +0 -7
- package/src/connectors/TrustTronConnector/TrustTronConnector.js +0 -27
- package/src/connectors/TrustTronConnector/index.d.ts +0 -1
- package/src/utils/detectTronNetworkFromProvider.cjs +0 -67
- package/src/utils/detectTronNetworkFromProvider.d.ts +0 -30
- package/src/utils/detectTronNetworkFromProvider.js +0 -62
- package/src/utils/getDefaultTronNetworks.cjs +0 -60
- package/src/utils/getDefaultTronNetworks.d.ts +0 -9
- package/src/utils/getDefaultTronNetworks.js +0 -53
- package/src/utils/getTronGasEstimation.cjs +0 -136
- package/src/utils/getTronGasEstimation.d.ts +0 -47
- package/src/utils/getTronGasEstimation.js +0 -129
- package/src/utils/provider.cjs +0 -59
- package/src/utils/provider.d.ts +0 -24
- package/src/utils/provider.js +0 -53
- package/src/wallet/TronWallet.cjs +0 -106
- package/src/wallet/TronWallet.d.ts +0 -22
- package/src/wallet/TronWallet.js +0 -102
- package/src/wallet/index.d.ts +0 -2
|
@@ -0,0 +1,436 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
5
|
+
|
|
6
|
+
var _tslib = require('../../../_virtual/_tslib.cjs');
|
|
7
|
+
var tronwalletAdapters = require('@tronweb3/tronwallet-adapters');
|
|
8
|
+
var walletConnectorCore = require('@dynamic-labs/wallet-connector-core');
|
|
9
|
+
var utils = require('@dynamic-labs/utils');
|
|
10
|
+
var TronWallet = require('../../wallet/TronWallet/TronWallet.cjs');
|
|
11
|
+
var TronUiTransaction = require('../../utils/TronUiTransaction/TronUiTransaction.cjs');
|
|
12
|
+
var convertChainIdToDecimal = require('../../utils/convertChainIdToDecimal/convertChainIdToDecimal.cjs');
|
|
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
|
+
};
|
|
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
|
+
}
|
|
38
|
+
constructor(opts) {
|
|
39
|
+
super(opts);
|
|
40
|
+
this.ChainWallet = TronWallet.TronWallet;
|
|
41
|
+
this.name = 'Tron Wallet';
|
|
42
|
+
this.overrideKey = 'tronwallet';
|
|
43
|
+
this.connectedChain = 'TRON';
|
|
44
|
+
this.supportedChains = ['TRON'];
|
|
45
|
+
this.switchNetworkOnlyFromWallet = true;
|
|
46
|
+
this.adapter = undefined;
|
|
47
|
+
this.balanceCache = new Map();
|
|
48
|
+
this.networkCache = null;
|
|
49
|
+
this.tronNetworks = opts.tronNetworks;
|
|
50
|
+
this.overrideKey = opts.overrideKey || 'tronwallet';
|
|
51
|
+
}
|
|
52
|
+
confirmTransactionStatus() {
|
|
53
|
+
throw new Error('Method not implemented.');
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
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.
|
|
59
|
+
*/
|
|
60
|
+
getAdapter() {
|
|
61
|
+
if (!this.adapter) {
|
|
62
|
+
throw new Error('Adapter not initialized. Call connect() first.');
|
|
63
|
+
}
|
|
64
|
+
return this.adapter;
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Check if the specific wallet is installed on the browser
|
|
68
|
+
* Each wallet adapter checks for its own specific browser injection
|
|
69
|
+
*/
|
|
70
|
+
isInstalledOnBrowser() {
|
|
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
|
+
}
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Connect to the wallet
|
|
91
|
+
*/
|
|
92
|
+
connect() {
|
|
93
|
+
return _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
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
|
|
100
|
+
if (adapter.connected) {
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
// Connect to the adapter
|
|
104
|
+
try {
|
|
105
|
+
yield adapter.connect();
|
|
106
|
+
}
|
|
107
|
+
catch (error) {
|
|
108
|
+
throw new utils.DynamicError(`Failed to connect to ${this.name}: ${error}`);
|
|
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
|
+
}
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Get the current address
|
|
129
|
+
*/
|
|
130
|
+
getAddress() {
|
|
131
|
+
return _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
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
|
|
138
|
+
if (!adapter.connected) {
|
|
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;
|
|
165
|
+
}
|
|
166
|
+
return address;
|
|
167
|
+
});
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* Get connected accounts
|
|
171
|
+
*/
|
|
172
|
+
getConnectedAccounts() {
|
|
173
|
+
return _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
174
|
+
const address = yield this.getAddress();
|
|
175
|
+
return address ? [address] : [];
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
/**
|
|
179
|
+
* Get the current network with caching to prevent rate limiting
|
|
180
|
+
*/
|
|
181
|
+
getNetwork() {
|
|
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
|
+
}
|
|
192
|
+
try {
|
|
193
|
+
const networkInfo = yield this.adapter.network();
|
|
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;
|
|
201
|
+
}
|
|
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
|
+
}
|
|
208
|
+
// If adapter is not connected, fall back to mainnet
|
|
209
|
+
return '728126428'; // Mainnet in decimal format
|
|
210
|
+
}
|
|
211
|
+
});
|
|
212
|
+
}
|
|
213
|
+
/**
|
|
214
|
+
* Check if current network is testnet
|
|
215
|
+
*/
|
|
216
|
+
isTestnet() {
|
|
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
|
+
}
|
|
222
|
+
try {
|
|
223
|
+
const networkInfo = yield this.adapter.network();
|
|
224
|
+
// Testnet networkTypes: Shasta, Nile
|
|
225
|
+
return (networkInfo.networkType === 'Shasta' ||
|
|
226
|
+
networkInfo.networkType === 'Nile');
|
|
227
|
+
}
|
|
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
|
+
}
|
|
234
|
+
// If adapter is not connected, assume mainnet (not testnet)
|
|
235
|
+
return false;
|
|
236
|
+
}
|
|
237
|
+
});
|
|
238
|
+
}
|
|
239
|
+
/**
|
|
240
|
+
* Sign a message using the adapter
|
|
241
|
+
*/
|
|
242
|
+
signMessage(messageToSign) {
|
|
243
|
+
return _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
244
|
+
if (!this.adapter) {
|
|
245
|
+
throw new utils.DynamicError('Wallet not connected');
|
|
246
|
+
}
|
|
247
|
+
const { adapter } = { adapter: this.adapter };
|
|
248
|
+
if (!adapter.connected) {
|
|
249
|
+
throw new utils.DynamicError('Wallet not connected');
|
|
250
|
+
}
|
|
251
|
+
try {
|
|
252
|
+
const signature = yield adapter.signMessage(messageToSign);
|
|
253
|
+
return signature;
|
|
254
|
+
}
|
|
255
|
+
catch (error) {
|
|
256
|
+
walletConnectorCore.logger.error(`[${this.name}] Sign message failed:`, error);
|
|
257
|
+
throw new utils.DynamicError(`Failed to sign message: ${error}`);
|
|
258
|
+
}
|
|
259
|
+
});
|
|
260
|
+
}
|
|
261
|
+
/**
|
|
262
|
+
* Get the TronWeb instance from the adapter
|
|
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
|
|
273
|
+
*/
|
|
274
|
+
getWalletTronWeb() {
|
|
275
|
+
var _a, _b, _c;
|
|
276
|
+
if (!this.adapter) {
|
|
277
|
+
return undefined;
|
|
278
|
+
}
|
|
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
|
|
282
|
+
if ((_a = adapter._wallet) === null || _a === void 0 ? void 0 : _a.tronWeb) {
|
|
283
|
+
return adapter._wallet.tronWeb;
|
|
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
|
+
}
|
|
289
|
+
return undefined;
|
|
290
|
+
}
|
|
291
|
+
/**
|
|
292
|
+
* Get balance of an address with caching to prevent rate limiting
|
|
293
|
+
*/
|
|
294
|
+
getBalance(address) {
|
|
295
|
+
return _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
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
|
|
306
|
+
const tronWeb = this.getWalletTronWeb();
|
|
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`);
|
|
309
|
+
return '0';
|
|
310
|
+
}
|
|
311
|
+
try {
|
|
312
|
+
const balance = yield tronWeb.trx.getBalance(address);
|
|
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;
|
|
320
|
+
}
|
|
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
|
+
}
|
|
327
|
+
walletConnectorCore.logger.error(`[${this.name}] Failed to get balance:`, error);
|
|
328
|
+
return '0';
|
|
329
|
+
}
|
|
330
|
+
});
|
|
331
|
+
}
|
|
332
|
+
/**
|
|
333
|
+
* Get enabled networks
|
|
334
|
+
*/
|
|
335
|
+
getEnabledNetworks() {
|
|
336
|
+
return this.tronNetworks;
|
|
337
|
+
}
|
|
338
|
+
/**
|
|
339
|
+
* Switch network (must be done in wallet UI)
|
|
340
|
+
*/
|
|
341
|
+
switchNetwork() {
|
|
342
|
+
return _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
343
|
+
throw new utils.DynamicError('Network switching must be done manually in the Tron wallet UI');
|
|
344
|
+
});
|
|
345
|
+
}
|
|
346
|
+
/**
|
|
347
|
+
* Get block explorer URLs for current network
|
|
348
|
+
*/
|
|
349
|
+
getBlockExplorerUrlsForCurrentNetwork() {
|
|
350
|
+
return _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
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
|
+
}
|
|
356
|
+
try {
|
|
357
|
+
const networkInfo = yield this.adapter.network();
|
|
358
|
+
const decimalChainId = convertChainIdToDecimal.convertChainIdToDecimal(networkInfo.chainId);
|
|
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 : []);
|
|
360
|
+
}
|
|
361
|
+
catch (error) {
|
|
362
|
+
// If adapter is not connected, return mainnet block explorer URLs
|
|
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 : []);
|
|
364
|
+
}
|
|
365
|
+
});
|
|
366
|
+
}
|
|
367
|
+
/**
|
|
368
|
+
* Disconnect and cleanup
|
|
369
|
+
*/
|
|
370
|
+
endSession() {
|
|
371
|
+
return _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
372
|
+
var _a;
|
|
373
|
+
if ((_a = this.adapter) === null || _a === void 0 ? void 0 : _a.connected) {
|
|
374
|
+
try {
|
|
375
|
+
yield this.adapter.disconnect();
|
|
376
|
+
}
|
|
377
|
+
catch (error) {
|
|
378
|
+
walletConnectorCore.logger.debug(`[${this.name}] Error during disconnect:`, error);
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
// Clear caches on disconnect
|
|
382
|
+
this.balanceCache.clear();
|
|
383
|
+
this.networkCache = null;
|
|
384
|
+
});
|
|
385
|
+
}
|
|
386
|
+
/**
|
|
387
|
+
* Create a UI transaction for the send balance flow
|
|
388
|
+
*
|
|
389
|
+
* @param from - The address sending the transaction
|
|
390
|
+
* @returns A TronUiTransaction instance
|
|
391
|
+
*/
|
|
392
|
+
createUiTransaction(from) {
|
|
393
|
+
return _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
394
|
+
yield this.validateActiveWallet(from);
|
|
395
|
+
// Get TronWeb from the adapter's internal wallet state
|
|
396
|
+
const tronWeb = this.getWalletTronWeb();
|
|
397
|
+
if (!tronWeb) {
|
|
398
|
+
throw new utils.DynamicError('TronWeb not available');
|
|
399
|
+
}
|
|
400
|
+
return new TronUiTransaction.TronUiTransaction({
|
|
401
|
+
from,
|
|
402
|
+
onSubmit: (transaction) => _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
403
|
+
var _a;
|
|
404
|
+
if (!transaction.to) {
|
|
405
|
+
throw new utils.DynamicError('Destination address is required');
|
|
406
|
+
}
|
|
407
|
+
// Create a minimal wallet instance for sending
|
|
408
|
+
const wallet = new TronWallet.TronWallet({
|
|
409
|
+
address: from,
|
|
410
|
+
chain: 'TRON',
|
|
411
|
+
connector: this,
|
|
412
|
+
id: 'temp-id',
|
|
413
|
+
isAuthenticated: false,
|
|
414
|
+
key: 'temp-key',
|
|
415
|
+
});
|
|
416
|
+
if (transaction.nonNativeAddress && transaction.nonNativeValue) {
|
|
417
|
+
// Handle TRC20 token transfers - not yet implemented
|
|
418
|
+
throw new utils.DynamicError('TRC20 token transfers not yet implemented');
|
|
419
|
+
}
|
|
420
|
+
else if (transaction.value) {
|
|
421
|
+
// Handle native TRX transfers
|
|
422
|
+
const trxAmount = Number(transaction.value) / 1000000; // Convert SUN to TRX
|
|
423
|
+
const result = yield wallet.sendTrx(transaction.to, trxAmount, {
|
|
424
|
+
from,
|
|
425
|
+
});
|
|
426
|
+
return result.txid || ((_a = result.transaction) === null || _a === void 0 ? void 0 : _a.txID);
|
|
427
|
+
}
|
|
428
|
+
throw new utils.DynamicError('Invalid transaction parameters');
|
|
429
|
+
}),
|
|
430
|
+
tronWeb,
|
|
431
|
+
});
|
|
432
|
+
});
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
exports.TronWalletAdapterConnector = TronWalletAdapterConnector;
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import type { Adapter, Network, TronWeb } from '@tronweb3/tronwallet-abstract-adapter';
|
|
2
|
+
import { Chain, ISendBalanceWalletConnector, WalletConnectorBase, WalletMetadata } from '@dynamic-labs/wallet-connector-core';
|
|
3
|
+
import type { WalletBookSchema } from '@dynamic-labs/wallet-book';
|
|
4
|
+
import type { GenericNetwork, IUITransaction, TransactionReceipt } from '@dynamic-labs/types';
|
|
5
|
+
import { TronWallet } from '../../wallet/TronWallet';
|
|
6
|
+
export type TronWalletAdapterConnectorOpts = {
|
|
7
|
+
walletBook: WalletBookSchema;
|
|
8
|
+
tronNetworks: GenericNetwork[];
|
|
9
|
+
metadata?: WalletMetadata;
|
|
10
|
+
overrideKey?: string;
|
|
11
|
+
walletData?: WalletBookSchema['wallets'][string];
|
|
12
|
+
};
|
|
13
|
+
/**
|
|
14
|
+
* TronWallet Adapter interface that includes the network() method
|
|
15
|
+
* All TronWallet adapters support this method
|
|
16
|
+
*/
|
|
17
|
+
interface TronAdapterWithNetwork extends Adapter {
|
|
18
|
+
network(): Promise<Network>;
|
|
19
|
+
}
|
|
20
|
+
export declare abstract class TronWalletAdapterConnector extends WalletConnectorBase<typeof TronWallet> implements ISendBalanceWalletConnector {
|
|
21
|
+
ChainWallet: typeof TronWallet;
|
|
22
|
+
name: string;
|
|
23
|
+
overrideKey: string;
|
|
24
|
+
connectedChain: Chain;
|
|
25
|
+
supportedChains: Chain[];
|
|
26
|
+
switchNetworkOnlyFromWallet: boolean;
|
|
27
|
+
protected tronNetworks: GenericNetwork[];
|
|
28
|
+
protected adapter?: TronAdapterWithNetwork;
|
|
29
|
+
private balanceCache;
|
|
30
|
+
private networkCache;
|
|
31
|
+
/**
|
|
32
|
+
* Create the TronWallet Adapter instance for this connector
|
|
33
|
+
* Uses the adapter registry to create the appropriate adapter
|
|
34
|
+
*/
|
|
35
|
+
protected createAdapter(): TronAdapterWithNetwork;
|
|
36
|
+
constructor(opts: TronWalletAdapterConnectorOpts);
|
|
37
|
+
confirmTransactionStatus?(): Promise<TransactionReceipt>;
|
|
38
|
+
/**
|
|
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.
|
|
42
|
+
*/
|
|
43
|
+
getAdapter(): TronAdapterWithNetwork;
|
|
44
|
+
/**
|
|
45
|
+
* Check if the specific wallet is installed on the browser
|
|
46
|
+
* Each wallet adapter checks for its own specific browser injection
|
|
47
|
+
*/
|
|
48
|
+
isInstalledOnBrowser(): boolean;
|
|
49
|
+
/**
|
|
50
|
+
* Connect to the wallet
|
|
51
|
+
*/
|
|
52
|
+
connect(): Promise<void>;
|
|
53
|
+
/**
|
|
54
|
+
* Get the current address
|
|
55
|
+
*/
|
|
56
|
+
getAddress(): Promise<string | undefined>;
|
|
57
|
+
/**
|
|
58
|
+
* Get connected accounts
|
|
59
|
+
*/
|
|
60
|
+
getConnectedAccounts(): Promise<string[]>;
|
|
61
|
+
/**
|
|
62
|
+
* Get the current network with caching to prevent rate limiting
|
|
63
|
+
*/
|
|
64
|
+
getNetwork(): Promise<string | undefined>;
|
|
65
|
+
/**
|
|
66
|
+
* Check if current network is testnet
|
|
67
|
+
*/
|
|
68
|
+
isTestnet(): Promise<boolean>;
|
|
69
|
+
/**
|
|
70
|
+
* Sign a message using the adapter
|
|
71
|
+
*/
|
|
72
|
+
signMessage(messageToSign: string): Promise<string>;
|
|
73
|
+
/**
|
|
74
|
+
* Get the TronWeb instance from the adapter
|
|
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
|
|
85
|
+
*/
|
|
86
|
+
getWalletTronWeb(): TronWeb | undefined;
|
|
87
|
+
/**
|
|
88
|
+
* Get balance of an address with caching to prevent rate limiting
|
|
89
|
+
*/
|
|
90
|
+
getBalance(address: string): Promise<string | undefined>;
|
|
91
|
+
/**
|
|
92
|
+
* Get enabled networks
|
|
93
|
+
*/
|
|
94
|
+
getEnabledNetworks(): GenericNetwork[];
|
|
95
|
+
/**
|
|
96
|
+
* Switch network (must be done in wallet UI)
|
|
97
|
+
*/
|
|
98
|
+
switchNetwork(): Promise<void>;
|
|
99
|
+
/**
|
|
100
|
+
* Get block explorer URLs for current network
|
|
101
|
+
*/
|
|
102
|
+
getBlockExplorerUrlsForCurrentNetwork(): Promise<string[]>;
|
|
103
|
+
/**
|
|
104
|
+
* Disconnect and cleanup
|
|
105
|
+
*/
|
|
106
|
+
endSession(): Promise<void>;
|
|
107
|
+
/**
|
|
108
|
+
* Create a UI transaction for the send balance flow
|
|
109
|
+
*
|
|
110
|
+
* @param from - The address sending the transaction
|
|
111
|
+
* @returns A TronUiTransaction instance
|
|
112
|
+
*/
|
|
113
|
+
createUiTransaction(from: string): Promise<IUITransaction>;
|
|
114
|
+
}
|
|
115
|
+
export {};
|