Skip to content

Commit

Permalink
feat(rpc): add more rpc endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
matthias-wright committed Jul 11, 2023
1 parent 3ee6e21 commit e5d4484
Show file tree
Hide file tree
Showing 2 changed files with 1,017 additions and 59 deletions.
123 changes: 120 additions & 3 deletions core/rpc/src/handlers.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
use std::sync::Arc;

use axum::{Extension, Json};
use draco_interfaces::{RpcInterface, SyncQueryRunnerInterface};
use draco_interfaces::{
types::{EpochInfo, NodeInfo, ProtocolParams},
RpcInterface, SyncQueryRunnerInterface,
};
use fleek_crypto::{EthAddress, NodePublicKey};
use hp_float::unsigned::HpUfloat;
use jsonrpc_v2::{Data, Error, MapRouter, Params, RequestObject, ResponseObjects, Server};

Expand Down Expand Up @@ -29,13 +33,43 @@ impl RpcServer {
let server = Server::new()
.with_data(Data::new(interface))
.with_method("flk_ping", ping_handler::<Q, I>)
.with_method("flk_get_balance", get_balance_handler::<Q, I>)
.with_method("flk_get_flk_balance", get_flk_balance_handler::<Q, I>)
.with_method(
"flk_get_bandwidth_balance",
get_bandwidth_balance_handler::<Q, I>,
)
.with_method("flk_get_locked", get_locked_handler::<Q, I>)
.with_method("flk_get_staked", get_staked_handler::<Q, I>)
.with_method(
"flk_get_stables_balance",
get_stables_balance_handler::<Q, I>,
)
.with_method(
"flk_get_stake_locked_until",
get_stake_locked_until_handler::<Q, I>,
)
.with_method("flk_get_locked_time", get_locked_time_handler::<Q, I>)
.with_method("flk_get_node_info", get_node_info_handler::<Q, I>)
.with_method("flk_get_staking_amount", get_staking_amount_handler::<Q, I>)
.with_method(
"flk_get_committee_members",
get_committee_members_handler::<Q, I>,
)
.with_method("flk_get_epoch", get_epoch_handler::<Q, I>)
.with_method("flk_get_epoch_info", get_epoch_info_handler::<Q, I>)
.with_method("flk_get_total_supply", get_total_supply_handler::<Q, I>)
.with_method(
"flk_get_year_start_supply",
get_year_start_supply_handler::<Q, I>,
)
.with_method(
"flk_get_protocol_fund_address",
get_protocol_fund_address_handler::<Q, I>,
)
.with_method(
"flk_get_protocol_params",
get_protocol_params_handler::<Q, I>,
)
.with_method("flk_get_reputation", get_reputation_handler::<Q, I>);

RpcServer(server.finish())
Expand All @@ -46,7 +80,7 @@ pub async fn ping_handler<Q: SyncQueryRunnerInterface, I: RpcInterface<Q>>() ->
Ok("pong".to_string())
}

pub async fn get_balance_handler<Q: SyncQueryRunnerInterface, I: RpcInterface<Q>>(
pub async fn get_flk_balance_handler<Q: SyncQueryRunnerInterface, I: RpcInterface<Q>>(
data: Data<Arc<I>>,
Params(params): Params<PublicKeyParam>,
) -> Result<HpUfloat<18>> {
Expand Down Expand Up @@ -83,3 +117,86 @@ pub async fn get_reputation_handler<Q: SyncQueryRunnerInterface, I: RpcInterface
) -> Result<Option<u8>> {
Ok(data.0.query_runner().get_reputation(&params.public_key))
}

pub async fn get_stables_balance_handler<Q: SyncQueryRunnerInterface, I: RpcInterface<Q>>(
data: Data<Arc<I>>,
Params(params): Params<PublicKeyParam>,
) -> Result<HpUfloat<6>> {
Ok(data
.0
.query_runner()
.get_stables_balance(&params.public_key))
}

pub async fn get_stake_locked_until_handler<Q: SyncQueryRunnerInterface, I: RpcInterface<Q>>(
data: Data<Arc<I>>,
Params(params): Params<NodeKeyParam>,
) -> Result<u64> {
Ok(data
.0
.query_runner()
.get_stake_locked_until(&params.public_key))
}

pub async fn get_locked_time_handler<Q: SyncQueryRunnerInterface, I: RpcInterface<Q>>(
data: Data<Arc<I>>,
Params(params): Params<NodeKeyParam>,
) -> Result<u64> {
Ok(data.0.query_runner().get_locked_time(&params.public_key))
}

pub async fn get_node_info_handler<Q: SyncQueryRunnerInterface, I: RpcInterface<Q>>(
data: Data<Arc<I>>,
Params(params): Params<NodeKeyParam>,
) -> Result<Option<NodeInfo>> {
Ok(data.0.query_runner().get_node_info(&params.public_key))
}

pub async fn get_staking_amount_handler<Q: SyncQueryRunnerInterface, I: RpcInterface<Q>>(
data: Data<Arc<I>>,
) -> Result<u128> {
Ok(data.0.query_runner().get_staking_amount())
}

pub async fn get_committee_members_handler<Q: SyncQueryRunnerInterface, I: RpcInterface<Q>>(
data: Data<Arc<I>>,
) -> Result<Vec<NodePublicKey>> {
Ok(data.0.query_runner().get_committee_members())
}

pub async fn get_epoch_handler<Q: SyncQueryRunnerInterface, I: RpcInterface<Q>>(
data: Data<Arc<I>>,
) -> Result<u64> {
Ok(data.0.query_runner().get_epoch())
}

pub async fn get_epoch_info_handler<Q: SyncQueryRunnerInterface, I: RpcInterface<Q>>(
data: Data<Arc<I>>,
) -> Result<EpochInfo> {
Ok(data.0.query_runner().get_epoch_info())
}

pub async fn get_total_supply_handler<Q: SyncQueryRunnerInterface, I: RpcInterface<Q>>(
data: Data<Arc<I>>,
) -> Result<HpUfloat<18>> {
Ok(data.0.query_runner().get_total_supply())
}

pub async fn get_year_start_supply_handler<Q: SyncQueryRunnerInterface, I: RpcInterface<Q>>(
data: Data<Arc<I>>,
) -> Result<HpUfloat<18>> {
Ok(data.0.query_runner().get_year_start_supply())
}

pub async fn get_protocol_fund_address_handler<Q: SyncQueryRunnerInterface, I: RpcInterface<Q>>(
data: Data<Arc<I>>,
) -> Result<EthAddress> {
Ok(data.0.query_runner().get_protocol_fund_address())
}

pub async fn get_protocol_params_handler<Q: SyncQueryRunnerInterface, I: RpcInterface<Q>>(
data: Data<Arc<I>>,
Params(params): Params<ProtocolParams>,
) -> Result<u128> {
Ok(data.0.query_runner().get_protocol_params(params))
}
Loading

0 comments on commit e5d4484

Please sign in to comment.