Skip to content
This repository was archived by the owner on Jun 1, 2026. It is now read-only.

Commit e8193d8

Browse files
committed
Refactor Tron fee calculation and improve code clarity
Refactored fee calculation logic for TRON token transfers, introducing a TokenTransferFee struct and utility methods for bandwidth and energy calculations. Updated related functions and tests to use the new abstractions, improving code clarity and maintainability. Also refactored contract call logic in the Tron RPC client for better separation of concerns and fixed minor formatting issues in test code.
1 parent 329c3ae commit e8193d8

5 files changed

Lines changed: 235 additions & 177 deletions

File tree

crates/fiat/src/client.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -668,9 +668,18 @@ mod tests {
668668
let transak = FiatQuote::mock("transak");
669669

670670
let mut quotes = vec![
671-
FiatQuote { crypto_amount: 0.036108, ..moonpay },
672-
FiatQuote { crypto_amount: 0.03311059, ..mercuryo },
673-
FiatQuote { crypto_amount: 0.03086637, ..transak },
671+
FiatQuote {
672+
crypto_amount: 0.036108,
673+
..moonpay
674+
},
675+
FiatQuote {
676+
crypto_amount: 0.03311059,
677+
..mercuryo
678+
},
679+
FiatQuote {
680+
crypto_amount: 0.03086637,
681+
..transak
682+
},
674683
];
675684

676685
quotes.sort_by(|a, b| sort_quotes_by_crypto_amount_asc(a, b, &providers));

crates/gem_tron/src/provider/address_mapper.rs

Lines changed: 44 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,18 @@ impl TronAccount {
4545
address: Some(address.to_string()),
4646
owner_permission: Some(TronAccountOwnerPermission {
4747
permission_name: "owner".to_string(),
48-
keys: Some(vec![TronAccountPermissionKey { address: address.to_string(), weight: 1 }]),
48+
keys: Some(vec![TronAccountPermissionKey {
49+
address: address.to_string(),
50+
weight: 1,
51+
}]),
4952
}),
5053
active_permission: Some(vec![TronAccountPermission {
5154
id: None,
5255
threshold: 1,
53-
keys: Some(vec![TronAccountPermissionKey { address: address.to_string(), weight: 1 }]),
56+
keys: Some(vec![TronAccountPermissionKey {
57+
address: address.to_string(),
58+
weight: 1,
59+
}]),
5460
}]),
5561
votes: None,
5662
frozen_v2: None,
@@ -83,16 +89,28 @@ mod tests {
8389
fn test_multiple_active_permissions() {
8490
let mut account = TronAccount::mock(ADDRESS);
8591
account.active_permission = Some(vec![
86-
TronAccountPermission { id: None, threshold: 1, keys: None },
87-
TronAccountPermission { id: None, threshold: 1, keys: None },
92+
TronAccountPermission {
93+
id: None,
94+
threshold: 1,
95+
keys: None,
96+
},
97+
TronAccountPermission {
98+
id: None,
99+
threshold: 1,
100+
keys: None,
101+
},
88102
]);
89103
assert_eq!(map_address_status(&account), vec![AddressStatus::MultiSignature]);
90104
}
91105

92106
#[test]
93107
fn test_high_threshold() {
94108
let mut account = TronAccount::mock(ADDRESS);
95-
account.active_permission = Some(vec![TronAccountPermission { id: None, threshold: 2, keys: None }]);
109+
account.active_permission = Some(vec![TronAccountPermission {
110+
id: None,
111+
threshold: 2,
112+
keys: None,
113+
}]);
96114
assert_eq!(map_address_status(&account), vec![AddressStatus::MultiSignature]);
97115
}
98116

@@ -109,7 +127,11 @@ mod tests {
109127
#[test]
110128
fn test_active_permission_id_not_default() {
111129
let mut account = TronAccount::mock(ADDRESS);
112-
account.active_permission = Some(vec![TronAccountPermission { id: Some(2), threshold: 1, keys: None }]);
130+
account.active_permission = Some(vec![TronAccountPermission {
131+
id: Some(2),
132+
threshold: 1,
133+
keys: None,
134+
}]);
113135
assert_eq!(map_address_status(&account), vec![AddressStatus::MultiSignature]);
114136
}
115137

@@ -118,7 +140,10 @@ mod tests {
118140
let mut account = TronAccount::mock(ADDRESS);
119141
account.owner_permission = Some(TronAccountOwnerPermission {
120142
permission_name: "owner".to_string(),
121-
keys: Some(vec![TronAccountPermissionKey { address: OTHER_ADDRESS.to_string(), weight: 1 }]),
143+
keys: Some(vec![TronAccountPermissionKey {
144+
address: OTHER_ADDRESS.to_string(),
145+
weight: 1,
146+
}]),
122147
});
123148
assert_eq!(map_address_status(&account), vec![AddressStatus::MultiSignature]);
124149
}
@@ -129,7 +154,10 @@ mod tests {
129154
account.active_permission = Some(vec![TronAccountPermission {
130155
id: None,
131156
threshold: 1,
132-
keys: Some(vec![TronAccountPermissionKey { address: OTHER_ADDRESS.to_string(), weight: 1 }]),
157+
keys: Some(vec![TronAccountPermissionKey {
158+
address: OTHER_ADDRESS.to_string(),
159+
weight: 1,
160+
}]),
133161
}]);
134162
assert_eq!(map_address_status(&account), vec![AddressStatus::MultiSignature]);
135163
}
@@ -140,8 +168,14 @@ mod tests {
140168
account.owner_permission = Some(TronAccountOwnerPermission {
141169
permission_name: "owner".to_string(),
142170
keys: Some(vec![
143-
TronAccountPermissionKey { address: ADDRESS.to_string(), weight: 1 },
144-
TronAccountPermissionKey { address: OTHER_ADDRESS.to_string(), weight: 1 },
171+
TronAccountPermissionKey {
172+
address: ADDRESS.to_string(),
173+
weight: 1,
174+
},
175+
TronAccountPermissionKey {
176+
address: OTHER_ADDRESS.to_string(),
177+
weight: 1,
178+
},
145179
]),
146180
});
147181
assert_eq!(map_address_status(&account), vec![AddressStatus::MultiSignature]);

crates/gem_tron/src/provider/preload.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use std::collections::HashMap;
22
use std::error::Error;
3-
use std::str::FromStr;
43

54
use async_trait::async_trait;
65
use chain_traits::ChainTransactionLoad;
@@ -104,11 +103,14 @@ impl<C: Client> TronClient<C> {
104103
let estimated_energy = self
105104
.estimate_trc20_transfer_gas(sender_address, token_id, format_address_parameter(&destination_address)?, value)
106105
.await?;
107-
let (total_fee, _chargeable_energy, energy_price) =
108-
calculate_transfer_token_fee_rate(chain_parameters, account_usage, BigInt::from_str(&estimated_energy)?)?;
109-
let gas_price_type = GasPriceType::regular(energy_price);
110-
111-
Ok(TransactionFee::new_gas_price_type(gas_price_type, total_fee.clone(), total_fee, HashMap::new()))
106+
let token_fee = calculate_transfer_token_fee_rate(chain_parameters, account_usage, estimated_energy)?;
107+
108+
Ok(TransactionFee::new_gas_price_type(
109+
GasPriceType::regular(BigInt::from(token_fee.energy_price)),
110+
BigInt::from(token_fee.fee),
111+
BigInt::from(token_fee.fee_limit),
112+
HashMap::new(),
113+
))
112114
}
113115

114116
async fn get_is_new_account_for_input_type(&self, address: &str, input_type: TransactionInputType) -> Result<bool, Box<dyn Error + Send + Sync>> {

0 commit comments

Comments
 (0)