Skip to content

Commit

Permalink
feat: tmpl
Browse files Browse the repository at this point in the history
  • Loading branch information
OnlyF0uR committed Nov 18, 2024
1 parent db17f7a commit 1ec1148
Showing 1 changed file with 79 additions and 10 deletions.
89 changes: 79 additions & 10 deletions crates/cesium-rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,30 +33,61 @@ impl From<std::io::Error> for RpcError {

#[rpc(server)]
pub trait Rpc {
// #[method(name = "getBalance")]
// async fn get_balance(&self, account: String) -> Result<u128, RpcError>;

#[method(name = "getVersion")]
async fn get_version(&self) -> Result<String, RpcError>;

// getCheckpoint is a method that returns the checkpoint at the given index.
// If no index is provided, it returns the latest checkpoint.
#[method(name = "getCheckpoint")]
async fn get_checkpoint(&self, index: Option<u64>) -> Result<String, RpcError>;

// getTransaction is a method that returns the transaction data given a transaction hash.
#[method(name = "getTransaction")]
async fn get_transaction(&self, hash: String) -> Result<String, RpcError>;

// getAccountInfo is a method that returns the account information for a given account.
// This can be called on base accounts, as well as on data accounts
#[method(name = "getAccountInfo")]
async fn get_account_info(&self, account: String) -> Result<String, RpcError>;

// checkpointsSub is a subscription method that broadcasts the latest checkpoint information.
#[subscription(name = "subscribeCheckpoints", item = usize, with_extensions)]
async fn sub(&self) -> SubscriptionResult;
async fn checkpoints_sub(&self) -> SubscriptionResult;

// transactionsSub is a subscription method that broadcasts the latest transaction information.
#[subscription(name = "subscribeTransactions", item = usize, with_extensions)]
async fn transactions_sub(&self) -> SubscriptionResult;

// accountSub is a subscription method that broadcasts the latest account information.
#[subscription(name = "subscribeAccount", item = usize, with_extensions)]
async fn account_sub(&self) -> SubscriptionResult;
}

pub struct RpcServerImpl;

#[async_trait]
impl RpcServer for RpcServerImpl {
// async fn get_balance(&self, account: String) -> Result<u128, RpcError> {
// println!("Received getBalance request for account: {}", account);
// Ok(1000)
// }

async fn get_version(&self) -> Result<String, RpcError> {
Ok(VERSION.to_string())
}

async fn sub(&self, pending: PendingSubscriptionSink, ext: &Extensions) -> SubscriptionResult {
async fn get_checkpoint(&self, _index: Option<u64>) -> Result<String, RpcError> {
Ok("todo".to_string())
}

async fn get_transaction(&self, _hash: String) -> Result<String, RpcError> {
Ok("todo".to_string())
}

async fn get_account_info(&self, _account: String) -> Result<String, RpcError> {
Ok("todo".to_string())
}

async fn checkpoints_sub(
&self,
pending: PendingSubscriptionSink,
ext: &Extensions,
) -> SubscriptionResult {
let sink = pending.accept().await?;
let conn_id = ext
.get::<ConnectionId>()
Expand All @@ -69,6 +100,44 @@ impl RpcServer for RpcServerImpl {
.await?;
Ok(())
}

async fn transactions_sub(
&self,
pending: PendingSubscriptionSink,
ext: &Extensions,
) -> SubscriptionResult {
let sink = pending.accept().await?;
let conn_id = ext
.get::<ConnectionId>()
.cloned()
.ok_or_else(|| ErrorObject::owned(0, "No connection details found", None::<()>))?;

// TODO: Implement saving the connection for future use

sink.send(SubscriptionMessage::from_json(&conn_id).unwrap())
.await?;

Ok(())
}

async fn account_sub(
&self,
pending: PendingSubscriptionSink,
ext: &Extensions,
) -> SubscriptionResult {
let sink = pending.accept().await?;
let conn_id = ext
.get::<ConnectionId>()
.cloned()
.ok_or_else(|| ErrorObject::owned(0, "No connection details found", None::<()>))?;

// TODO: Implement saving the connection for future use

sink.send(SubscriptionMessage::from_json(&conn_id).unwrap())
.await?;

Ok(())
}
}

async fn run_server() -> Result<SocketAddr, RpcError> {
Expand Down

0 comments on commit 1ec1148

Please sign in to comment.