@dynamic-labs/tron 4.34.0 → 4.36.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.
Files changed (50) hide show
  1. package/CHANGELOG.md +26 -0
  2. package/_virtual/_tslib.cjs +36 -0
  3. package/_virtual/_tslib.js +32 -0
  4. package/package.cjs +1 -1
  5. package/package.js +1 -1
  6. package/package.json +6 -3
  7. package/src/connectors/BitgetTronConnector/BitgetTronConnector.cjs +20 -0
  8. package/src/connectors/BitgetTronConnector/BitgetTronConnector.d.ts +7 -0
  9. package/src/connectors/BitgetTronConnector/BitgetTronConnector.js +16 -0
  10. package/src/connectors/BitgetTronConnector/index.d.ts +1 -0
  11. package/src/connectors/BybitTronConnector/BybitTronConnector.cjs +20 -0
  12. package/src/connectors/BybitTronConnector/BybitTronConnector.d.ts +7 -0
  13. package/src/connectors/BybitTronConnector/BybitTronConnector.js +16 -0
  14. package/src/connectors/BybitTronConnector/index.d.ts +1 -0
  15. package/src/connectors/OKXTronConnector/OKXTronConnector.cjs +20 -0
  16. package/src/connectors/OKXTronConnector/OKXTronConnector.d.ts +7 -0
  17. package/src/connectors/OKXTronConnector/OKXTronConnector.js +16 -0
  18. package/src/connectors/OKXTronConnector/index.d.ts +1 -0
  19. package/src/connectors/TronWalletConnector/TronWalletConnector.cjs +255 -0
  20. package/src/connectors/TronWalletConnector/TronWalletConnector.d.ts +42 -0
  21. package/src/connectors/TronWalletConnector/TronWalletConnector.js +251 -0
  22. package/src/connectors/TronWalletConnector/index.d.ts +1 -0
  23. package/src/connectors/TrustTronConnector/TrustTronConnector.cjs +20 -0
  24. package/src/connectors/TrustTronConnector/TrustTronConnector.d.ts +7 -0
  25. package/src/connectors/TrustTronConnector/TrustTronConnector.js +16 -0
  26. package/src/connectors/TrustTronConnector/index.d.ts +1 -0
  27. package/src/index.cjs +18 -0
  28. package/src/index.d.ts +6 -1
  29. package/src/index.js +14 -0
  30. package/src/types.d.ts +13 -2
  31. package/src/utils/TronUiTransaction/TronUiTransaction.cjs +121 -0
  32. package/src/utils/TronUiTransaction/TronUiTransaction.d.ts +39 -0
  33. package/src/utils/TronUiTransaction/TronUiTransaction.js +116 -0
  34. package/src/utils/TronUiTransaction/index.d.ts +1 -0
  35. package/src/utils/getDefaultTronNetworks.cjs +60 -0
  36. package/src/utils/getDefaultTronNetworks.js +53 -0
  37. package/src/utils/getTronGasEstimation.cjs +136 -0
  38. package/src/utils/getTronGasEstimation.d.ts +47 -0
  39. package/src/utils/getTronGasEstimation.js +129 -0
  40. package/src/utils/index.d.ts +1 -0
  41. package/src/utils/provider.cjs +59 -0
  42. package/src/utils/provider.js +53 -0
  43. package/src/wallet/TronWallet.cjs +106 -0
  44. package/src/wallet/TronWallet.d.ts +22 -0
  45. package/src/wallet/TronWallet.js +102 -0
  46. package/src/wallet/index.d.ts +2 -0
  47. package/src/wallet/isTronWallet/index.d.ts +1 -0
  48. package/src/wallet/isTronWallet/isTronWallet.cjs +8 -0
  49. package/src/wallet/isTronWallet/isTronWallet.d.ts +3 -0
  50. package/src/wallet/isTronWallet/isTronWallet.js +4 -0
@@ -0,0 +1,251 @@
1
+ 'use client'
2
+ import { __awaiter } from '../../../_virtual/_tslib.js';
3
+ import { WalletConnectorBase, logger } from '@dynamic-labs/wallet-connector-core';
4
+ import { DynamicError } from '@dynamic-labs/utils';
5
+ import { TronWallet } from '../../wallet/TronWallet.js';
6
+ import { assertProvider, getCurrentAddress, isProviderReady } from '../../utils/provider.js';
7
+ import { getDefaultTronNetworks } from '../../utils/getDefaultTronNetworks.js';
8
+ import { TronUiTransaction } from '../../utils/TronUiTransaction/TronUiTransaction.js';
9
+
10
+ class TronWalletConnector extends WalletConnectorBase {
11
+ constructor(opts) {
12
+ super(opts);
13
+ this.ChainWallet = TronWallet;
14
+ this.name = 'Tron';
15
+ this.overrideKey = 'injectedtron';
16
+ this.connectedChain = 'TRON';
17
+ this.supportedChains = ['TRON'];
18
+ this.switchNetworkOnlyFromWallet = true;
19
+ this.tronNetworks = opts.tronNetworks || getDefaultTronNetworks();
20
+ this.setupNetworkChangeListener();
21
+ }
22
+ setupNetworkChangeListener() {
23
+ const provider = this.getProvider();
24
+ if (!provider)
25
+ return;
26
+ if (provider.onNetworkChange &&
27
+ typeof provider.onNetworkChange === 'function') {
28
+ provider.onNetworkChange((network) => {
29
+ logger.debug(`[${this.name}] Network change via provider:`, network);
30
+ this.emit('chainChange', {
31
+ chain: network.chainId || network.name || 'unknown',
32
+ });
33
+ });
34
+ }
35
+ }
36
+ connect() {
37
+ return __awaiter(this, void 0, void 0, function* () {
38
+ const provider = assertProvider(this.getProvider(), 'Tron');
39
+ try {
40
+ if (provider.request) {
41
+ yield provider.request({ method: 'tron_requestAccounts' });
42
+ }
43
+ else if (provider.connect) {
44
+ yield provider.connect();
45
+ }
46
+ logger.debug(`[${this.name}] Connected successfully`);
47
+ }
48
+ catch (error) {
49
+ logger.error(`[${this.name}] Connection failed:`, error);
50
+ throw new DynamicError('Failed to connect to Tron wallet');
51
+ }
52
+ });
53
+ }
54
+ getAddress() {
55
+ return __awaiter(this, void 0, void 0, function* () {
56
+ const provider = this.getProvider();
57
+ if (!provider)
58
+ return undefined;
59
+ const address = getCurrentAddress(provider);
60
+ if (address)
61
+ return address;
62
+ if (!isProviderReady(provider)) {
63
+ yield this.connect();
64
+ return getCurrentAddress(provider);
65
+ }
66
+ return undefined;
67
+ });
68
+ }
69
+ getNetwork() {
70
+ return __awaiter(this, void 0, void 0, function* () {
71
+ var _a, _b;
72
+ const provider = this.getProvider();
73
+ if (!provider)
74
+ return undefined;
75
+ if (provider.chainId)
76
+ return String(provider.chainId);
77
+ if (provider.getNetwork) {
78
+ try {
79
+ const networkInfo = yield provider.getNetwork();
80
+ return (networkInfo === null || networkInfo === void 0 ? void 0 : networkInfo.chainId) ? String(networkInfo.chainId) : undefined;
81
+ }
82
+ catch (_c) {
83
+ return undefined;
84
+ }
85
+ }
86
+ if ((_b = (_a = provider.tronWeb) === null || _a === void 0 ? void 0 : _a.fullNode) === null || _b === void 0 ? void 0 : _b.host) {
87
+ const { host } = provider.tronWeb.fullNode;
88
+ const match = this.findNetworkByRpcUrl(host) || this.findNetworkByHostPattern(host);
89
+ return (match === null || match === void 0 ? void 0 : match.chainId) ? String(match.chainId) : undefined;
90
+ }
91
+ return undefined;
92
+ });
93
+ }
94
+ findNetworkByRpcUrl(host) {
95
+ return this.tronNetworks.find((network) => network.rpcUrls.some((rpcUrl) => {
96
+ try {
97
+ const rpcHostname = new URL(rpcUrl).hostname;
98
+ const providerHostname = new URL(host).hostname;
99
+ return (providerHostname.includes(rpcHostname) ||
100
+ rpcHostname.includes(providerHostname));
101
+ }
102
+ catch (_a) {
103
+ return host.includes(rpcUrl) || rpcUrl.includes(host);
104
+ }
105
+ }));
106
+ }
107
+ findNetworkByHostPattern(host) {
108
+ const hostLower = host.toLowerCase();
109
+ return this.tronNetworks.find((network) => {
110
+ const networkNameLower = network.name.toLowerCase();
111
+ if (hostLower.includes('nile') && networkNameLower.includes('nile'))
112
+ return true;
113
+ if (hostLower.includes('shasta') && networkNameLower.includes('shasta'))
114
+ return true;
115
+ if (hostLower.includes('mainnet') && networkNameLower.includes('mainnet'))
116
+ return true;
117
+ if (hostLower.includes('trongrid') &&
118
+ !hostLower.includes('nile') &&
119
+ !hostLower.includes('shasta') &&
120
+ networkNameLower.includes('mainnet'))
121
+ return true;
122
+ return false;
123
+ });
124
+ }
125
+ getConnectedAccounts() {
126
+ return __awaiter(this, void 0, void 0, function* () {
127
+ const address = yield this.getAddress();
128
+ return address ? [address] : [];
129
+ });
130
+ }
131
+ isTestnet() {
132
+ return __awaiter(this, void 0, void 0, function* () {
133
+ var _a;
134
+ const networkId = yield this.getNetwork();
135
+ const tronNetwork = this.tronNetworks.find((n) => n.chainId === networkId);
136
+ return (_a = tronNetwork === null || tronNetwork === void 0 ? void 0 : tronNetwork.isTestnet) !== null && _a !== void 0 ? _a : false;
137
+ });
138
+ }
139
+ /**
140
+ * 🔑 Enforce signMessageV2 only
141
+ */
142
+ signMessage(messageToSign) {
143
+ return __awaiter(this, void 0, void 0, function* () {
144
+ var _a, _b;
145
+ const provider = assertProvider(this.getProvider(), 'Tron');
146
+ if (!isProviderReady(provider)) {
147
+ yield this.connect();
148
+ }
149
+ if ((_b = (_a = provider.tronWeb) === null || _a === void 0 ? void 0 : _a.trx) === null || _b === void 0 ? void 0 : _b.signMessageV2) {
150
+ const sig = yield provider.tronWeb.trx.signMessageV2(messageToSign);
151
+ if (typeof sig !== 'string') {
152
+ throw new DynamicError('Invalid signature format from wallet');
153
+ }
154
+ return sig;
155
+ }
156
+ throw new DynamicError('Wallet does not support signMessageV2, which is required');
157
+ });
158
+ }
159
+ getBalance(address) {
160
+ return __awaiter(this, void 0, void 0, function* () {
161
+ var _a, _b;
162
+ const provider = this.getProvider();
163
+ if (!((_b = (_a = provider === null || provider === void 0 ? void 0 : provider.tronWeb) === null || _a === void 0 ? void 0 : _a.trx) === null || _b === void 0 ? void 0 : _b.getBalance))
164
+ return '0';
165
+ try {
166
+ const balance = yield provider.tronWeb.trx.getBalance(address);
167
+ return (balance / 1000000).toFixed(6); // SUN → TRX
168
+ }
169
+ catch (error) {
170
+ logger.error(`[${this.name}] Failed to get balance:`, error);
171
+ return '0';
172
+ }
173
+ });
174
+ }
175
+ isInstalledOnBrowser() {
176
+ const provider = this.getProvider();
177
+ return Boolean(provider);
178
+ }
179
+ getEnabledNetworks() {
180
+ return this.tronNetworks;
181
+ }
182
+ switchNetwork() {
183
+ return __awaiter(this, void 0, void 0, function* () {
184
+ throw new DynamicError('Network switching must be done manually in the Tron wallet UI');
185
+ });
186
+ }
187
+ getBlockExplorerUrlsForCurrentNetwork() {
188
+ return __awaiter(this, void 0, void 0, function* () {
189
+ var _a, _b;
190
+ const currentNetwork = yield this.getNetwork();
191
+ return ((_b = (_a = this.tronNetworks.find((n) => n.chainId.toString() === (currentNetwork === null || currentNetwork === void 0 ? void 0 : currentNetwork.toString()))) === null || _a === void 0 ? void 0 : _a.blockExplorerUrls) !== null && _b !== void 0 ? _b : []);
192
+ });
193
+ }
194
+ endSession() {
195
+ return __awaiter(this, void 0, void 0, function* () {
196
+ const provider = this.getProvider();
197
+ if (provider === null || provider === void 0 ? void 0 : provider.disconnect) {
198
+ try {
199
+ yield provider.disconnect();
200
+ }
201
+ catch (error) {
202
+ logger.debug(`[${this.name}] Error during disconnect:`, error);
203
+ }
204
+ }
205
+ });
206
+ }
207
+ /** Function used to create transactions in the SDK interface */
208
+ createUiTransaction(from) {
209
+ return __awaiter(this, void 0, void 0, function* () {
210
+ yield this.validateActiveWallet(from);
211
+ const provider = assertProvider(this.getProvider(), 'Tron');
212
+ if (!provider.tronWeb) {
213
+ throw new DynamicError('TronWeb not available');
214
+ }
215
+ return new TronUiTransaction({
216
+ from,
217
+ onSubmit: (transaction) => __awaiter(this, void 0, void 0, function* () {
218
+ var _a;
219
+ if (!transaction.to) {
220
+ throw new DynamicError('Destination address is required');
221
+ }
222
+ // Create a minimal wallet instance for sending
223
+ const wallet = new TronWallet({
224
+ address: from,
225
+ chain: 'TRON',
226
+ connector: this,
227
+ id: 'temp-id',
228
+ isAuthenticated: false,
229
+ key: 'temp-key',
230
+ });
231
+ if (transaction.nonNativeAddress && transaction.nonNativeValue) {
232
+ // Handle TRC20 token transfers - simplified for now
233
+ throw new DynamicError('TRC20 token transfers not yet implemented');
234
+ }
235
+ else if (transaction.value) {
236
+ // Handle native TRX transfers
237
+ const trxAmount = Number(transaction.value) / 1000000; // Convert SUN to TRX
238
+ const result = yield wallet.sendTrx(transaction.to, trxAmount, {
239
+ from,
240
+ });
241
+ return result.txid || ((_a = result.transaction) === null || _a === void 0 ? void 0 : _a.txID);
242
+ }
243
+ throw new DynamicError('Invalid transaction parameters');
244
+ }),
245
+ provider,
246
+ });
247
+ });
248
+ }
249
+ }
250
+
251
+ export { TronWalletConnector };
@@ -0,0 +1 @@
1
+ export { TronWalletConnector } from './TronWalletConnector';
@@ -0,0 +1,20 @@
1
+ 'use client'
2
+ 'use strict';
3
+
4
+ Object.defineProperty(exports, '__esModule', { value: true });
5
+
6
+ var TronWalletConnector = require('../TronWalletConnector/TronWalletConnector.cjs');
7
+
8
+ class TrustTronConnector extends TronWalletConnector.TronWalletConnector {
9
+ constructor() {
10
+ super(...arguments);
11
+ this.name = 'Trust Wallet';
12
+ this.overrideKey = 'trusttron';
13
+ }
14
+ getProvider() {
15
+ var _a;
16
+ return (_a = window.trustwallet) === null || _a === void 0 ? void 0 : _a.tronLink;
17
+ }
18
+ }
19
+
20
+ exports.TrustTronConnector = TrustTronConnector;
@@ -0,0 +1,7 @@
1
+ import type { ITronProvider } from '../../types';
2
+ import { TronWalletConnector } from '../TronWalletConnector';
3
+ export declare class TrustTronConnector extends TronWalletConnector {
4
+ name: string;
5
+ overrideKey: string;
6
+ getProvider(): ITronProvider | undefined;
7
+ }
@@ -0,0 +1,16 @@
1
+ 'use client'
2
+ import { TronWalletConnector } from '../TronWalletConnector/TronWalletConnector.js';
3
+
4
+ class TrustTronConnector extends TronWalletConnector {
5
+ constructor() {
6
+ super(...arguments);
7
+ this.name = 'Trust Wallet';
8
+ this.overrideKey = 'trusttron';
9
+ }
10
+ getProvider() {
11
+ var _a;
12
+ return (_a = window.trustwallet) === null || _a === void 0 ? void 0 : _a.tronLink;
13
+ }
14
+ }
15
+
16
+ export { TrustTronConnector };
@@ -0,0 +1 @@
1
+ export { TrustTronConnector } from './TrustTronConnector';
package/src/index.cjs CHANGED
@@ -1,7 +1,25 @@
1
1
  'use client'
2
2
  'use strict';
3
3
 
4
+ Object.defineProperty(exports, '__esModule', { value: true });
5
+
4
6
  var assertPackageVersion = require('@dynamic-labs/assert-package-version');
5
7
  var _package = require('../package.cjs');
8
+ var BitgetTronConnector = require('./connectors/BitgetTronConnector/BitgetTronConnector.cjs');
9
+ var BybitTronConnector = require('./connectors/BybitTronConnector/BybitTronConnector.cjs');
10
+ var OKXTronConnector = require('./connectors/OKXTronConnector/OKXTronConnector.cjs');
11
+ var TrustTronConnector = require('./connectors/TrustTronConnector/TrustTronConnector.cjs');
12
+ var isTronWallet = require('./wallet/isTronWallet/isTronWallet.cjs');
13
+ var TronWallet = require('./wallet/TronWallet.cjs');
6
14
 
7
15
  assertPackageVersion.assertPackageVersion('@dynamic-labs/tron', _package.version);
16
+ const TronWalletConnectors = () => [
17
+ BitgetTronConnector.BitgetTronConnector,
18
+ BybitTronConnector.BybitTronConnector,
19
+ OKXTronConnector.OKXTronConnector,
20
+ TrustTronConnector.TrustTronConnector,
21
+ ];
22
+
23
+ exports.isTronWallet = isTronWallet.isTronWallet;
24
+ exports.TronWallet = TronWallet.TronWallet;
25
+ exports.TronWalletConnectors = TronWalletConnectors;
package/src/index.d.ts CHANGED
@@ -1 +1,6 @@
1
- export { type ITronProvider } from './types';
1
+ import { BitgetTronConnector } from './connectors/BitgetTronConnector';
2
+ export { type ITronProvider, type TronConnectionResult } from './types';
3
+ export { type TronWalletConnector } from './connectors/TronWalletConnector';
4
+ export { isTronWallet } from './wallet/isTronWallet';
5
+ export { TronWallet } from './wallet/TronWallet';
6
+ export declare const TronWalletConnectors: () => (typeof BitgetTronConnector)[];
package/src/index.js CHANGED
@@ -1,5 +1,19 @@
1
1
  'use client'
2
2
  import { assertPackageVersion } from '@dynamic-labs/assert-package-version';
3
3
  import { version } from '../package.js';
4
+ import { BitgetTronConnector } from './connectors/BitgetTronConnector/BitgetTronConnector.js';
5
+ import { BybitTronConnector } from './connectors/BybitTronConnector/BybitTronConnector.js';
6
+ import { OKXTronConnector } from './connectors/OKXTronConnector/OKXTronConnector.js';
7
+ import { TrustTronConnector } from './connectors/TrustTronConnector/TrustTronConnector.js';
8
+ export { isTronWallet } from './wallet/isTronWallet/isTronWallet.js';
9
+ export { TronWallet } from './wallet/TronWallet.js';
4
10
 
5
11
  assertPackageVersion('@dynamic-labs/tron', version);
12
+ const TronWalletConnectors = () => [
13
+ BitgetTronConnector,
14
+ BybitTronConnector,
15
+ OKXTronConnector,
16
+ TrustTronConnector,
17
+ ];
18
+
19
+ export { TronWalletConnectors };
package/src/types.d.ts CHANGED
@@ -6,9 +6,20 @@ declare global {
6
6
  interface Window {
7
7
  tronLink?: ITronProvider;
8
8
  tronWeb?: TronWeb;
9
- tokenpocket?: {
9
+ bitkeep?: {
10
+ tron?: ITronProvider;
11
+ };
12
+ bybitWallet?: {
13
+ tronLink?: ITronProvider;
14
+ tronWeb?: TronWeb;
15
+ };
16
+ okxwallet?: {
17
+ tronLink?: ITronProvider;
18
+ tronWeb?: TronWeb;
19
+ };
20
+ trustwallet?: {
21
+ tronLink?: ITronProvider;
10
22
  tronWeb?: TronWeb;
11
- tron?: unknown;
12
23
  };
13
24
  }
14
25
  }
@@ -0,0 +1,121 @@
1
+ 'use client'
2
+ 'use strict';
3
+
4
+ Object.defineProperty(exports, '__esModule', { value: true });
5
+
6
+ var _tslib = require('../../../_virtual/_tslib.cjs');
7
+ var utils = require('@dynamic-labs/utils');
8
+ var walletConnectorCore = require('@dynamic-labs/wallet-connector-core');
9
+ var getTronGasEstimation = require('../getTronGasEstimation.cjs');
10
+
11
+ // 1 TRX = 1,000,000 SUN (atomic units)
12
+ const SUN_PER_TRX = 1000000;
13
+ class TronUiTransaction {
14
+ constructor({ onSubmit, from, provider }) {
15
+ this.chain = 'TRON';
16
+ this.data = undefined;
17
+ this.fee = { gas: undefined };
18
+ this.from = from;
19
+ this.onSubmit = onSubmit;
20
+ this.provider = provider;
21
+ }
22
+ fetchFee() {
23
+ return _tslib.__awaiter(this, void 0, void 0, function* () {
24
+ if (this.fee.gas)
25
+ return;
26
+ this.fee.gas = yield getTronGasEstimation.getTronGasEstimation({
27
+ from: this.from,
28
+ nonNativeAddress: this.nonNativeAddress,
29
+ nonNativeValue: this.nonNativeValue,
30
+ provider: this.provider,
31
+ to: this.to,
32
+ value: this.value,
33
+ });
34
+ });
35
+ }
36
+ parse(input) {
37
+ const numericValue = parseFloat(input);
38
+ if (isNaN(numericValue))
39
+ return BigInt(0);
40
+ return BigInt(Math.floor(numericValue * SUN_PER_TRX));
41
+ }
42
+ parseNonNativeToken(input, decimals) {
43
+ const numericValue = parseFloat(input);
44
+ if (isNaN(numericValue))
45
+ return BigInt(0);
46
+ return BigInt(Math.floor(numericValue * Math.pow(10, decimals)));
47
+ }
48
+ format(value, options) {
49
+ var _a;
50
+ const trxValue = Number(value) / SUN_PER_TRX;
51
+ return utils.formatNumberText(String(trxValue), {
52
+ precision: (_a = options === null || options === void 0 ? void 0 : options.precision) !== null && _a !== void 0 ? _a : 6,
53
+ });
54
+ }
55
+ formatNonNativeToken(value, decimals) {
56
+ const tokenValue = Number(value) / Math.pow(10, decimals);
57
+ return utils.formatNumberText(String(tokenValue), {
58
+ precision: decimals > 6 ? 6 : decimals,
59
+ });
60
+ }
61
+ submit() {
62
+ return _tslib.__awaiter(this, void 0, void 0, function* () {
63
+ if (!this.to) {
64
+ throw new Error('Destination address is required');
65
+ }
66
+ if (!this.value && !this.nonNativeValue) {
67
+ throw new Error('Transaction value is required');
68
+ }
69
+ return this.onSubmit(this);
70
+ });
71
+ }
72
+ getBalance() {
73
+ return _tslib.__awaiter(this, void 0, void 0, function* () {
74
+ try {
75
+ const { tronWeb } = this.provider;
76
+ if (!tronWeb)
77
+ return BigInt(0);
78
+ if (this.nonNativeAddress) {
79
+ // Get TRC20 token balance
80
+ const contract = yield tronWeb.contract().at(this.nonNativeAddress);
81
+ const balance = yield contract.balanceOf(String(this.from)).call();
82
+ return BigInt(typeof balance === 'string' ? balance : balance.toString());
83
+ }
84
+ else {
85
+ // Get native TRX balance
86
+ const balance = yield tronWeb.trx.getBalance(String(this.from));
87
+ return BigInt(String(balance));
88
+ }
89
+ }
90
+ catch (error) {
91
+ walletConnectorCore.logger.error('Error occurred while fetching TRON balance. Please try again or contact support.');
92
+ return BigInt(0);
93
+ }
94
+ });
95
+ }
96
+ validateAddressFormat(address) {
97
+ if (address === 'dyn_send_transaction.multiple_recipients') {
98
+ return true;
99
+ }
100
+ try {
101
+ const { tronWeb } = this.provider;
102
+ if (!tronWeb)
103
+ return false;
104
+ // TRON addresses start with 'T' and are 34 characters long
105
+ if (!address.startsWith('T') || address.length !== 34) {
106
+ return false;
107
+ }
108
+ // Use TronWeb's built-in address validation
109
+ return tronWeb.isAddress(address);
110
+ }
111
+ catch (_a) {
112
+ return false;
113
+ }
114
+ }
115
+ isGasSponsored() {
116
+ return false;
117
+ }
118
+ }
119
+
120
+ exports.SUN_PER_TRX = SUN_PER_TRX;
121
+ exports.TronUiTransaction = TronUiTransaction;
@@ -0,0 +1,39 @@
1
+ import { IUITransaction, IUITransactionFormatOptions } from '@dynamic-labs/types';
2
+ import type { ITronProvider } from '../../types';
3
+ export declare const SUN_PER_TRX = 1000000;
4
+ type TronUiTransactionProps = {
5
+ /** TRON Address of the sender */
6
+ from: string;
7
+ /** The TRON provider instance, needed to interact with TronWeb */
8
+ provider: ITronProvider;
9
+ /** The function to call when the transaction is submitted */
10
+ onSubmit: (transaction: TronUiTransaction) => Promise<string>;
11
+ };
12
+ export declare class TronUiTransaction implements IUITransaction {
13
+ to: string | undefined;
14
+ from: string;
15
+ value: bigint | undefined;
16
+ chain: string;
17
+ receipt: string | undefined;
18
+ data: undefined;
19
+ fee: {
20
+ gas: bigint | undefined;
21
+ };
22
+ nonNativeAddress?: string;
23
+ nonNativeDecimal?: number;
24
+ nonNativeValue?: bigint;
25
+ nativePrice?: number;
26
+ private onSubmit;
27
+ private provider;
28
+ constructor({ onSubmit, from, provider }: TronUiTransactionProps);
29
+ fetchFee(): Promise<void>;
30
+ parse(input: string): bigint;
31
+ parseNonNativeToken(input: string, decimals: number): bigint;
32
+ format(value: bigint, options?: IUITransactionFormatOptions): string;
33
+ formatNonNativeToken(value: bigint, decimals: number): string;
34
+ submit(): Promise<string>;
35
+ getBalance(): Promise<bigint>;
36
+ validateAddressFormat(address: string): boolean;
37
+ isGasSponsored(): boolean;
38
+ }
39
+ export {};
@@ -0,0 +1,116 @@
1
+ 'use client'
2
+ import { __awaiter } from '../../../_virtual/_tslib.js';
3
+ import { formatNumberText } from '@dynamic-labs/utils';
4
+ import { logger } from '@dynamic-labs/wallet-connector-core';
5
+ import { getTronGasEstimation } from '../getTronGasEstimation.js';
6
+
7
+ // 1 TRX = 1,000,000 SUN (atomic units)
8
+ const SUN_PER_TRX = 1000000;
9
+ class TronUiTransaction {
10
+ constructor({ onSubmit, from, provider }) {
11
+ this.chain = 'TRON';
12
+ this.data = undefined;
13
+ this.fee = { gas: undefined };
14
+ this.from = from;
15
+ this.onSubmit = onSubmit;
16
+ this.provider = provider;
17
+ }
18
+ fetchFee() {
19
+ return __awaiter(this, void 0, void 0, function* () {
20
+ if (this.fee.gas)
21
+ return;
22
+ this.fee.gas = yield getTronGasEstimation({
23
+ from: this.from,
24
+ nonNativeAddress: this.nonNativeAddress,
25
+ nonNativeValue: this.nonNativeValue,
26
+ provider: this.provider,
27
+ to: this.to,
28
+ value: this.value,
29
+ });
30
+ });
31
+ }
32
+ parse(input) {
33
+ const numericValue = parseFloat(input);
34
+ if (isNaN(numericValue))
35
+ return BigInt(0);
36
+ return BigInt(Math.floor(numericValue * SUN_PER_TRX));
37
+ }
38
+ parseNonNativeToken(input, decimals) {
39
+ const numericValue = parseFloat(input);
40
+ if (isNaN(numericValue))
41
+ return BigInt(0);
42
+ return BigInt(Math.floor(numericValue * Math.pow(10, decimals)));
43
+ }
44
+ format(value, options) {
45
+ var _a;
46
+ const trxValue = Number(value) / SUN_PER_TRX;
47
+ return formatNumberText(String(trxValue), {
48
+ precision: (_a = options === null || options === void 0 ? void 0 : options.precision) !== null && _a !== void 0 ? _a : 6,
49
+ });
50
+ }
51
+ formatNonNativeToken(value, decimals) {
52
+ const tokenValue = Number(value) / Math.pow(10, decimals);
53
+ return formatNumberText(String(tokenValue), {
54
+ precision: decimals > 6 ? 6 : decimals,
55
+ });
56
+ }
57
+ submit() {
58
+ return __awaiter(this, void 0, void 0, function* () {
59
+ if (!this.to) {
60
+ throw new Error('Destination address is required');
61
+ }
62
+ if (!this.value && !this.nonNativeValue) {
63
+ throw new Error('Transaction value is required');
64
+ }
65
+ return this.onSubmit(this);
66
+ });
67
+ }
68
+ getBalance() {
69
+ return __awaiter(this, void 0, void 0, function* () {
70
+ try {
71
+ const { tronWeb } = this.provider;
72
+ if (!tronWeb)
73
+ return BigInt(0);
74
+ if (this.nonNativeAddress) {
75
+ // Get TRC20 token balance
76
+ const contract = yield tronWeb.contract().at(this.nonNativeAddress);
77
+ const balance = yield contract.balanceOf(String(this.from)).call();
78
+ return BigInt(typeof balance === 'string' ? balance : balance.toString());
79
+ }
80
+ else {
81
+ // Get native TRX balance
82
+ const balance = yield tronWeb.trx.getBalance(String(this.from));
83
+ return BigInt(String(balance));
84
+ }
85
+ }
86
+ catch (error) {
87
+ logger.error('Error occurred while fetching TRON balance. Please try again or contact support.');
88
+ return BigInt(0);
89
+ }
90
+ });
91
+ }
92
+ validateAddressFormat(address) {
93
+ if (address === 'dyn_send_transaction.multiple_recipients') {
94
+ return true;
95
+ }
96
+ try {
97
+ const { tronWeb } = this.provider;
98
+ if (!tronWeb)
99
+ return false;
100
+ // TRON addresses start with 'T' and are 34 characters long
101
+ if (!address.startsWith('T') || address.length !== 34) {
102
+ return false;
103
+ }
104
+ // Use TronWeb's built-in address validation
105
+ return tronWeb.isAddress(address);
106
+ }
107
+ catch (_a) {
108
+ return false;
109
+ }
110
+ }
111
+ isGasSponsored() {
112
+ return false;
113
+ }
114
+ }
115
+
116
+ export { SUN_PER_TRX, TronUiTransaction };
@@ -0,0 +1 @@
1
+ export { TronUiTransaction, SUN_PER_TRX } from './TronUiTransaction';