@dynamic-labs/tron 4.39.0 → 4.40.1

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.
Files changed (63) hide show
  1. package/CHANGELOG.md +19 -0
  2. package/README.md +264 -20
  3. package/package.cjs +1 -1
  4. package/package.js +1 -1
  5. package/package.json +8 -6
  6. package/src/connectors/BitgetTronConnector/BitgetTronConnector.cjs +5 -5
  7. package/src/connectors/BitgetTronConnector/BitgetTronConnector.d.ts +4 -4
  8. package/src/connectors/BitgetTronConnector/BitgetTronConnector.js +5 -5
  9. package/src/connectors/OKXTronConnector/OKXTronConnector.cjs +5 -5
  10. package/src/connectors/OKXTronConnector/OKXTronConnector.d.ts +4 -4
  11. package/src/connectors/OKXTronConnector/OKXTronConnector.js +5 -5
  12. package/src/connectors/TokenPocketTronConnector/TokenPocketTronConnector.cjs +5 -13
  13. package/src/connectors/TokenPocketTronConnector/TokenPocketTronConnector.d.ts +4 -4
  14. package/src/connectors/TokenPocketTronConnector/TokenPocketTronConnector.js +5 -13
  15. package/src/connectors/TronWalletAdapterConnector/TronWalletAdapterConnector.cjs +304 -0
  16. package/src/connectors/TronWalletAdapterConnector/TronWalletAdapterConnector.d.ts +107 -0
  17. package/src/connectors/TronWalletAdapterConnector/TronWalletAdapterConnector.js +300 -0
  18. package/src/connectors/TronWalletAdapterConnector/index.d.ts +1 -0
  19. package/src/connectors/TrustTronConnector/TrustTronConnector.cjs +5 -16
  20. package/src/connectors/TrustTronConnector/TrustTronConnector.d.ts +4 -4
  21. package/src/connectors/TrustTronConnector/TrustTronConnector.js +5 -16
  22. package/src/index.cjs +2 -3
  23. package/src/index.d.ts +5 -4
  24. package/src/index.js +1 -2
  25. package/src/types.d.ts +11 -181
  26. package/src/utils/TronUiTransaction/TronUiTransaction.cjs +67 -26
  27. package/src/utils/TronUiTransaction/TronUiTransaction.d.ts +38 -5
  28. package/src/utils/TronUiTransaction/TronUiTransaction.js +67 -26
  29. package/src/utils/convertChainIdToDecimal/convertChainIdToDecimal.cjs +16 -0
  30. package/src/utils/convertChainIdToDecimal/convertChainIdToDecimal.d.ts +9 -0
  31. package/src/utils/convertChainIdToDecimal/convertChainIdToDecimal.js +12 -0
  32. package/src/utils/convertChainIdToDecimal/index.d.ts +1 -0
  33. package/src/utils/getTronGasEstimation/getTronGasEstimation.cjs +162 -0
  34. package/src/utils/getTronGasEstimation/getTronGasEstimation.d.ts +49 -0
  35. package/src/utils/getTronGasEstimation/getTronGasEstimation.js +158 -0
  36. package/src/utils/getTronGasEstimation/index.d.ts +1 -0
  37. package/src/wallet/TronWallet/TronWallet.cjs +124 -0
  38. package/src/wallet/TronWallet/TronWallet.d.ts +51 -0
  39. package/src/wallet/TronWallet/TronWallet.js +120 -0
  40. package/src/wallet/TronWallet/index.d.ts +1 -0
  41. package/src/connectors/BybitTronConnector/BybitTronConnector.d.ts +0 -7
  42. package/src/connectors/BybitTronConnector/index.d.ts +0 -1
  43. package/src/connectors/TronWalletConnector/TronWalletConnector.cjs +0 -255
  44. package/src/connectors/TronWalletConnector/TronWalletConnector.d.ts +0 -42
  45. package/src/connectors/TronWalletConnector/TronWalletConnector.js +0 -251
  46. package/src/connectors/TronWalletConnector/index.d.ts +0 -1
  47. package/src/utils/detectTronNetworkFromProvider.cjs +0 -67
  48. package/src/utils/detectTronNetworkFromProvider.d.ts +0 -30
  49. package/src/utils/detectTronNetworkFromProvider.js +0 -62
  50. package/src/utils/getDefaultTronNetworks.cjs +0 -60
  51. package/src/utils/getDefaultTronNetworks.d.ts +0 -9
  52. package/src/utils/getDefaultTronNetworks.js +0 -53
  53. package/src/utils/getTronGasEstimation.cjs +0 -136
  54. package/src/utils/getTronGasEstimation.d.ts +0 -47
  55. package/src/utils/getTronGasEstimation.js +0 -129
  56. package/src/utils/index.d.ts +0 -4
  57. package/src/utils/provider.cjs +0 -59
  58. package/src/utils/provider.d.ts +0 -24
  59. package/src/utils/provider.js +0 -53
  60. package/src/wallet/TronWallet.cjs +0 -106
  61. package/src/wallet/TronWallet.d.ts +0 -22
  62. package/src/wallet/TronWallet.js +0 -102
  63. package/src/wallet/index.d.ts +0 -2
@@ -0,0 +1,304 @@
1
+ 'use client'
2
+ 'use strict';
3
+
4
+ Object.defineProperty(exports, '__esModule', { value: true });
5
+
6
+ var _tslib = require('../../../_virtual/_tslib.cjs');
7
+ var walletConnectorCore = require('@dynamic-labs/wallet-connector-core');
8
+ var utils = require('@dynamic-labs/utils');
9
+ var TronWallet = require('../../wallet/TronWallet/TronWallet.cjs');
10
+ var TronUiTransaction = require('../../utils/TronUiTransaction/TronUiTransaction.cjs');
11
+ var convertChainIdToDecimal = require('../../utils/convertChainIdToDecimal/convertChainIdToDecimal.cjs');
12
+
13
+ class TronWalletAdapterConnector extends walletConnectorCore.WalletConnectorBase {
14
+ constructor(opts) {
15
+ super(opts);
16
+ this.ChainWallet = TronWallet.TronWallet;
17
+ this.name = 'Tron';
18
+ this.overrideKey = 'tron';
19
+ this.connectedChain = 'TRON';
20
+ this.supportedChains = ['TRON'];
21
+ this.switchNetworkOnlyFromWallet = true;
22
+ this.tronNetworks = opts.tronNetworks;
23
+ // Initialize adapter immediately so it can start detecting the wallet
24
+ this.adapter = this.createAdapter();
25
+ }
26
+ confirmTransactionStatus() {
27
+ throw new Error('Method not implemented.');
28
+ }
29
+ /**
30
+ * Get the TronWallet Adapter instance
31
+ */
32
+ getAdapter() {
33
+ return this.adapter;
34
+ }
35
+ /**
36
+ * Check if the wallet is installed
37
+ */
38
+ isInstalledOnBrowser() {
39
+ const adapter = this.getAdapter();
40
+ return adapter.readyState === 'Found';
41
+ }
42
+ /**
43
+ * Connect to the wallet
44
+ */
45
+ connect() {
46
+ return _tslib.__awaiter(this, void 0, void 0, function* () {
47
+ const adapter = this.getAdapter();
48
+ if (adapter.connected) {
49
+ return;
50
+ }
51
+ if (adapter.readyState !== 'Found') {
52
+ throw new utils.DynamicError(`${this.name} wallet not found. Please install the extension.`);
53
+ }
54
+ try {
55
+ yield adapter.connect();
56
+ }
57
+ catch (error) {
58
+ throw new utils.DynamicError(`Failed to connect to ${this.name}: ${error}`);
59
+ }
60
+ });
61
+ }
62
+ /**
63
+ * Get the current address
64
+ */
65
+ getAddress() {
66
+ return _tslib.__awaiter(this, void 0, void 0, function* () {
67
+ const adapter = this.getAdapter();
68
+ // If not connected, trigger the connection flow
69
+ if (!adapter.connected) {
70
+ yield this.connect();
71
+ }
72
+ return adapter.address || undefined;
73
+ });
74
+ }
75
+ /**
76
+ * Get connected accounts
77
+ */
78
+ getConnectedAccounts() {
79
+ return _tslib.__awaiter(this, void 0, void 0, function* () {
80
+ const address = yield this.getAddress();
81
+ return address ? [address] : [];
82
+ });
83
+ }
84
+ /**
85
+ * Get the current network
86
+ */
87
+ getNetwork() {
88
+ return _tslib.__awaiter(this, void 0, void 0, function* () {
89
+ try {
90
+ const networkInfo = yield this.adapter.network();
91
+ return convertChainIdToDecimal.convertChainIdToDecimal(networkInfo.chainId);
92
+ }
93
+ catch (error) {
94
+ // If adapter is not connected, fall back to mainnet
95
+ return '728126428'; // Mainnet in decimal format
96
+ }
97
+ });
98
+ }
99
+ /**
100
+ * Check if current network is testnet
101
+ */
102
+ isTestnet() {
103
+ return _tslib.__awaiter(this, void 0, void 0, function* () {
104
+ try {
105
+ const networkInfo = yield this.adapter.network();
106
+ // Testnet networkTypes: Shasta, Nile
107
+ return (networkInfo.networkType === 'Shasta' ||
108
+ networkInfo.networkType === 'Nile');
109
+ }
110
+ catch (error) {
111
+ // If adapter is not connected, assume mainnet (not testnet)
112
+ return false;
113
+ }
114
+ });
115
+ }
116
+ /**
117
+ * Sign a message using the adapter
118
+ */
119
+ signMessage(messageToSign) {
120
+ return _tslib.__awaiter(this, void 0, void 0, function* () {
121
+ const adapter = this.getAdapter();
122
+ if (!adapter.connected) {
123
+ throw new utils.DynamicError('Wallet not connected');
124
+ }
125
+ try {
126
+ const signature = yield adapter.signMessage(messageToSign);
127
+ return signature;
128
+ }
129
+ catch (error) {
130
+ walletConnectorCore.logger.error(`[${this.name}] Sign message failed:`, error);
131
+ throw new utils.DynamicError(`Failed to sign message: ${error}`);
132
+ }
133
+ });
134
+ }
135
+ /**
136
+ * Get the TronWeb instance from the adapter
137
+ * The adapters store TronWeb in their internal _wallet property
138
+ */
139
+ getWalletTronWeb() {
140
+ var _a;
141
+ const adapter = this.adapter;
142
+ // Try to access adapter's internal tronWeb directly
143
+ if (adapter.tronWeb) {
144
+ return adapter.tronWeb;
145
+ }
146
+ // Most adapters store it in _wallet.tronWeb
147
+ if ((_a = adapter._wallet) === null || _a === void 0 ? void 0 : _a.tronWeb) {
148
+ return adapter._wallet.tronWeb;
149
+ }
150
+ return undefined;
151
+ }
152
+ /**
153
+ * Get balance of an address
154
+ */
155
+ getBalance(address) {
156
+ return _tslib.__awaiter(this, void 0, void 0, function* () {
157
+ var _a;
158
+ const tronWeb = this.getWalletTronWeb();
159
+ if (!((_a = tronWeb === null || tronWeb === void 0 ? void 0 : tronWeb.trx) === null || _a === void 0 ? void 0 : _a.getBalance)) {
160
+ return '0';
161
+ }
162
+ try {
163
+ const balance = yield tronWeb.trx.getBalance(address);
164
+ return (balance / 1000000).toFixed(6); // SUN → TRX
165
+ }
166
+ catch (error) {
167
+ walletConnectorCore.logger.error(`[${this.name}] Failed to get balance:`, error);
168
+ return '0';
169
+ }
170
+ });
171
+ }
172
+ /**
173
+ * Get enabled networks
174
+ */
175
+ getEnabledNetworks() {
176
+ return this.tronNetworks;
177
+ }
178
+ /**
179
+ * Switch network (must be done in wallet UI)
180
+ */
181
+ switchNetwork() {
182
+ return _tslib.__awaiter(this, void 0, void 0, function* () {
183
+ throw new utils.DynamicError('Network switching must be done manually in the Tron wallet UI');
184
+ });
185
+ }
186
+ /**
187
+ * Get block explorer URLs for current network
188
+ */
189
+ getBlockExplorerUrlsForCurrentNetwork() {
190
+ return _tslib.__awaiter(this, void 0, void 0, function* () {
191
+ var _a, _b, _c, _d;
192
+ try {
193
+ const networkInfo = yield this.adapter.network();
194
+ const decimalChainId = convertChainIdToDecimal.convertChainIdToDecimal(networkInfo.chainId);
195
+ return ((_b = (_a = this.tronNetworks.find((n) => String(n.chainId) === decimalChainId)) === null || _a === void 0 ? void 0 : _a.blockExplorerUrls) !== null && _b !== void 0 ? _b : []);
196
+ }
197
+ catch (error) {
198
+ // If adapter is not connected, return mainnet block explorer URLs
199
+ return ((_d = (_c = this.tronNetworks.find((n) => String(n.chainId) === '728126428')) === null || _c === void 0 ? void 0 : _c.blockExplorerUrls) !== null && _d !== void 0 ? _d : []);
200
+ }
201
+ });
202
+ }
203
+ /**
204
+ * Disconnect and cleanup
205
+ */
206
+ endSession() {
207
+ return _tslib.__awaiter(this, void 0, void 0, function* () {
208
+ const adapter = this.getAdapter();
209
+ if (adapter.connected) {
210
+ try {
211
+ yield adapter.disconnect();
212
+ }
213
+ catch (error) {
214
+ walletConnectorCore.logger.debug(`[${this.name}] Error during disconnect:`, error);
215
+ }
216
+ }
217
+ });
218
+ }
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
+ /**
255
+ * Create a UI transaction for the send balance flow
256
+ *
257
+ * @param from - The address sending the transaction
258
+ * @returns A TronUiTransaction instance
259
+ */
260
+ createUiTransaction(from) {
261
+ return _tslib.__awaiter(this, void 0, void 0, function* () {
262
+ yield this.validateActiveWallet(from);
263
+ // Get TronWeb from the wallet-specific window location
264
+ const tronWeb = this.getWalletTronWeb();
265
+ if (!tronWeb) {
266
+ throw new utils.DynamicError('TronWeb not available');
267
+ }
268
+ return new TronUiTransaction.TronUiTransaction({
269
+ from,
270
+ onSubmit: (transaction) => _tslib.__awaiter(this, void 0, void 0, function* () {
271
+ var _a;
272
+ if (!transaction.to) {
273
+ throw new utils.DynamicError('Destination address is required');
274
+ }
275
+ // Create a minimal wallet instance for sending
276
+ const wallet = new TronWallet.TronWallet({
277
+ address: from,
278
+ chain: 'TRON',
279
+ connector: this,
280
+ id: 'temp-id',
281
+ isAuthenticated: false,
282
+ key: 'temp-key',
283
+ });
284
+ if (transaction.nonNativeAddress && transaction.nonNativeValue) {
285
+ // Handle TRC20 token transfers - not yet implemented
286
+ throw new utils.DynamicError('TRC20 token transfers not yet implemented');
287
+ }
288
+ else if (transaction.value) {
289
+ // Handle native TRX transfers
290
+ const trxAmount = Number(transaction.value) / 1000000; // Convert SUN to TRX
291
+ const result = yield wallet.sendTrx(transaction.to, trxAmount, {
292
+ from,
293
+ });
294
+ return result.txid || ((_a = result.transaction) === null || _a === void 0 ? void 0 : _a.txID);
295
+ }
296
+ throw new utils.DynamicError('Invalid transaction parameters');
297
+ }),
298
+ tronWeb,
299
+ });
300
+ });
301
+ }
302
+ }
303
+
304
+ exports.TronWalletAdapterConnector = TronWalletAdapterConnector;
@@ -0,0 +1,107 @@
1
+ import type { Adapter, Network, TronWeb } from '@tronweb3/tronwallet-abstract-adapter';
2
+ import type { WalletBookSchema } from '@dynamic-labs/wallet-book';
3
+ import { Chain, ISendBalanceWalletConnector, WalletConnectorBase, WalletMetadata } from '@dynamic-labs/wallet-connector-core';
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
+ };
11
+ /**
12
+ * TronWallet Adapter interface that includes the network() method
13
+ * All TronWallet adapters support this method
14
+ */
15
+ interface TronAdapterWithNetwork extends Adapter {
16
+ network(): Promise<Network>;
17
+ }
18
+ export declare abstract class TronWalletAdapterConnector extends WalletConnectorBase<typeof TronWallet> implements ISendBalanceWalletConnector {
19
+ ChainWallet: typeof TronWallet;
20
+ name: string;
21
+ overrideKey: string;
22
+ connectedChain: Chain;
23
+ supportedChains: Chain[];
24
+ switchNetworkOnlyFromWallet: boolean;
25
+ protected tronNetworks: GenericNetwork[];
26
+ protected adapter: TronAdapterWithNetwork;
27
+ /**
28
+ * Create the TronWallet Adapter instance for this connector
29
+ * Each connector must implement this to return the appropriate adapter
30
+ */
31
+ protected abstract createAdapter(): TronAdapterWithNetwork;
32
+ constructor(opts: TronWalletAdapterConnectorOpts);
33
+ confirmTransactionStatus?(): Promise<TransactionReceipt>;
34
+ /**
35
+ * Get the TronWallet Adapter instance
36
+ */
37
+ getAdapter(): Adapter;
38
+ /**
39
+ * Check if the wallet is installed
40
+ */
41
+ isInstalledOnBrowser(): boolean;
42
+ /**
43
+ * Connect to the wallet
44
+ */
45
+ connect(): Promise<void>;
46
+ /**
47
+ * Get the current address
48
+ */
49
+ getAddress(): Promise<string | undefined>;
50
+ /**
51
+ * Get connected accounts
52
+ */
53
+ getConnectedAccounts(): Promise<string[]>;
54
+ /**
55
+ * Get the current network
56
+ */
57
+ getNetwork(): Promise<string | undefined>;
58
+ /**
59
+ * Check if current network is testnet
60
+ */
61
+ isTestnet(): Promise<boolean>;
62
+ /**
63
+ * Sign a message using the adapter
64
+ */
65
+ signMessage(messageToSign: string): Promise<string>;
66
+ /**
67
+ * Get the TronWeb instance from the adapter
68
+ * The adapters store TronWeb in their internal _wallet property
69
+ */
70
+ getWalletTronWeb(): TronWeb | undefined;
71
+ /**
72
+ * Get balance of an address
73
+ */
74
+ getBalance(address: string): Promise<string | undefined>;
75
+ /**
76
+ * Get enabled networks
77
+ */
78
+ getEnabledNetworks(): GenericNetwork[];
79
+ /**
80
+ * Switch network (must be done in wallet UI)
81
+ */
82
+ switchNetwork(): Promise<void>;
83
+ /**
84
+ * Get block explorer URLs for current network
85
+ */
86
+ getBlockExplorerUrlsForCurrentNetwork(): Promise<string[]>;
87
+ /**
88
+ * Disconnect and cleanup
89
+ */
90
+ 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
+ /**
100
+ * Create a UI transaction for the send balance flow
101
+ *
102
+ * @param from - The address sending the transaction
103
+ * @returns A TronUiTransaction instance
104
+ */
105
+ createUiTransaction(from: string): Promise<IUITransaction>;
106
+ }
107
+ export {};