@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.
- package/CHANGELOG.md +26 -0
- package/_virtual/_tslib.cjs +36 -0
- package/_virtual/_tslib.js +32 -0
- package/package.cjs +1 -1
- package/package.js +1 -1
- package/package.json +6 -3
- package/src/connectors/BitgetTronConnector/BitgetTronConnector.cjs +20 -0
- package/src/connectors/BitgetTronConnector/BitgetTronConnector.d.ts +7 -0
- package/src/connectors/BitgetTronConnector/BitgetTronConnector.js +16 -0
- package/src/connectors/BitgetTronConnector/index.d.ts +1 -0
- package/src/connectors/BybitTronConnector/BybitTronConnector.cjs +20 -0
- package/src/connectors/BybitTronConnector/BybitTronConnector.d.ts +7 -0
- package/src/connectors/BybitTronConnector/BybitTronConnector.js +16 -0
- package/src/connectors/BybitTronConnector/index.d.ts +1 -0
- package/src/connectors/OKXTronConnector/OKXTronConnector.cjs +20 -0
- package/src/connectors/OKXTronConnector/OKXTronConnector.d.ts +7 -0
- package/src/connectors/OKXTronConnector/OKXTronConnector.js +16 -0
- package/src/connectors/OKXTronConnector/index.d.ts +1 -0
- package/src/connectors/TronWalletConnector/TronWalletConnector.cjs +255 -0
- package/src/connectors/TronWalletConnector/TronWalletConnector.d.ts +42 -0
- package/src/connectors/TronWalletConnector/TronWalletConnector.js +251 -0
- package/src/connectors/TronWalletConnector/index.d.ts +1 -0
- package/src/connectors/TrustTronConnector/TrustTronConnector.cjs +20 -0
- package/src/connectors/TrustTronConnector/TrustTronConnector.d.ts +7 -0
- package/src/connectors/TrustTronConnector/TrustTronConnector.js +16 -0
- package/src/connectors/TrustTronConnector/index.d.ts +1 -0
- package/src/index.cjs +18 -0
- package/src/index.d.ts +6 -1
- package/src/index.js +14 -0
- package/src/types.d.ts +13 -2
- package/src/utils/TronUiTransaction/TronUiTransaction.cjs +121 -0
- package/src/utils/TronUiTransaction/TronUiTransaction.d.ts +39 -0
- package/src/utils/TronUiTransaction/TronUiTransaction.js +116 -0
- package/src/utils/TronUiTransaction/index.d.ts +1 -0
- package/src/utils/getDefaultTronNetworks.cjs +60 -0
- package/src/utils/getDefaultTronNetworks.js +53 -0
- package/src/utils/getTronGasEstimation.cjs +136 -0
- package/src/utils/getTronGasEstimation.d.ts +47 -0
- package/src/utils/getTronGasEstimation.js +129 -0
- package/src/utils/index.d.ts +1 -0
- package/src/utils/provider.cjs +59 -0
- package/src/utils/provider.js +53 -0
- package/src/wallet/TronWallet.cjs +106 -0
- package/src/wallet/TronWallet.d.ts +22 -0
- package/src/wallet/TronWallet.js +102 -0
- package/src/wallet/index.d.ts +2 -0
- package/src/wallet/isTronWallet/index.d.ts +1 -0
- package/src/wallet/isTronWallet/isTronWallet.cjs +8 -0
- package/src/wallet/isTronWallet/isTronWallet.d.ts +3 -0
- package/src/wallet/isTronWallet/isTronWallet.js +4 -0
|
@@ -0,0 +1,60 @@
|
|
|
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;
|
|
@@ -0,0 +1,53 @@
|
|
|
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 };
|
|
@@ -0,0 +1,136 @@
|
|
|
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;
|
|
@@ -0,0 +1,47 @@
|
|
|
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>;
|
|
@@ -0,0 +1,129 @@
|
|
|
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 };
|
package/src/utils/index.d.ts
CHANGED
|
@@ -0,0 +1,59 @@
|
|
|
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;
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
/**
|
|
3
|
+
* Asserts that a provider is available and throws an error if not
|
|
4
|
+
* @param provider - The provider to validate
|
|
5
|
+
* @param providerName - Human-readable name for error messages
|
|
6
|
+
* @returns The validated provider
|
|
7
|
+
* @throws {Error} If provider is not available
|
|
8
|
+
*/
|
|
9
|
+
const assertProvider = (provider, providerName) => {
|
|
10
|
+
if (!provider) {
|
|
11
|
+
throw new Error(`${providerName} provider not available`);
|
|
12
|
+
}
|
|
13
|
+
return provider;
|
|
14
|
+
};
|
|
15
|
+
/**
|
|
16
|
+
* Gets the current address from a Tron provider using various methods
|
|
17
|
+
* @param provider - The Tron provider
|
|
18
|
+
* @returns The current address or undefined if not available
|
|
19
|
+
*/
|
|
20
|
+
const getCurrentAddress = (provider) => {
|
|
21
|
+
var _a, _b, _c, _d;
|
|
22
|
+
// Try different ways to get the address
|
|
23
|
+
if (provider.address) {
|
|
24
|
+
return provider.address;
|
|
25
|
+
}
|
|
26
|
+
if ((_b = (_a = provider.tronWeb) === null || _a === void 0 ? void 0 : _a.defaultAddress) === null || _b === void 0 ? void 0 : _b.base58) {
|
|
27
|
+
return provider.tronWeb.defaultAddress.base58;
|
|
28
|
+
}
|
|
29
|
+
if (typeof ((_c = provider.tronWeb) === null || _c === void 0 ? void 0 : _c.defaultAddress) === 'string') {
|
|
30
|
+
return provider.tronWeb.defaultAddress;
|
|
31
|
+
}
|
|
32
|
+
// Check global tronWeb
|
|
33
|
+
if (typeof window !== 'undefined' && ((_d = window.tronWeb) === null || _d === void 0 ? void 0 : _d.defaultAddress)) {
|
|
34
|
+
if (typeof window.tronWeb.defaultAddress === 'string') {
|
|
35
|
+
return window.tronWeb.defaultAddress;
|
|
36
|
+
}
|
|
37
|
+
if (window.tronWeb.defaultAddress.base58) {
|
|
38
|
+
return window.tronWeb.defaultAddress.base58;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
return undefined;
|
|
42
|
+
};
|
|
43
|
+
/**
|
|
44
|
+
* Checks if a Tron provider is ready and connected
|
|
45
|
+
* @param provider - The Tron provider
|
|
46
|
+
* @returns True if the provider is ready and connected
|
|
47
|
+
*/
|
|
48
|
+
const isProviderReady = (provider) => {
|
|
49
|
+
const hasAddress = getCurrentAddress(provider);
|
|
50
|
+
return Boolean(provider.ready !== false && (provider.isConnected || hasAddress));
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
export { assertProvider, getCurrentAddress, isProviderReady };
|
|
@@ -0,0 +1,106 @@
|
|
|
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 provider = require('../utils/provider.cjs');
|
|
10
|
+
|
|
11
|
+
class TronWallet extends walletConnectorCore.Wallet {
|
|
12
|
+
constructor(props) {
|
|
13
|
+
super(props);
|
|
14
|
+
}
|
|
15
|
+
getProvider() {
|
|
16
|
+
return this._connector.getProvider();
|
|
17
|
+
}
|
|
18
|
+
getTronWeb() {
|
|
19
|
+
var _a;
|
|
20
|
+
return (_a = this.getProvider()) === null || _a === void 0 ? void 0 : _a.tronWeb;
|
|
21
|
+
}
|
|
22
|
+
getAddress() {
|
|
23
|
+
return _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
24
|
+
const provider$1 = provider.assertProvider(this.getProvider(), 'Tron');
|
|
25
|
+
const address = provider.getCurrentAddress(provider$1);
|
|
26
|
+
if (!address)
|
|
27
|
+
throw new utils.DynamicError('No address available');
|
|
28
|
+
return address;
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
getNetwork() {
|
|
32
|
+
return _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
33
|
+
return this._connector.getNetwork();
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
signMessage(message) {
|
|
37
|
+
return _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
38
|
+
const messageString = typeof message === 'string' ? message : new TextDecoder().decode(message);
|
|
39
|
+
const signature = yield this._connector.signMessage(messageString);
|
|
40
|
+
if (!signature) {
|
|
41
|
+
throw new utils.DynamicError('Failed to sign message with signMessageV2');
|
|
42
|
+
}
|
|
43
|
+
return signature;
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
getBalance(address) {
|
|
47
|
+
return _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
48
|
+
const targetAddress = address || (yield this.getAddress());
|
|
49
|
+
return this._connector.getBalance(targetAddress);
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
sendBalance() {
|
|
53
|
+
return _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
54
|
+
throw new utils.DynamicError('sendBalance not implemented. Use sendTrx or sendToken.');
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
sendTrx(to, amount, options) {
|
|
58
|
+
return _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
59
|
+
var _a, _b;
|
|
60
|
+
const provider$1 = provider.assertProvider(this.getProvider(), 'Tron');
|
|
61
|
+
if (!((_b = (_a = provider$1.tronWeb) === null || _a === void 0 ? void 0 : _a.trx) === null || _b === void 0 ? void 0 : _b.sendTransaction)) {
|
|
62
|
+
throw new utils.DynamicError('TRX transfer not supported');
|
|
63
|
+
}
|
|
64
|
+
const fromAddress = (options === null || options === void 0 ? void 0 : options.from) || (yield this.getAddress());
|
|
65
|
+
const amountInSun = provider$1.tronWeb.toSun(amount);
|
|
66
|
+
return provider$1.tronWeb.trx.sendTransaction(to, Number(amountInSun), {
|
|
67
|
+
address: fromAddress,
|
|
68
|
+
});
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
getTokenBalance(tokenId, address) {
|
|
72
|
+
return _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
73
|
+
var _a;
|
|
74
|
+
const provider$1 = provider.assertProvider(this.getProvider(), 'Tron');
|
|
75
|
+
const targetAddress = address || (yield this.getAddress());
|
|
76
|
+
if (!((_a = provider$1.tronWeb) === null || _a === void 0 ? void 0 : _a.contract)) {
|
|
77
|
+
throw new utils.DynamicError('TRC20 token balance not supported by this wallet');
|
|
78
|
+
}
|
|
79
|
+
try {
|
|
80
|
+
const contract = yield provider$1.tronWeb.contract().at(tokenId);
|
|
81
|
+
const balance = yield contract.balanceOf(targetAddress).call();
|
|
82
|
+
return typeof balance === 'string'
|
|
83
|
+
? parseInt(balance, 10)
|
|
84
|
+
: Number(balance);
|
|
85
|
+
}
|
|
86
|
+
catch (error) {
|
|
87
|
+
throw new utils.DynamicError(`TRC20 token balance query failed: ${error}`);
|
|
88
|
+
}
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
getNetworkDetails() {
|
|
92
|
+
return _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
93
|
+
const provider$1 = provider.assertProvider(this.getProvider(), 'Tron');
|
|
94
|
+
if (provider$1.getNetwork) {
|
|
95
|
+
return provider$1.getNetwork();
|
|
96
|
+
}
|
|
97
|
+
const chainId = yield this._connector.getNetwork();
|
|
98
|
+
return {
|
|
99
|
+
chainId: chainId || '0x2b6653dc', // mainnet default
|
|
100
|
+
name: chainId === '0x94a9059e' ? 'Nile Testnet' : 'Mainnet',
|
|
101
|
+
};
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
exports.TronWallet = TronWallet;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { TronWeb, Types } from 'tronweb';
|
|
2
|
+
import { Wallet, WalletProps } from '@dynamic-labs/wallet-connector-core';
|
|
3
|
+
import type { ITronProvider } from '../types';
|
|
4
|
+
import type { TronWalletConnector } from '../connectors/TronWalletConnector';
|
|
5
|
+
export declare class TronWallet extends Wallet<TronWalletConnector> {
|
|
6
|
+
constructor(props: WalletProps<TronWalletConnector>);
|
|
7
|
+
getProvider(): ITronProvider | undefined;
|
|
8
|
+
getTronWeb(): TronWeb | undefined;
|
|
9
|
+
getAddress(): Promise<string>;
|
|
10
|
+
getNetwork(): Promise<string | number | undefined>;
|
|
11
|
+
signMessage(message: string | Uint8Array): Promise<string>;
|
|
12
|
+
getBalance(address?: string): Promise<string | undefined>;
|
|
13
|
+
sendBalance(): Promise<string | undefined>;
|
|
14
|
+
sendTrx(to: string, amount: number, options?: {
|
|
15
|
+
from?: string;
|
|
16
|
+
}): Promise<Types.BroadcastReturn<Types.SignedTransaction>>;
|
|
17
|
+
getTokenBalance(tokenId: string, address?: string): Promise<number>;
|
|
18
|
+
getNetworkDetails(): Promise<{
|
|
19
|
+
chainId: string;
|
|
20
|
+
name: string;
|
|
21
|
+
}>;
|
|
22
|
+
}
|