Skip to content

Commit

Permalink
Merge pull request #52 from fpco/rujira-pool
Browse files Browse the repository at this point in the history
Rujira pool queries
  • Loading branch information
snoyberg authored Nov 27, 2024
2 parents 53940ba + cbd6c79 commit 27b4dbb
Show file tree
Hide file tree
Showing 7 changed files with 243 additions and 2 deletions.
5 changes: 5 additions & 0 deletions packages/cosmos-bin/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,4 +119,9 @@ pub(crate) enum Subcommand {
#[clap(subcommand)]
opt: crate::config::Opt,
},
/// Rujira-specific commands
Rujira {
#[clap(subcommand)]
opt: crate::rujira::Subcommand,
},
}
2 changes: 2 additions & 0 deletions packages/cosmos-bin/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ mod contract;
mod cw3;
mod my_duration;
mod nft;
mod rujira;
mod tokenfactory;
mod wallet;

Expand Down Expand Up @@ -70,6 +71,7 @@ impl Subcommand {
cw3::go(cosmos, inner).await?;
}
Subcommand::Config { opt: inner } => config::go(opt, inner)?,
Subcommand::Rujira { opt: inner } => rujira::go(opt, inner).await?,
}

Ok(())
Expand Down
29 changes: 29 additions & 0 deletions packages/cosmos-bin/src/rujira.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use anyhow::Result;

#[derive(clap::Parser)]
pub(crate) enum Subcommand {
/// Print information about a pool
Pool {
/// Asset to check
asset: String,
},
/// Print information about all pools
Pools {},
}

pub(crate) async fn go(opt: crate::cli::Opt, inner: Subcommand) -> Result<()> {
match inner {
Subcommand::Pool { asset } => {
let cosmos = opt.network_opt.build().await?;
let x = cosmos.rujira_pool(asset).await?;
println!("{x:#?}");
}
Subcommand::Pools {} => {
let cosmos = opt.network_opt.build().await?;
let x = cosmos.rujira_pools().await?;
println!("{x:#?}");
}
}

Ok(())
}
4 changes: 2 additions & 2 deletions packages/cosmos/src/client.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
mod node;
pub(crate) mod node;
mod node_chooser;
mod pool;
mod query;
pub(crate) mod query;

use std::{
collections::HashMap,
Expand Down
5 changes: 5 additions & 0 deletions packages/cosmos/src/client/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use crate::{
Action, BuilderError, ConnectionError, LastNodeError, NodeHealthLevel, QueryErrorDetails,
SingleNodeHealthReport,
},
rujira::RujiraQueryClient,
CosmosBuilder,
};

Expand Down Expand Up @@ -274,6 +275,10 @@ impl Node {
client.max_decoding_message_size(self.node_inner.max_decoding_message_size)
}

pub(crate) fn rujira_query_client(&self) -> RujiraQueryClient<CosmosChannel> {
RujiraQueryClient::new(self.node_inner.channel.clone())
}

pub(crate) fn bank_query_client(
&self,
) -> cosmos_sdk_proto::cosmos::bank::v1beta1::query_client::QueryClient<CosmosChannel> {
Expand Down
1 change: 1 addition & 0 deletions packages/cosmos/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ mod ext;
mod gas_multiplier;
mod injective;
mod parsed_coin;
mod rujira;
mod tokenfactory;
mod txbuilder;
mod wallet;
Expand Down
199 changes: 199 additions & 0 deletions packages/cosmos/src/rujira.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
use tonic::{async_trait, GrpcMethod};

use crate::{
client::{node::Node, query::GrpcRequest},
error::Action,
Cosmos,
};

impl Cosmos {
/// Query information on a Rujira pool
pub async fn rujira_pool(
&self,
asset: impl Into<String>,
) -> Result<QueryPoolResponse, crate::Error> {
Ok(self
.perform_query(
QueryPoolRequest {
asset: asset.into(),
height: "".to_owned(),
},
Action::GetLatestBlock,
)
.run()
.await?
.into_inner())
}

/// Query information on available Rujira pools
pub async fn rujira_pools(&self) -> Result<QueryPoolsResponse, crate::Error> {
Ok(self
.perform_query(
QueryPoolsRequest {
height: "".to_owned(),
},
Action::GetLatestBlock,
)
.run()
.await?
.into_inner())
}
}

pub(crate) struct RujiraQueryClient<T> {
inner: tonic::client::Grpc<T>,
}
impl<T> RujiraQueryClient<T>
where
T: tonic::client::GrpcService<tonic::body::BoxBody>,
T::Error: Into<tonic::codegen::StdError>,
T::ResponseBody: tonic::codegen::Body<Data = tonic::codegen::Bytes> + Send + 'static,
<T::ResponseBody as tonic::codegen::Body>::Error: Into<tonic::codegen::StdError> + Send,
{
pub(crate) fn new(inner: T) -> Self {
let inner = tonic::client::Grpc::new(inner);
Self { inner }
}

async fn pool(
&mut self,
request: impl tonic::IntoRequest<QueryPoolRequest>,
) -> Result<tonic::Response<QueryPoolResponse>, tonic::Status> {
self.inner.ready().await.map_err(|e| {
tonic::Status::new(
tonic::Code::Unknown,
format!("Service was not ready: {}", e.into()),
)
})?;
let codec = tonic::codec::ProstCodec::default();
let path = http::uri::PathAndQuery::from_static("/types.Query/Pool");
let mut req = request.into_request();
req.extensions_mut()
.insert(GrpcMethod::new("types.Query", "Pool"));
self.inner.unary(req, path, codec).await
}

async fn pools(
&mut self,
request: impl tonic::IntoRequest<QueryPoolsRequest>,
) -> Result<tonic::Response<QueryPoolsResponse>, tonic::Status> {
self.inner.ready().await.map_err(|e| {
tonic::Status::new(
tonic::Code::Unknown,
format!("Service was not ready: {}", e.into()),
)
})?;
let codec = tonic::codec::ProstCodec::default();
let path = http::uri::PathAndQuery::from_static("/types.Query/Pools");
let mut req = request.into_request();
req.extensions_mut()
.insert(GrpcMethod::new("types.Query", "Pools"));
self.inner.unary(req, path, codec).await
}
}

#[async_trait]
impl GrpcRequest for QueryPoolRequest {
type Response = QueryPoolResponse;

async fn perform(
req: tonic::Request<Self>,
inner: &Node,
) -> Result<tonic::Response<Self::Response>, tonic::Status> {
inner.rujira_query_client().pool(req).await
}
}

#[async_trait]
impl GrpcRequest for QueryPoolsRequest {
type Response = QueryPoolsResponse;

async fn perform(
req: tonic::Request<Self>,
inner: &Node,
) -> Result<tonic::Response<Self::Response>, tonic::Status> {
inner.rujira_query_client().pools(req).await
}
}

#[derive(Clone, PartialEq, ::prost::Message)]
pub struct QueryPoolRequest {
#[prost(string, tag = "1")]
pub asset: ::prost::alloc::string::String,
#[prost(string, tag = "2")]
pub height: ::prost::alloc::string::String,
}
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct QueryPoolResponse {
#[prost(string, tag = "1")]
pub asset: ::prost::alloc::string::String,
#[prost(string, tag = "2")]
pub short_code: ::prost::alloc::string::String,
#[prost(string, tag = "3")]
pub status: ::prost::alloc::string::String,
#[prost(int64, tag = "4")]
pub decimals: i64,
#[prost(string, tag = "5")]
pub pending_inbound_asset: ::prost::alloc::string::String,
#[prost(string, tag = "6")]
pub pending_inbound_rune: ::prost::alloc::string::String,
#[prost(string, tag = "7")]
pub balance_asset: ::prost::alloc::string::String,
#[prost(string, tag = "8")]
pub balance_rune: ::prost::alloc::string::String,
/// the USD (TOR) price of the asset in 1e8
#[prost(string, tag = "9")]
pub asset_tor_price: ::prost::alloc::string::String,
/// the total pool units, this is the sum of LP and synth units
#[prost(string, tag = "10")]
pub pool_units: ::prost::alloc::string::String,
/// the total pool liquidity provider units
#[prost(string, tag = "11")]
pub lp_units: ::prost::alloc::string::String,
/// the total synth units in the pool
#[prost(string, tag = "12")]
pub synth_units: ::prost::alloc::string::String,
/// the total supply of synths for the asset
#[prost(string, tag = "13")]
pub synth_supply: ::prost::alloc::string::String,
/// the balance of L1 asset deposited into the Savers Vault
#[prost(string, tag = "14")]
pub savers_depth: ::prost::alloc::string::String,
/// the number of units owned by Savers
#[prost(string, tag = "15")]
pub savers_units: ::prost::alloc::string::String,
/// the filled savers capacity in basis points, 4500/10000 = 45%
#[prost(string, tag = "16")]
pub savers_fill_bps: ::prost::alloc::string::String,
/// amount of remaining capacity in asset
#[prost(string, tag = "17")]
pub savers_capacity_remaining: ::prost::alloc::string::String,
/// whether additional synths cannot be minted
#[prost(bool, tag = "18")]
pub synth_mint_paused: bool,
/// the amount of synth supply remaining before the current max supply is reached
#[prost(string, tag = "19")]
pub synth_supply_remaining: ::prost::alloc::string::String,
/// the amount of collateral collects for loans
#[prost(string, tag = "20")]
pub loan_collateral: ::prost::alloc::string::String,
/// the amount of remaining collateral collects for loans
#[prost(string, tag = "21")]
pub loan_collateral_remaining: ::prost::alloc::string::String,
/// the current loan collateralization ratio
#[prost(string, tag = "22")]
pub loan_cr: ::prost::alloc::string::String,
/// the depth of the derived virtual pool relative to L1 pool (in basis points)
#[prost(string, tag = "23")]
pub derived_depth_bps: ::prost::alloc::string::String,
}
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct QueryPoolsRequest {
#[prost(string, tag = "1")]
pub height: ::prost::alloc::string::String,
}
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct QueryPoolsResponse {
#[prost(message, repeated, tag = "1")]
pub pools: ::prost::alloc::vec::Vec<QueryPoolResponse>,
}

0 comments on commit 27b4dbb

Please sign in to comment.