Skip to content

Commit 323a569

Browse files
committed
Fix review comments
1 parent 2ba828d commit 323a569

File tree

3 files changed

+35
-33
lines changed

3 files changed

+35
-33
lines changed

common/src/chain/tokens/issuance.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ impl IsTokenUnfreezable {
101101

102102
/// Indicates whether a token is frozen at the moment or not. If it is then no operations with this token can be performed.
103103
/// Meaning transfers, burns, minting, unminting, supply locks etc. Frozen token can only be unfrozen
104-
/// is such an option was provided while freezing.
104+
/// if such an option was provided while freezing.
105105
#[derive(
106106
Debug,
107107
Copy,

orders-accounting/src/error.rs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,37 +19,37 @@ use common::{chain::OrderId, primitives::Amount};
1919
pub enum Error {
2020
#[error("Accounting storage error")]
2121
StorageError(#[from] chainstate_types::storage_result::Error),
22-
#[error("Base accounting error: {0}")]
22+
#[error("Base accounting error: `{0}`")]
2323
AccountingError(#[from] accounting::Error),
24-
#[error("Order already exist: `{0}`")]
24+
#[error("Order already exists: `{0}`")]
2525
OrderAlreadyExists(OrderId),
26-
#[error("Data for order {0}` not found")]
26+
#[error("Data for order `{0}` not found")]
2727
OrderDataNotFound(OrderId),
28-
#[error("Ask balance for order {0}` not found")]
28+
#[error("Ask balance for order `{0}` not found")]
2929
OrderAskBalanceNotFound(OrderId),
30-
#[error("Give balance for order {0}` not found")]
30+
#[error("Give balance for order `{0}` not found")]
3131
OrderGiveBalanceNotFound(OrderId),
3232
#[error("Attempt to create an order with zero exchange value `{0}`")]
3333
OrderWithZeroValue(OrderId),
34-
#[error("Data for order {0}` not found for undo")]
34+
#[error("Data for order `{0}` not found for undo")]
3535
InvariantOrderDataNotFoundForUndo(OrderId),
36-
#[error("Ask balance for order {0}` not found for undo")]
36+
#[error("Ask balance for order `{0}` not found for undo")]
3737
InvariantOrderAskBalanceNotFoundForUndo(OrderId),
38-
#[error("Ask balance for order {0}` changed for undo")]
38+
#[error("Ask balance for order `{0}` changed for undo")]
3939
InvariantOrderAskBalanceChangedForUndo(OrderId),
40-
#[error("Give balance for order {0}` not found for undo")]
40+
#[error("Give balance for order `{0}` not found for undo")]
4141
InvariantOrderGiveBalanceNotFoundForUndo(OrderId),
42-
#[error("Give balance for order {0}` changed for undo")]
42+
#[error("Give balance for order `{0}` changed for undo")]
4343
InvariantOrderGiveBalanceChangedForUndo(OrderId),
44-
#[error("Data for order {0}` still exist on conclude undo")]
44+
#[error("Data for order `{0}` still exist on conclude undo")]
4545
InvariantOrderDataExistForConcludeUndo(OrderId),
46-
#[error("Ask balance for order {0}` still exist on conclude undo")]
46+
#[error("Ask balance for order `{0}` still exist on conclude undo")]
4747
InvariantOrderAskBalanceExistForConcludeUndo(OrderId),
48-
#[error("Give balance for order {0}` still exist on conclude undo")]
48+
#[error("Give balance for order `{0}` still exist on conclude undo")]
4949
InvariantOrderGiveBalanceExistForConcludeUndo(OrderId),
50-
#[error("Ask balance for non-existing order {0}` is not zero")]
50+
#[error("Ask balance for non-existing order `{0}` is not zero")]
5151
InvariantNonzeroAskBalanceForMissingOrder(OrderId),
52-
#[error("Give balance for non-existing order {0}` is not zero")]
52+
#[error("Give balance for non-existing order `{0}` is not zero")]
5353
InvariantNonzeroGiveBalanceForMissingOrder(OrderId),
5454
#[error("Coin type mismatch")]
5555
CurrencyMismatch,

orders-accounting/src/price_calculation.rs

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use common::{
1717
chain::{output_value::OutputValue, OrderId},
1818
primitives::Amount,
19+
Uint256,
1920
};
2021
use utils::ensure;
2122

@@ -59,34 +60,35 @@ pub fn calculate_fill_order(
5960
}
6061

6162
fn calculate_filled_amount_impl(ask: Amount, give: Amount, fill: Amount) -> Option<Amount> {
62-
(give * fill.into_atoms()).and_then(|v| v / ask.into_atoms())
63+
let give = Uint256::from_u128(give.into_atoms());
64+
let fill = Uint256::from_u128(fill.into_atoms());
65+
let ask = Uint256::from_u128(ask.into_atoms());
66+
67+
let result = ((give * fill).expect("cannot overflow") / ask)?;
68+
let result: u128 = result.try_into().ok()?;
69+
70+
Some(Amount::from_atoms(result))
6371
}
6472

6573
fn ensure_currencies_and_amounts_match(
6674
order_id: OrderId,
67-
left: &OutputValue,
68-
right: &OutputValue,
75+
ask: &OutputValue,
76+
fill: &OutputValue,
6977
) -> Result<()> {
70-
match (left, right) {
71-
(OutputValue::Coin(amount1), OutputValue::Coin(amount2)) => {
72-
ensure!(
73-
amount1 >= amount2,
74-
Error::OrderOverbid(order_id, *amount1, *amount2)
75-
);
78+
match (ask, fill) {
79+
(OutputValue::Coin(ask), OutputValue::Coin(fill)) => {
80+
ensure!(ask >= fill, Error::OrderOverbid(order_id, *ask, *fill));
7681
Ok(())
7782
}
78-
(OutputValue::TokenV1(id1, amount1), OutputValue::TokenV1(id2, amount2)) => {
79-
ensure!(
80-
amount1 >= amount2,
81-
Error::OrderOverbid(order_id, *amount1, *amount2)
82-
);
83+
(OutputValue::TokenV1(id1, ask), OutputValue::TokenV1(id2, fill)) => {
84+
ensure!(ask >= fill, Error::OrderOverbid(order_id, *ask, *fill));
8385
ensure!(id1 == id2, Error::CurrencyMismatch);
8486
Ok(())
8587
}
8688
(OutputValue::Coin(_), OutputValue::TokenV1(_, _))
8789
| (OutputValue::TokenV1(_, _), OutputValue::Coin(_)) => Err(Error::CurrencyMismatch),
8890
(OutputValue::TokenV0(_), _) | (_, OutputValue::TokenV0(_)) => {
89-
unreachable!()
91+
Err(Error::UnsupportedTokenVersion)
9092
}
9193
}
9294
}
@@ -133,7 +135,7 @@ mod tests {
133135
#[case(0, 0, 0, None)]
134136
#[case(0, 1, 1, None)]
135137
#[case(0, u128::MAX, 1, None)]
136-
#[case(3, u128::MAX, 2, None)]
138+
#[case(2, u128::MAX, 2, Some(u128::MAX))]
137139
#[case(1, 0, 0, Some(0))]
138140
#[case(1, 0, 1, Some(0))]
139141
#[case(1, 1, 1, Some(1))]
@@ -191,6 +193,7 @@ mod tests {
191193
#[case(coin!(3), coin!(100), coin!(2), 66)]
192194
#[case(coin!(3), coin!(100), coin!(3), 100)]
193195
#[case(coin!(1), token!(u128::MAX), coin!(1), u128::MAX)]
196+
#[case(coin!(2), token!(u128::MAX), coin!(2), u128::MAX)]
194197
fn fill_order_valid_values(
195198
#[case] ask: OutputValue,
196199
#[case] give: OutputValue,
@@ -219,7 +222,6 @@ mod tests {
219222
#[case(token!(0), coin!(1), token!(1), Error::OrderOverbid(OrderId::zero(), Amount::from_atoms(0), Amount::from_atoms(1)))]
220223
#[case(coin!(1), token!(1), coin!(2), Error::OrderOverbid(OrderId::zero(), Amount::from_atoms(1), Amount::from_atoms(2)))]
221224
#[case(coin!(1), token!(u128::MAX), coin!(2), Error::OrderOverbid(OrderId::zero(), Amount::from_atoms(1), Amount::from_atoms(2)))]
222-
#[case(coin!(2), token!(u128::MAX), coin!(2), Error::OrderOverflow(OrderId::zero()))]
223225
#[case(coin!(1), token!(1), token!(1), Error::CurrencyMismatch)]
224226
#[case(coin!(1), token!(1), token!(1), Error::CurrencyMismatch)]
225227
#[case(token!(1), token2!(1), token2!(1), Error::CurrencyMismatch)]

0 commit comments

Comments
 (0)