Skip to content

Commit

Permalink
Handle both versions in api-server
Browse files Browse the repository at this point in the history
  • Loading branch information
azarovh committed Feb 28, 2025
1 parent 20727ab commit fdae0b9
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 5 deletions.
20 changes: 17 additions & 3 deletions api-server/api-server-common/src/storage/storage_api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,10 +194,24 @@ pub struct Order {
}

impl Order {
pub fn fill(self, fill_amount_in_ask_currency: Amount) -> Self {
pub fn fill(
self,
chain_config: &ChainConfig,
block_height: BlockHeight,
fill_amount_in_ask_currency: Amount,
) -> Self {
let (ask_balance, give_balance) = match chain_config
.chainstate_upgrades()
.version_at_height(block_height)
.1
.orders_version()
{
common::chain::OrdersVersion::V0 => (self.ask_balance, self.give_balance),
common::chain::OrdersVersion::V1 => (self.initially_asked, self.initially_given),
};
let filled_amount = orders_accounting::calculate_filled_amount(
self.initially_asked,
self.initially_given,
ask_balance,
give_balance,
fill_amount_in_ask_currency,
)
.expect("must succeed");
Expand Down
3 changes: 2 additions & 1 deletion api-server/scanner-lib/src/blockchain_state/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1260,7 +1260,8 @@ async fn update_tables_from_transaction_inputs<T: ApiServerStorageWrite>(
}
AccountCommand::FillOrder(order_id, fill_amount_in_ask_currency, _) => {
let order = db_tx.get_order(*order_id).await?.expect("must exist");
let order = order.fill(*fill_amount_in_ask_currency);
let order =
order.fill(&chain_config, block_height, *fill_amount_in_ask_currency);

db_tx.set_order_at_height(*order_id, &order, block_height).await?;
}
Expand Down
3 changes: 2 additions & 1 deletion api-server/storage-test-suite/src/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1430,6 +1430,7 @@ async fn orders<'a, S: for<'b> Transactional<'b>>(
rng: &mut (impl Rng + CryptoRng),
storage: &'a mut S,
) {
let chain_config = common::chain::config::create_regtest();
{
let db_tx = storage.transaction_ro().await.unwrap();
let random_order_id = OrderId::new(H256::random_using(rng));
Expand Down Expand Up @@ -1542,7 +1543,7 @@ async fn orders<'a, S: for<'b> Transactional<'b>>(
);

// Fill one order
let order2_filled = order2.clone().fill(Amount::from_atoms(1));
let order2_filled = order2.clone().fill(&chain_config, block_height, Amount::from_atoms(1));
db_tx
.set_order_at_height(order2_id, &order2_filled, block_height.next_height())
.await
Expand Down

0 comments on commit fdae0b9

Please sign in to comment.