Skip to content

Commit

Permalink
add in more rpc methods
Browse files Browse the repository at this point in the history
  • Loading branch information
austbot committed Aug 22, 2022
1 parent 3cbd625 commit e9c4775
Show file tree
Hide file tree
Showing 29 changed files with 2,890 additions and 639 deletions.
26 changes: 26 additions & 0 deletions das_api/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
[package]
name = "das_api"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
digital_asset_types = { path = "../digital_asset_types", features = ["json_types", "sql_types"] }
jsonrpsee = {version = "0.14.0", features = ["server", "macros"]}
hyper = "0.14.19"
tracing = "0.1.35"
metrics = "0.19.0"
figment = { version = "0.10.6", features = ["env"] }
serde = "1.0.137"
thiserror = "1.0.31"
tokio = {version="1.19.2"}
async-trait = "0.1.56"
serde_json = "1.0.81"
cadence = "0.29.0"
cadence-macros = "0.29.0"
sqlx = { version = "0.6.0", features = ["macros", "runtime-tokio-rustls", "postgres", "uuid", "offline", "json"] }
sea-orm = { version = "0.9.0", features = ["macros", "runtime-tokio-rustls", "sqlx-postgres"] }
tokio-postgres = "0.7.5"
solana-sdk = { version = "=1.10.10" }
bs58 = "0.4.0"
152 changes: 152 additions & 0 deletions das_api/src/api.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
use crate::{DasApiError, RpcModule};
use async_trait::async_trait;
use digital_asset_types::rpc::filter::{AssetSorting, ListingSorting, OfferSorting};
use digital_asset_types::rpc::response::{AssetList, ListingsList, OfferList};
use digital_asset_types::rpc::{Asset, AssetProof};

#[async_trait]
pub trait ApiContract: Send + Sync + 'static {
async fn check_health(&self) -> Result<(), DasApiError>;
async fn get_asset_proof(&self, asset_id: String) -> Result<AssetProof, DasApiError>;
async fn get_asset(&self, asset_id: String) -> Result<Asset, DasApiError>;
async fn get_assets_by_owner(
&self,
owner_address: String,
sort_by: AssetSorting,
limit: u32,
page: u32,
before: String,
after: String,
) -> Result<AssetList, DasApiError>;
async fn get_listed_assets_by_owner(
&self,
owner_address: String,
sort_by: ListingSorting,
limit: u32,
page: u32,
before: String,
after: String,
) -> Result<ListingsList, DasApiError>;
async fn get_offers_by_owner(
&self,
owner_address: String,
sort_by: OfferSorting,
limit: u32,
page: u32,
before: String,
after: String,
) -> Result<OfferList, DasApiError>;
async fn get_assets_by_group(
&self,
group_expression: Vec<String>,
sort_by: AssetSorting,
limit: u32,
page: u32,
before: String,
after: String,
) -> Result<AssetList, DasApiError>;
async fn get_assets_by_creator(
&self,
creator_expression: Vec<String>,
sort_by: AssetSorting,
limit: u32,
page: u32,
before: String,
after: String,
) -> Result<AssetList, DasApiError>;
async fn search_assets(
&mut self,
search_expression: String,
sort_by: AssetSorting,
limit: u32,
page: u32,
before: String,
after: String,
) -> Result<AssetList, DasApiError>;
}

pub struct RpcApiBuilder;

impl<'a> RpcApiBuilder {
pub fn build(
contract: Box<dyn ApiContract>,
) -> Result<RpcModule<Box<dyn ApiContract>>, DasApiError> {
let mut module = RpcModule::new(contract);
module.register_async_method("healthz", |rpc_params, rpc_context| async move {
println!("Checking Health");
rpc_context.check_health()
.await
.map_err(Into::into)
})?;
module.register_async_method("get_asset_proof", |rpc_params, rpc_context| async move {
let asset_id = rpc_params.one::<String>()?;
println!("Asset Id {}", asset_id);
rpc_context
.get_asset_proof(asset_id)
.await
.map_err(Into::into)
})?;
module.register_async_method("get_asset", |rpc_params, rpc_context| async move {
let asset_id = rpc_params.one::<String>()?;
println!("Asset Id {}", asset_id);
rpc_context.get_asset(asset_id).await.map_err(Into::into)
})?;
module.register_async_method(
"get_assets_by_owner",
|rpc_params, rpc_context| async move {
let (owner_address, sort_by, limit, page, before, after) =
rpc_params.parse().unwrap();
rpc_context
.get_assets_by_owner(owner_address, sort_by, limit, page, before, after)
.await
.map_err(Into::into)
},
)?;
module.register_async_method(
"get_assets_by_creator",
|rpc_params, rpc_context| async move {
let (creator_expression, sort_by, limit, page, before, after) =
rpc_params.parse().unwrap();
rpc_context
.get_assets_by_creator(creator_expression, sort_by, limit, page, before, after)
.await
.map_err(Into::into)
},
)?;
module.register_async_method(
"get_assets_by_group",
|rpc_params, rpc_context| async move {
let (group_expression, sort_by, limit, page, before, after) =
rpc_params.parse().unwrap();
rpc_context
.get_assets_by_group(group_expression, sort_by, limit, page, before, after)
.await
.map_err(Into::into)
},
)?;
module.register_async_method(
"get_listed_assets_by_owner",
|rpc_params, rpc_context| async move {
let (owner_address, sort_by, limit, page, before, after) =
rpc_params.parse().unwrap();
rpc_context
.get_listed_assets_by_owner(owner_address, sort_by, limit, page, before, after)
.await
.map_err(Into::into)
},
)?;
module.register_async_method(
"get_offers_by_owner",
|rpc_params, rpc_context| async move {
let (owner_address, sort_by, limit, page, before, after) =
rpc_params.parse().unwrap();
rpc_context
.get_offers_by_owner(owner_address, sort_by, limit, page, before, after)
.await
.map_err(Into::into)
},
)?;

Ok(module)
}
}
Loading

0 comments on commit e9c4775

Please sign in to comment.