@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
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
5
|
+
|
|
6
|
+
var walletBook = require('@dynamic-labs/wallet-book');
|
|
7
|
+
var TronWalletAdapterConnector = require('../../connectors/TronWalletAdapterConnector/TronWalletAdapterConnector.cjs');
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Checks if a wallet configuration supports TronWallet Adapters.
|
|
11
|
+
*
|
|
12
|
+
* A wallet is considered valid if it has:
|
|
13
|
+
* - An injected config for the 'tron' chain
|
|
14
|
+
* - The tronwallet-adapters feature included in wallet standard features
|
|
15
|
+
* - A valid provider ID
|
|
16
|
+
*
|
|
17
|
+
* @param wallet - The wallet configuration to check
|
|
18
|
+
* @returns true if the wallet supports TronWallet Adapters, false otherwise
|
|
19
|
+
*/
|
|
20
|
+
const isValidTronWalletAdapterWallet = (wallet) => {
|
|
21
|
+
var _a;
|
|
22
|
+
return Boolean((_a = wallet.injectedConfig) === null || _a === void 0 ? void 0 : _a.find((config) => {
|
|
23
|
+
var _a, _b;
|
|
24
|
+
return config.chain === 'tron' &&
|
|
25
|
+
Array.isArray((_a = config.walletStandard) === null || _a === void 0 ? void 0 : _a.features) &&
|
|
26
|
+
config.walletStandard.features.includes('tronwallet-adapters:') &&
|
|
27
|
+
((_b = config.walletStandard) === null || _b === void 0 ? void 0 : _b.providerId);
|
|
28
|
+
}));
|
|
29
|
+
};
|
|
30
|
+
/**
|
|
31
|
+
* Fetches all Tron wallet connectors that support TronWallet Adapters from the wallet book.
|
|
32
|
+
*
|
|
33
|
+
* This function scans the wallet book for wallets with Tron chain configuration
|
|
34
|
+
* and creates connector classes for each matching wallet.
|
|
35
|
+
*
|
|
36
|
+
* @param walletBook - The wallet book schema containing all wallet configurations
|
|
37
|
+
* @param tronNetworks - The Tron networks configuration
|
|
38
|
+
* @returns Array of wallet connector constructors for TronWallet Adapter enabled wallets
|
|
39
|
+
*
|
|
40
|
+
* @example
|
|
41
|
+
* ```typescript
|
|
42
|
+
* const tronConnectors = fetchTronWalletAdapterConnectors({ walletBook, tronNetworks });
|
|
43
|
+
* // Returns connectors for wallets like TronLink, TokenPocket, OKX, etc.
|
|
44
|
+
* ```
|
|
45
|
+
*/
|
|
46
|
+
const fetchTronWalletAdapterConnectors = ({ walletBook: walletBook$1, tronNetworks, }) => {
|
|
47
|
+
var _a;
|
|
48
|
+
return Object.entries((_a = walletBook$1 === null || walletBook$1 === void 0 ? void 0 : walletBook$1.wallets) !== null && _a !== void 0 ? _a : {})
|
|
49
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
50
|
+
.filter(([_, wallet]) => isValidTronWalletAdapterWallet(wallet))
|
|
51
|
+
.map(([key, wallet]) => {
|
|
52
|
+
const { shortName } = wallet;
|
|
53
|
+
const name = shortName || wallet.name;
|
|
54
|
+
return class extends TronWalletAdapterConnector.TronWalletAdapterConnector {
|
|
55
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
56
|
+
constructor(props) {
|
|
57
|
+
var _a;
|
|
58
|
+
super(Object.assign(Object.assign({}, props), { metadata: {
|
|
59
|
+
brandColor: undefined,
|
|
60
|
+
deepLinks: undefined,
|
|
61
|
+
downloadLinks: undefined,
|
|
62
|
+
groupKey: undefined,
|
|
63
|
+
icon: ((_a = wallet === null || wallet === void 0 ? void 0 : wallet.brand) === null || _a === void 0 ? void 0 : _a.spriteId)
|
|
64
|
+
? walletBook.renderTemplate('iconicUrl', wallet.brand.spriteId)
|
|
65
|
+
: '',
|
|
66
|
+
id: key,
|
|
67
|
+
name: name,
|
|
68
|
+
rdns: undefined,
|
|
69
|
+
supportedHardwareWallets: undefined,
|
|
70
|
+
walletLimitations: undefined,
|
|
71
|
+
}, overrideKey: key, tronNetworks, walletData: wallet }));
|
|
72
|
+
this.name = name;
|
|
73
|
+
this.overrideKey = key;
|
|
74
|
+
}
|
|
75
|
+
};
|
|
76
|
+
});
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
exports.fetchTronWalletAdapterConnectors = fetchTronWalletAdapterConnectors;
|
|
80
|
+
exports.isValidTronWalletAdapterWallet = isValidTronWalletAdapterWallet;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { WalletBookSchema } from '@dynamic-labs/wallet-book';
|
|
2
|
+
import { WalletConnectorConstructor } from '@dynamic-labs/wallet-connector-core';
|
|
3
|
+
import type { GenericNetwork } from '@dynamic-labs/types';
|
|
4
|
+
/**
|
|
5
|
+
* Checks if a wallet configuration supports TronWallet Adapters.
|
|
6
|
+
*
|
|
7
|
+
* A wallet is considered valid if it has:
|
|
8
|
+
* - An injected config for the 'tron' chain
|
|
9
|
+
* - The tronwallet-adapters feature included in wallet standard features
|
|
10
|
+
* - A valid provider ID
|
|
11
|
+
*
|
|
12
|
+
* @param wallet - The wallet configuration to check
|
|
13
|
+
* @returns true if the wallet supports TronWallet Adapters, false otherwise
|
|
14
|
+
*/
|
|
15
|
+
export declare const isValidTronWalletAdapterWallet: (wallet: WalletBookSchema['wallets'][string]) => boolean;
|
|
16
|
+
/**
|
|
17
|
+
* Fetches all Tron wallet connectors that support TronWallet Adapters from the wallet book.
|
|
18
|
+
*
|
|
19
|
+
* This function scans the wallet book for wallets with Tron chain configuration
|
|
20
|
+
* and creates connector classes for each matching wallet.
|
|
21
|
+
*
|
|
22
|
+
* @param walletBook - The wallet book schema containing all wallet configurations
|
|
23
|
+
* @param tronNetworks - The Tron networks configuration
|
|
24
|
+
* @returns Array of wallet connector constructors for TronWallet Adapter enabled wallets
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* ```typescript
|
|
28
|
+
* const tronConnectors = fetchTronWalletAdapterConnectors({ walletBook, tronNetworks });
|
|
29
|
+
* // Returns connectors for wallets like TronLink, TokenPocket, OKX, etc.
|
|
30
|
+
* ```
|
|
31
|
+
*/
|
|
32
|
+
export declare const fetchTronWalletAdapterConnectors: ({ walletBook, tronNetworks, }: {
|
|
33
|
+
walletBook: WalletBookSchema;
|
|
34
|
+
tronNetworks: GenericNetwork[];
|
|
35
|
+
}) => WalletConnectorConstructor[];
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
import { renderTemplate } from '@dynamic-labs/wallet-book';
|
|
3
|
+
import { TronWalletAdapterConnector } from '../../connectors/TronWalletAdapterConnector/TronWalletAdapterConnector.js';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Checks if a wallet configuration supports TronWallet Adapters.
|
|
7
|
+
*
|
|
8
|
+
* A wallet is considered valid if it has:
|
|
9
|
+
* - An injected config for the 'tron' chain
|
|
10
|
+
* - The tronwallet-adapters feature included in wallet standard features
|
|
11
|
+
* - A valid provider ID
|
|
12
|
+
*
|
|
13
|
+
* @param wallet - The wallet configuration to check
|
|
14
|
+
* @returns true if the wallet supports TronWallet Adapters, false otherwise
|
|
15
|
+
*/
|
|
16
|
+
const isValidTronWalletAdapterWallet = (wallet) => {
|
|
17
|
+
var _a;
|
|
18
|
+
return Boolean((_a = wallet.injectedConfig) === null || _a === void 0 ? void 0 : _a.find((config) => {
|
|
19
|
+
var _a, _b;
|
|
20
|
+
return config.chain === 'tron' &&
|
|
21
|
+
Array.isArray((_a = config.walletStandard) === null || _a === void 0 ? void 0 : _a.features) &&
|
|
22
|
+
config.walletStandard.features.includes('tronwallet-adapters:') &&
|
|
23
|
+
((_b = config.walletStandard) === null || _b === void 0 ? void 0 : _b.providerId);
|
|
24
|
+
}));
|
|
25
|
+
};
|
|
26
|
+
/**
|
|
27
|
+
* Fetches all Tron wallet connectors that support TronWallet Adapters from the wallet book.
|
|
28
|
+
*
|
|
29
|
+
* This function scans the wallet book for wallets with Tron chain configuration
|
|
30
|
+
* and creates connector classes for each matching wallet.
|
|
31
|
+
*
|
|
32
|
+
* @param walletBook - The wallet book schema containing all wallet configurations
|
|
33
|
+
* @param tronNetworks - The Tron networks configuration
|
|
34
|
+
* @returns Array of wallet connector constructors for TronWallet Adapter enabled wallets
|
|
35
|
+
*
|
|
36
|
+
* @example
|
|
37
|
+
* ```typescript
|
|
38
|
+
* const tronConnectors = fetchTronWalletAdapterConnectors({ walletBook, tronNetworks });
|
|
39
|
+
* // Returns connectors for wallets like TronLink, TokenPocket, OKX, etc.
|
|
40
|
+
* ```
|
|
41
|
+
*/
|
|
42
|
+
const fetchTronWalletAdapterConnectors = ({ walletBook, tronNetworks, }) => {
|
|
43
|
+
var _a;
|
|
44
|
+
return Object.entries((_a = walletBook === null || walletBook === void 0 ? void 0 : walletBook.wallets) !== null && _a !== void 0 ? _a : {})
|
|
45
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
46
|
+
.filter(([_, wallet]) => isValidTronWalletAdapterWallet(wallet))
|
|
47
|
+
.map(([key, wallet]) => {
|
|
48
|
+
const { shortName } = wallet;
|
|
49
|
+
const name = shortName || wallet.name;
|
|
50
|
+
return class extends TronWalletAdapterConnector {
|
|
51
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
52
|
+
constructor(props) {
|
|
53
|
+
var _a;
|
|
54
|
+
super(Object.assign(Object.assign({}, props), { metadata: {
|
|
55
|
+
brandColor: undefined,
|
|
56
|
+
deepLinks: undefined,
|
|
57
|
+
downloadLinks: undefined,
|
|
58
|
+
groupKey: undefined,
|
|
59
|
+
icon: ((_a = wallet === null || wallet === void 0 ? void 0 : wallet.brand) === null || _a === void 0 ? void 0 : _a.spriteId)
|
|
60
|
+
? renderTemplate('iconicUrl', wallet.brand.spriteId)
|
|
61
|
+
: '',
|
|
62
|
+
id: key,
|
|
63
|
+
name: name,
|
|
64
|
+
rdns: undefined,
|
|
65
|
+
supportedHardwareWallets: undefined,
|
|
66
|
+
walletLimitations: undefined,
|
|
67
|
+
}, overrideKey: key, tronNetworks, walletData: wallet }));
|
|
68
|
+
this.name = name;
|
|
69
|
+
this.overrideKey = key;
|
|
70
|
+
}
|
|
71
|
+
};
|
|
72
|
+
});
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
export { fetchTronWalletAdapterConnectors, isValidTronWalletAdapterWallet };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { fetchTronWalletAdapterConnectors } from './fetchTronWalletAdapterConnectors';
|
|
@@ -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 {};
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
import { __awaiter } from '../../../_virtual/_tslib.js';
|
|
3
|
+
import { logger } from '@dynamic-labs/wallet-connector-core';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Extract fee from transaction data
|
|
7
|
+
* @param transaction - The transaction object from TronWeb
|
|
8
|
+
* @returns The fee amount in SUN, or 0 if not found
|
|
9
|
+
*/
|
|
10
|
+
const extractFeeFromTransaction = (transaction) => {
|
|
11
|
+
var _a, _b, _c, _d, _e;
|
|
12
|
+
const transactionData = transaction;
|
|
13
|
+
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);
|
|
14
|
+
};
|
|
15
|
+
/**
|
|
16
|
+
* Check if user has available energy for TRC20 operations
|
|
17
|
+
* @param from - The user's address
|
|
18
|
+
* @param tronWeb - The TronWeb instance
|
|
19
|
+
* @returns True if user has unused energy, false otherwise
|
|
20
|
+
*/
|
|
21
|
+
const hasAvailableEnergy = (from, tronWeb) => __awaiter(void 0, void 0, void 0, function* () {
|
|
22
|
+
const accountResources = yield tronWeb.trx.getAccountResources(from);
|
|
23
|
+
const energyLimit = (accountResources === null || accountResources === void 0 ? void 0 : accountResources.EnergyLimit) || 0;
|
|
24
|
+
const energyUsed = (accountResources === null || accountResources === void 0 ? void 0 : accountResources.EnergyUsed) || 0;
|
|
25
|
+
return energyUsed < energyLimit;
|
|
26
|
+
});
|
|
27
|
+
/**
|
|
28
|
+
* Check if user has available free bandwidth for TRX transfers
|
|
29
|
+
* @param from - The user's address
|
|
30
|
+
* @param tronWeb - The TronWeb instance
|
|
31
|
+
* @returns True if user has unused free bandwidth, false otherwise
|
|
32
|
+
*/
|
|
33
|
+
const hasAvailableBandwidth = (from, tronWeb) => __awaiter(void 0, void 0, void 0, function* () {
|
|
34
|
+
const accountResources = yield tronWeb.trx.getAccountResources(from);
|
|
35
|
+
const freeNetLimit = (accountResources === null || accountResources === void 0 ? void 0 : accountResources.freeNetLimit) || 600;
|
|
36
|
+
const freeNetUsed = (accountResources === null || accountResources === void 0 ? void 0 : accountResources.freeNetUsed) || 0;
|
|
37
|
+
return freeNetUsed < freeNetLimit;
|
|
38
|
+
});
|
|
39
|
+
/**
|
|
40
|
+
* Estimate TRC20 token transfer fees
|
|
41
|
+
* @param from - The sender's address
|
|
42
|
+
* @param to - The recipient's address
|
|
43
|
+
* @param nonNativeAddress - The TRC20 token contract address
|
|
44
|
+
* @param nonNativeValue - The amount of tokens to transfer
|
|
45
|
+
* @param tronWeb - The TronWeb instance
|
|
46
|
+
* @returns The estimated fee in SUN (0 if user has energy, actual fee or fallback otherwise)
|
|
47
|
+
*/
|
|
48
|
+
const estimateTRC20Fees = (from, to, nonNativeAddress, nonNativeValue, tronWeb) => __awaiter(void 0, void 0, void 0, function* () {
|
|
49
|
+
try {
|
|
50
|
+
// Check if user has energy available
|
|
51
|
+
if (yield hasAvailableEnergy(from, tronWeb)) {
|
|
52
|
+
return BigInt(0);
|
|
53
|
+
}
|
|
54
|
+
// Try to get actual fee from contract
|
|
55
|
+
try {
|
|
56
|
+
const contract = yield tronWeb.contract().at(nonNativeAddress);
|
|
57
|
+
const transaction = yield contract
|
|
58
|
+
.transfer(to, nonNativeValue.toString())
|
|
59
|
+
.send({
|
|
60
|
+
feeLimit: 100000000, // 100 TRX max fee limit
|
|
61
|
+
from,
|
|
62
|
+
});
|
|
63
|
+
const fee = extractFeeFromTransaction(transaction);
|
|
64
|
+
return BigInt(fee);
|
|
65
|
+
}
|
|
66
|
+
catch (contractError) {
|
|
67
|
+
logger.debug('TRC20 contract estimation failed, using conservative estimate:', contractError);
|
|
68
|
+
return BigInt(1000000); // ~1 TRX max
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
catch (error) {
|
|
72
|
+
logger.debug('TRC20 estimation failed, defaulting to conservative estimate:', error);
|
|
73
|
+
return BigInt(1000000); // ~1 TRX max
|
|
74
|
+
}
|
|
75
|
+
});
|
|
76
|
+
/**
|
|
77
|
+
* Estimate native TRX transfer fees
|
|
78
|
+
* @param from - The sender's address
|
|
79
|
+
* @param to - The recipient's address
|
|
80
|
+
* @param value - The amount of TRX to transfer (in SUN)
|
|
81
|
+
* @param tronWeb - The TronWeb instance
|
|
82
|
+
* @returns The estimated fee in SUN (0 if user has bandwidth, actual fee or fallback otherwise)
|
|
83
|
+
*/
|
|
84
|
+
const estimateTRXFees = (from, to, value, tronWeb) => __awaiter(void 0, void 0, void 0, function* () {
|
|
85
|
+
try {
|
|
86
|
+
// Check if user has free bandwidth
|
|
87
|
+
if (yield hasAvailableBandwidth(from, tronWeb)) {
|
|
88
|
+
return BigInt(0);
|
|
89
|
+
}
|
|
90
|
+
// Try to get actual fee from transaction
|
|
91
|
+
try {
|
|
92
|
+
const transaction = yield tronWeb.trx.sendTrx(to, Number(value), {
|
|
93
|
+
address: from,
|
|
94
|
+
});
|
|
95
|
+
const fee = extractFeeFromTransaction(transaction);
|
|
96
|
+
return BigInt(fee);
|
|
97
|
+
}
|
|
98
|
+
catch (txError) {
|
|
99
|
+
logger.debug('TRX transaction estimation failed, using conservative estimate:', txError);
|
|
100
|
+
return BigInt(100000); // ~0.1 TRX max
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
catch (error) {
|
|
104
|
+
logger.debug('TRX estimation failed, defaulting to conservative estimate:', error);
|
|
105
|
+
return BigInt(100000); // ~0.1 TRX max
|
|
106
|
+
}
|
|
107
|
+
});
|
|
108
|
+
/**
|
|
109
|
+
* Estimate gas fees for Tron transactions
|
|
110
|
+
*
|
|
111
|
+
* This function estimates the transaction fees for both native TRX transfers and TRC20 token transfers.
|
|
112
|
+
* It checks if the user has available energy (for TRC20) or bandwidth (for TRX) to determine if fees are needed.
|
|
113
|
+
*
|
|
114
|
+
* @param params - The gas estimation parameters
|
|
115
|
+
* @returns The estimated fee in SUN (smallest unit of TRX)
|
|
116
|
+
*
|
|
117
|
+
* @example
|
|
118
|
+
* ```typescript
|
|
119
|
+
* // Estimate fee for TRX transfer
|
|
120
|
+
* const fee = await getTronGasEstimation({
|
|
121
|
+
* from: 'TGkJYQZp1tGrNmbv7NbqcMh1j5stixBqrV',
|
|
122
|
+
* to: 'TRecipient123456789',
|
|
123
|
+
* value: BigInt(1000000), // 1 TRX in SUN
|
|
124
|
+
* tronWeb: tronWebInstance
|
|
125
|
+
* });
|
|
126
|
+
*
|
|
127
|
+
* // Estimate fee for TRC20 transfer
|
|
128
|
+
* const tokenFee = await getTronGasEstimation({
|
|
129
|
+
* from: 'TGkJYQZp1tGrNmbv7NbqcMh1j5stixBqrV',
|
|
130
|
+
* to: 'TRecipient123456789',
|
|
131
|
+
* nonNativeAddress: 'TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t',
|
|
132
|
+
* nonNativeValue: BigInt(1000000),
|
|
133
|
+
* tronWeb: tronWebInstance
|
|
134
|
+
* });
|
|
135
|
+
* ```
|
|
136
|
+
*/
|
|
137
|
+
const getTronGasEstimation = (params) => __awaiter(void 0, void 0, void 0, function* () {
|
|
138
|
+
const { from, to, value, nonNativeAddress, nonNativeValue, tronWeb } = params;
|
|
139
|
+
// Return 0 if we don't have enough info
|
|
140
|
+
if (!to) {
|
|
141
|
+
return BigInt(0);
|
|
142
|
+
}
|
|
143
|
+
try {
|
|
144
|
+
if (nonNativeAddress && nonNativeValue) {
|
|
145
|
+
return yield estimateTRC20Fees(from, to, nonNativeAddress, nonNativeValue, tronWeb);
|
|
146
|
+
}
|
|
147
|
+
if (value) {
|
|
148
|
+
return yield estimateTRXFees(from, to, value, tronWeb);
|
|
149
|
+
}
|
|
150
|
+
return BigInt(0);
|
|
151
|
+
}
|
|
152
|
+
catch (error) {
|
|
153
|
+
logger.error('Failed to estimate Tron gas:', error);
|
|
154
|
+
return BigInt(0);
|
|
155
|
+
}
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
export { getTronGasEstimation };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { getTronGasEstimation } from './getTronGasEstimation';
|
package/src/utils/index.d.ts
CHANGED
|
@@ -1,4 +1 @@
|
|
|
1
|
-
export {
|
|
2
|
-
export { getDefaultTronNetworks } from './getDefaultTronNetworks';
|
|
3
|
-
export { TronUiTransaction, SUN_PER_TRX } from './TronUiTransaction';
|
|
4
|
-
export { detectTronNetworkFromProvider, DEFAULT_TRON_ENDPOINT_MAPPINGS, type TronNetworkConfig, type TronEndpointMapping, } from './detectTronNetworkFromProvider';
|
|
1
|
+
export { fetchTronWalletAdapterConnectors } from './fetchTronWalletAdapterConnectors/fetchTronWalletAdapterConnectors';
|