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

991 get vault balances for an order in cli #996

Open
wants to merge 34 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
844fe4c
Create balance.rs
ninokeldishvili Nov 13, 2024
c68bbd9
Merge branch 'main' into 991-get-vault-balances-for-an-order-in-cli
ninokeldishvili Nov 13, 2024
61eb01b
Merge branch 'main' into 991-get-vault-balances-for-an-order-in-cli
ninokeldishvili Nov 14, 2024
b334069
Add balance
ninokeldishvili Nov 14, 2024
021e4f3
Update balance fetching
ninokeldishvili Nov 14, 2024
b72678d
Update lib for balances
ninokeldishvili Nov 14, 2024
dbb7124
Add test
ninokeldishvili Nov 14, 2024
6139da9
Add serde_json in dependencies
ninokeldishvili Nov 15, 2024
10aeb41
Fix build problems
ninokeldishvili Nov 15, 2024
8c1ca2d
Merge branch 'main' into 991-get-vault-balances-for-an-order-in-cli
ninokeldishvili Nov 18, 2024
16df9ca
Import VaultsListQuery
ninokeldishvili Nov 18, 2024
d2997f7
Use OrdersListQuery
ninokeldishvili Nov 19, 2024
ceba747
Add balance in mod list. fix build issues
ninokeldishvili Nov 19, 2024
fb377df
Merge branch 'main' into 991-get-vault-balances-for-an-order-in-cli
ninokeldishvili Nov 19, 2024
c5a8c46
Remove tokio import
ninokeldishvili Nov 19, 2024
e28ec75
Add test with mock call and check output balance
ninokeldishvili Nov 19, 2024
dd70122
Add some comments
ninokeldishvili Nov 19, 2024
9311ba6
Merge branch 'main' into 991-get-vault-balances-for-an-order-in-cli
ninokeldishvili Nov 20, 2024
7797e4a
Add some test_get_balances_data_mainnet with hardcoded url
ninokeldishvili Nov 20, 2024
722c539
Use env SUBGRAPH_URL for tests
ninokeldishvili Nov 20, 2024
c8d24da
Remove mock test
ninokeldishvili Nov 20, 2024
2aef5df
Merge branch 'main' into 991-get-vault-balances-for-an-order-in-cli
ninokeldishvili Nov 21, 2024
a6879d9
Update env variable name for subgraph and add variable in yaml
ninokeldishvili Nov 21, 2024
960c82c
Change secrets with vars
ninokeldishvili Nov 21, 2024
c1cdbae
Merge branch 'main' into 991-get-vault-balances-for-an-order-in-cli
ninokeldishvili Nov 21, 2024
f99aaf2
Remove comments and unnecessary println
ninokeldishvili Nov 21, 2024
2190a63
Update get_balances_single_order function and get specific order by hash
ninokeldishvili Nov 21, 2024
0300afa
Merge branch 'main' into 991-get-vault-balances-for-an-order-in-cli
thedavidmeister Nov 21, 2024
9b0a0c4
Remove decode/encode for order_hash
ninokeldishvili Nov 21, 2024
ec58107
Merge branch 'main' into 991-get-vault-balances-for-an-order-in-cli
ninokeldishvili Nov 22, 2024
0d1c3ad
Merge branch 'main' into 991-get-vault-balances-for-an-order-in-cli
thedavidmeister Nov 22, 2024
fbe6e13
Merge branch 'main' into 991-get-vault-balances-for-an-order-in-cli
ninokeldishvili Nov 25, 2024
cad28c8
Update test
ninokeldishvili Nov 25, 2024
e3d2f48
Merge branch 'main' into 991-get-vault-balances-for-an-order-in-cli
ninokeldishvili Nov 27, 2024
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
1 change: 1 addition & 0 deletions .github/workflows/rainix.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ jobs:
RPC_URL_ETHEREUM_FORK: ${{ secrets.RPC_URL_ETHEREUM_FORK }}
CI_DEPLOY_FLARE_RPC_URL: ${{ secrets.CI_DEPLOY_FLARE_RPC_URL }}
COMMIT_SHA: ${{ github.sha }}
ORDERBOOK_MAINNET_SUBGRAPH_URL: ${{ vars.ORDERBOOK_MAINNET_SUBGRAPH_URL }}
steps:
- uses: actions/checkout@v4
with:
Expand Down
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions crates/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@ reqwest = { workspace = true }
rust-bigint = { workspace = true }
serde = { workspace = true }
serde_bytes = { workspace = true }
serde_json = { workspace = true }
tracing = { workspace = true }
tracing-subscriber = { workspace = true, features = ['env-filter'] }
comfy-table = { workspace = true }
chrono = { workspace = true }
csv = { workspace = true }
cynic = { workspace = true }

[target.'cfg(not(target_family = "wasm"))'.dependencies]
tokio = { workspace = true, features = ["full"] }
Expand Down
133 changes: 133 additions & 0 deletions crates/cli/src/balance.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
use alloy::hex;
use anyhow::Result;
use cynic::QueryBuilder;
use rain_orderbook_subgraph_client::types::common::{
Bytes, OrdersListQueryFilters, OrdersListQueryVariables,
};
use rain_orderbook_subgraph_client::types::order::OrdersListQuery;
use reqwest::Client;
use serde_json::Value;

/// Fetches data from subgraph
async fn fetch_order_details(url: &str, variables: OrdersListQueryVariables) -> Result<Value> {
let client = Client::new();

// Build the GraphQL query with the provided variables.
let query = OrdersListQuery::build(variables);

let req = client
.post(url)
.header("Content-Type", "application/json")
.json(&query)
.send()
.await?;

let text = req.text().await?;

// Parse the response JSON.
let response: Value = serde_json::from_str(&text)?;
Ok(serde_json::from_str(&text)?)
}

/// Retrieves data from subgraph and checks for errors in the response.
async fn get_data(url: &str, variables: OrdersListQueryVariables) -> Result<Value> {
let data = fetch_order_details(url, variables).await?;
if let Some(errors) = data.get("errors") {
return Err(anyhow::anyhow!("Error(s) occurred: {:?}", errors));
}
Ok(data)
}

pub async fn get_balances_single_order(subgraph_url: &str, order_hash: &str) -> Result<Value> {
let hex_order_hash = &order_hash[2..];

let variables = OrdersListQueryVariables {
skip: None, // No need to skip when querying a specific order
first: None, // No need to limit since we expect a single result
filters: Some(OrdersListQueryFilters {
owner_in: Vec::new(), // Not filtering by owner
active: None, // Not filtering by active
order_hash: Some(Bytes(hex_order_hash.to_string())), // Pass the hex string to Bytes
}),
};

let res = get_data(subgraph_url, variables).await?;
Ok(res)
}

#[cfg(test)]
mod tests {
use super::*;

#[tokio::test]
async fn test_get_balances_is_ok() {
let orderbook_mainnet_subgraph_url = std::env::var("ORDERBOOK_MAINNET_SUBGRAPH_URL")
.expect("Environment variable ORDERBOOK_MAINNET_SUBGRAPH_URL must be set for tests.");

let order_hash = "0x12863c37d7dd314984b237619f569f6f6f645383bb39aec4cb219abd52f8eff2";

let result = get_balances_single_order(&orderbook_mainnet_subgraph_url, order_hash).await;
assert!(result.is_ok(), "Failed to fetch balances: {:?}", result);
}

#[tokio::test]
async fn test_get_balances_data_mainnet() {
let orderbook_mainnet_subgraph_url = std::env::var("ORDERBOOK_MAINNET_SUBGRAPH_URL")
.expect("Environment variable ORDERBOOK_MAINNET_SUBGRAPH_URL must be set for tests.");

let order_hash = "0x12863c37d7dd314984b237619f569f6f6f645383bb39aec4cb219abd52f8eff2";

let result = get_balances_single_order(&orderbook_mainnet_subgraph_url, order_hash).await;

// Assert the function call was successful
assert!(result.is_ok(), "Failed to fetch balances: {:?}", result);
ninokeldishvili marked this conversation as resolved.
Show resolved Hide resolved

if let Ok(data) = result {
// Ensure "data" key exists
let orders = data.get("data").and_then(|d| d.get("orders"));
assert!(orders.is_some(), "Orders data missing in response");

// Validate the returned data structure and values
if let Some(order_array) = orders.and_then(|o| o.as_array()) {
// Find the order with the matching `id`
let target_order_id =
"0x389d61c749f571e2da90a56385600ec421b487f8679ec7a98e2dcbd888a3c1ed";
let target_order = order_array
.iter()
.find(|order| order.get("id").map_or(false, |id| id == target_order_id));

// Ensure the target order was found
assert!(
target_order.is_some(),
"Order with ID {} not found",
target_order_id
);

if let Some(order) = target_order {
assert_eq!(
order.get("owner").unwrap(),
"0x5ef02599f44eed91ec7b3be4892b1a0665944a04"
);
assert_eq!(order.get("active").unwrap(), true);

// Validate the `outputs` -> `balance`
let outputs = order.get("outputs").unwrap().as_array().unwrap();

let first_output = &outputs[0];
assert_eq!(
first_output.get("balance").unwrap(),
"0",
"Unexpected balance in first output"
);

let second_output = &outputs[1];
assert_eq!(
second_output.get("balance").unwrap(),
"0",
"Unexpected balance in second output"
);
}
}
}
}
}
1 change: 1 addition & 0 deletions crates/cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use anyhow::Result;
use clap::Subcommand;
use rain_orderbook_quote::cli::Quoter;

mod balance;
mod commands;
mod execute;
mod output;
Expand Down