Skip to content

Commit 739d45e

Browse files
committed
Merge remote-tracking branch 'evm/main'
2 parents d79eb00 + ff637a6 commit 739d45e

File tree

13 files changed

+1292
-306
lines changed

13 files changed

+1292
-306
lines changed

Cargo.lock

Lines changed: 87 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@ keywords = ["crypto", "math", "ekubo", "protocol", "defi"]
1313
[dependencies]
1414
num-traits = "0.2.19"
1515
uint = "0.10.0"
16+
serde = { version = "1.0", features = ["derive"], optional = true }
17+
18+
[dev-dependencies]
19+
serde_json = { version = "1.0" }
1620

1721
[lib]
1822
path = "src/lib.rs"
23+
24+
[features]
25+
serde = ["dep:serde"]

src/math/delta.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ fn sort_ratios(sqrt_ratio_a: U256, sqrt_ratio_b: U256) -> Option<(U256, U256)> {
1616
}
1717
}
1818

19-
#[derive(Debug, PartialEq, Clone, Copy)]
19+
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
2020
pub enum AmountDeltaError {
2121
ZeroRatio,
2222
Overflow,

src/math/muldiv.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::math::uint::U256;
22
use num_traits::Zero;
33

4-
#[derive(Debug, PartialEq, Clone, Copy)]
4+
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
55
pub enum MuldivError {
66
Overflow,
77
DenominatorZero,

src/math/swap.rs

Lines changed: 24 additions & 32 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
}
@@ -52,7 +52,7 @@ fn no_op(sqrt_ratio_next: U256) -> SwapResult {
5252
}
5353
}
5454

55-
#[derive(Debug, PartialEq, Clone, Copy)]
55+
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
5656
pub enum ComputeStepError {
5757
WrongDirection,
5858
AmountBeforeFeeOverflow,
@@ -122,18 +122,14 @@ pub fn compute_step(
122122
.ok_or(ComputeStepError::AmountBeforeFeeOverflow)?;
123123
Ok(SwapResult {
124124
consumed_amount: amount,
125-
calculated_amount: including_fee
126-
.try_into()
127-
.map_err(ComputeStepError::SignedIntegerOverflow)?,
125+
calculated_amount: including_fee,
128126
sqrt_ratio_next,
129127
fee_amount: including_fee - calculated_amount_excluding_fee,
130128
})
131129
} else {
132130
Ok(SwapResult {
133131
consumed_amount: amount,
134-
calculated_amount: calculated_amount_excluding_fee
135-
.try_into()
136-
.map_err(ComputeStepError::SignedIntegerOverflow)?,
132+
calculated_amount: calculated_amount_excluding_fee,
137133
sqrt_ratio_next,
138134
fee_amount: amount.unsigned_abs() - price_impact_amount.unsigned_abs(),
139135
})
@@ -164,9 +160,7 @@ pub fn compute_step(
164160
.map_err(ComputeStepError::AmountDeltaError)?
165161
.try_into()
166162
.map_err(ComputeStepError::SignedIntegerOverflow)?,
167-
calculated_amount: before_fee
168-
.try_into()
169-
.map_err(ComputeStepError::SignedIntegerOverflow)?,
163+
calculated_amount: before_fee,
170164
fee_amount: before_fee - amount_after_fee,
171165
sqrt_ratio_next: sqrt_ratio_limit,
172166
})
@@ -182,9 +176,7 @@ pub fn compute_step(
182176
consumed_amount: before_fee
183177
.try_into()
184178
.map_err(ComputeStepError::SignedIntegerOverflow)?,
185-
calculated_amount: calculated_amount
186-
.try_into()
187-
.map_err(ComputeStepError::SignedIntegerOverflow)?,
179+
calculated_amount: calculated_amount,
188180
fee_amount: before_fee - specified_amount,
189181
sqrt_ratio_next: sqrt_ratio_limit,
190182
})
@@ -215,8 +207,8 @@ mod tests {
215207
)
216208
.unwrap();
217209

218-
assert_eq!(result.calculated_amount, 0i128);
219-
assert_eq!(result.consumed_amount, 0i128);
210+
assert_eq!(result.calculated_amount, 0);
211+
assert_eq!(result.consumed_amount, 0);
220212
assert_eq!(result.fee_amount, 0u128);
221213
assert_eq!(result.sqrt_ratio_next, sqrt_ratio);
222214
}
@@ -240,8 +232,8 @@ mod tests {
240232
)
241233
.unwrap();
242234

243-
assert_eq!(result.calculated_amount, 0i128);
244-
assert_eq!(result.consumed_amount, 0i128);
235+
assert_eq!(result.calculated_amount, 0);
236+
assert_eq!(result.consumed_amount, 0);
245237
assert_eq!(result.fee_amount, 0u128);
246238
assert_eq!(result.sqrt_ratio_next, sqrt_ratio);
247239
}
@@ -265,8 +257,8 @@ mod tests {
265257
)
266258
.unwrap();
267259

268-
assert_eq!(result.calculated_amount, 0i128);
269-
assert_eq!(result.consumed_amount, 0i128);
260+
assert_eq!(result.calculated_amount, 0);
261+
assert_eq!(result.consumed_amount, 0);
270262
assert_eq!(result.fee_amount, 0u128);
271263
assert_eq!(result.sqrt_ratio_next, sqrt_ratio);
272264
}
@@ -290,8 +282,8 @@ mod tests {
290282
)
291283
.unwrap();
292284

293-
assert_eq!(result.calculated_amount, 4_761i128);
294-
assert_eq!(result.consumed_amount, 10_000i128);
285+
assert_eq!(result.calculated_amount, 4_761);
286+
assert_eq!(result.consumed_amount, 10_000);
295287
assert_eq!(result.fee_amount, 5_000u128);
296288
assert_eq!(
297289
result.sqrt_ratio_next,
@@ -318,8 +310,8 @@ mod tests {
318310
)
319311
.unwrap();
320312

321-
assert_eq!(result.calculated_amount, 4_761i128);
322-
assert_eq!(result.consumed_amount, 10_000i128);
313+
assert_eq!(result.calculated_amount, 4_761);
314+
assert_eq!(result.consumed_amount, 10_000);
323315
assert_eq!(result.fee_amount, 5_000u128);
324316
assert_eq!(
325317
result.sqrt_ratio_next,
@@ -346,8 +338,8 @@ mod tests {
346338
)
347339
.unwrap();
348340

349-
assert_eq!(result.calculated_amount, 22_224i128);
350-
assert_eq!(result.consumed_amount, -10_000i128);
341+
assert_eq!(result.calculated_amount, 22_224);
342+
assert_eq!(result.consumed_amount, -10_000);
351343
assert_eq!(result.fee_amount, 11_112u128);
352344
assert_eq!(
353345
result.sqrt_ratio_next,
@@ -374,8 +366,8 @@ mod tests {
374366
)
375367
.unwrap();
376368

377-
assert_eq!(result.calculated_amount, 22_224i128);
378-
assert_eq!(result.consumed_amount, -10_000i128);
369+
assert_eq!(result.calculated_amount, 22_224);
370+
assert_eq!(result.consumed_amount, -10_000);
379371
assert_eq!(result.fee_amount, 11_112u128);
380372
assert_eq!(
381373
result.sqrt_ratio_next,
@@ -403,8 +395,8 @@ mod tests {
403395
)
404396
.unwrap();
405397

406-
assert_eq!(result.calculated_amount, 11_112i128);
407-
assert_eq!(result.consumed_amount, -5_263i128);
398+
assert_eq!(result.calculated_amount, 11_112);
399+
assert_eq!(result.consumed_amount, -5_263);
408400
assert_eq!(result.fee_amount, 5_556u128);
409401
assert_eq!(result.sqrt_ratio_next, sqrt_ratio_limit);
410402
}
@@ -429,8 +421,8 @@ mod tests {
429421
)
430422
.unwrap();
431423

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

0 commit comments

Comments
 (0)