@dynamic-labs/tron 4.40.0 → 4.40.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +13 -0
- package/README.md +264 -20
- package/package.cjs +1 -1
- package/package.js +1 -1
- package/package.json +8 -6
- package/src/connectors/TronWalletAdapterConnector/TronWalletAdapterConnector.cjs +436 -0
- package/src/connectors/TronWalletAdapterConnector/TronWalletAdapterConnector.d.ts +115 -0
- package/src/connectors/TronWalletAdapterConnector/TronWalletAdapterConnector.js +432 -0
- package/src/connectors/TronWalletAdapterConnector/index.d.ts +1 -0
- package/src/index.cjs +4 -13
- package/src/index.d.ts +5 -5
- package/src/index.js +3 -12
- package/src/types.d.ts +11 -181
- package/src/utils/TronUiTransaction/TronUiTransaction.cjs +67 -26
- package/src/utils/TronUiTransaction/TronUiTransaction.d.ts +38 -5
- package/src/utils/TronUiTransaction/TronUiTransaction.js +67 -26
- package/src/utils/convertChainIdToDecimal/convertChainIdToDecimal.cjs +16 -0
- package/src/utils/convertChainIdToDecimal/convertChainIdToDecimal.d.ts +9 -0
- package/src/utils/convertChainIdToDecimal/convertChainIdToDecimal.js +12 -0
- package/src/utils/convertChainIdToDecimal/index.d.ts +1 -0
- package/src/utils/fetchTronWalletAdapterConnectors/fetchTronWalletAdapterConnectors.cjs +80 -0
- package/src/utils/fetchTronWalletAdapterConnectors/fetchTronWalletAdapterConnectors.d.ts +35 -0
- package/src/utils/fetchTronWalletAdapterConnectors/fetchTronWalletAdapterConnectors.js +75 -0
- package/src/utils/fetchTronWalletAdapterConnectors/index.d.ts +1 -0
- package/src/utils/getTronGasEstimation/getTronGasEstimation.cjs +162 -0
- package/src/utils/getTronGasEstimation/getTronGasEstimation.d.ts +49 -0
- package/src/utils/getTronGasEstimation/getTronGasEstimation.js +158 -0
- package/src/utils/getTronGasEstimation/index.d.ts +1 -0
- package/src/utils/index.d.ts +1 -4
- package/src/wallet/TronWallet/TronWallet.cjs +124 -0
- package/src/wallet/TronWallet/TronWallet.d.ts +51 -0
- package/src/wallet/TronWallet/TronWallet.js +120 -0
- package/src/wallet/TronWallet/index.d.ts +1 -0
- package/src/connectors/BitgetTronConnector/BitgetTronConnector.cjs +0 -20
- package/src/connectors/BitgetTronConnector/BitgetTronConnector.d.ts +0 -7
- package/src/connectors/BitgetTronConnector/BitgetTronConnector.js +0 -16
- package/src/connectors/BitgetTronConnector/index.d.ts +0 -1
- package/src/connectors/BybitTronConnector/BybitTronConnector.d.ts +0 -7
- package/src/connectors/BybitTronConnector/index.d.ts +0 -1
- package/src/connectors/OKXTronConnector/OKXTronConnector.cjs +0 -20
- package/src/connectors/OKXTronConnector/OKXTronConnector.d.ts +0 -7
- package/src/connectors/OKXTronConnector/OKXTronConnector.js +0 -16
- package/src/connectors/OKXTronConnector/index.d.ts +0 -1
- package/src/connectors/TokenPocketTronConnector/TokenPocketTronConnector.cjs +0 -28
- package/src/connectors/TokenPocketTronConnector/TokenPocketTronConnector.d.ts +0 -7
- package/src/connectors/TokenPocketTronConnector/TokenPocketTronConnector.js +0 -24
- package/src/connectors/TokenPocketTronConnector/index.d.ts +0 -1
- package/src/connectors/TronWalletConnector/TronWalletConnector.cjs +0 -255
- package/src/connectors/TronWalletConnector/TronWalletConnector.d.ts +0 -42
- package/src/connectors/TronWalletConnector/TronWalletConnector.js +0 -251
- package/src/connectors/TronWalletConnector/index.d.ts +0 -1
- package/src/connectors/TrustTronConnector/TrustTronConnector.cjs +0 -31
- package/src/connectors/TrustTronConnector/TrustTronConnector.d.ts +0 -7
- package/src/connectors/TrustTronConnector/TrustTronConnector.js +0 -27
- package/src/connectors/TrustTronConnector/index.d.ts +0 -1
- package/src/utils/detectTronNetworkFromProvider.cjs +0 -67
- package/src/utils/detectTronNetworkFromProvider.d.ts +0 -30
- package/src/utils/detectTronNetworkFromProvider.js +0 -62
- package/src/utils/getDefaultTronNetworks.cjs +0 -60
- package/src/utils/getDefaultTronNetworks.d.ts +0 -9
- package/src/utils/getDefaultTronNetworks.js +0 -53
- package/src/utils/getTronGasEstimation.cjs +0 -136
- package/src/utils/getTronGasEstimation.d.ts +0 -47
- package/src/utils/getTronGasEstimation.js +0 -129
- package/src/utils/provider.cjs +0 -59
- package/src/utils/provider.d.ts +0 -24
- package/src/utils/provider.js +0 -53
- package/src/wallet/TronWallet.cjs +0 -106
- package/src/wallet/TronWallet.d.ts +0 -22
- package/src/wallet/TronWallet.js +0 -102
- package/src/wallet/index.d.ts +0 -2
package/src/types.d.ts
CHANGED
|
@@ -1,182 +1,12 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
tronWeb?: TronWeb;
|
|
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;
|
|
22
|
-
tronWeb?: TronWeb;
|
|
23
|
-
};
|
|
24
|
-
tokenpocket?: {
|
|
25
|
-
tron?: ITronProvider;
|
|
26
|
-
tronWeb?: TronWeb;
|
|
27
|
-
};
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
/**
|
|
31
|
-
* Interface that all Tron wallet providers must implement.
|
|
32
|
-
*
|
|
33
|
-
* This interface is designed to be flexible and work with different
|
|
34
|
-
* Tron wallet implementations like TronLink, TokenPocket, etc.
|
|
35
|
-
*/
|
|
36
|
-
export interface ITronProvider {
|
|
37
|
-
/** Whether the wallet is currently connected */
|
|
38
|
-
isConnected?: boolean;
|
|
39
|
-
/** Whether the wallet is ready */
|
|
40
|
-
ready?: boolean;
|
|
41
|
-
/** The current network chain ID */
|
|
42
|
-
chainId?: string;
|
|
43
|
-
/** The current network name */
|
|
44
|
-
network?: string;
|
|
45
|
-
/** The current wallet address */
|
|
46
|
-
address?: string;
|
|
47
|
-
/** TronWeb instance */
|
|
48
|
-
tronWeb?: TronWeb;
|
|
49
|
-
/** TronLink specific flag */
|
|
50
|
-
isTronLink?: boolean;
|
|
51
|
-
/**
|
|
52
|
-
* Request connection to the wallet
|
|
53
|
-
* @returns Promise that resolves to connection result
|
|
54
|
-
*/
|
|
55
|
-
request?(params: {
|
|
56
|
-
method: string;
|
|
57
|
-
params?: unknown;
|
|
58
|
-
}): Promise<unknown>;
|
|
59
|
-
/**
|
|
60
|
-
* Connect to the wallet (TronLink style)
|
|
61
|
-
* @returns Promise that resolves to connection result
|
|
62
|
-
*/
|
|
63
|
-
connect?(): Promise<TronConnectionResult>;
|
|
64
|
-
/**
|
|
65
|
-
* Disconnect from the wallet
|
|
66
|
-
* @returns Promise that resolves when disconnected
|
|
67
|
-
*/
|
|
68
|
-
disconnect?(): Promise<void>;
|
|
69
|
-
/**
|
|
70
|
-
* Sign a message
|
|
71
|
-
* @param message - Message to sign
|
|
72
|
-
* @returns Promise that resolves to the signature
|
|
73
|
-
*/
|
|
74
|
-
signMessage?(message: string): Promise<string>;
|
|
75
|
-
/**
|
|
76
|
-
* Sign a transaction
|
|
77
|
-
* @param transaction - Transaction to sign
|
|
78
|
-
* @returns Promise that resolves to the signed transaction
|
|
79
|
-
*/
|
|
80
|
-
signTransaction?(transaction: Types.Transaction): Promise<Types.SignedTransaction>;
|
|
81
|
-
/**
|
|
82
|
-
* Send a transaction
|
|
83
|
-
* @param transaction - Transaction to send
|
|
84
|
-
* @returns Promise that resolves to transaction result
|
|
85
|
-
*/
|
|
86
|
-
sendTransaction?(transaction: Types.SignedTransaction): Promise<Types.BroadcastReturn<Types.SignedTransaction>>;
|
|
87
|
-
/**
|
|
88
|
-
* Send TRX to an address
|
|
89
|
-
* @param to - Recipient address
|
|
90
|
-
* @param amount - Amount in TRX
|
|
91
|
-
* @param options - Optional parameters
|
|
92
|
-
* @returns Promise that resolves to transaction result
|
|
93
|
-
*/
|
|
94
|
-
sendTrx?(to: string, amount: number, options?: {
|
|
95
|
-
from?: string;
|
|
96
|
-
}): Promise<Types.BroadcastReturn<Types.SignedTransaction>>;
|
|
97
|
-
/**
|
|
98
|
-
* Send tokens to an address
|
|
99
|
-
* @param to - Recipient address
|
|
100
|
-
* @param amount - Amount of tokens
|
|
101
|
-
* @param tokenId - Token ID
|
|
102
|
-
* @param options - Optional parameters
|
|
103
|
-
* @returns Promise that resolves to transaction result
|
|
104
|
-
*/
|
|
105
|
-
sendToken?(to: string, amount: number, tokenId: string | number, options?: {
|
|
106
|
-
from?: string;
|
|
107
|
-
}): Promise<Types.BroadcastReturn<Types.SignedTransaction>>;
|
|
108
|
-
/**
|
|
109
|
-
* Get account balance
|
|
110
|
-
* @param address - Optional address (defaults to current account)
|
|
111
|
-
* @returns Promise that resolves to balance in SUN
|
|
112
|
-
*/
|
|
113
|
-
getBalance?(address?: string): Promise<number>;
|
|
114
|
-
/**
|
|
115
|
-
* Get token balance
|
|
116
|
-
* @param tokenId - Token ID
|
|
117
|
-
* @param address - Optional address (defaults to current account)
|
|
118
|
-
* @returns Promise that resolves to token balance
|
|
119
|
-
*/
|
|
120
|
-
getTokenBalance?(tokenId: string, address?: string): Promise<number>;
|
|
121
|
-
/**
|
|
122
|
-
* Get current network information
|
|
123
|
-
* @returns Promise that resolves to network info
|
|
124
|
-
*/
|
|
125
|
-
getNetwork?(): Promise<{
|
|
1
|
+
import type { Adapter, AdapterState, WalletReadyState } from '@tronweb3/tronwallet-abstract-adapter';
|
|
2
|
+
export type { Adapter, AdapterState, WalletReadyState };
|
|
3
|
+
export type TronAdapterEvents = {
|
|
4
|
+
connect: (address: string) => void;
|
|
5
|
+
disconnect: () => void;
|
|
6
|
+
accountsChanged: (address: string, preAddress?: string) => void;
|
|
7
|
+
chainChanged: (chainData: {
|
|
126
8
|
chainId: string;
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
* @param networkId - Network ID to switch to
|
|
132
|
-
* @returns Promise that resolves when network is switched
|
|
133
|
-
*/
|
|
134
|
-
switchNetwork?(networkId: string): Promise<void>;
|
|
135
|
-
/**
|
|
136
|
-
* Add network
|
|
137
|
-
* @param network - Network configuration to add
|
|
138
|
-
* @returns Promise that resolves when network is added
|
|
139
|
-
*/
|
|
140
|
-
addNetwork?(network: {
|
|
141
|
-
networkId: string;
|
|
142
|
-
name: string;
|
|
143
|
-
rpcUrl: string;
|
|
144
|
-
chainId?: string;
|
|
145
|
-
}): Promise<void>;
|
|
146
|
-
/**
|
|
147
|
-
* Listen for account changes
|
|
148
|
-
* @param callback - Callback function for account changes
|
|
149
|
-
*/
|
|
150
|
-
onAccountChange?(callback: (address: string) => void): void;
|
|
151
|
-
/**
|
|
152
|
-
* Listen for network changes
|
|
153
|
-
* @param callback - Callback function for network changes
|
|
154
|
-
*/
|
|
155
|
-
onNetworkChange?(callback: (network: {
|
|
156
|
-
chainId: string;
|
|
157
|
-
name: string;
|
|
158
|
-
}) => void): void;
|
|
159
|
-
/**
|
|
160
|
-
* Remove account change listener
|
|
161
|
-
* @param callback - Callback function to remove
|
|
162
|
-
*/
|
|
163
|
-
removeAccountChangeListener?(callback: (address: string) => void): void;
|
|
164
|
-
/**
|
|
165
|
-
* Remove network change listener
|
|
166
|
-
* @param callback - Callback function to remove
|
|
167
|
-
*/
|
|
168
|
-
removeNetworkChangeListener?(callback: (network: {
|
|
169
|
-
chainId: string;
|
|
170
|
-
name: string;
|
|
171
|
-
}) => void): void;
|
|
172
|
-
/** Additional properties that providers may implement */
|
|
173
|
-
[key: string]: unknown;
|
|
174
|
-
}
|
|
175
|
-
/**
|
|
176
|
-
* Result from wallet connection
|
|
177
|
-
*/
|
|
178
|
-
export type TronConnectionResult = {
|
|
179
|
-
address: string;
|
|
180
|
-
chainId?: string;
|
|
181
|
-
network?: string;
|
|
182
|
-
} | string | boolean;
|
|
9
|
+
}) => void;
|
|
10
|
+
readyStateChanged: (state: WalletReadyState) => void;
|
|
11
|
+
error: (error: Error) => void;
|
|
12
|
+
};
|
|
@@ -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';
|