Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide Amount instead of OutputValue to FillOrder command #1819

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions api-server/stack-test-suite/tests/v2/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ async fn multiple_tx_in_same_block(#[case] seed: Seed) {
"is_replaceable": transaction.is_replaceable(),
"flags": transaction.flags(),
"inputs": transaction.inputs().iter().zip(utxos).map(|(inp, utxo)| json!({
"input": tx_input_to_json(inp, &chain_config, &TokenDecimals::Single(None)),
"input": tx_input_to_json(inp, &chain_config),
"utxo": utxo.as_ref().map(|txo| txoutput_to_json(txo, &chain_config, &TokenDecimals::Single(None))),
})).collect::<Vec<_>>(),
"outputs": transaction.outputs()
Expand Down Expand Up @@ -338,7 +338,7 @@ async fn ok(#[case] seed: Seed) {
"is_replaceable": transaction.is_replaceable(),
"flags": transaction.flags(),
"inputs": transaction.inputs().iter().zip(utxos).map(|(inp, utxo)| json!({
"input": tx_input_to_json(inp, &chain_config, &TokenDecimals::Single(None)),
"input": tx_input_to_json(inp, &chain_config),
"utxo": utxo.as_ref().map(|txo| txoutput_to_json(txo, &chain_config, &TokenDecimals::Single(None))),
})).collect::<Vec<_>>(),
"outputs": transaction.outputs()
Expand Down
11 changes: 4 additions & 7 deletions api-server/web-server/src/api/json_helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,11 +254,7 @@ pub fn utxo_outpoint_to_json(utxo: &UtxoOutPoint) -> serde_json::Value {
}
}

pub fn tx_input_to_json(
inp: &TxInput,
chain_config: &ChainConfig,
token_decimals: &TokenDecimals,
) -> serde_json::Value {
pub fn tx_input_to_json(inp: &TxInput, chain_config: &ChainConfig) -> serde_json::Value {
match inp {
TxInput::Utxo(utxo) => match utxo.source_id() {
OutPointSourceId::Transaction(tx_id) => {
Expand Down Expand Up @@ -357,7 +353,8 @@ pub fn tx_input_to_json(
"input_type": "AccountCommand",
"command": "FillOrder",
"order_id": Address::new(chain_config, *order_id).expect("addressable").to_string(),
"fill_value": outputvalue_to_json(fill, chain_config, token_decimals),
// TODO(orders)
"fill_atoms": json!({"atoms": fill.into_atoms().to_string()}),
"destination": Address::new(chain_config, dest.clone()).expect("no error").as_str(),
})
}
Expand Down Expand Up @@ -385,7 +382,7 @@ pub fn tx_to_json(
"flags": tx.flags(),
"fee": amount_to_json(additional_info.fee, chain_config.coin_decimals()),
"inputs": tx.inputs().iter().zip(additional_info.input_utxos.iter()).map(|(inp, utxo)| json!({
"input": tx_input_to_json(inp, chain_config, &(&additional_info.token_decimals).into()),
"input": tx_input_to_json(inp, chain_config),
"utxo": utxo.as_ref().map(|txo| txoutput_to_json(txo, chain_config, &(&additional_info.token_decimals).into())),
})).collect::<Vec<_>>(),
"outputs": tx.outputs()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -327,20 +327,20 @@ impl ConstrainedValueAccumulator {

Ok((CoinOrTokenId::Coin, Amount::ZERO))
}
AccountCommand::FillOrder(id, fill_value, _) => {
AccountCommand::FillOrder(id, fill_amount, _) => {
let order_data = orders_accounting_delta
.get_order_data(id)
.map_err(|_| orders_accounting::Error::ViewFail)?
.ok_or(orders_accounting::Error::OrderDataNotFound(*id))?;
let filled_amount = orders_accounting::calculate_fill_order(
&orders_accounting_delta,
*id,
fill_value,
*fill_amount,
)?;

{
// Ensure that spending won't result in negative balance
let _ = orders_accounting_delta.fill_order(*id, fill_value.clone())?;
let _ = orders_accounting_delta.fill_order(*id, *fill_amount)?;
let _ = orders_accounting_delta.get_ask_balance(id)?;
let _ = orders_accounting_delta.get_give_balance(id)?;
}
Expand All @@ -349,10 +349,10 @@ impl ConstrainedValueAccumulator {
.ok_or(Error::UnsupportedTokenVersion)?;
insert_or_increase(&mut self.unconstrained_value, give_currency, filled_amount)?;

let ask_currency = CoinOrTokenId::from_output_value(fill_value)
let ask_currency = CoinOrTokenId::from_output_value(order_data.ask())
.ok_or(Error::UnsupportedTokenVersion)?;

Ok((ask_currency, output_value_amount(fill_value)?))
Ok((ask_currency, *fill_amount))
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ fn fill_order_constraints(#[case] seed: Seed) {
AccountNonce::new(0),
AccountCommand::FillOrder(
order_id,
OutputValue::TokenV1(token_id, (ask_amount + Amount::from_atoms(1)).unwrap()),
(ask_amount + Amount::from_atoms(1)).unwrap(),
Destination::AnyoneCanSpend,
),
),
Expand Down Expand Up @@ -335,11 +335,7 @@ fn fill_order_constraints(#[case] seed: Seed) {
)),
TxInput::AccountCommand(
AccountNonce::new(0),
AccountCommand::FillOrder(
order_id,
OutputValue::Coin(ask_amount),
Destination::AnyoneCanSpend,
),
AccountCommand::FillOrder(order_id, ask_amount, Destination::AnyoneCanSpend),
),
];
let input_utxos = vec![
Expand All @@ -360,10 +356,7 @@ fn fill_order_constraints(#[case] seed: Seed) {
&input_utxos,
);

assert_eq!(
result.unwrap_err(),
Error::OrdersAccountingError(orders_accounting::Error::CurrencyMismatch)
);
assert_eq!(result.unwrap_err(), Error::AttemptToViolateFeeRequirements);
}

// try to print coins in output
Expand All @@ -375,11 +368,7 @@ fn fill_order_constraints(#[case] seed: Seed) {
)),
TxInput::AccountCommand(
AccountNonce::new(0),
AccountCommand::FillOrder(
order_id,
OutputValue::TokenV1(token_id, ask_amount),
Destination::AnyoneCanSpend,
),
AccountCommand::FillOrder(order_id, ask_amount, Destination::AnyoneCanSpend),
),
];
let input_utxos = vec![
Expand Down Expand Up @@ -427,11 +416,7 @@ fn fill_order_constraints(#[case] seed: Seed) {
)),
TxInput::AccountCommand(
AccountNonce::new(0),
AccountCommand::FillOrder(
order_id,
OutputValue::TokenV1(token_id, ask_amount),
Destination::AnyoneCanSpend,
),
AccountCommand::FillOrder(order_id, ask_amount, Destination::AnyoneCanSpend),
),
];
let input_utxos = vec![
Expand Down Expand Up @@ -484,11 +469,7 @@ fn fill_order_constraints(#[case] seed: Seed) {
)),
TxInput::AccountCommand(
AccountNonce::new(0),
AccountCommand::FillOrder(
order_id,
OutputValue::TokenV1(token_id, ask_amount),
Destination::AnyoneCanSpend,
),
AccountCommand::FillOrder(order_id, ask_amount, Destination::AnyoneCanSpend),
),
];
let input_utxos = vec![
Expand Down Expand Up @@ -539,11 +520,7 @@ fn fill_order_constraints(#[case] seed: Seed) {
)),
TxInput::AccountCommand(
AccountNonce::new(0),
AccountCommand::FillOrder(
order_id,
OutputValue::TokenV1(token_id, ask_amount),
Destination::AnyoneCanSpend,
),
AccountCommand::FillOrder(order_id, ask_amount, Destination::AnyoneCanSpend),
),
];
let input_utxos = vec![
Expand Down
1 change: 0 additions & 1 deletion chainstate/src/detail/ban_score.rs
Original file line number Diff line number Diff line change
Expand Up @@ -674,7 +674,6 @@ impl BanScore for orders_accounting::Error {
Error::InvariantOrderDataExistForConcludeUndo(_) => 100,
Error::InvariantOrderAskBalanceExistForConcludeUndo(_) => 100,
Error::InvariantOrderGiveBalanceExistForConcludeUndo(_) => 100,
Error::CurrencyMismatch => 100,
Error::OrderOverflow(_) => 100,
Error::OrderOverbid(_, _, _) => 100,
Error::AttemptedConcludeNonexistingOrderData(_) => 100,
Expand Down
1 change: 0 additions & 1 deletion chainstate/src/detail/error_classification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -909,7 +909,6 @@ impl BlockProcessingErrorClassification for orders_accounting::Error {
| Error::InvariantOrderDataExistForConcludeUndo(_)
| Error::InvariantOrderAskBalanceExistForConcludeUndo(_)
| Error::InvariantOrderGiveBalanceExistForConcludeUndo(_)
| Error::CurrencyMismatch
| Error::OrderOverflow(_)
| Error::OrderOverbid(_, _, _)
| Error::AttemptedConcludeNonexistingOrderData(_)
Expand Down
6 changes: 2 additions & 4 deletions chainstate/src/rpc/types/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ use common::{
};
use rpc::types::RpcHexString;

use super::output::RpcOutputValue;

#[derive(Debug, Clone, serde::Serialize, rpc_description::HasValueHint)]
#[serde(tag = "type", content = "content")]
pub enum RpcAccountSpending {
Expand Down Expand Up @@ -84,7 +82,7 @@ pub enum RpcAccountCommand {
},
FillOrder {
order_id: RpcAddress<OrderId>,
fill_value: RpcOutputValue,
fill_value: RpcAmountOut,
destination: RpcAddress<Destination>,
},
}
Expand Down Expand Up @@ -129,7 +127,7 @@ impl RpcAccountCommand {
},
AccountCommand::FillOrder(id, fill, dest) => RpcAccountCommand::FillOrder {
order_id: RpcAddress::new(chain_config, *id)?,
fill_value: RpcOutputValue::new(chain_config, fill.clone())?,
fill_value: RpcAmountOut::from_amount(*fill, chain_config.coin_decimals()),
destination: RpcAddress::new(chain_config, dest.clone())?,
},
};
Expand Down
24 changes: 14 additions & 10 deletions chainstate/test-framework/src/random_tx_maker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1010,15 +1010,15 @@ impl<'a> RandomTxMaker<'a> {
get_random_order_to_fill(self.orders_store, &orders_cache, &fill_value)
{
let filled_value =
calculate_filled_order_value(&orders_cache, order_id, &fill_value);
calculate_filled_order_value(&orders_cache, order_id, amount_to_spend);

if !is_frozen_token(&filled_value, tokens_cache) {
let new_nonce = self.get_next_nonce(AccountType::Order(order_id));
let input = TxInput::AccountCommand(
new_nonce,
AccountCommand::FillOrder(
order_id,
fill_value.clone(),
amount_to_spend,
key_manager
.new_destination(self.chainstate.get_chain_config(), rng),
),
Expand All @@ -1029,7 +1029,7 @@ impl<'a> RandomTxMaker<'a> {
key_manager.new_destination(self.chainstate.get_chain_config(), rng),
);

let _ = orders_cache.fill_order(order_id, fill_value).unwrap();
let _ = orders_cache.fill_order(order_id, amount_to_spend).unwrap();
self.account_command_used = true;

result_inputs.push(input);
Expand Down Expand Up @@ -1236,8 +1236,11 @@ impl<'a> RandomTxMaker<'a> {
if let Some(order_id) =
get_random_order_to_fill(self.orders_store, &orders_cache, &output_value)
{
let filled_value =
calculate_filled_order_value(&orders_cache, order_id, &output_value);
let filled_value = calculate_filled_order_value(
&orders_cache,
order_id,
Amount::from_atoms(atoms),
);

if !is_frozen_token(&filled_value, tokens_cache) {
result_outputs.push(TxOutput::Transfer(
Expand All @@ -1251,13 +1254,15 @@ impl<'a> RandomTxMaker<'a> {
new_nonce,
AccountCommand::FillOrder(
order_id,
output_value.clone(),
Amount::from_atoms(atoms),
key_manager
.new_destination(self.chainstate.get_chain_config(), rng),
),
));

let _ = orders_cache.fill_order(order_id, output_value).unwrap();
let _ = orders_cache
.fill_order(order_id, Amount::from_atoms(atoms))
.unwrap();
self.account_command_used = true;
}
}
Expand Down Expand Up @@ -1478,12 +1483,11 @@ fn is_frozen_token(value: &OutputValue, view: &impl TokensAccountingView) -> boo
fn calculate_filled_order_value(
view: &impl OrdersAccountingView,
order_id: OrderId,
fill_value: &OutputValue,
fill: Amount,
) -> OutputValue {
let order_data = view.get_order_data(&order_id).unwrap().unwrap();

let filled_amount =
orders_accounting::calculate_fill_order(view, order_id, fill_value).unwrap();
let filled_amount = orders_accounting::calculate_fill_order(view, order_id, fill).unwrap();

output_value_with_amount(order_data.give(), filled_amount)
}
Loading
Loading