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

Commit afa917f

Browse files
committed
Improve tron max swap
1 parent b5e889a commit afa917f

1 file changed

Lines changed: 16 additions & 5 deletions

File tree

crates/gem_tron/src/provider/preload_mapper.rs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ pub fn calculate_transfer_fee_rate(
2424
let activation_fee = get_chain_parameter_value(chain_parameters, GET_CREATE_NEW_ACCOUNT_FEE_IN_SYSTEM_CONTRACT)?;
2525
let new_account_bandwidth_fee = get_chain_parameter_value(chain_parameters, GET_CREATE_ACCOUNT_FEE)?;
2626

27-
if account_usage.available_bandwidth() >= DEFAULT_BANDWIDTH_BYTES {
27+
// Account activation only waives this fixed bandwidth fee when the sender has staked/delegated bandwidth.
28+
if account_usage.available_staked_bandwidth() >= DEFAULT_BANDWIDTH_BYTES {
2829
activation_fee
2930
} else {
3031
activation_fee + new_account_bandwidth_fee
@@ -166,8 +167,11 @@ pub fn map_stake_data(account: &TronAccount, stake_type: &StakeType, raw_amount:
166167
impl TronAccountUsage {
167168
pub fn available_bandwidth(&self) -> u64 {
168169
let free = self.free_net_limit.saturating_sub(self.free_net_used);
169-
let staked = self.net_limit.saturating_sub(self.net_used);
170-
free.saturating_add(staked)
170+
free.saturating_add(self.available_staked_bandwidth())
171+
}
172+
173+
pub fn available_staked_bandwidth(&self) -> u64 {
174+
self.net_limit.saturating_sub(self.net_used)
171175
}
172176

173177
pub fn missing_bandwidth(&self, required: u64) -> u64 {
@@ -226,6 +230,7 @@ mod tests {
226230
};
227231

228232
assert_eq!(usage.available_bandwidth(), 1200); // (1000-100) + (500-200)
233+
assert_eq!(usage.available_staked_bandwidth(), 300);
229234
assert_eq!(usage.missing_bandwidth(1500), 300);
230235
assert_eq!(usage.missing_bandwidth(1000), 0);
231236
}
@@ -328,9 +333,15 @@ mod tests {
328333
BigInt::from(2_100_000), // activation + bandwidth + memo
329334
);
330335

331-
let with_bandwidth = account_usage(DEFAULT_BANDWIDTH_BYTES, 0, 0);
336+
let with_free_bandwidth = account_usage(DEFAULT_BANDWIDTH_BYTES, 0, 0);
337+
assert_eq!(
338+
calculate_transfer_fee_rate(&params, &with_free_bandwidth, true, false).unwrap(),
339+
BigInt::from(1_100_000), // activation + fixed account creation bandwidth fee
340+
);
341+
342+
let with_staked_bandwidth = account_usage(0, DEFAULT_BANDWIDTH_BYTES, 0);
332343
assert_eq!(
333-
calculate_transfer_fee_rate(&params, &with_bandwidth, true, false).unwrap(),
344+
calculate_transfer_fee_rate(&params, &with_staked_bandwidth, true, false).unwrap(),
334345
BigInt::from(1_000_000), // only activation
335346
);
336347
}

0 commit comments

Comments
 (0)