-
Notifications
You must be signed in to change notification settings - Fork 91
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
Update settlement contract balances in background task #2108
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
use { | ||
e2e::{setup::*, tx}, | ||
ethcontract::prelude::U256, | ||
model::{ | ||
order::{OrderCreation, OrderKind}, | ||
signature::EcdsaSigningScheme, | ||
}, | ||
secp256k1::SecretKey, | ||
shared::ethrpc::Web3, | ||
web3::signing::SecretKeyRef, | ||
}; | ||
|
||
#[tokio::test] | ||
#[ignore] | ||
async fn local_node_buffers() { | ||
run_test(onchain_settlement_without_liquidity).await; | ||
} | ||
|
||
async fn onchain_settlement_without_liquidity(web3: Web3) { | ||
let mut onchain = OnchainComponents::deploy(web3).await; | ||
|
||
let [solver] = onchain.make_solvers(to_wei(1)).await; | ||
let [trader] = onchain.make_accounts(to_wei(1)).await; | ||
let [token_a, token_b] = onchain | ||
.deploy_tokens_with_weth_uni_v2_pools(to_wei(1_000), to_wei(1_000)) | ||
.await; | ||
|
||
// Fund trader, settlement accounts, and pool creation | ||
token_a.mint(trader.address(), to_wei(100)).await; | ||
token_b | ||
.mint(onchain.contracts().gp_settlement.address(), to_wei(5)) | ||
.await; | ||
token_a.mint(solver.address(), to_wei(1000)).await; | ||
fleupold marked this conversation as resolved.
Show resolved
Hide resolved
|
||
token_b.mint(solver.address(), to_wei(1000)).await; | ||
|
||
// Approve GPv2 for trading | ||
tx!( | ||
trader.account(), | ||
token_a.approve(onchain.contracts().allowance, to_wei(100)) | ||
); | ||
|
||
// Start system | ||
let solver_endpoint = colocation::start_solver(onchain.contracts().weth.address()).await; | ||
colocation::start_driver(onchain.contracts(), &solver_endpoint, &solver); | ||
|
||
let services = Services::new(onchain.contracts()).await; | ||
services.start_autopilot(vec![ | ||
"--enable-colocation=true".to_string(), | ||
format!( | ||
"--trusted-tokens={weth:#x},{token_a:#x},{token_b:#x}", | ||
weth = onchain.contracts().weth.address(), | ||
token_a = token_a.address(), | ||
token_b = token_b.address() | ||
), | ||
"--drivers=test_solver|http://localhost:11088/test_solver".to_string(), | ||
]); | ||
services.start_api(vec![]).await; | ||
|
||
// Place Order | ||
let order = OrderCreation { | ||
sell_token: token_a.address(), | ||
sell_amount: to_wei(9), | ||
fee_amount: to_wei(1), | ||
buy_token: token_b.address(), | ||
buy_amount: to_wei(5), | ||
valid_to: model::time::now_in_epoch_seconds() + 300, | ||
kind: OrderKind::Buy, | ||
..Default::default() | ||
} | ||
.sign( | ||
EcdsaSigningScheme::Eip712, | ||
&onchain.contracts().domain_separator, | ||
SecretKeyRef::from(&SecretKey::from_slice(trader.private_key()).unwrap()), | ||
); | ||
services.create_order(&order).await.unwrap(); | ||
|
||
tracing::info!("waiting for first trade"); | ||
let trade_happened = | ||
|| async { token_b.balance_of(trader.address()).call().await.unwrap() == order.buy_amount }; | ||
wait_for_condition(TIMEOUT, trade_happened).await.unwrap(); | ||
|
||
// Check that settlement buffers were traded. | ||
let settlement_contract_balance = token_b | ||
.balance_of(onchain.contracts().gp_settlement.address()) | ||
.call() | ||
.await | ||
.unwrap(); | ||
// Check that internal buffers were used | ||
assert!(settlement_contract_balance == 0.into()); | ||
|
||
// Same order can trade again with external liquidity | ||
let order = OrderCreation { | ||
valid_to: model::time::now_in_epoch_seconds() + 301, | ||
..order | ||
} | ||
.sign( | ||
EcdsaSigningScheme::Eip712, | ||
&onchain.contracts().domain_separator, | ||
SecretKeyRef::from(&SecretKey::from_slice(trader.private_key()).unwrap()), | ||
); | ||
services.create_order(&order).await.unwrap(); | ||
|
||
tracing::info!("waiting for second trade"); | ||
let trade_happened = || async { | ||
token_b.balance_of(trader.address()).call().await.unwrap() == order.buy_amount * 2 | ||
}; | ||
wait_for_condition(TIMEOUT, trade_happened).await.unwrap(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
What is the purpose of the
INFO
level here?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.
It's supposed to help us to debug the rare race condition where we add a new token balance to the cache while the cache update is in flight.
The new item may not be updated (it should be up to date but in theory the block could also have just changed) and therefore we could pass a slightly outdated buffer list to the solvers. Again, I think this is extremely rare, but if it happens we can at least see if from the logs now.