-
Notifications
You must be signed in to change notification settings - Fork 29
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
Introduce TxInput::OrderAccountCommand
without nonces
#1888
base: master
Are you sure you want to change the base?
Changes from all commits
bef71a2
4e26298
fa881ef
36d9e08
b83e974
5343ebf
c78bc0b
df47aea
4a81f01
ad3e38d
3061854
384a8cb
2a29a48
f640306
3e99975
461b8fa
816b30b
1962093
b8a6529
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,8 +34,8 @@ use common::{ | |
tokens::{get_referenced_token_ids, make_token_id, IsTokenFrozen, TokenId, TokenIssuance}, | ||
transaction::OutPointSourceId, | ||
AccountCommand, AccountNonce, AccountSpending, Block, DelegationId, Destination, GenBlock, | ||
Genesis, OrderData, OrderId, PoolId, SignedTransaction, Transaction, TxInput, TxOutput, | ||
UtxoOutPoint, | ||
Genesis, OrderAccountCommand, OrderData, OrderId, PoolId, SignedTransaction, Transaction, | ||
TxInput, TxOutput, UtxoOutPoint, | ||
}, | ||
primitives::{id::WithId, Amount, BlockHeight, CoinOrTokenId, Fee, Id, Idable, H256}, | ||
}; | ||
|
@@ -631,6 +631,28 @@ async fn calculate_tx_fee_and_collect_token_info<T: ApiServerStorageWrite>( | |
}; | ||
} | ||
}, | ||
TxInput::OrderAccountCommand(cmd) => match cmd { | ||
OrderAccountCommand::FillOrder(order_id, _, _) | ||
| OrderAccountCommand::ConcludeOrder { | ||
order_id, | ||
filled_amount: _, | ||
remaining_give_amount: _, | ||
} => { | ||
let order = db_tx.get_order(*order_id).await?.expect("must exist"); | ||
match order.ask_currency { | ||
CoinOrTokenId::Coin => {} | ||
CoinOrTokenId::TokenId(id) => { | ||
token_ids.insert(id); | ||
} | ||
}; | ||
match order.give_currency { | ||
CoinOrTokenId::Coin => {} | ||
CoinOrTokenId::TokenId(id) => { | ||
token_ids.insert(id); | ||
} | ||
}; | ||
} | ||
}, | ||
}; | ||
} | ||
|
||
|
@@ -675,7 +697,9 @@ async fn fetch_utxo<T: ApiServerStorageRead>( | |
.expect("must be present"); | ||
Ok(Some(utxo)) | ||
} | ||
TxInput::Account(_) | TxInput::AccountCommand(_, _) => Ok(None), | ||
TxInput::Account(_) | TxInput::AccountCommand(_, _) | TxInput::OrderAccountCommand(_) => { | ||
Ok(None) | ||
} | ||
} | ||
} | ||
|
||
|
@@ -842,9 +866,40 @@ async fn prefetch_orders<T: ApiServerStorageRead>( | |
let mut ask_balances = BTreeMap::<OrderId, Amount>::new(); | ||
let mut give_balances = BTreeMap::<OrderId, Amount>::new(); | ||
|
||
let to_order_data = |order: &Order| { | ||
let ask = match order.ask_currency { | ||
CoinOrTokenId::Coin => OutputValue::Coin(order.initially_asked), | ||
CoinOrTokenId::TokenId(token_id) => { | ||
OutputValue::TokenV1(token_id, order.initially_asked) | ||
} | ||
}; | ||
let give = match order.give_currency { | ||
CoinOrTokenId::Coin => OutputValue::Coin(order.initially_given), | ||
CoinOrTokenId::TokenId(token_id) => { | ||
OutputValue::TokenV1(token_id, order.initially_given) | ||
} | ||
}; | ||
Comment on lines
+870
to
+881
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add method fn from_output_value(&self, Amount) to CoinOrTokenId? |
||
OrderData::new(order.conclude_destination.clone(), ask, give) | ||
}; | ||
|
||
for input in inputs { | ||
match input { | ||
TxInput::Utxo(_) | TxInput::Account(_) => {} | ||
TxInput::OrderAccountCommand(cmd) => match cmd { | ||
OrderAccountCommand::FillOrder(order_id, _, _) | ||
| OrderAccountCommand::ConcludeOrder { | ||
order_id, | ||
filled_amount: _, | ||
remaining_give_amount: _, | ||
} => { | ||
let order = db_tx.get_order(*order_id).await?.expect("must be present "); | ||
let order_data = to_order_data(&order); | ||
|
||
ask_balances.insert(*order_id, order.ask_balance); | ||
give_balances.insert(*order_id, order.give_balance); | ||
orders_data.insert(*order_id, order_data); | ||
} | ||
}, | ||
TxInput::AccountCommand(_, account_command) => match account_command { | ||
AccountCommand::MintTokens(_, _) | ||
| AccountCommand::UnmintTokens(_) | ||
|
@@ -856,21 +911,10 @@ async fn prefetch_orders<T: ApiServerStorageRead>( | |
AccountCommand::FillOrder(order_id, _, _) | ||
| AccountCommand::ConcludeOrder(order_id) => { | ||
let order = db_tx.get_order(*order_id).await?.expect("must be present "); | ||
let order_data = to_order_data(&order); | ||
|
||
ask_balances.insert(*order_id, order.ask_balance); | ||
give_balances.insert(*order_id, order.give_balance); | ||
let ask = match order.ask_currency { | ||
CoinOrTokenId::Coin => OutputValue::Coin(order.initially_asked), | ||
CoinOrTokenId::TokenId(token_id) => { | ||
OutputValue::TokenV1(token_id, order.initially_asked) | ||
} | ||
}; | ||
let give = match order.give_currency { | ||
CoinOrTokenId::Coin => OutputValue::Coin(order.initially_given), | ||
CoinOrTokenId::TokenId(token_id) => { | ||
OutputValue::TokenV1(token_id, order.initially_given) | ||
} | ||
}; | ||
let order_data = OrderData::new(order.conclude_destination.clone(), ask, give); | ||
orders_data.insert(*order_id, order_data); | ||
} | ||
}, | ||
|
@@ -922,7 +966,9 @@ async fn update_tables_from_consensus_data<T: ApiServerStorageWrite>( | |
) | ||
.await; | ||
} | ||
TxInput::Account(_) | TxInput::AccountCommand(_, _) => {} | ||
TxInput::Account(_) | ||
| TxInput::AccountCommand(_, _) | ||
| TxInput::OrderAccountCommand(_) => {} | ||
} | ||
} | ||
|
||
|
@@ -1215,7 +1261,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?; | ||
} | ||
|
@@ -1226,6 +1273,25 @@ async fn update_tables_from_transaction_inputs<T: ApiServerStorageWrite>( | |
db_tx.set_order_at_height(*order_id, &order, block_height).await?; | ||
} | ||
}, | ||
TxInput::OrderAccountCommand(cmd) => match cmd { | ||
OrderAccountCommand::FillOrder(order_id, fill_amount_in_ask_currency, _) => { | ||
let order = db_tx.get_order(*order_id).await?.expect("must exist"); | ||
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?; | ||
} | ||
OrderAccountCommand::ConcludeOrder { | ||
order_id, | ||
filled_amount: _, | ||
remaining_give_amount: _, | ||
} => { | ||
let order = db_tx.get_order(*order_id).await?.expect("must exist"); | ||
let order = order.conclude(); | ||
|
||
db_tx.set_order_at_height(*order_id, &order, block_height).await?; | ||
} | ||
}, | ||
TxInput::Account(outpoint) => { | ||
match outpoint.account() { | ||
AccountSpending::DelegationBalance(delegation_id, amount) => { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -646,6 +646,7 @@ fn update_statistics( | |
} | ||
AccountCommand::ConcludeOrder(_) | AccountCommand::FillOrder(_, _, _) => {} | ||
}, | ||
TxInput::OrderAccountCommand(..) => {} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So I guess these tests don't currently use OrderAccountCommand? |
||
}); | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,7 +13,7 @@ | |
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
use common::chain::{make_order_id, AccountCommand, AccountNonce, OrderData}; | ||
use common::chain::{make_order_id, OrderAccountCommand, OrderData}; | ||
|
||
use super::*; | ||
|
||
|
@@ -68,14 +68,11 @@ async fn create_fill_conclude_order(#[case] seed: Seed) { | |
InputWitness::NoSignature(None), | ||
) | ||
.add_input( | ||
TxInput::AccountCommand( | ||
AccountNonce::new(0), | ||
AccountCommand::FillOrder( | ||
order_id, | ||
Amount::from_atoms(1), | ||
Destination::AnyoneCanSpend, | ||
), | ||
), | ||
TxInput::OrderAccountCommand(OrderAccountCommand::FillOrder( | ||
order_id, | ||
Comment on lines
70
to
+72
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not parameterize this test to check both old and new commands? |
||
Amount::from_atoms(1), | ||
Destination::AnyoneCanSpend, | ||
)), | ||
InputWitness::NoSignature(None), | ||
) | ||
.add_output(TxOutput::Transfer( | ||
|
@@ -91,10 +88,11 @@ async fn create_fill_conclude_order(#[case] seed: Seed) { | |
// Conclude order | ||
let tx3 = TransactionBuilder::new() | ||
.add_input( | ||
TxInput::AccountCommand( | ||
AccountNonce::new(1), | ||
AccountCommand::ConcludeOrder(order_id), | ||
), | ||
TxInput::OrderAccountCommand(OrderAccountCommand::ConcludeOrder { | ||
order_id, | ||
filled_amount: Amount::from_atoms(1), | ||
remaining_give_amount: Amount::from_atoms(9), | ||
}), | ||
InputWitness::NoSignature(None), | ||
) | ||
.add_output(TxOutput::Transfer( | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,8 +24,8 @@ use common::{ | |
block::ConsensusData, | ||
output_value::OutputValue, | ||
tokens::{IsTokenUnfreezable, NftIssuance, TokenId, TokenTotalSupply}, | ||
AccountCommand, AccountSpending, Block, ChainConfig, Destination, OrderId, | ||
OutPointSourceId, Transaction, TxInput, TxOutput, UtxoOutPoint, | ||
AccountCommand, AccountSpending, Block, ChainConfig, Destination, OrderAccountCommand, | ||
OrderId, OutPointSourceId, Transaction, TxInput, TxOutput, UtxoOutPoint, | ||
}, | ||
primitives::{Amount, BlockHeight, CoinOrTokenId, Idable}, | ||
Uint256, | ||
|
@@ -318,6 +318,30 @@ pub fn tx_input_to_json(inp: &TxInput, chain_config: &ChainConfig) -> serde_json | |
}) | ||
} | ||
}, | ||
TxInput::OrderAccountCommand(cmd) => match cmd { | ||
OrderAccountCommand::FillOrder(id, fill, dest) => { | ||
json!({ | ||
"input_type": "OrderAccountCommand", | ||
"command": "FillOrder", | ||
"order_id": Address::new(chain_config, *id).expect("addressable").to_string(), | ||
"fill_atoms": json!({"atoms": fill.into_atoms().to_string()}), | ||
"destination": Address::new(chain_config, dest.clone()).expect("no error").as_str(), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IMO we should try to get rid of all these optimistic I'm not insisting on doing it right now, just a thing to consider. (If you keep the "expect"s, plz at least use consistent messages) |
||
}) | ||
} | ||
OrderAccountCommand::ConcludeOrder { | ||
order_id, | ||
filled_amount, | ||
remaining_give_amount, | ||
} => { | ||
json!({ | ||
"input_type": "OrderAccountCommand", | ||
"command": "ConcludeOrder", | ||
"order_id": Address::new(chain_config, *order_id).expect("addressable").to_string(), | ||
"filled_atoms": json!({"atoms": filled_amount.into_atoms().to_string()}), | ||
"remaining_give_atoms": json!({"atoms": remaining_give_amount.into_atoms().to_string()}), | ||
}) | ||
} | ||
}, | ||
TxInput::AccountCommand(nonce, cmd) => match cmd { | ||
AccountCommand::MintTokens(token_id, amount) => { | ||
json!({ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This
match
is repeated in many places. Maybe add a methodCoinOrTokenId::token_id(&self) -> Option<TokenId>
?Or maybe 2 separate ones:
token_id(&self) -> Option<&TokenId>
andinto_token_id(self) -> Option<TokenId>
.