@dynamic-labs/tron 4.40.0 → 4.40.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +6 -0
- package/README.md +264 -20
- package/package.cjs +1 -1
- package/package.js +1 -1
- package/package.json +8 -6
- package/src/connectors/BitgetTronConnector/BitgetTronConnector.cjs +5 -5
- package/src/connectors/BitgetTronConnector/BitgetTronConnector.d.ts +4 -4
- package/src/connectors/BitgetTronConnector/BitgetTronConnector.js +5 -5
- package/src/connectors/OKXTronConnector/OKXTronConnector.cjs +5 -5
- package/src/connectors/OKXTronConnector/OKXTronConnector.d.ts +4 -4
- package/src/connectors/OKXTronConnector/OKXTronConnector.js +5 -5
- package/src/connectors/TokenPocketTronConnector/TokenPocketTronConnector.cjs +5 -13
- package/src/connectors/TokenPocketTronConnector/TokenPocketTronConnector.d.ts +4 -4
- package/src/connectors/TokenPocketTronConnector/TokenPocketTronConnector.js +5 -13
- package/src/connectors/TronWalletAdapterConnector/TronWalletAdapterConnector.cjs +304 -0
- package/src/connectors/TronWalletAdapterConnector/TronWalletAdapterConnector.d.ts +107 -0
- package/src/connectors/TronWalletAdapterConnector/TronWalletAdapterConnector.js +300 -0
- package/src/connectors/TronWalletAdapterConnector/index.d.ts +1 -0
- package/src/connectors/TrustTronConnector/TrustTronConnector.cjs +5 -16
- package/src/connectors/TrustTronConnector/TrustTronConnector.d.ts +4 -4
- package/src/connectors/TrustTronConnector/TrustTronConnector.js +5 -16
- package/src/index.cjs +2 -3
- package/src/index.d.ts +5 -4
- package/src/index.js +1 -2
- package/src/types.d.ts +11 -181
- package/src/utils/TronUiTransaction/TronUiTransaction.cjs +67 -26
- package/src/utils/TronUiTransaction/TronUiTransaction.d.ts +38 -5
- package/src/utils/TronUiTransaction/TronUiTransaction.js +67 -26
- package/src/utils/convertChainIdToDecimal/convertChainIdToDecimal.cjs +16 -0
- package/src/utils/convertChainIdToDecimal/convertChainIdToDecimal.d.ts +9 -0
- package/src/utils/convertChainIdToDecimal/convertChainIdToDecimal.js +12 -0
- package/src/utils/convertChainIdToDecimal/index.d.ts +1 -0
- package/src/utils/getTronGasEstimation/getTronGasEstimation.cjs +162 -0
- package/src/utils/getTronGasEstimation/getTronGasEstimation.d.ts +49 -0
- package/src/utils/getTronGasEstimation/getTronGasEstimation.js +158 -0
- package/src/utils/getTronGasEstimation/index.d.ts +1 -0
- package/src/wallet/TronWallet/TronWallet.cjs +124 -0
- package/src/wallet/TronWallet/TronWallet.d.ts +51 -0
- package/src/wallet/TronWallet/TronWallet.js +120 -0
- package/src/wallet/TronWallet/index.d.ts +1 -0
- package/src/connectors/BybitTronConnector/BybitTronConnector.d.ts +0 -7
- package/src/connectors/BybitTronConnector/index.d.ts +0 -1
- package/src/connectors/TronWalletConnector/TronWalletConnector.cjs +0 -255
- package/src/connectors/TronWalletConnector/TronWalletConnector.d.ts +0 -42
- package/src/connectors/TronWalletConnector/TronWalletConnector.js +0 -251
- package/src/connectors/TronWalletConnector/index.d.ts +0 -1
- package/src/utils/detectTronNetworkFromProvider.cjs +0 -67
- package/src/utils/detectTronNetworkFromProvider.d.ts +0 -30
- package/src/utils/detectTronNetworkFromProvider.js +0 -62
- package/src/utils/getDefaultTronNetworks.cjs +0 -60
- package/src/utils/getDefaultTronNetworks.d.ts +0 -9
- package/src/utils/getDefaultTronNetworks.js +0 -53
- package/src/utils/getTronGasEstimation.cjs +0 -136
- package/src/utils/getTronGasEstimation.d.ts +0 -47
- package/src/utils/getTronGasEstimation.js +0 -129
- package/src/utils/index.d.ts +0 -4
- package/src/utils/provider.cjs +0 -59
- package/src/utils/provider.d.ts +0 -24
- package/src/utils/provider.js +0 -53
- package/src/wallet/TronWallet.cjs +0 -106
- package/src/wallet/TronWallet.d.ts +0 -22
- package/src/wallet/TronWallet.js +0 -102
- package/src/wallet/index.d.ts +0 -2
|
@@ -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';
|
|
@@ -0,0 +1,124 @@
|
|
|
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
|
+
|
|
10
|
+
class TronWallet extends walletConnectorCore.Wallet {
|
|
11
|
+
constructor(props) {
|
|
12
|
+
super(props);
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Get TronWeb instance from the connected wallet
|
|
16
|
+
*/
|
|
17
|
+
getTronWeb() {
|
|
18
|
+
// Access the wallet-specific TronWeb through the connector
|
|
19
|
+
const connectorWithMethod = this._connector;
|
|
20
|
+
if (connectorWithMethod.getWalletTronWeb) {
|
|
21
|
+
return connectorWithMethod.getWalletTronWeb();
|
|
22
|
+
}
|
|
23
|
+
return undefined;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Sign a message
|
|
27
|
+
*/
|
|
28
|
+
signMessage(message) {
|
|
29
|
+
return _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
30
|
+
const messageString = typeof message === 'string' ? message : new TextDecoder().decode(message);
|
|
31
|
+
const signature = yield this._connector.signMessage(messageString);
|
|
32
|
+
if (signature === undefined || signature === '') {
|
|
33
|
+
throw new utils.DynamicError('Failed to sign message');
|
|
34
|
+
}
|
|
35
|
+
return signature;
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Get balance of an address
|
|
40
|
+
*/
|
|
41
|
+
getBalance(address) {
|
|
42
|
+
return _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
43
|
+
const targetAddress = address || this.address;
|
|
44
|
+
return this._connector.getBalance(targetAddress);
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Send balance (sends TRX)
|
|
49
|
+
*/
|
|
50
|
+
sendBalance(_a) {
|
|
51
|
+
return _tslib.__awaiter(this, arguments, void 0, function* ({ amount, toAddress, }) {
|
|
52
|
+
const result = yield this.sendTrx(toAddress, Number(amount));
|
|
53
|
+
return result.txid;
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Send TRX to an address
|
|
58
|
+
*
|
|
59
|
+
* @param to - Recipient address
|
|
60
|
+
* @param amount - Amount in TRX
|
|
61
|
+
* @param options - Optional parameters
|
|
62
|
+
* @returns Transaction result
|
|
63
|
+
*/
|
|
64
|
+
sendTrx(to, amount, options) {
|
|
65
|
+
return _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
66
|
+
var _a;
|
|
67
|
+
const tronWeb = this.getTronWeb();
|
|
68
|
+
if (!((_a = tronWeb === null || tronWeb === void 0 ? void 0 : tronWeb.trx) === null || _a === void 0 ? void 0 : _a.sendTransaction)) {
|
|
69
|
+
throw new utils.DynamicError('TRX transfer not supported');
|
|
70
|
+
}
|
|
71
|
+
const fromAddress = (options === null || options === void 0 ? void 0 : options.from) || this.address;
|
|
72
|
+
const amountInSun = tronWeb.toSun(amount);
|
|
73
|
+
return tronWeb.trx.sendTransaction(to, Number(amountInSun), {
|
|
74
|
+
address: fromAddress,
|
|
75
|
+
});
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Get TRC20 token balance
|
|
80
|
+
*
|
|
81
|
+
* @param tokenId - Token contract address
|
|
82
|
+
* @param address - Optional address (defaults to current account)
|
|
83
|
+
* @returns Token balance
|
|
84
|
+
*/
|
|
85
|
+
getTokenBalance(tokenId, address) {
|
|
86
|
+
return _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
87
|
+
const tronWeb = this.getTronWeb();
|
|
88
|
+
if (!(tronWeb === null || tronWeb === void 0 ? void 0 : tronWeb.contract)) {
|
|
89
|
+
throw new utils.DynamicError('TRC20 token operations not supported by this wallet');
|
|
90
|
+
}
|
|
91
|
+
const targetAddress = address || this.address;
|
|
92
|
+
try {
|
|
93
|
+
const contract = yield tronWeb.contract().at(tokenId);
|
|
94
|
+
const balance = yield contract.balanceOf(targetAddress).call();
|
|
95
|
+
return typeof balance === 'string'
|
|
96
|
+
? parseInt(balance, 10)
|
|
97
|
+
: Number(balance);
|
|
98
|
+
}
|
|
99
|
+
catch (error) {
|
|
100
|
+
throw new utils.DynamicError(`Failed to get TRC20 token balance: ${error}`);
|
|
101
|
+
}
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Get network details
|
|
106
|
+
*/
|
|
107
|
+
getNetworkDetails() {
|
|
108
|
+
return _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
109
|
+
const chainId = yield this._connector.getNetwork();
|
|
110
|
+
// Map chain IDs to network names
|
|
111
|
+
const networkMap = {
|
|
112
|
+
'0x2b6653dc': 'Mainnet',
|
|
113
|
+
'0x94a9059e': 'Shasta Testnet',
|
|
114
|
+
'0xcd8690dc': 'Nile Testnet',
|
|
115
|
+
};
|
|
116
|
+
return {
|
|
117
|
+
chainId: chainId || '0x2b6653dc', // default to mainnet
|
|
118
|
+
name: networkMap[chainId || '0x2b6653dc'] || 'Unknown',
|
|
119
|
+
};
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
exports.TronWallet = TronWallet;
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import type { TronWeb, Types } from 'tronweb';
|
|
2
|
+
import { Wallet, WalletProps } from '@dynamic-labs/wallet-connector-core';
|
|
3
|
+
import type { TronWalletAdapterConnector } from '../../connectors/TronWalletAdapterConnector/TronWalletAdapterConnector';
|
|
4
|
+
export declare class TronWallet extends Wallet<TronWalletAdapterConnector> {
|
|
5
|
+
constructor(props: WalletProps<TronWalletAdapterConnector>);
|
|
6
|
+
/**
|
|
7
|
+
* Get TronWeb instance from the connected wallet
|
|
8
|
+
*/
|
|
9
|
+
getTronWeb(): TronWeb | undefined;
|
|
10
|
+
/**
|
|
11
|
+
* Sign a message
|
|
12
|
+
*/
|
|
13
|
+
signMessage(message: string | Uint8Array): Promise<string>;
|
|
14
|
+
/**
|
|
15
|
+
* Get balance of an address
|
|
16
|
+
*/
|
|
17
|
+
getBalance(address?: string): Promise<string | undefined>;
|
|
18
|
+
/**
|
|
19
|
+
* Send balance (sends TRX)
|
|
20
|
+
*/
|
|
21
|
+
sendBalance({ amount, toAddress, }: {
|
|
22
|
+
amount: string;
|
|
23
|
+
toAddress: string;
|
|
24
|
+
}): Promise<string | undefined>;
|
|
25
|
+
/**
|
|
26
|
+
* Send TRX to an address
|
|
27
|
+
*
|
|
28
|
+
* @param to - Recipient address
|
|
29
|
+
* @param amount - Amount in TRX
|
|
30
|
+
* @param options - Optional parameters
|
|
31
|
+
* @returns Transaction result
|
|
32
|
+
*/
|
|
33
|
+
sendTrx(to: string, amount: number, options?: {
|
|
34
|
+
from?: string;
|
|
35
|
+
}): Promise<Types.BroadcastReturn<Types.SignedTransaction>>;
|
|
36
|
+
/**
|
|
37
|
+
* Get TRC20 token balance
|
|
38
|
+
*
|
|
39
|
+
* @param tokenId - Token contract address
|
|
40
|
+
* @param address - Optional address (defaults to current account)
|
|
41
|
+
* @returns Token balance
|
|
42
|
+
*/
|
|
43
|
+
getTokenBalance(tokenId: string, address?: string): Promise<number>;
|
|
44
|
+
/**
|
|
45
|
+
* Get network details
|
|
46
|
+
*/
|
|
47
|
+
getNetworkDetails(): Promise<{
|
|
48
|
+
chainId: string;
|
|
49
|
+
name: string;
|
|
50
|
+
}>;
|
|
51
|
+
}
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
import { __awaiter } from '../../../_virtual/_tslib.js';
|
|
3
|
+
import { DynamicError } from '@dynamic-labs/utils';
|
|
4
|
+
import { Wallet } from '@dynamic-labs/wallet-connector-core';
|
|
5
|
+
|
|
6
|
+
class TronWallet extends Wallet {
|
|
7
|
+
constructor(props) {
|
|
8
|
+
super(props);
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Get TronWeb instance from the connected wallet
|
|
12
|
+
*/
|
|
13
|
+
getTronWeb() {
|
|
14
|
+
// Access the wallet-specific TronWeb through the connector
|
|
15
|
+
const connectorWithMethod = this._connector;
|
|
16
|
+
if (connectorWithMethod.getWalletTronWeb) {
|
|
17
|
+
return connectorWithMethod.getWalletTronWeb();
|
|
18
|
+
}
|
|
19
|
+
return undefined;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Sign a message
|
|
23
|
+
*/
|
|
24
|
+
signMessage(message) {
|
|
25
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
26
|
+
const messageString = typeof message === 'string' ? message : new TextDecoder().decode(message);
|
|
27
|
+
const signature = yield this._connector.signMessage(messageString);
|
|
28
|
+
if (signature === undefined || signature === '') {
|
|
29
|
+
throw new DynamicError('Failed to sign message');
|
|
30
|
+
}
|
|
31
|
+
return signature;
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Get balance of an address
|
|
36
|
+
*/
|
|
37
|
+
getBalance(address) {
|
|
38
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
39
|
+
const targetAddress = address || this.address;
|
|
40
|
+
return this._connector.getBalance(targetAddress);
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Send balance (sends TRX)
|
|
45
|
+
*/
|
|
46
|
+
sendBalance(_a) {
|
|
47
|
+
return __awaiter(this, arguments, void 0, function* ({ amount, toAddress, }) {
|
|
48
|
+
const result = yield this.sendTrx(toAddress, Number(amount));
|
|
49
|
+
return result.txid;
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Send TRX to an address
|
|
54
|
+
*
|
|
55
|
+
* @param to - Recipient address
|
|
56
|
+
* @param amount - Amount in TRX
|
|
57
|
+
* @param options - Optional parameters
|
|
58
|
+
* @returns Transaction result
|
|
59
|
+
*/
|
|
60
|
+
sendTrx(to, amount, options) {
|
|
61
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
62
|
+
var _a;
|
|
63
|
+
const tronWeb = this.getTronWeb();
|
|
64
|
+
if (!((_a = tronWeb === null || tronWeb === void 0 ? void 0 : tronWeb.trx) === null || _a === void 0 ? void 0 : _a.sendTransaction)) {
|
|
65
|
+
throw new DynamicError('TRX transfer not supported');
|
|
66
|
+
}
|
|
67
|
+
const fromAddress = (options === null || options === void 0 ? void 0 : options.from) || this.address;
|
|
68
|
+
const amountInSun = tronWeb.toSun(amount);
|
|
69
|
+
return tronWeb.trx.sendTransaction(to, Number(amountInSun), {
|
|
70
|
+
address: fromAddress,
|
|
71
|
+
});
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Get TRC20 token balance
|
|
76
|
+
*
|
|
77
|
+
* @param tokenId - Token contract address
|
|
78
|
+
* @param address - Optional address (defaults to current account)
|
|
79
|
+
* @returns Token balance
|
|
80
|
+
*/
|
|
81
|
+
getTokenBalance(tokenId, address) {
|
|
82
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
83
|
+
const tronWeb = this.getTronWeb();
|
|
84
|
+
if (!(tronWeb === null || tronWeb === void 0 ? void 0 : tronWeb.contract)) {
|
|
85
|
+
throw new DynamicError('TRC20 token operations not supported by this wallet');
|
|
86
|
+
}
|
|
87
|
+
const targetAddress = address || this.address;
|
|
88
|
+
try {
|
|
89
|
+
const contract = yield tronWeb.contract().at(tokenId);
|
|
90
|
+
const balance = yield contract.balanceOf(targetAddress).call();
|
|
91
|
+
return typeof balance === 'string'
|
|
92
|
+
? parseInt(balance, 10)
|
|
93
|
+
: Number(balance);
|
|
94
|
+
}
|
|
95
|
+
catch (error) {
|
|
96
|
+
throw new DynamicError(`Failed to get TRC20 token balance: ${error}`);
|
|
97
|
+
}
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Get network details
|
|
102
|
+
*/
|
|
103
|
+
getNetworkDetails() {
|
|
104
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
105
|
+
const chainId = yield this._connector.getNetwork();
|
|
106
|
+
// Map chain IDs to network names
|
|
107
|
+
const networkMap = {
|
|
108
|
+
'0x2b6653dc': 'Mainnet',
|
|
109
|
+
'0x94a9059e': 'Shasta Testnet',
|
|
110
|
+
'0xcd8690dc': 'Nile Testnet',
|
|
111
|
+
};
|
|
112
|
+
return {
|
|
113
|
+
chainId: chainId || '0x2b6653dc', // default to mainnet
|
|
114
|
+
name: networkMap[chainId || '0x2b6653dc'] || 'Unknown',
|
|
115
|
+
};
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
export { TronWallet };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { TronWallet } from './TronWallet';
|
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
import type { ITronProvider } from '../../types';
|
|
2
|
-
import { TronWalletConnector } from '../TronWalletConnector';
|
|
3
|
-
export declare class BybitTronConnector extends TronWalletConnector {
|
|
4
|
-
name: string;
|
|
5
|
-
overrideKey: string;
|
|
6
|
-
getProvider(): ITronProvider | undefined;
|
|
7
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export { BybitTronConnector } from './BybitTronConnector';
|