Skip to content

Commit

Permalink
Merge pull request #225 from rainlanguage/2024-02-12-cli-take-orders
Browse files Browse the repository at this point in the history
2024 02 12 cli take orders list
  • Loading branch information
thedavidmeister authored Feb 14, 2024
2 parents 757cd0b + 75ade5e commit 409d300
Show file tree
Hide file tree
Showing 13 changed files with 487 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ cynic querygen --schema crates/subgraph/schema/orderbook.graphql --query crates/
cynic querygen --schema crates/subgraph/schema/orderbook.graphql --query crates/subgraph/queries/ordersList.graphql > crates/subgraph/src/types/orders_list.rs
cynic querygen --schema crates/subgraph/schema/orderbook.graphql --query crates/subgraph/queries/vaultBalanceChangesList.graphql > crates/subgraph/src/types/vault_list_balance_changes.rs
cynic querygen --schema crates/subgraph/schema/orderbook.graphql --query crates/subgraph/queries/orderClearsList.graphql > crates/subgraph/src/types/order_clears_list.rs
cynic querygen --schema crates/subgraph/schema/orderbook.graphql --query crates/subgraph/queries/orderTakesList.graphql > crates/subgraph/src/types/order_takes_list.rs
```

2. Prepend each generated types file with the following:
Expand Down
3 changes: 2 additions & 1 deletion crates/cli/src/commands/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
mod order;
mod order_clear;
mod order_take;
mod vault;

pub use self::{order::Order, order_clear::OrderClear, vault::Vault};
pub use self::{order::Order, order_clear::OrderClear, order_take::OrderTake, vault::Vault};
83 changes: 83 additions & 0 deletions crates/cli/src/commands/order_take/list.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
use crate::{
execute::Execute,
subgraph::{CliPaginationArgs, CliSubgraphArgs},
};
use anyhow::Result;
use clap::Args;
use comfy_table::Table;
use rain_orderbook_common::subgraph::SubgraphArgs;
use rain_orderbook_subgraph_client::{
types::flattened::{OrderTakeFlattened, TryIntoFlattenedError},
PaginationArgs, TryIntoCsv,
};
use tracing::info;

#[derive(Args, Clone)]
pub struct CliOrderTakesListArgs {
#[arg(short = 'i', long, help = "ID of the Order")]
order_id: String,

#[clap(flatten)]
pagination_args: CliPaginationArgs,

#[clap(flatten)]
subgraph_args: CliSubgraphArgs,
}

impl Execute for CliOrderTakesListArgs {
async fn execute(&self) -> Result<()> {
let subgraph_args: SubgraphArgs = self.subgraph_args.clone().into();
let pagination_args: PaginationArgs = self.pagination_args.clone().into();
let order_takes = subgraph_args
.to_subgraph_client()
.await?
.order_takes_list(self.order_id.clone().into(), pagination_args)
.await?;
let order_takes_flattened: Vec<OrderTakeFlattened> = order_takes
.into_iter()
.map(|o| o.try_into())
.collect::<Result<Vec<OrderTakeFlattened>, TryIntoFlattenedError>>()?;

if self.pagination_args.csv {
let csv_text = order_takes_flattened.try_into_csv()?;
println!("{}", csv_text);
} else {
let table = build_table(order_takes_flattened)?;
info!("\n{}", table);
}

Ok(())
}
}

fn build_table(order_take: Vec<OrderTakeFlattened>) -> Result<Table> {
let mut table = comfy_table::Table::new();
table
.load_preset(comfy_table::presets::UTF8_FULL)
.set_content_arrangement(comfy_table::ContentArrangement::Dynamic)
.set_header(vec![
"ID", "Taken At", "Sender", "Input", "Output", "IO Ratio",
]);

for order_take in order_take.into_iter() {
table.add_row(vec![
order_take.id,
order_take.timestamp_display,
order_take.sender.0,
format!(
"{} {}",
order_take.input_display.0, order_take.input_token_symbol
),
format!(
"{} {}",
order_take.output_display.0, order_take.output_token_symbol
),
format!(
"{} {}/{}",
order_take.ioratio.0, order_take.input_token_symbol, order_take.output_token_symbol
),
]);
}

Ok(table)
}
20 changes: 20 additions & 0 deletions crates/cli/src/commands/order_take/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
mod list;

use crate::execute::Execute;
use anyhow::Result;
use clap::Parser;
use list::CliOrderTakesListArgs;

#[derive(Parser)]
pub enum OrderTake {
#[command(about = "List takes for an Order", alias = "ls")]
List(CliOrderTakesListArgs),
}

impl Execute for OrderTake {
async fn execute(&self) -> Result<()> {
match self {
OrderTake::List(list) => list.execute().await,
}
}
}
6 changes: 5 additions & 1 deletion crates/cli/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::commands::{Order, OrderClear, Vault};
use crate::commands::{Order, OrderClear, OrderTake, Vault};
use crate::execute::Execute;
use anyhow::Result;
use clap::Subcommand;
Expand All @@ -19,6 +19,9 @@ pub enum Orderbook {

#[command(subcommand)]
OrderClear(OrderClear),

#[command(subcommand)]
OrderTake(OrderTake),
}

impl Orderbook {
Expand All @@ -27,6 +30,7 @@ impl Orderbook {
Orderbook::Order(order) => order.execute().await,
Orderbook::Vault(vault) => (*vault).execute().await,
Orderbook::OrderClear(order_clear) => (order_clear).execute().await,
Orderbook::OrderTake(order_take) => (order_take).execute().await,
}
}
}
44 changes: 44 additions & 0 deletions crates/subgraph/queries/orderTakesList.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
query OrderTakesListQuery($id: ID!, $skip: Int = 0, $first: Int = 25) {
takeOrderEntities(orderBy: timestamp, orderDirection: desc, skip: $skip, first: $first, where: {
order_: {
id: $id
}
}) {
id
transaction {
id
}
sender {
id
}
timestamp
order {
id
}
IORatio
input
inputDisplay
inputToken {
id
name
symbol
decimals
}
inputIOIndex
output
outputDisplay
outputToken {
id
name
symbol
decimals
}
outputIOIndex
context {
callingContext
calculationsContext
vaultInputsContext
vaultOutputsContext
}
}
}
22 changes: 22 additions & 0 deletions crates/subgraph/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ use crate::types::{
order_clears_list::{OrderClearsListQuery, OrderClearsListQueryVariables},
order_detail,
order_detail::{OrderDetailQuery, OrderDetailQueryVariables},
order_takes_list,
order_takes_list::{OrderTakesListQuery, OrderTakesListQueryVariables},
orders_list,
orders_list::{OrdersListQuery, OrdersListQueryVariables},
vault_balance_change::VaultBalanceChange,
Expand Down Expand Up @@ -155,6 +157,26 @@ impl OrderbookSubgraphClient {

Ok(data.order_clears)
}

pub async fn order_takes_list(
&self,
order_id: cynic::Id,
pagination_args: PaginationArgs,
) -> Result<Vec<order_takes_list::TakeOrderEntity>, OrderbookSubgraphClientError> {
let pagination_variables = Self::parse_pagination_args(pagination_args);
let data = self
.query::<OrderTakesListQuery, OrderTakesListQueryVariables>(
self.url.clone(),
OrderTakesListQueryVariables {
id: &order_id,
first: pagination_variables.first,
skip: pagination_variables.skip,
},
)
.await?;

Ok(data.take_order_entities)
}
}

pub struct VaultListBalanceChangesPageQueryClient {
Expand Down
51 changes: 50 additions & 1 deletion crates/subgraph/src/types/flattened.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::num::ParseIntError;

use super::{
order_clears_list, orders_list, vault_balance_change::VaultBalanceChange,
order_clears_list, order_takes_list, orders_list, vault_balance_change::VaultBalanceChange,
vault_list_balance_changes, vaults_list,
};
use crate::utils::format_bigint_timestamp_display;
Expand Down Expand Up @@ -204,3 +204,52 @@ impl TryFrom<order_clears_list::OrderClear> for OrderClearFlattened {
}

impl TryIntoCsv<OrderClearFlattened> for Vec<OrderClearFlattened> {}

#[derive(Serialize, Deserialize, Clone)]
pub struct OrderTakeFlattened {
pub id: String,
pub timestamp: order_takes_list::BigInt,
pub timestamp_display: String,
pub transaction: cynic::Id,
pub sender: order_takes_list::Bytes,
pub order_id: cynic::Id,
pub ioratio: order_takes_list::BigDecimal,
pub input: order_takes_list::BigInt,
pub input_display: order_takes_list::BigDecimal,
pub input_token_id: cynic::Id,
pub input_token_symbol: String,
pub input_ioindex: order_takes_list::BigInt,
pub output: order_takes_list::BigInt,
pub output_display: order_takes_list::BigDecimal,
pub output_token_id: cynic::Id,
pub output_token_symbol: String,
pub output_ioindex: order_takes_list::BigInt,
}

impl TryFrom<order_takes_list::TakeOrderEntity> for OrderTakeFlattened {
type Error = TryIntoFlattenedError;

fn try_from(val: order_takes_list::TakeOrderEntity) -> Result<Self, Self::Error> {
Ok(Self {
id: val.id.into_inner(),
timestamp: val.timestamp.clone(),
timestamp_display: format_bigint_timestamp_display(val.timestamp.0)?,
transaction: val.transaction.id,
sender: val.sender.id,
order_id: val.order.id,
ioratio: val.ioratio,
input: val.input,
input_display: val.input_display,
input_token_id: val.input_token.id,
input_token_symbol: val.input_token.symbol,
input_ioindex: val.input_ioindex,
output: val.output,
output_display: val.output_display,
output_token_id: val.output_token.id,
output_token_symbol: val.output_token.symbol,
output_ioindex: val.output_ioindex,
})
}
}

impl TryIntoCsv<OrderTakeFlattened> for Vec<OrderTakeFlattened> {}
1 change: 1 addition & 0 deletions crates/subgraph/src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ pub mod flattened;
pub mod order_clears_list;
pub mod order_detail;
pub mod order_detail_traits;
pub mod order_takes_list;
pub mod orders_list;
pub mod vault_balance_change;
pub mod vault_detail;
Expand Down
Loading

0 comments on commit 409d300

Please sign in to comment.