-
Notifications
You must be signed in to change notification settings - Fork 152
Reimplement sell=buy PoC PR #3894
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
base: main
Are you sure you want to change the base?
Changes from all commits
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 |
|---|---|---|
|
|
@@ -19,10 +19,22 @@ use { | |
|
|
||
| #[tokio::test] | ||
| #[ignore] | ||
| async fn local_node_test() { | ||
| async fn local_node_place_order_with_quote_basic() { | ||
| run_test(place_order_with_quote).await; | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| #[ignore] | ||
| async fn local_node_place_order_with_quote_same_token_pair() { | ||
| run_test(place_order_with_quote_same_token_pair).await; | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| #[ignore] | ||
| async fn local_node_place_order_with_quote_same_token_pair_error() { | ||
| run_test(place_order_with_quote_same_token_pair_error).await; | ||
| } | ||
|
|
||
| async fn place_order_with_quote(web3: Web3) { | ||
| let mut onchain = OnchainComponents::deploy(web3.clone()).await; | ||
|
|
||
|
|
@@ -118,3 +130,136 @@ async fn place_order_with_quote(web3: Web3) { | |
| assert_eq!(quote_response.verified, order_quote.verified); | ||
| assert_eq!(quote_metadata.unwrap().0, order_quote.metadata); | ||
| } | ||
|
|
||
| async fn place_order_with_quote_same_token_pair_error(web3: Web3) { | ||
| let mut onchain = OnchainComponents::deploy(web3.clone()).await; | ||
|
|
||
| let [solver] = onchain.make_solvers(to_wei(10)).await; | ||
| let [trader] = onchain.make_accounts(to_wei(10)).await; | ||
| let [token] = onchain | ||
| .deploy_tokens_with_weth_uni_v2_pools(to_wei(1_000), to_wei(1_000)) | ||
| .await; | ||
|
|
||
| token.mint(trader.address(), to_wei(10)).await; | ||
|
|
||
| token | ||
| .approve(onchain.contracts().allowance.into_alloy(), eth(10)) | ||
| .from(trader.address().into_alloy()) | ||
| .send_and_watch() | ||
| .await | ||
| .unwrap(); | ||
|
|
||
| tracing::info!("Starting services."); | ||
| let services = Services::new(&onchain).await; | ||
| services.start_protocol(solver.clone()).await; | ||
|
|
||
| // Disable auto-mine so we don't accidentally mine a settlement | ||
| web3.api::<TestNodeApi<_>>() | ||
| .set_automine_enabled(false) | ||
| .await | ||
| .expect("Must be able to disable automine"); | ||
|
|
||
| tracing::info!("Quoting"); | ||
| let quote_sell_amount = to_wei(1); | ||
| let quote_request = OrderQuoteRequest { | ||
| from: trader.address(), | ||
| sell_token: token.address().into_legacy(), | ||
| buy_token: token.address().into_legacy(), | ||
| side: OrderQuoteSide::Sell { | ||
| sell_amount: SellAmount::BeforeFee { | ||
| value: NonZeroU256::try_from(quote_sell_amount).unwrap(), | ||
| }, | ||
| }, | ||
| ..Default::default() | ||
| }; | ||
| assert!(services.submit_quote("e_request).await.is_err()); | ||
| } | ||
|
|
||
| async fn place_order_with_quote_same_token_pair(web3: Web3) { | ||
| let mut onchain = OnchainComponents::deploy(web3.clone()).await; | ||
|
|
||
| let [solver] = onchain.make_solvers(to_wei(10)).await; | ||
| let [trader] = onchain.make_accounts(to_wei(10)).await; | ||
| let [token] = onchain | ||
| .deploy_tokens_with_weth_uni_v2_pools(to_wei(1_000), to_wei(1_000)) | ||
| .await; | ||
|
|
||
| token.mint(trader.address(), to_wei(10)).await; | ||
|
|
||
| token | ||
| .approve(onchain.contracts().allowance.into_alloy(), eth(10)) | ||
| .from(trader.address().into_alloy()) | ||
| .send_and_watch() | ||
| .await | ||
| .unwrap(); | ||
|
|
||
| tracing::info!("Starting services."); | ||
| let services = Services::new(&onchain).await; | ||
| services | ||
| .start_protocol_with_args( | ||
| ExtraServiceArgs { | ||
| api: vec!["--allow-same-sell-and-buy-token=true".to_string()], | ||
| ..Default::default() | ||
| }, | ||
| solver.clone(), | ||
| ) | ||
| .await; | ||
|
|
||
| // Disable auto-mine so we don't accidentally mine a settlement | ||
| web3.api::<TestNodeApi<_>>() | ||
| .set_automine_enabled(false) | ||
| .await | ||
| .expect("Must be able to disable automine"); | ||
|
|
||
| tracing::info!("Quoting"); | ||
| let quote_sell_amount = to_wei(1); | ||
| let quote_request = OrderQuoteRequest { | ||
| from: trader.address(), | ||
| sell_token: token.address().into_legacy(), | ||
| buy_token: token.address().into_legacy(), | ||
| side: OrderQuoteSide::Sell { | ||
| sell_amount: SellAmount::BeforeFee { | ||
| value: NonZeroU256::try_from(quote_sell_amount).unwrap(), | ||
| }, | ||
| }, | ||
| ..Default::default() | ||
| }; | ||
| let quote_response = services.submit_quote("e_request).await.unwrap(); | ||
| tracing::debug!(?quote_response); | ||
| assert!(quote_response.id.is_some()); | ||
| assert!(quote_response.verified); | ||
|
|
||
| let quote_metadata = | ||
| crate::database::quote_metadata(services.db(), quote_response.id.unwrap()).await; | ||
| assert!(quote_metadata.is_some()); | ||
| tracing::debug!(?quote_metadata); | ||
|
|
||
| tracing::info!("Placing order"); | ||
| let order = OrderCreation { | ||
| quote_id: quote_response.id, | ||
| sell_token: token.address().into_legacy(), | ||
| sell_amount: quote_sell_amount, | ||
| buy_token: token.address().into_legacy(), | ||
| buy_amount: quote_response.quote.buy_amount, | ||
| valid_to: model::time::now_in_epoch_seconds() + 300, | ||
| kind: OrderKind::Sell, | ||
| ..Default::default() | ||
| } | ||
| .sign( | ||
| EcdsaSigningScheme::Eip712, | ||
| &onchain.contracts().domain_separator, | ||
| SecretKeyRef::from(&SecretKey::from_slice(trader.private_key()).unwrap()), | ||
| ); | ||
| let order_uid = services.create_order(&order).await.unwrap(); | ||
|
|
||
| tracing::info!("Order quote verification"); | ||
| let order_quote = database::orders::read_quote( | ||
| services.db().acquire().await.unwrap().deref_mut(), | ||
| &database::byte_array::ByteArray(order_uid.0), | ||
| ) | ||
| .await | ||
| .unwrap() | ||
| .unwrap(); | ||
| assert_eq!(quote_response.verified, order_quote.verified); | ||
|
Contributor
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. What are these asserts for? 🤔 What does it signify that the order has a verified quote and has metadata?
Contributor
Author
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. These assertions are also present on the normal place_order_with_quote and it makes sure that the quote and order's quote are verified. |
||
| assert_eq!(quote_metadata.unwrap().0, order_quote.metadata); | ||
| } | ||
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.
Not related to your changes, but it's super confusing that the method is called liquidity_pairs (plural) when it only returns one pair. 😵💫
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.
I will figure out a more fitting name