Skip to content

Commit e3d62b3

Browse files
committed
make calculated amount unsigned since we always return a positive value
1 parent 3e716e0 commit e3d62b3

File tree

4 files changed

+28
-35
lines changed

4 files changed

+28
-35
lines changed

src/math/swap.rs

Lines changed: 23 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use num_traits::Zero;
77
#[derive(Debug, PartialEq)]
88
pub struct SwapResult {
99
pub consumed_amount: i128,
10-
pub calculated_amount: i128,
10+
pub calculated_amount: u128,
1111
pub sqrt_ratio_next: U256,
1212
pub fee_amount: u128,
1313
}
@@ -121,18 +121,14 @@ pub fn compute_step(
121121
.ok_or(ComputeStepError::AmountBeforeFeeOverflow)?;
122122
Ok(SwapResult {
123123
consumed_amount: amount,
124-
calculated_amount: including_fee
125-
.try_into()
126-
.map_err(ComputeStepError::SignedIntegerOverflow)?,
124+
calculated_amount: including_fee,
127125
sqrt_ratio_next,
128126
fee_amount: including_fee - calculated_amount_excluding_fee,
129127
})
130128
} else {
131129
Ok(SwapResult {
132130
consumed_amount: amount,
133-
calculated_amount: calculated_amount_excluding_fee
134-
.try_into()
135-
.map_err(ComputeStepError::SignedIntegerOverflow)?,
131+
calculated_amount: calculated_amount_excluding_fee,
136132
sqrt_ratio_next,
137133
fee_amount: amount.unsigned_abs() - price_impact_amount.unsigned_abs(),
138134
})
@@ -163,9 +159,7 @@ pub fn compute_step(
163159
.map_err(ComputeStepError::AmountDeltaError)?
164160
.try_into()
165161
.map_err(ComputeStepError::SignedIntegerOverflow)?,
166-
calculated_amount: before_fee
167-
.try_into()
168-
.map_err(ComputeStepError::SignedIntegerOverflow)?,
162+
calculated_amount: before_fee,
169163
fee_amount: before_fee - amount_after_fee,
170164
sqrt_ratio_next: sqrt_ratio_limit,
171165
})
@@ -181,9 +175,7 @@ pub fn compute_step(
181175
consumed_amount: before_fee
182176
.try_into()
183177
.map_err(ComputeStepError::SignedIntegerOverflow)?,
184-
calculated_amount: calculated_amount
185-
.try_into()
186-
.map_err(ComputeStepError::SignedIntegerOverflow)?,
178+
calculated_amount: calculated_amount,
187179
fee_amount: before_fee - specified_amount,
188180
sqrt_ratio_next: sqrt_ratio_limit,
189181
})
@@ -214,8 +206,8 @@ mod tests {
214206
)
215207
.unwrap();
216208

217-
assert_eq!(result.calculated_amount, 0i128);
218-
assert_eq!(result.consumed_amount, 0i128);
209+
assert_eq!(result.calculated_amount, 0);
210+
assert_eq!(result.consumed_amount, 0);
219211
assert_eq!(result.fee_amount, 0u128);
220212
assert_eq!(result.sqrt_ratio_next, sqrt_ratio);
221213
}
@@ -239,8 +231,8 @@ mod tests {
239231
)
240232
.unwrap();
241233

242-
assert_eq!(result.calculated_amount, 0i128);
243-
assert_eq!(result.consumed_amount, 0i128);
234+
assert_eq!(result.calculated_amount, 0);
235+
assert_eq!(result.consumed_amount, 0);
244236
assert_eq!(result.fee_amount, 0u128);
245237
assert_eq!(result.sqrt_ratio_next, sqrt_ratio);
246238
}
@@ -264,8 +256,8 @@ mod tests {
264256
)
265257
.unwrap();
266258

267-
assert_eq!(result.calculated_amount, 0i128);
268-
assert_eq!(result.consumed_amount, 0i128);
259+
assert_eq!(result.calculated_amount, 0);
260+
assert_eq!(result.consumed_amount, 0);
269261
assert_eq!(result.fee_amount, 0u128);
270262
assert_eq!(result.sqrt_ratio_next, sqrt_ratio);
271263
}
@@ -289,8 +281,8 @@ mod tests {
289281
)
290282
.unwrap();
291283

292-
assert_eq!(result.calculated_amount, 4_761i128);
293-
assert_eq!(result.consumed_amount, 10_000i128);
284+
assert_eq!(result.calculated_amount, 4_761);
285+
assert_eq!(result.consumed_amount, 10_000);
294286
assert_eq!(result.fee_amount, 5_000u128);
295287
assert_eq!(
296288
result.sqrt_ratio_next,
@@ -317,8 +309,8 @@ mod tests {
317309
)
318310
.unwrap();
319311

320-
assert_eq!(result.calculated_amount, 4_761i128);
321-
assert_eq!(result.consumed_amount, 10_000i128);
312+
assert_eq!(result.calculated_amount, 4_761);
313+
assert_eq!(result.consumed_amount, 10_000);
322314
assert_eq!(result.fee_amount, 5_000u128);
323315
assert_eq!(
324316
result.sqrt_ratio_next,
@@ -345,8 +337,8 @@ mod tests {
345337
)
346338
.unwrap();
347339

348-
assert_eq!(result.calculated_amount, 22_224i128);
349-
assert_eq!(result.consumed_amount, -10_000i128);
340+
assert_eq!(result.calculated_amount, 22_224);
341+
assert_eq!(result.consumed_amount, -10_000);
350342
assert_eq!(result.fee_amount, 11_112u128);
351343
assert_eq!(
352344
result.sqrt_ratio_next,
@@ -373,8 +365,8 @@ mod tests {
373365
)
374366
.unwrap();
375367

376-
assert_eq!(result.calculated_amount, 22_224i128);
377-
assert_eq!(result.consumed_amount, -10_000i128);
368+
assert_eq!(result.calculated_amount, 22_224);
369+
assert_eq!(result.consumed_amount, -10_000);
378370
assert_eq!(result.fee_amount, 11_112u128);
379371
assert_eq!(
380372
result.sqrt_ratio_next,
@@ -402,8 +394,8 @@ mod tests {
402394
)
403395
.unwrap();
404396

405-
assert_eq!(result.calculated_amount, 11_112i128);
406-
assert_eq!(result.consumed_amount, -5_263i128);
397+
assert_eq!(result.calculated_amount, 11_112);
398+
assert_eq!(result.consumed_amount, -5_263);
407399
assert_eq!(result.fee_amount, 5_556u128);
408400
assert_eq!(result.sqrt_ratio_next, sqrt_ratio_limit);
409401
}
@@ -428,8 +420,8 @@ mod tests {
428420
)
429421
.unwrap();
430422

431-
assert_eq!(result.calculated_amount, 10_528i128);
432-
assert_eq!(result.consumed_amount, -5_000i128);
423+
assert_eq!(result.calculated_amount, 10_528);
424+
assert_eq!(result.consumed_amount, -5_000);
433425
assert_eq!(result.fee_amount, 5_264u128);
434426
assert_eq!(result.sqrt_ratio_next, sqrt_ratio_limit);
435427
}

src/quoting/base_pool.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -506,7 +506,7 @@ impl Pool for BasePool {
506506
}
507507
};
508508

509-
let mut calculated_amount: i128 = 0;
509+
let mut calculated_amount: u128 = 0;
510510
let mut fees_paid: u128 = 0;
511511
let mut initialized_ticks_crossed: u32 = 0;
512512
let mut amount_remaining = amount;

src/quoting/mev_resist_pool.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ impl Pool for MEVResistPool {
168168
if params.token_amount.amount >= 0 {
169169
// exact input, remove the additional fee from the output
170170
calculated_amount -=
171-
compute_fee(calculated_amount as u128, fixed_point_additional_fee) as i128;
171+
compute_fee(calculated_amount as u128, fixed_point_additional_fee);
172172
} else {
173173
let input_amount_fee: u128 =
174174
compute_fee(calculated_amount as u128, pool_config.fee);
@@ -177,7 +177,7 @@ impl Pool for MEVResistPool {
177177
if let Some(bf) = amount_before_fee(input_amount, fixed_point_additional_fee) {
178178
let fee = bf - input_amount;
179179
// exact output, compute the additional fee for the output
180-
calculated_amount += fee as i128;
180+
calculated_amount += fee;
181181
} else {
182182
return Err(BasePoolQuoteError::FailedComputeSwapStep(
183183
crate::math::swap::ComputeStepError::AmountBeforeFeeOverflow,
@@ -221,6 +221,7 @@ impl Pool for MEVResistPool {
221221
}
222222
}
223223

224+
#[cfg(test)]
224225
mod tests {
225226
use alloc::vec;
226227

src/quoting/types.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ pub struct QuoteParams<S, M> {
9292
pub struct Quote<R, S> {
9393
pub is_price_increasing: bool,
9494
pub consumed_amount: i128,
95-
pub calculated_amount: i128,
95+
pub calculated_amount: u128,
9696
pub execution_resources: R,
9797
pub state_after: S,
9898
pub fees_paid: u128,

0 commit comments

Comments
 (0)