@dynamic-labs/tron 4.40.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.
- package/CHANGELOG.md +6 -0
- package/README.md +264 -20
- package/package.cjs +1 -1
- package/package.js +1 -1
- package/package.json +8 -6
- package/src/connectors/BitgetTronConnector/BitgetTronConnector.cjs +5 -5
- package/src/connectors/BitgetTronConnector/BitgetTronConnector.d.ts +4 -4
- package/src/connectors/BitgetTronConnector/BitgetTronConnector.js +5 -5
- package/src/connectors/OKXTronConnector/OKXTronConnector.cjs +5 -5
- package/src/connectors/OKXTronConnector/OKXTronConnector.d.ts +4 -4
- package/src/connectors/OKXTronConnector/OKXTronConnector.js +5 -5
- package/src/connectors/TokenPocketTronConnector/TokenPocketTronConnector.cjs +5 -13
- package/src/connectors/TokenPocketTronConnector/TokenPocketTronConnector.d.ts +4 -4
- package/src/connectors/TokenPocketTronConnector/TokenPocketTronConnector.js +5 -13
- package/src/connectors/TronWalletAdapterConnector/TronWalletAdapterConnector.cjs +304 -0
- package/src/connectors/TronWalletAdapterConnector/TronWalletAdapterConnector.d.ts +107 -0
- package/src/connectors/TronWalletAdapterConnector/TronWalletAdapterConnector.js +300 -0
- package/src/connectors/TronWalletAdapterConnector/index.d.ts +1 -0
- package/src/connectors/TrustTronConnector/TrustTronConnector.cjs +5 -16
- package/src/connectors/TrustTronConnector/TrustTronConnector.d.ts +4 -4
- package/src/connectors/TrustTronConnector/TrustTronConnector.js +5 -16
- package/src/index.cjs +2 -3
- package/src/index.d.ts +5 -4
- package/src/index.js +1 -2
- 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/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/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/BybitTronConnector/BybitTronConnector.d.ts +0 -7
- package/src/connectors/BybitTronConnector/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/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/index.d.ts +0 -4
- 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
|
@@ -6,45 +6,77 @@ Object.defineProperty(exports, '__esModule', { value: true });
|
|
|
6
6
|
var _tslib = require('../../../_virtual/_tslib.cjs');
|
|
7
7
|
var utils = require('@dynamic-labs/utils');
|
|
8
8
|
var walletConnectorCore = require('@dynamic-labs/wallet-connector-core');
|
|
9
|
-
var getTronGasEstimation = require('../getTronGasEstimation.cjs');
|
|
9
|
+
var getTronGasEstimation = require('../getTronGasEstimation/getTronGasEstimation.cjs');
|
|
10
10
|
|
|
11
|
-
|
|
11
|
+
/**
|
|
12
|
+
* 1 TRX = 1,000,000 SUN (atomic units)
|
|
13
|
+
*/
|
|
12
14
|
const SUN_PER_TRX = 1000000;
|
|
15
|
+
/**
|
|
16
|
+
* TronUiTransaction class for managing Tron transactions in the UI
|
|
17
|
+
*/
|
|
13
18
|
class TronUiTransaction {
|
|
14
|
-
constructor({ onSubmit, from,
|
|
19
|
+
constructor({ onSubmit, from, tronWeb }) {
|
|
15
20
|
this.chain = 'TRON';
|
|
16
21
|
this.data = undefined;
|
|
17
22
|
this.fee = { gas: undefined };
|
|
18
23
|
this.from = from;
|
|
19
24
|
this.onSubmit = onSubmit;
|
|
20
|
-
this.
|
|
25
|
+
this.tronWeb = tronWeb;
|
|
21
26
|
}
|
|
27
|
+
/**
|
|
28
|
+
* Fetch the estimated transaction fee
|
|
29
|
+
*/
|
|
22
30
|
fetchFee() {
|
|
23
31
|
return _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
24
|
-
if
|
|
32
|
+
// Skip if we already fetched
|
|
33
|
+
if (this.fee.gas !== undefined)
|
|
25
34
|
return;
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
35
|
+
// Default to 0 for most Tron transactions (free with bandwidth)
|
|
36
|
+
this.fee.gas = BigInt(0);
|
|
37
|
+
// Try to get a more accurate estimate if we have the data
|
|
38
|
+
if (!this.to || (!this.value && !this.nonNativeValue)) {
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
try {
|
|
42
|
+
const estimatedFee = yield getTronGasEstimation.getTronGasEstimation({
|
|
43
|
+
from: this.from,
|
|
44
|
+
nonNativeAddress: this.nonNativeAddress,
|
|
45
|
+
nonNativeValue: this.nonNativeValue,
|
|
46
|
+
to: this.to,
|
|
47
|
+
tronWeb: this.tronWeb,
|
|
48
|
+
value: this.value,
|
|
49
|
+
});
|
|
50
|
+
// Update with the actual estimate
|
|
51
|
+
this.fee.gas = estimatedFee;
|
|
52
|
+
}
|
|
53
|
+
catch (error) {
|
|
54
|
+
walletConnectorCore.logger.error('Failed to fetch Tron transaction fee:', error);
|
|
55
|
+
// Keep the default 0 we already set
|
|
56
|
+
}
|
|
34
57
|
});
|
|
35
58
|
}
|
|
59
|
+
/**
|
|
60
|
+
* Parse string input to bigint (for TRX)
|
|
61
|
+
*/
|
|
36
62
|
parse(input) {
|
|
37
63
|
const numericValue = parseFloat(input);
|
|
38
64
|
if (isNaN(numericValue))
|
|
39
65
|
return BigInt(0);
|
|
40
66
|
return BigInt(Math.floor(numericValue * SUN_PER_TRX));
|
|
41
67
|
}
|
|
68
|
+
/**
|
|
69
|
+
* Parse string input to bigint (for TRC20 tokens)
|
|
70
|
+
*/
|
|
42
71
|
parseNonNativeToken(input, decimals) {
|
|
43
72
|
const numericValue = parseFloat(input);
|
|
44
73
|
if (isNaN(numericValue))
|
|
45
74
|
return BigInt(0);
|
|
46
75
|
return BigInt(Math.floor(numericValue * Math.pow(10, decimals)));
|
|
47
76
|
}
|
|
77
|
+
/**
|
|
78
|
+
* Format bigint value to string (for TRX)
|
|
79
|
+
*/
|
|
48
80
|
format(value, options) {
|
|
49
81
|
var _a;
|
|
50
82
|
const trxValue = Number(value) / SUN_PER_TRX;
|
|
@@ -52,12 +84,18 @@ class TronUiTransaction {
|
|
|
52
84
|
precision: (_a = options === null || options === void 0 ? void 0 : options.precision) !== null && _a !== void 0 ? _a : 6,
|
|
53
85
|
});
|
|
54
86
|
}
|
|
87
|
+
/**
|
|
88
|
+
* Format bigint value to string (for TRC20 tokens)
|
|
89
|
+
*/
|
|
55
90
|
formatNonNativeToken(value, decimals) {
|
|
56
91
|
const tokenValue = Number(value) / Math.pow(10, decimals);
|
|
57
92
|
return utils.formatNumberText(String(tokenValue), {
|
|
58
93
|
precision: decimals > 6 ? 6 : decimals,
|
|
59
94
|
});
|
|
60
95
|
}
|
|
96
|
+
/**
|
|
97
|
+
* Submit the transaction
|
|
98
|
+
*/
|
|
61
99
|
submit() {
|
|
62
100
|
return _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
63
101
|
if (!this.to) {
|
|
@@ -69,23 +107,23 @@ class TronUiTransaction {
|
|
|
69
107
|
return this.onSubmit(this);
|
|
70
108
|
});
|
|
71
109
|
}
|
|
110
|
+
/**
|
|
111
|
+
* Get the balance for the from address
|
|
112
|
+
*/
|
|
72
113
|
getBalance() {
|
|
73
114
|
return _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
74
115
|
try {
|
|
75
|
-
const { tronWeb } = this.provider;
|
|
76
|
-
if (!tronWeb)
|
|
77
|
-
return BigInt(0);
|
|
78
116
|
if (this.nonNativeAddress) {
|
|
79
117
|
// Get TRC20 token balance
|
|
80
|
-
const contract = yield
|
|
118
|
+
const contract = yield this.tronWeb
|
|
119
|
+
.contract()
|
|
120
|
+
.at(this.nonNativeAddress);
|
|
81
121
|
const balance = yield contract.balanceOf(String(this.from)).call();
|
|
82
122
|
return BigInt(typeof balance === 'string' ? balance : balance.toString());
|
|
83
123
|
}
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
return BigInt(String(balance));
|
|
88
|
-
}
|
|
124
|
+
// Get native TRX balance
|
|
125
|
+
const balance = yield this.tronWeb.trx.getBalance(String(this.from));
|
|
126
|
+
return BigInt(String(balance));
|
|
89
127
|
}
|
|
90
128
|
catch (error) {
|
|
91
129
|
walletConnectorCore.logger.error('Error occurred while fetching TRON balance. Please try again or contact support.');
|
|
@@ -93,25 +131,28 @@ class TronUiTransaction {
|
|
|
93
131
|
}
|
|
94
132
|
});
|
|
95
133
|
}
|
|
134
|
+
/**
|
|
135
|
+
* Validate Tron address format
|
|
136
|
+
*/
|
|
96
137
|
validateAddressFormat(address) {
|
|
97
138
|
if (address === 'dyn_send_transaction.multiple_recipients') {
|
|
98
139
|
return true;
|
|
99
140
|
}
|
|
100
141
|
try {
|
|
101
|
-
const { tronWeb } = this.provider;
|
|
102
|
-
if (!tronWeb)
|
|
103
|
-
return false;
|
|
104
142
|
// TRON addresses start with 'T' and are 34 characters long
|
|
105
143
|
if (!address.startsWith('T') || address.length !== 34) {
|
|
106
144
|
return false;
|
|
107
145
|
}
|
|
108
146
|
// Use TronWeb's built-in address validation
|
|
109
|
-
return tronWeb.isAddress(address);
|
|
147
|
+
return this.tronWeb.isAddress(address);
|
|
110
148
|
}
|
|
111
149
|
catch (_a) {
|
|
112
150
|
return false;
|
|
113
151
|
}
|
|
114
152
|
}
|
|
153
|
+
/**
|
|
154
|
+
* Check if gas is sponsored
|
|
155
|
+
*/
|
|
115
156
|
isGasSponsored() {
|
|
116
157
|
return false;
|
|
117
158
|
}
|
|
@@ -1,14 +1,20 @@
|
|
|
1
|
+
import type { TronWeb } from 'tronweb';
|
|
1
2
|
import { IUITransaction, IUITransactionFormatOptions } from '@dynamic-labs/types';
|
|
2
|
-
|
|
3
|
+
/**
|
|
4
|
+
* 1 TRX = 1,000,000 SUN (atomic units)
|
|
5
|
+
*/
|
|
3
6
|
export declare const SUN_PER_TRX = 1000000;
|
|
4
7
|
type TronUiTransactionProps = {
|
|
5
8
|
/** TRON Address of the sender */
|
|
6
9
|
from: string;
|
|
7
|
-
/** The
|
|
8
|
-
|
|
10
|
+
/** The TronWeb instance for interacting with the network */
|
|
11
|
+
tronWeb: TronWeb;
|
|
9
12
|
/** The function to call when the transaction is submitted */
|
|
10
13
|
onSubmit: (transaction: TronUiTransaction) => Promise<string>;
|
|
11
14
|
};
|
|
15
|
+
/**
|
|
16
|
+
* TronUiTransaction class for managing Tron transactions in the UI
|
|
17
|
+
*/
|
|
12
18
|
export declare class TronUiTransaction implements IUITransaction {
|
|
13
19
|
to: string | undefined;
|
|
14
20
|
from: string;
|
|
@@ -24,16 +30,43 @@ export declare class TronUiTransaction implements IUITransaction {
|
|
|
24
30
|
nonNativeValue?: bigint;
|
|
25
31
|
nativePrice?: number;
|
|
26
32
|
private onSubmit;
|
|
27
|
-
private
|
|
28
|
-
constructor({ onSubmit, from,
|
|
33
|
+
private tronWeb;
|
|
34
|
+
constructor({ onSubmit, from, tronWeb }: TronUiTransactionProps);
|
|
35
|
+
/**
|
|
36
|
+
* Fetch the estimated transaction fee
|
|
37
|
+
*/
|
|
29
38
|
fetchFee(): Promise<void>;
|
|
39
|
+
/**
|
|
40
|
+
* Parse string input to bigint (for TRX)
|
|
41
|
+
*/
|
|
30
42
|
parse(input: string): bigint;
|
|
43
|
+
/**
|
|
44
|
+
* Parse string input to bigint (for TRC20 tokens)
|
|
45
|
+
*/
|
|
31
46
|
parseNonNativeToken(input: string, decimals: number): bigint;
|
|
47
|
+
/**
|
|
48
|
+
* Format bigint value to string (for TRX)
|
|
49
|
+
*/
|
|
32
50
|
format(value: bigint, options?: IUITransactionFormatOptions): string;
|
|
51
|
+
/**
|
|
52
|
+
* Format bigint value to string (for TRC20 tokens)
|
|
53
|
+
*/
|
|
33
54
|
formatNonNativeToken(value: bigint, decimals: number): string;
|
|
55
|
+
/**
|
|
56
|
+
* Submit the transaction
|
|
57
|
+
*/
|
|
34
58
|
submit(): Promise<string>;
|
|
59
|
+
/**
|
|
60
|
+
* Get the balance for the from address
|
|
61
|
+
*/
|
|
35
62
|
getBalance(): Promise<bigint>;
|
|
63
|
+
/**
|
|
64
|
+
* Validate Tron address format
|
|
65
|
+
*/
|
|
36
66
|
validateAddressFormat(address: string): boolean;
|
|
67
|
+
/**
|
|
68
|
+
* Check if gas is sponsored
|
|
69
|
+
*/
|
|
37
70
|
isGasSponsored(): boolean;
|
|
38
71
|
}
|
|
39
72
|
export {};
|
|
@@ -2,45 +2,77 @@
|
|
|
2
2
|
import { __awaiter } from '../../../_virtual/_tslib.js';
|
|
3
3
|
import { formatNumberText } from '@dynamic-labs/utils';
|
|
4
4
|
import { logger } from '@dynamic-labs/wallet-connector-core';
|
|
5
|
-
import { getTronGasEstimation } from '../getTronGasEstimation.js';
|
|
5
|
+
import { getTronGasEstimation } from '../getTronGasEstimation/getTronGasEstimation.js';
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
/**
|
|
8
|
+
* 1 TRX = 1,000,000 SUN (atomic units)
|
|
9
|
+
*/
|
|
8
10
|
const SUN_PER_TRX = 1000000;
|
|
11
|
+
/**
|
|
12
|
+
* TronUiTransaction class for managing Tron transactions in the UI
|
|
13
|
+
*/
|
|
9
14
|
class TronUiTransaction {
|
|
10
|
-
constructor({ onSubmit, from,
|
|
15
|
+
constructor({ onSubmit, from, tronWeb }) {
|
|
11
16
|
this.chain = 'TRON';
|
|
12
17
|
this.data = undefined;
|
|
13
18
|
this.fee = { gas: undefined };
|
|
14
19
|
this.from = from;
|
|
15
20
|
this.onSubmit = onSubmit;
|
|
16
|
-
this.
|
|
21
|
+
this.tronWeb = tronWeb;
|
|
17
22
|
}
|
|
23
|
+
/**
|
|
24
|
+
* Fetch the estimated transaction fee
|
|
25
|
+
*/
|
|
18
26
|
fetchFee() {
|
|
19
27
|
return __awaiter(this, void 0, void 0, function* () {
|
|
20
|
-
if
|
|
28
|
+
// Skip if we already fetched
|
|
29
|
+
if (this.fee.gas !== undefined)
|
|
21
30
|
return;
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
31
|
+
// Default to 0 for most Tron transactions (free with bandwidth)
|
|
32
|
+
this.fee.gas = BigInt(0);
|
|
33
|
+
// Try to get a more accurate estimate if we have the data
|
|
34
|
+
if (!this.to || (!this.value && !this.nonNativeValue)) {
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
try {
|
|
38
|
+
const estimatedFee = yield getTronGasEstimation({
|
|
39
|
+
from: this.from,
|
|
40
|
+
nonNativeAddress: this.nonNativeAddress,
|
|
41
|
+
nonNativeValue: this.nonNativeValue,
|
|
42
|
+
to: this.to,
|
|
43
|
+
tronWeb: this.tronWeb,
|
|
44
|
+
value: this.value,
|
|
45
|
+
});
|
|
46
|
+
// Update with the actual estimate
|
|
47
|
+
this.fee.gas = estimatedFee;
|
|
48
|
+
}
|
|
49
|
+
catch (error) {
|
|
50
|
+
logger.error('Failed to fetch Tron transaction fee:', error);
|
|
51
|
+
// Keep the default 0 we already set
|
|
52
|
+
}
|
|
30
53
|
});
|
|
31
54
|
}
|
|
55
|
+
/**
|
|
56
|
+
* Parse string input to bigint (for TRX)
|
|
57
|
+
*/
|
|
32
58
|
parse(input) {
|
|
33
59
|
const numericValue = parseFloat(input);
|
|
34
60
|
if (isNaN(numericValue))
|
|
35
61
|
return BigInt(0);
|
|
36
62
|
return BigInt(Math.floor(numericValue * SUN_PER_TRX));
|
|
37
63
|
}
|
|
64
|
+
/**
|
|
65
|
+
* Parse string input to bigint (for TRC20 tokens)
|
|
66
|
+
*/
|
|
38
67
|
parseNonNativeToken(input, decimals) {
|
|
39
68
|
const numericValue = parseFloat(input);
|
|
40
69
|
if (isNaN(numericValue))
|
|
41
70
|
return BigInt(0);
|
|
42
71
|
return BigInt(Math.floor(numericValue * Math.pow(10, decimals)));
|
|
43
72
|
}
|
|
73
|
+
/**
|
|
74
|
+
* Format bigint value to string (for TRX)
|
|
75
|
+
*/
|
|
44
76
|
format(value, options) {
|
|
45
77
|
var _a;
|
|
46
78
|
const trxValue = Number(value) / SUN_PER_TRX;
|
|
@@ -48,12 +80,18 @@ class TronUiTransaction {
|
|
|
48
80
|
precision: (_a = options === null || options === void 0 ? void 0 : options.precision) !== null && _a !== void 0 ? _a : 6,
|
|
49
81
|
});
|
|
50
82
|
}
|
|
83
|
+
/**
|
|
84
|
+
* Format bigint value to string (for TRC20 tokens)
|
|
85
|
+
*/
|
|
51
86
|
formatNonNativeToken(value, decimals) {
|
|
52
87
|
const tokenValue = Number(value) / Math.pow(10, decimals);
|
|
53
88
|
return formatNumberText(String(tokenValue), {
|
|
54
89
|
precision: decimals > 6 ? 6 : decimals,
|
|
55
90
|
});
|
|
56
91
|
}
|
|
92
|
+
/**
|
|
93
|
+
* Submit the transaction
|
|
94
|
+
*/
|
|
57
95
|
submit() {
|
|
58
96
|
return __awaiter(this, void 0, void 0, function* () {
|
|
59
97
|
if (!this.to) {
|
|
@@ -65,23 +103,23 @@ class TronUiTransaction {
|
|
|
65
103
|
return this.onSubmit(this);
|
|
66
104
|
});
|
|
67
105
|
}
|
|
106
|
+
/**
|
|
107
|
+
* Get the balance for the from address
|
|
108
|
+
*/
|
|
68
109
|
getBalance() {
|
|
69
110
|
return __awaiter(this, void 0, void 0, function* () {
|
|
70
111
|
try {
|
|
71
|
-
const { tronWeb } = this.provider;
|
|
72
|
-
if (!tronWeb)
|
|
73
|
-
return BigInt(0);
|
|
74
112
|
if (this.nonNativeAddress) {
|
|
75
113
|
// Get TRC20 token balance
|
|
76
|
-
const contract = yield
|
|
114
|
+
const contract = yield this.tronWeb
|
|
115
|
+
.contract()
|
|
116
|
+
.at(this.nonNativeAddress);
|
|
77
117
|
const balance = yield contract.balanceOf(String(this.from)).call();
|
|
78
118
|
return BigInt(typeof balance === 'string' ? balance : balance.toString());
|
|
79
119
|
}
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
return BigInt(String(balance));
|
|
84
|
-
}
|
|
120
|
+
// Get native TRX balance
|
|
121
|
+
const balance = yield this.tronWeb.trx.getBalance(String(this.from));
|
|
122
|
+
return BigInt(String(balance));
|
|
85
123
|
}
|
|
86
124
|
catch (error) {
|
|
87
125
|
logger.error('Error occurred while fetching TRON balance. Please try again or contact support.');
|
|
@@ -89,25 +127,28 @@ class TronUiTransaction {
|
|
|
89
127
|
}
|
|
90
128
|
});
|
|
91
129
|
}
|
|
130
|
+
/**
|
|
131
|
+
* Validate Tron address format
|
|
132
|
+
*/
|
|
92
133
|
validateAddressFormat(address) {
|
|
93
134
|
if (address === 'dyn_send_transaction.multiple_recipients') {
|
|
94
135
|
return true;
|
|
95
136
|
}
|
|
96
137
|
try {
|
|
97
|
-
const { tronWeb } = this.provider;
|
|
98
|
-
if (!tronWeb)
|
|
99
|
-
return false;
|
|
100
138
|
// TRON addresses start with 'T' and are 34 characters long
|
|
101
139
|
if (!address.startsWith('T') || address.length !== 34) {
|
|
102
140
|
return false;
|
|
103
141
|
}
|
|
104
142
|
// Use TronWeb's built-in address validation
|
|
105
|
-
return tronWeb.isAddress(address);
|
|
143
|
+
return this.tronWeb.isAddress(address);
|
|
106
144
|
}
|
|
107
145
|
catch (_a) {
|
|
108
146
|
return false;
|
|
109
147
|
}
|
|
110
148
|
}
|
|
149
|
+
/**
|
|
150
|
+
* Check if gas is sponsored
|
|
151
|
+
*/
|
|
111
152
|
isGasSponsored() {
|
|
112
153
|
return false;
|
|
113
154
|
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Utility functions for Tron network operations
|
|
8
|
+
*/
|
|
9
|
+
/**
|
|
10
|
+
* Convert hex chainId to decimal format to match backend expectations
|
|
11
|
+
* @param chainId - The chainId in hex (0x...) or decimal format
|
|
12
|
+
* @returns The chainId in decimal format
|
|
13
|
+
*/
|
|
14
|
+
const convertChainIdToDecimal = (chainId) => chainId.startsWith('0x') ? parseInt(chainId, 16).toString() : chainId;
|
|
15
|
+
|
|
16
|
+
exports.convertChainIdToDecimal = convertChainIdToDecimal;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Utility functions for Tron network operations
|
|
3
|
+
*/
|
|
4
|
+
/**
|
|
5
|
+
* Convert hex chainId to decimal format to match backend expectations
|
|
6
|
+
* @param chainId - The chainId in hex (0x...) or decimal format
|
|
7
|
+
* @returns The chainId in decimal format
|
|
8
|
+
*/
|
|
9
|
+
export declare const convertChainIdToDecimal: (chainId: string) => string;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
/**
|
|
3
|
+
* Utility functions for Tron network operations
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* Convert hex chainId to decimal format to match backend expectations
|
|
7
|
+
* @param chainId - The chainId in hex (0x...) or decimal format
|
|
8
|
+
* @returns The chainId in decimal format
|
|
9
|
+
*/
|
|
10
|
+
const convertChainIdToDecimal = (chainId) => chainId.startsWith('0x') ? parseInt(chainId, 16).toString() : chainId;
|
|
11
|
+
|
|
12
|
+
export { convertChainIdToDecimal };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { convertChainIdToDecimal } from './convertChainIdToDecimal';
|
|
@@ -0,0 +1,162 @@
|
|
|
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
|
+
* Extract fee from transaction data
|
|
11
|
+
* @param transaction - The transaction object from TronWeb
|
|
12
|
+
* @returns The fee amount in SUN, or 0 if not found
|
|
13
|
+
*/
|
|
14
|
+
const extractFeeFromTransaction = (transaction) => {
|
|
15
|
+
var _a, _b, _c, _d, _e;
|
|
16
|
+
const transactionData = transaction;
|
|
17
|
+
return (((_e = (_d = (_c = (_b = (_a = transactionData.raw_data) === null || _a === void 0 ? void 0 : _a.contract) === null || _b === void 0 ? void 0 : _b[0]) === null || _c === void 0 ? void 0 : _c.parameter) === null || _d === void 0 ? void 0 : _d.value) === null || _e === void 0 ? void 0 : _e.fee_limit) || 0);
|
|
18
|
+
};
|
|
19
|
+
/**
|
|
20
|
+
* Check if user has available energy for TRC20 operations
|
|
21
|
+
* @param from - The user's address
|
|
22
|
+
* @param tronWeb - The TronWeb instance
|
|
23
|
+
* @returns True if user has unused energy, false otherwise
|
|
24
|
+
*/
|
|
25
|
+
const hasAvailableEnergy = (from, tronWeb) => _tslib.__awaiter(void 0, void 0, void 0, function* () {
|
|
26
|
+
const accountResources = yield tronWeb.trx.getAccountResources(from);
|
|
27
|
+
const energyLimit = (accountResources === null || accountResources === void 0 ? void 0 : accountResources.EnergyLimit) || 0;
|
|
28
|
+
const energyUsed = (accountResources === null || accountResources === void 0 ? void 0 : accountResources.EnergyUsed) || 0;
|
|
29
|
+
return energyUsed < energyLimit;
|
|
30
|
+
});
|
|
31
|
+
/**
|
|
32
|
+
* Check if user has available free bandwidth for TRX transfers
|
|
33
|
+
* @param from - The user's address
|
|
34
|
+
* @param tronWeb - The TronWeb instance
|
|
35
|
+
* @returns True if user has unused free bandwidth, false otherwise
|
|
36
|
+
*/
|
|
37
|
+
const hasAvailableBandwidth = (from, tronWeb) => _tslib.__awaiter(void 0, void 0, void 0, function* () {
|
|
38
|
+
const accountResources = yield tronWeb.trx.getAccountResources(from);
|
|
39
|
+
const freeNetLimit = (accountResources === null || accountResources === void 0 ? void 0 : accountResources.freeNetLimit) || 600;
|
|
40
|
+
const freeNetUsed = (accountResources === null || accountResources === void 0 ? void 0 : accountResources.freeNetUsed) || 0;
|
|
41
|
+
return freeNetUsed < freeNetLimit;
|
|
42
|
+
});
|
|
43
|
+
/**
|
|
44
|
+
* Estimate TRC20 token transfer fees
|
|
45
|
+
* @param from - The sender's address
|
|
46
|
+
* @param to - The recipient's address
|
|
47
|
+
* @param nonNativeAddress - The TRC20 token contract address
|
|
48
|
+
* @param nonNativeValue - The amount of tokens to transfer
|
|
49
|
+
* @param tronWeb - The TronWeb instance
|
|
50
|
+
* @returns The estimated fee in SUN (0 if user has energy, actual fee or fallback otherwise)
|
|
51
|
+
*/
|
|
52
|
+
const estimateTRC20Fees = (from, to, nonNativeAddress, nonNativeValue, tronWeb) => _tslib.__awaiter(void 0, void 0, void 0, function* () {
|
|
53
|
+
try {
|
|
54
|
+
// Check if user has energy available
|
|
55
|
+
if (yield hasAvailableEnergy(from, tronWeb)) {
|
|
56
|
+
return BigInt(0);
|
|
57
|
+
}
|
|
58
|
+
// Try to get actual fee from contract
|
|
59
|
+
try {
|
|
60
|
+
const contract = yield tronWeb.contract().at(nonNativeAddress);
|
|
61
|
+
const transaction = yield contract
|
|
62
|
+
.transfer(to, nonNativeValue.toString())
|
|
63
|
+
.send({
|
|
64
|
+
feeLimit: 100000000, // 100 TRX max fee limit
|
|
65
|
+
from,
|
|
66
|
+
});
|
|
67
|
+
const fee = extractFeeFromTransaction(transaction);
|
|
68
|
+
return BigInt(fee);
|
|
69
|
+
}
|
|
70
|
+
catch (contractError) {
|
|
71
|
+
walletConnectorCore.logger.debug('TRC20 contract estimation failed, using conservative estimate:', contractError);
|
|
72
|
+
return BigInt(1000000); // ~1 TRX max
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
catch (error) {
|
|
76
|
+
walletConnectorCore.logger.debug('TRC20 estimation failed, defaulting to conservative estimate:', error);
|
|
77
|
+
return BigInt(1000000); // ~1 TRX max
|
|
78
|
+
}
|
|
79
|
+
});
|
|
80
|
+
/**
|
|
81
|
+
* Estimate native TRX transfer fees
|
|
82
|
+
* @param from - The sender's address
|
|
83
|
+
* @param to - The recipient's address
|
|
84
|
+
* @param value - The amount of TRX to transfer (in SUN)
|
|
85
|
+
* @param tronWeb - The TronWeb instance
|
|
86
|
+
* @returns The estimated fee in SUN (0 if user has bandwidth, actual fee or fallback otherwise)
|
|
87
|
+
*/
|
|
88
|
+
const estimateTRXFees = (from, to, value, tronWeb) => _tslib.__awaiter(void 0, void 0, void 0, function* () {
|
|
89
|
+
try {
|
|
90
|
+
// Check if user has free bandwidth
|
|
91
|
+
if (yield hasAvailableBandwidth(from, tronWeb)) {
|
|
92
|
+
return BigInt(0);
|
|
93
|
+
}
|
|
94
|
+
// Try to get actual fee from transaction
|
|
95
|
+
try {
|
|
96
|
+
const transaction = yield tronWeb.trx.sendTrx(to, Number(value), {
|
|
97
|
+
address: from,
|
|
98
|
+
});
|
|
99
|
+
const fee = extractFeeFromTransaction(transaction);
|
|
100
|
+
return BigInt(fee);
|
|
101
|
+
}
|
|
102
|
+
catch (txError) {
|
|
103
|
+
walletConnectorCore.logger.debug('TRX transaction estimation failed, using conservative estimate:', txError);
|
|
104
|
+
return BigInt(100000); // ~0.1 TRX max
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
catch (error) {
|
|
108
|
+
walletConnectorCore.logger.debug('TRX estimation failed, defaulting to conservative estimate:', error);
|
|
109
|
+
return BigInt(100000); // ~0.1 TRX max
|
|
110
|
+
}
|
|
111
|
+
});
|
|
112
|
+
/**
|
|
113
|
+
* Estimate gas fees for Tron transactions
|
|
114
|
+
*
|
|
115
|
+
* This function estimates the transaction fees for both native TRX transfers and TRC20 token transfers.
|
|
116
|
+
* It checks if the user has available energy (for TRC20) or bandwidth (for TRX) to determine if fees are needed.
|
|
117
|
+
*
|
|
118
|
+
* @param params - The gas estimation parameters
|
|
119
|
+
* @returns The estimated fee in SUN (smallest unit of TRX)
|
|
120
|
+
*
|
|
121
|
+
* @example
|
|
122
|
+
* ```typescript
|
|
123
|
+
* // Estimate fee for TRX transfer
|
|
124
|
+
* const fee = await getTronGasEstimation({
|
|
125
|
+
* from: 'TGkJYQZp1tGrNmbv7NbqcMh1j5stixBqrV',
|
|
126
|
+
* to: 'TRecipient123456789',
|
|
127
|
+
* value: BigInt(1000000), // 1 TRX in SUN
|
|
128
|
+
* tronWeb: tronWebInstance
|
|
129
|
+
* });
|
|
130
|
+
*
|
|
131
|
+
* // Estimate fee for TRC20 transfer
|
|
132
|
+
* const tokenFee = await getTronGasEstimation({
|
|
133
|
+
* from: 'TGkJYQZp1tGrNmbv7NbqcMh1j5stixBqrV',
|
|
134
|
+
* to: 'TRecipient123456789',
|
|
135
|
+
* nonNativeAddress: 'TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t',
|
|
136
|
+
* nonNativeValue: BigInt(1000000),
|
|
137
|
+
* tronWeb: tronWebInstance
|
|
138
|
+
* });
|
|
139
|
+
* ```
|
|
140
|
+
*/
|
|
141
|
+
const getTronGasEstimation = (params) => _tslib.__awaiter(void 0, void 0, void 0, function* () {
|
|
142
|
+
const { from, to, value, nonNativeAddress, nonNativeValue, tronWeb } = params;
|
|
143
|
+
// Return 0 if we don't have enough info
|
|
144
|
+
if (!to) {
|
|
145
|
+
return BigInt(0);
|
|
146
|
+
}
|
|
147
|
+
try {
|
|
148
|
+
if (nonNativeAddress && nonNativeValue) {
|
|
149
|
+
return yield estimateTRC20Fees(from, to, nonNativeAddress, nonNativeValue, tronWeb);
|
|
150
|
+
}
|
|
151
|
+
if (value) {
|
|
152
|
+
return yield estimateTRXFees(from, to, value, tronWeb);
|
|
153
|
+
}
|
|
154
|
+
return BigInt(0);
|
|
155
|
+
}
|
|
156
|
+
catch (error) {
|
|
157
|
+
walletConnectorCore.logger.error('Failed to estimate Tron gas:', error);
|
|
158
|
+
return BigInt(0);
|
|
159
|
+
}
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
exports.getTronGasEstimation = getTronGasEstimation;
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import type { TronWeb } from 'tronweb';
|
|
2
|
+
/**
|
|
3
|
+
* Parameters for gas estimation
|
|
4
|
+
*/
|
|
5
|
+
type GasEstimationParams = {
|
|
6
|
+
/** The sender's address */
|
|
7
|
+
from: string;
|
|
8
|
+
/** The recipient's address */
|
|
9
|
+
to?: string;
|
|
10
|
+
/** The amount of TRX to transfer (in SUN) */
|
|
11
|
+
value?: bigint;
|
|
12
|
+
/** The TRC20 token contract address */
|
|
13
|
+
nonNativeAddress?: string;
|
|
14
|
+
/** The amount of tokens to transfer */
|
|
15
|
+
nonNativeValue?: bigint;
|
|
16
|
+
/** The TronWeb instance */
|
|
17
|
+
tronWeb: TronWeb;
|
|
18
|
+
};
|
|
19
|
+
/**
|
|
20
|
+
* Estimate gas fees for Tron transactions
|
|
21
|
+
*
|
|
22
|
+
* This function estimates the transaction fees for both native TRX transfers and TRC20 token transfers.
|
|
23
|
+
* It checks if the user has available energy (for TRC20) or bandwidth (for TRX) to determine if fees are needed.
|
|
24
|
+
*
|
|
25
|
+
* @param params - The gas estimation parameters
|
|
26
|
+
* @returns The estimated fee in SUN (smallest unit of TRX)
|
|
27
|
+
*
|
|
28
|
+
* @example
|
|
29
|
+
* ```typescript
|
|
30
|
+
* // Estimate fee for TRX transfer
|
|
31
|
+
* const fee = await getTronGasEstimation({
|
|
32
|
+
* from: 'TGkJYQZp1tGrNmbv7NbqcMh1j5stixBqrV',
|
|
33
|
+
* to: 'TRecipient123456789',
|
|
34
|
+
* value: BigInt(1000000), // 1 TRX in SUN
|
|
35
|
+
* tronWeb: tronWebInstance
|
|
36
|
+
* });
|
|
37
|
+
*
|
|
38
|
+
* // Estimate fee for TRC20 transfer
|
|
39
|
+
* const tokenFee = await getTronGasEstimation({
|
|
40
|
+
* from: 'TGkJYQZp1tGrNmbv7NbqcMh1j5stixBqrV',
|
|
41
|
+
* to: 'TRecipient123456789',
|
|
42
|
+
* nonNativeAddress: 'TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t',
|
|
43
|
+
* nonNativeValue: BigInt(1000000),
|
|
44
|
+
* tronWeb: tronWebInstance
|
|
45
|
+
* });
|
|
46
|
+
* ```
|
|
47
|
+
*/
|
|
48
|
+
export declare const getTronGasEstimation: (params: GasEstimationParams) => Promise<bigint>;
|
|
49
|
+
export {};
|