@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.
Files changed (71) hide show
  1. package/CHANGELOG.md +13 -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/TronWalletAdapterConnector/TronWalletAdapterConnector.cjs +436 -0
  7. package/src/connectors/TronWalletAdapterConnector/TronWalletAdapterConnector.d.ts +115 -0
  8. package/src/connectors/TronWalletAdapterConnector/TronWalletAdapterConnector.js +432 -0
  9. package/src/connectors/TronWalletAdapterConnector/index.d.ts +1 -0
  10. package/src/index.cjs +4 -13
  11. package/src/index.d.ts +5 -5
  12. package/src/index.js +3 -12
  13. package/src/types.d.ts +11 -181
  14. package/src/utils/TronUiTransaction/TronUiTransaction.cjs +67 -26
  15. package/src/utils/TronUiTransaction/TronUiTransaction.d.ts +38 -5
  16. package/src/utils/TronUiTransaction/TronUiTransaction.js +67 -26
  17. package/src/utils/convertChainIdToDecimal/convertChainIdToDecimal.cjs +16 -0
  18. package/src/utils/convertChainIdToDecimal/convertChainIdToDecimal.d.ts +9 -0
  19. package/src/utils/convertChainIdToDecimal/convertChainIdToDecimal.js +12 -0
  20. package/src/utils/convertChainIdToDecimal/index.d.ts +1 -0
  21. package/src/utils/fetchTronWalletAdapterConnectors/fetchTronWalletAdapterConnectors.cjs +80 -0
  22. package/src/utils/fetchTronWalletAdapterConnectors/fetchTronWalletAdapterConnectors.d.ts +35 -0
  23. package/src/utils/fetchTronWalletAdapterConnectors/fetchTronWalletAdapterConnectors.js +75 -0
  24. package/src/utils/fetchTronWalletAdapterConnectors/index.d.ts +1 -0
  25. package/src/utils/getTronGasEstimation/getTronGasEstimation.cjs +162 -0
  26. package/src/utils/getTronGasEstimation/getTronGasEstimation.d.ts +49 -0
  27. package/src/utils/getTronGasEstimation/getTronGasEstimation.js +158 -0
  28. package/src/utils/getTronGasEstimation/index.d.ts +1 -0
  29. package/src/utils/index.d.ts +1 -4
  30. package/src/wallet/TronWallet/TronWallet.cjs +124 -0
  31. package/src/wallet/TronWallet/TronWallet.d.ts +51 -0
  32. package/src/wallet/TronWallet/TronWallet.js +120 -0
  33. package/src/wallet/TronWallet/index.d.ts +1 -0
  34. package/src/connectors/BitgetTronConnector/BitgetTronConnector.cjs +0 -20
  35. package/src/connectors/BitgetTronConnector/BitgetTronConnector.d.ts +0 -7
  36. package/src/connectors/BitgetTronConnector/BitgetTronConnector.js +0 -16
  37. package/src/connectors/BitgetTronConnector/index.d.ts +0 -1
  38. package/src/connectors/BybitTronConnector/BybitTronConnector.d.ts +0 -7
  39. package/src/connectors/BybitTronConnector/index.d.ts +0 -1
  40. package/src/connectors/OKXTronConnector/OKXTronConnector.cjs +0 -20
  41. package/src/connectors/OKXTronConnector/OKXTronConnector.d.ts +0 -7
  42. package/src/connectors/OKXTronConnector/OKXTronConnector.js +0 -16
  43. package/src/connectors/OKXTronConnector/index.d.ts +0 -1
  44. package/src/connectors/TokenPocketTronConnector/TokenPocketTronConnector.cjs +0 -28
  45. package/src/connectors/TokenPocketTronConnector/TokenPocketTronConnector.d.ts +0 -7
  46. package/src/connectors/TokenPocketTronConnector/TokenPocketTronConnector.js +0 -24
  47. package/src/connectors/TokenPocketTronConnector/index.d.ts +0 -1
  48. package/src/connectors/TronWalletConnector/TronWalletConnector.cjs +0 -255
  49. package/src/connectors/TronWalletConnector/TronWalletConnector.d.ts +0 -42
  50. package/src/connectors/TronWalletConnector/TronWalletConnector.js +0 -251
  51. package/src/connectors/TronWalletConnector/index.d.ts +0 -1
  52. package/src/connectors/TrustTronConnector/TrustTronConnector.cjs +0 -31
  53. package/src/connectors/TrustTronConnector/TrustTronConnector.d.ts +0 -7
  54. package/src/connectors/TrustTronConnector/TrustTronConnector.js +0 -27
  55. package/src/connectors/TrustTronConnector/index.d.ts +0 -1
  56. package/src/utils/detectTronNetworkFromProvider.cjs +0 -67
  57. package/src/utils/detectTronNetworkFromProvider.d.ts +0 -30
  58. package/src/utils/detectTronNetworkFromProvider.js +0 -62
  59. package/src/utils/getDefaultTronNetworks.cjs +0 -60
  60. package/src/utils/getDefaultTronNetworks.d.ts +0 -9
  61. package/src/utils/getDefaultTronNetworks.js +0 -53
  62. package/src/utils/getTronGasEstimation.cjs +0 -136
  63. package/src/utils/getTronGasEstimation.d.ts +0 -47
  64. package/src/utils/getTronGasEstimation.js +0 -129
  65. package/src/utils/provider.cjs +0 -59
  66. package/src/utils/provider.d.ts +0 -24
  67. package/src/utils/provider.js +0 -53
  68. package/src/wallet/TronWallet.cjs +0 -106
  69. package/src/wallet/TronWallet.d.ts +0 -22
  70. package/src/wallet/TronWallet.js +0 -102
  71. package/src/wallet/index.d.ts +0 -2
@@ -1,67 +0,0 @@
1
- 'use client'
2
- 'use strict';
3
-
4
- Object.defineProperty(exports, '__esModule', { value: true });
5
-
6
- /**
7
- * Default endpoint mappings for common TRON networks
8
- */
9
- const DEFAULT_TRON_ENDPOINT_MAPPINGS = [
10
- {
11
- hostPatterns: ['api.trongrid.io', 'trx.mytokenpocket.vip'],
12
- networkConfig: {
13
- chainId: '728126428',
14
- network: 'mainnet',
15
- },
16
- },
17
- {
18
- hostPatterns: ['api.shasta.trongrid.io'],
19
- networkConfig: {
20
- chainId: '2494104990',
21
- network: 'shasta',
22
- },
23
- },
24
- {
25
- hostPatterns: ['api.nileex.io'],
26
- networkConfig: {
27
- chainId: '3448148188',
28
- network: 'nile',
29
- },
30
- },
31
- ];
32
- /**
33
- * Detects and sets TRON network information based on the provider's TronWeb endpoint.
34
- * This is useful for wallets that don't expose chainId/network properties directly.
35
- *
36
- * @param provider - The TRON provider to detect network information for
37
- * @param additionalMappings - Additional endpoint mappings to check before the defaults.
38
- * @returns The provider with network information set (if detected), or the original provider
39
- */
40
- const detectTronNetworkFromProvider = (provider, additionalMappings = []) => {
41
- var _a, _b;
42
- if (!provider || provider.chainId) {
43
- // Return early if provider is null/undefined or already has chainId
44
- return provider;
45
- }
46
- const host = (_b = (_a = provider.tronWeb) === null || _a === void 0 ? void 0 : _a.fullNode) === null || _b === void 0 ? void 0 : _b.host;
47
- if (!host) {
48
- return provider;
49
- }
50
- // Combine additional mappings with defaults, with additional mappings taking precedence
51
- const allMappings = [
52
- ...additionalMappings,
53
- ...DEFAULT_TRON_ENDPOINT_MAPPINGS,
54
- ];
55
- // Find the first matching endpoint mapping
56
- for (const mapping of allMappings) {
57
- const isMatch = mapping.hostPatterns.some((pattern) => host.includes(pattern));
58
- if (isMatch) {
59
- Object.assign(provider, mapping.networkConfig);
60
- break;
61
- }
62
- }
63
- return provider;
64
- };
65
-
66
- exports.DEFAULT_TRON_ENDPOINT_MAPPINGS = DEFAULT_TRON_ENDPOINT_MAPPINGS;
67
- exports.detectTronNetworkFromProvider = detectTronNetworkFromProvider;
@@ -1,30 +0,0 @@
1
- import { ITronProvider } from '../types';
2
- /**
3
- * Network configuration for TRON networks
4
- */
5
- export interface TronNetworkConfig {
6
- chainId: string;
7
- network: string;
8
- }
9
- /**
10
- * Endpoint mapping configuration for network detection
11
- */
12
- export interface TronEndpointMapping {
13
- /** URL patterns that should match this network (checked with includes()) */
14
- hostPatterns: string[];
15
- /** Network configuration to apply when pattern matches */
16
- networkConfig: TronNetworkConfig;
17
- }
18
- /**
19
- * Default endpoint mappings for common TRON networks
20
- */
21
- export declare const DEFAULT_TRON_ENDPOINT_MAPPINGS: TronEndpointMapping[];
22
- /**
23
- * Detects and sets TRON network information based on the provider's TronWeb endpoint.
24
- * This is useful for wallets that don't expose chainId/network properties directly.
25
- *
26
- * @param provider - The TRON provider to detect network information for
27
- * @param additionalMappings - Additional endpoint mappings to check before the defaults.
28
- * @returns The provider with network information set (if detected), or the original provider
29
- */
30
- export declare const detectTronNetworkFromProvider: <T extends ITronProvider>(provider: T, additionalMappings?: TronEndpointMapping[]) => T;
@@ -1,62 +0,0 @@
1
- 'use client'
2
- /**
3
- * Default endpoint mappings for common TRON networks
4
- */
5
- const DEFAULT_TRON_ENDPOINT_MAPPINGS = [
6
- {
7
- hostPatterns: ['api.trongrid.io', 'trx.mytokenpocket.vip'],
8
- networkConfig: {
9
- chainId: '728126428',
10
- network: 'mainnet',
11
- },
12
- },
13
- {
14
- hostPatterns: ['api.shasta.trongrid.io'],
15
- networkConfig: {
16
- chainId: '2494104990',
17
- network: 'shasta',
18
- },
19
- },
20
- {
21
- hostPatterns: ['api.nileex.io'],
22
- networkConfig: {
23
- chainId: '3448148188',
24
- network: 'nile',
25
- },
26
- },
27
- ];
28
- /**
29
- * Detects and sets TRON network information based on the provider's TronWeb endpoint.
30
- * This is useful for wallets that don't expose chainId/network properties directly.
31
- *
32
- * @param provider - The TRON provider to detect network information for
33
- * @param additionalMappings - Additional endpoint mappings to check before the defaults.
34
- * @returns The provider with network information set (if detected), or the original provider
35
- */
36
- const detectTronNetworkFromProvider = (provider, additionalMappings = []) => {
37
- var _a, _b;
38
- if (!provider || provider.chainId) {
39
- // Return early if provider is null/undefined or already has chainId
40
- return provider;
41
- }
42
- const host = (_b = (_a = provider.tronWeb) === null || _a === void 0 ? void 0 : _a.fullNode) === null || _b === void 0 ? void 0 : _b.host;
43
- if (!host) {
44
- return provider;
45
- }
46
- // Combine additional mappings with defaults, with additional mappings taking precedence
47
- const allMappings = [
48
- ...additionalMappings,
49
- ...DEFAULT_TRON_ENDPOINT_MAPPINGS,
50
- ];
51
- // Find the first matching endpoint mapping
52
- for (const mapping of allMappings) {
53
- const isMatch = mapping.hostPatterns.some((pattern) => host.includes(pattern));
54
- if (isMatch) {
55
- Object.assign(provider, mapping.networkConfig);
56
- break;
57
- }
58
- }
59
- return provider;
60
- };
61
-
62
- export { DEFAULT_TRON_ENDPOINT_MAPPINGS, detectTronNetworkFromProvider };
@@ -1,60 +0,0 @@
1
- 'use client'
2
- 'use strict';
3
-
4
- Object.defineProperty(exports, '__esModule', { value: true });
5
-
6
- const TRON_MAINNET_NETWORK = {
7
- blockExplorerUrls: ['https://tronscan.org/'],
8
- chainId: '728126428',
9
- iconUrls: ['https://app.dynamic.xyz/assets/networks/tron.svg'],
10
- name: 'Tron Mainnet',
11
- nativeCurrency: {
12
- decimals: 6,
13
- name: 'Tron',
14
- symbol: 'TRX',
15
- },
16
- networkId: '728126428',
17
- rpcUrls: ['https://api.trongrid.io'],
18
- };
19
- const TRON_NILE_TESTNET_NETWORK = {
20
- blockExplorerUrls: ['https://nile.tronscan.org/'],
21
- chainId: '728126430',
22
- iconUrls: ['https://app.dynamic.xyz/assets/networks/tron.svg'],
23
- isTestnet: true,
24
- name: 'Tron Nile Testnet',
25
- nativeCurrency: {
26
- decimals: 6,
27
- name: 'Tron',
28
- symbol: 'TRX',
29
- },
30
- networkId: '728126430',
31
- rpcUrls: ['https://api.nileex.io'],
32
- };
33
- const TRON_SHASTA_TESTNET_NETWORK = {
34
- blockExplorerUrls: ['https://shasta.tronscan.org/'],
35
- chainId: '728126429',
36
- iconUrls: ['https://app.dynamic.xyz/assets/networks/tron.svg'],
37
- isTestnet: true,
38
- name: 'Tron Shasta Testnet',
39
- nativeCurrency: {
40
- decimals: 6,
41
- name: 'Tron',
42
- symbol: 'TRX',
43
- },
44
- networkId: '728126429',
45
- rpcUrls: ['https://api.shasta.trongrid.io'],
46
- };
47
- /**
48
- * Gets the default Tron networks configuration
49
- * @returns Array of default Tron networks
50
- */
51
- const getDefaultTronNetworks = () => [
52
- TRON_MAINNET_NETWORK,
53
- TRON_NILE_TESTNET_NETWORK,
54
- TRON_SHASTA_TESTNET_NETWORK,
55
- ];
56
-
57
- exports.TRON_MAINNET_NETWORK = TRON_MAINNET_NETWORK;
58
- exports.TRON_NILE_TESTNET_NETWORK = TRON_NILE_TESTNET_NETWORK;
59
- exports.TRON_SHASTA_TESTNET_NETWORK = TRON_SHASTA_TESTNET_NETWORK;
60
- exports.getDefaultTronNetworks = getDefaultTronNetworks;
@@ -1,9 +0,0 @@
1
- import type { GenericNetwork } from '@dynamic-labs/types';
2
- export declare const TRON_MAINNET_NETWORK: GenericNetwork;
3
- export declare const TRON_NILE_TESTNET_NETWORK: GenericNetwork;
4
- export declare const TRON_SHASTA_TESTNET_NETWORK: GenericNetwork;
5
- /**
6
- * Gets the default Tron networks configuration
7
- * @returns Array of default Tron networks
8
- */
9
- export declare const getDefaultTronNetworks: () => GenericNetwork[];
@@ -1,53 +0,0 @@
1
- 'use client'
2
- const TRON_MAINNET_NETWORK = {
3
- blockExplorerUrls: ['https://tronscan.org/'],
4
- chainId: '728126428',
5
- iconUrls: ['https://app.dynamic.xyz/assets/networks/tron.svg'],
6
- name: 'Tron Mainnet',
7
- nativeCurrency: {
8
- decimals: 6,
9
- name: 'Tron',
10
- symbol: 'TRX',
11
- },
12
- networkId: '728126428',
13
- rpcUrls: ['https://api.trongrid.io'],
14
- };
15
- const TRON_NILE_TESTNET_NETWORK = {
16
- blockExplorerUrls: ['https://nile.tronscan.org/'],
17
- chainId: '728126430',
18
- iconUrls: ['https://app.dynamic.xyz/assets/networks/tron.svg'],
19
- isTestnet: true,
20
- name: 'Tron Nile Testnet',
21
- nativeCurrency: {
22
- decimals: 6,
23
- name: 'Tron',
24
- symbol: 'TRX',
25
- },
26
- networkId: '728126430',
27
- rpcUrls: ['https://api.nileex.io'],
28
- };
29
- const TRON_SHASTA_TESTNET_NETWORK = {
30
- blockExplorerUrls: ['https://shasta.tronscan.org/'],
31
- chainId: '728126429',
32
- iconUrls: ['https://app.dynamic.xyz/assets/networks/tron.svg'],
33
- isTestnet: true,
34
- name: 'Tron Shasta Testnet',
35
- nativeCurrency: {
36
- decimals: 6,
37
- name: 'Tron',
38
- symbol: 'TRX',
39
- },
40
- networkId: '728126429',
41
- rpcUrls: ['https://api.shasta.trongrid.io'],
42
- };
43
- /**
44
- * Gets the default Tron networks configuration
45
- * @returns Array of default Tron networks
46
- */
47
- const getDefaultTronNetworks = () => [
48
- TRON_MAINNET_NETWORK,
49
- TRON_NILE_TESTNET_NETWORK,
50
- TRON_SHASTA_TESTNET_NETWORK,
51
- ];
52
-
53
- export { TRON_MAINNET_NETWORK, TRON_NILE_TESTNET_NETWORK, TRON_SHASTA_TESTNET_NETWORK, getDefaultTronNetworks };
@@ -1,136 +0,0 @@
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
-
9
- /**
10
- * Estimates gas fee for TRC20 token transfers
11
- * @param params TRC20 estimation parameters
12
- * @returns Estimated gas fee in SUN or null if estimation fails
13
- */
14
- const estimateTRC20Gas = (params) => _tslib.__awaiter(void 0, void 0, void 0, function* () {
15
- const { tronWeb, from, to, contractAddress, amount } = params;
16
- try {
17
- const functionSelector = 'transfer(address,uint256)';
18
- const parameters = [
19
- { type: 'address', value: to },
20
- { type: 'uint256', value: amount.toString() },
21
- ];
22
- const result = yield tronWeb.transactionBuilder.estimateEnergy(contractAddress, functionSelector, {}, parameters, from);
23
- if (result === null || result === void 0 ? void 0 : result.energy_required) {
24
- // Convert energy to TRX (energy cost is typically ~420 SUN per unit)
25
- const estimatedFee = Math.max(result.energy_required * 420, 500000);
26
- return BigInt(estimatedFee);
27
- }
28
- return null;
29
- }
30
- catch (error) {
31
- walletConnectorCore.logger.error('TRC20 energy estimation failed:', error);
32
- return null;
33
- }
34
- });
35
- /**
36
- * Estimates gas fee for native TRX transfers
37
- * @param params Native TRX estimation parameters
38
- * @returns Estimated gas fee in SUN or null if estimation fails
39
- */
40
- const estimateNativeTRXGas = (params) => _tslib.__awaiter(void 0, void 0, void 0, function* () {
41
- const { tronWeb, from, to, value } = params;
42
- try {
43
- // Build the transaction to get accurate bandwidth cost
44
- const transaction = yield tronWeb.transactionBuilder.sendTrx(to, Number(value), from);
45
- // Get bandwidth cost
46
- const bandwidth = transaction.raw_data_hex
47
- ? transaction.raw_data_hex.length / 2
48
- : 200; // fallback estimate
49
- // Check account resources (free bandwidth, energy, etc.)
50
- let accountResources;
51
- try {
52
- accountResources = yield tronWeb.trx.getAccountResources(from);
53
- }
54
- catch (resourceError) {
55
- walletConnectorCore.logger.error('Could not fetch account resources:', resourceError);
56
- accountResources = null;
57
- }
58
- // Calculate if transaction can use free bandwidth
59
- const freeNetAvailable = accountResources
60
- ? (accountResources.freeNetLimit || 0) -
61
- (accountResources.freeNetUsed || 0)
62
- : 0;
63
- const delegatedNetAvailable = accountResources
64
- ? (accountResources.NetLimit || 0) - (accountResources.NetUsed || 0)
65
- : 0;
66
- const totalFreeBandwidth = freeNetAvailable + delegatedNetAvailable;
67
- // If we have enough free bandwidth, the fee should be 0
68
- if (bandwidth <= totalFreeBandwidth) {
69
- return BigInt(0);
70
- }
71
- // Otherwise calculate the fee for the bandwidth we need to pay for
72
- const paidBandwidth = bandwidth - totalFreeBandwidth;
73
- const estimatedFee = Math.max(paidBandwidth * 1000, 0);
74
- return BigInt(estimatedFee);
75
- }
76
- catch (error) {
77
- walletConnectorCore.logger.error('Native TRX estimation failed:', error);
78
- return null;
79
- }
80
- });
81
- /**
82
- * Gets fallback gas fee based on transaction type
83
- * @param isTokenTransfer Whether this is a token transfer
84
- * @returns Fallback gas fee in SUN
85
- */
86
- const getFallbackGasFee = (isTokenTransfer) => isTokenTransfer ? BigInt(500000) : BigInt(270000);
87
- /**
88
- * Estimates gas fees for TRON transactions
89
- * @param params Transaction parameters for estimation
90
- * @returns Estimated gas fee in SUN
91
- */
92
- const getTronGasEstimation = (params) => _tslib.__awaiter(void 0, void 0, void 0, function* () {
93
- const { provider, from, to, value, nonNativeAddress, nonNativeValue } = params;
94
- try {
95
- const { tronWeb } = provider;
96
- if (!tronWeb) {
97
- return getFallbackGasFee(Boolean(nonNativeAddress));
98
- }
99
- // TRC20 Token Transfer Estimation
100
- if (nonNativeAddress && to && nonNativeValue) {
101
- const trc20Fee = yield estimateTRC20Gas({
102
- amount: nonNativeValue,
103
- contractAddress: nonNativeAddress,
104
- from,
105
- to,
106
- tronWeb,
107
- });
108
- if (trc20Fee) {
109
- return trc20Fee;
110
- }
111
- }
112
- // Native TRX Transfer Estimation
113
- if (to && value && value > 0) {
114
- const nativeFee = yield estimateNativeTRXGas({
115
- from,
116
- to,
117
- tronWeb,
118
- value,
119
- });
120
- if (nativeFee) {
121
- return nativeFee;
122
- }
123
- }
124
- // Fallback to reasonable defaults
125
- return getFallbackGasFee(Boolean(nonNativeAddress));
126
- }
127
- catch (error) {
128
- walletConnectorCore.logger.error('Gas estimation failed:', error);
129
- return getFallbackGasFee(Boolean(nonNativeAddress));
130
- }
131
- });
132
-
133
- exports.estimateNativeTRXGas = estimateNativeTRXGas;
134
- exports.estimateTRC20Gas = estimateTRC20Gas;
135
- exports.getFallbackGasFee = getFallbackGasFee;
136
- exports.getTronGasEstimation = getTronGasEstimation;
@@ -1,47 +0,0 @@
1
- import type { TronWeb } from 'tronweb';
2
- import type { ITronProvider } from '../types';
3
- export interface TronGasEstimationParams {
4
- provider: ITronProvider;
5
- from: string;
6
- to?: string;
7
- value?: bigint;
8
- nonNativeAddress?: string;
9
- nonNativeValue?: bigint;
10
- }
11
- export interface TRC20EstimationParams {
12
- tronWeb: TronWeb;
13
- from: string;
14
- to: string;
15
- contractAddress: string;
16
- amount: bigint;
17
- }
18
- export interface NativeTRXEstimationParams {
19
- tronWeb: TronWeb;
20
- from: string;
21
- to: string;
22
- value: bigint;
23
- }
24
- /**
25
- * Estimates gas fee for TRC20 token transfers
26
- * @param params TRC20 estimation parameters
27
- * @returns Estimated gas fee in SUN or null if estimation fails
28
- */
29
- export declare const estimateTRC20Gas: (params: TRC20EstimationParams) => Promise<bigint | null>;
30
- /**
31
- * Estimates gas fee for native TRX transfers
32
- * @param params Native TRX estimation parameters
33
- * @returns Estimated gas fee in SUN or null if estimation fails
34
- */
35
- export declare const estimateNativeTRXGas: (params: NativeTRXEstimationParams) => Promise<bigint | null>;
36
- /**
37
- * Gets fallback gas fee based on transaction type
38
- * @param isTokenTransfer Whether this is a token transfer
39
- * @returns Fallback gas fee in SUN
40
- */
41
- export declare const getFallbackGasFee: (isTokenTransfer: boolean) => bigint;
42
- /**
43
- * Estimates gas fees for TRON transactions
44
- * @param params Transaction parameters for estimation
45
- * @returns Estimated gas fee in SUN
46
- */
47
- export declare const getTronGasEstimation: (params: TronGasEstimationParams) => Promise<bigint>;
@@ -1,129 +0,0 @@
1
- 'use client'
2
- import { __awaiter } from '../../_virtual/_tslib.js';
3
- import { logger } from '@dynamic-labs/wallet-connector-core';
4
-
5
- /**
6
- * Estimates gas fee for TRC20 token transfers
7
- * @param params TRC20 estimation parameters
8
- * @returns Estimated gas fee in SUN or null if estimation fails
9
- */
10
- const estimateTRC20Gas = (params) => __awaiter(void 0, void 0, void 0, function* () {
11
- const { tronWeb, from, to, contractAddress, amount } = params;
12
- try {
13
- const functionSelector = 'transfer(address,uint256)';
14
- const parameters = [
15
- { type: 'address', value: to },
16
- { type: 'uint256', value: amount.toString() },
17
- ];
18
- const result = yield tronWeb.transactionBuilder.estimateEnergy(contractAddress, functionSelector, {}, parameters, from);
19
- if (result === null || result === void 0 ? void 0 : result.energy_required) {
20
- // Convert energy to TRX (energy cost is typically ~420 SUN per unit)
21
- const estimatedFee = Math.max(result.energy_required * 420, 500000);
22
- return BigInt(estimatedFee);
23
- }
24
- return null;
25
- }
26
- catch (error) {
27
- logger.error('TRC20 energy estimation failed:', error);
28
- return null;
29
- }
30
- });
31
- /**
32
- * Estimates gas fee for native TRX transfers
33
- * @param params Native TRX estimation parameters
34
- * @returns Estimated gas fee in SUN or null if estimation fails
35
- */
36
- const estimateNativeTRXGas = (params) => __awaiter(void 0, void 0, void 0, function* () {
37
- const { tronWeb, from, to, value } = params;
38
- try {
39
- // Build the transaction to get accurate bandwidth cost
40
- const transaction = yield tronWeb.transactionBuilder.sendTrx(to, Number(value), from);
41
- // Get bandwidth cost
42
- const bandwidth = transaction.raw_data_hex
43
- ? transaction.raw_data_hex.length / 2
44
- : 200; // fallback estimate
45
- // Check account resources (free bandwidth, energy, etc.)
46
- let accountResources;
47
- try {
48
- accountResources = yield tronWeb.trx.getAccountResources(from);
49
- }
50
- catch (resourceError) {
51
- logger.error('Could not fetch account resources:', resourceError);
52
- accountResources = null;
53
- }
54
- // Calculate if transaction can use free bandwidth
55
- const freeNetAvailable = accountResources
56
- ? (accountResources.freeNetLimit || 0) -
57
- (accountResources.freeNetUsed || 0)
58
- : 0;
59
- const delegatedNetAvailable = accountResources
60
- ? (accountResources.NetLimit || 0) - (accountResources.NetUsed || 0)
61
- : 0;
62
- const totalFreeBandwidth = freeNetAvailable + delegatedNetAvailable;
63
- // If we have enough free bandwidth, the fee should be 0
64
- if (bandwidth <= totalFreeBandwidth) {
65
- return BigInt(0);
66
- }
67
- // Otherwise calculate the fee for the bandwidth we need to pay for
68
- const paidBandwidth = bandwidth - totalFreeBandwidth;
69
- const estimatedFee = Math.max(paidBandwidth * 1000, 0);
70
- return BigInt(estimatedFee);
71
- }
72
- catch (error) {
73
- logger.error('Native TRX estimation failed:', error);
74
- return null;
75
- }
76
- });
77
- /**
78
- * Gets fallback gas fee based on transaction type
79
- * @param isTokenTransfer Whether this is a token transfer
80
- * @returns Fallback gas fee in SUN
81
- */
82
- const getFallbackGasFee = (isTokenTransfer) => isTokenTransfer ? BigInt(500000) : BigInt(270000);
83
- /**
84
- * Estimates gas fees for TRON transactions
85
- * @param params Transaction parameters for estimation
86
- * @returns Estimated gas fee in SUN
87
- */
88
- const getTronGasEstimation = (params) => __awaiter(void 0, void 0, void 0, function* () {
89
- const { provider, from, to, value, nonNativeAddress, nonNativeValue } = params;
90
- try {
91
- const { tronWeb } = provider;
92
- if (!tronWeb) {
93
- return getFallbackGasFee(Boolean(nonNativeAddress));
94
- }
95
- // TRC20 Token Transfer Estimation
96
- if (nonNativeAddress && to && nonNativeValue) {
97
- const trc20Fee = yield estimateTRC20Gas({
98
- amount: nonNativeValue,
99
- contractAddress: nonNativeAddress,
100
- from,
101
- to,
102
- tronWeb,
103
- });
104
- if (trc20Fee) {
105
- return trc20Fee;
106
- }
107
- }
108
- // Native TRX Transfer Estimation
109
- if (to && value && value > 0) {
110
- const nativeFee = yield estimateNativeTRXGas({
111
- from,
112
- to,
113
- tronWeb,
114
- value,
115
- });
116
- if (nativeFee) {
117
- return nativeFee;
118
- }
119
- }
120
- // Fallback to reasonable defaults
121
- return getFallbackGasFee(Boolean(nonNativeAddress));
122
- }
123
- catch (error) {
124
- logger.error('Gas estimation failed:', error);
125
- return getFallbackGasFee(Boolean(nonNativeAddress));
126
- }
127
- });
128
-
129
- export { estimateNativeTRXGas, estimateTRC20Gas, getFallbackGasFee, getTronGasEstimation };
@@ -1,59 +0,0 @@
1
- 'use client'
2
- 'use strict';
3
-
4
- Object.defineProperty(exports, '__esModule', { value: true });
5
-
6
- /**
7
- * Asserts that a provider is available and throws an error if not
8
- * @param provider - The provider to validate
9
- * @param providerName - Human-readable name for error messages
10
- * @returns The validated provider
11
- * @throws {Error} If provider is not available
12
- */
13
- const assertProvider = (provider, providerName) => {
14
- if (!provider) {
15
- throw new Error(`${providerName} provider not available`);
16
- }
17
- return provider;
18
- };
19
- /**
20
- * Gets the current address from a Tron provider using various methods
21
- * @param provider - The Tron provider
22
- * @returns The current address or undefined if not available
23
- */
24
- const getCurrentAddress = (provider) => {
25
- var _a, _b, _c, _d;
26
- // Try different ways to get the address
27
- if (provider.address) {
28
- return provider.address;
29
- }
30
- if ((_b = (_a = provider.tronWeb) === null || _a === void 0 ? void 0 : _a.defaultAddress) === null || _b === void 0 ? void 0 : _b.base58) {
31
- return provider.tronWeb.defaultAddress.base58;
32
- }
33
- if (typeof ((_c = provider.tronWeb) === null || _c === void 0 ? void 0 : _c.defaultAddress) === 'string') {
34
- return provider.tronWeb.defaultAddress;
35
- }
36
- // Check global tronWeb
37
- if (typeof window !== 'undefined' && ((_d = window.tronWeb) === null || _d === void 0 ? void 0 : _d.defaultAddress)) {
38
- if (typeof window.tronWeb.defaultAddress === 'string') {
39
- return window.tronWeb.defaultAddress;
40
- }
41
- if (window.tronWeb.defaultAddress.base58) {
42
- return window.tronWeb.defaultAddress.base58;
43
- }
44
- }
45
- return undefined;
46
- };
47
- /**
48
- * Checks if a Tron provider is ready and connected
49
- * @param provider - The Tron provider
50
- * @returns True if the provider is ready and connected
51
- */
52
- const isProviderReady = (provider) => {
53
- const hasAddress = getCurrentAddress(provider);
54
- return Boolean(provider.ready !== false && (provider.isConnected || hasAddress));
55
- };
56
-
57
- exports.assertProvider = assertProvider;
58
- exports.getCurrentAddress = getCurrentAddress;
59
- exports.isProviderReady = isProviderReady;
@@ -1,24 +0,0 @@
1
- /**
2
- * Utility functions for Tron provider validation and error handling
3
- */
4
- import type { ITronProvider } from '../types';
5
- /**
6
- * Asserts that a provider is available and throws an error if not
7
- * @param provider - The provider to validate
8
- * @param providerName - Human-readable name for error messages
9
- * @returns The validated provider
10
- * @throws {Error} If provider is not available
11
- */
12
- export declare const assertProvider: <T>(provider: T | undefined, providerName: string) => T;
13
- /**
14
- * Gets the current address from a Tron provider using various methods
15
- * @param provider - The Tron provider
16
- * @returns The current address or undefined if not available
17
- */
18
- export declare const getCurrentAddress: (provider: ITronProvider) => string | undefined;
19
- /**
20
- * Checks if a Tron provider is ready and connected
21
- * @param provider - The Tron provider
22
- * @returns True if the provider is ready and connected
23
- */
24
- export declare const isProviderReady: (provider: ITronProvider) => boolean;