Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Escrow RPCs #689

Merged
merged 2 commits into from
Aug 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions crates/escrow/rpc/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[package]
authors = ["Interlay Ltd"]
edition = "2021"
name = "module-escrow-rpc"
version = '0.3.0'

[dependencies]
codec = { package = "parity-scale-codec", version = "3.0.0" }
jsonrpsee = { version = "0.13.0", features = ["server", "macros"] }
sp-runtime = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.24" }
sp-api = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.24" }
sp-blockchain = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.24" }
module-escrow-rpc-runtime-api = { path = "runtime-api" }

[dependencies.module-oracle-rpc-runtime-api]
path = '../../oracle/rpc/runtime-api'
21 changes: 21 additions & 0 deletions crates/escrow/rpc/runtime-api/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[package]
authors = ["Interlay Ltd"]
edition = "2021"
name = "module-escrow-rpc-runtime-api"
version = '0.3.0'

[dependencies]
codec = { package = "parity-scale-codec", version = "3.0.0", default-features = false, features = ["derive", "max-encoded-len"] }
sp-api = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.24", default-features = false }

[dependencies.module-oracle-rpc-runtime-api]
default-features = false
path = '../../../oracle/rpc/runtime-api'

[features]
default = ["std"]
std = [
"codec/std",
"sp-api/std",
"module-oracle-rpc-runtime-api/std",
]
20 changes: 20 additions & 0 deletions crates/escrow/rpc/runtime-api/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//! Runtime API definition for the Escrow Module.

#![cfg_attr(not(feature = "std"), no_std)]

use codec::Codec;
use module_oracle_rpc_runtime_api::BalanceWrapper;

sp_api::decl_runtime_apis! {
pub trait EscrowApi<AccountId, BlockNumber, Balance> where
AccountId: Codec,
BlockNumber: Codec,
Balance: Codec,
{
/// Get a given user's escrowed balance
fn balance_at(account_id: AccountId, height: Option<BlockNumber>) -> BalanceWrapper<Balance>;

/// Get the total voting supply in the system
fn total_supply(height: Option<BlockNumber>) -> BalanceWrapper<Balance>;
}
}
106 changes: 106 additions & 0 deletions crates/escrow/rpc/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
//! RPC interface for the Escrow Module.

use codec::Codec;
use jsonrpsee::{
core::{async_trait, Error as JsonRpseeError, RpcResult},
proc_macros::rpc,
types::error::{CallError, ErrorCode, ErrorObject},
};
use module_oracle_rpc_runtime_api::BalanceWrapper;
use sp_api::{ApiError, ProvideRuntimeApi};
use sp_blockchain::HeaderBackend;
use sp_runtime::{
generic::BlockId,
traits::{Block as BlockT, MaybeDisplay, MaybeFromStr},
};
use std::sync::Arc;

pub use module_escrow_rpc_runtime_api::EscrowApi as EscrowRuntimeApi;

#[rpc(client, server)]
pub trait EscrowApi<BlockHash, AccountId, BlockNumber, Balance>
where
Balance: Codec + MaybeDisplay + MaybeFromStr,
BlockNumber: Codec,
AccountId: Codec,
{
#[method(name = "escrow_balanceAt")]
fn balance_at(
&self,
account_id: AccountId,
height: Option<BlockNumber>,
at: Option<BlockHash>,
sander2 marked this conversation as resolved.
Show resolved Hide resolved
) -> RpcResult<BalanceWrapper<Balance>>;

#[method(name = "escrow_totalSupply")]
fn total_supply(&self, height: Option<BlockNumber>, at: Option<BlockHash>) -> RpcResult<BalanceWrapper<Balance>>;
}

fn internal_err<T: ToString>(message: T) -> JsonRpseeError {
JsonRpseeError::Call(CallError::Custom(ErrorObject::owned(
ErrorCode::InternalError.code(),
message.to_string(),
None::<()>,
)))
}

/// A struct that implements the [`EscrowApi`].
pub struct Escrow<C, B> {
client: Arc<C>,
_marker: std::marker::PhantomData<B>,
}

impl<C, B> Escrow<C, B> {
/// Create new `Escrow` with the given reference to the client.
pub fn new(client: Arc<C>) -> Self {
Escrow {
client,
_marker: Default::default(),
}
}
}

fn handle_response<T>(result: Result<T, ApiError>, msg: String) -> RpcResult<T> {
result.map_err(|err| internal_err(format!("Runtime error: {:?}: {:?}", msg, err)))
}

#[async_trait]
impl<C, Block, AccountId, BlockNumber, Balance>
EscrowApiServer<<Block as BlockT>::Hash, AccountId, BlockNumber, Balance> for Escrow<C, Block>
where
Block: BlockT,
C: Send + Sync + 'static + ProvideRuntimeApi<Block> + HeaderBackend<Block>,
C::Api: EscrowRuntimeApi<Block, AccountId, BlockNumber, Balance>,
AccountId: Codec,
BlockNumber: Codec,
Balance: Codec + MaybeDisplay + MaybeFromStr,
{
fn balance_at(
&self,
account_id: AccountId,
height: Option<BlockNumber>,
at: Option<<Block as BlockT>::Hash>,
) -> RpcResult<BalanceWrapper<Balance>> {
let api = self.client.runtime_api();
let at = BlockId::hash(at.unwrap_or_else(|| self.client.info().best_hash));
sander2 marked this conversation as resolved.
Show resolved Hide resolved

handle_response(
api.balance_at(&at, account_id, height),
"Unable to obtain the escrow balance".into(),
)
}

fn total_supply(
&self,
height: Option<BlockNumber>,
at: Option<<Block as BlockT>::Hash>,
) -> RpcResult<BalanceWrapper<Balance>> {
let api = self.client.runtime_api();
let at = BlockId::hash(at.unwrap_or_else(|| self.client.info().best_hash));

handle_response(
api.total_supply(&at, height),
"Unable to obtain the escrow total supply".into(),
)
}
}
4 changes: 2 additions & 2 deletions crates/escrow/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ fn should_increase_unlock_height() {
}

#[test]
fn should_calculate_total_supply() {
fn should_calculate_total_supply_and_locked() {
run_test(|| {
let end_time_1 = 100;
let amount_1 = 1000;
Expand All @@ -125,7 +125,7 @@ fn should_calculate_total_supply() {
}

#[test]
fn should_calculate_total_supply_after_withdraw() {
fn should_calculate_total_supply_and_locked_after_withdraw() {
run_test(|| {
let end_time_1 = 100;
let amount_1 = 1000;
Expand Down
3 changes: 2 additions & 1 deletion parachain/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ primitives = { package = "interbtc-primitives", path = "../primitives" }
module-btc-relay-rpc-runtime-api = { path = "../crates/btc-relay/rpc/runtime-api" }
module-oracle-rpc-runtime-api = { path = "../crates/oracle/rpc/runtime-api" }
module-vault-registry-rpc-runtime-api = { path = "../crates/vault-registry/rpc/runtime-api" }
module-escrow-rpc-runtime-api = { path = "../crates/escrow/rpc/runtime-api" }
module-issue-rpc-runtime-api = { path = "../crates/issue/rpc/runtime-api" }
module-redeem-rpc-runtime-api = { path = "../crates/redeem/rpc/runtime-api" }
module-replace-rpc-runtime-api = { path = "../crates/replace/rpc/runtime-api" }
Expand Down Expand Up @@ -105,4 +106,4 @@ rococo-native = [ "polkadot-cli/rococo-native" ]
runtime-benchmarks = [
"interlay-runtime/runtime-benchmarks",
"polkadot-service/runtime-benchmarks",
]
]
2 changes: 2 additions & 0 deletions parachain/runtime/interlay/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ primitives = { package = "interbtc-primitives", path = "../../../primitives", de
module-btc-relay-rpc-runtime-api = { path = "../../../crates/btc-relay/rpc/runtime-api", default-features = false }
module-oracle-rpc-runtime-api = { path = "../../../crates/oracle/rpc/runtime-api", default-features = false }
module-vault-registry-rpc-runtime-api = { path = "../../../crates/vault-registry/rpc/runtime-api", default-features = false }
module-escrow-rpc-runtime-api = { path = "../../../crates/escrow/rpc/runtime-api", default-features = false }
module-issue-rpc-runtime-api = { path = "../../../crates/issue/rpc/runtime-api", default-features = false }
module-redeem-rpc-runtime-api = { path = "../../../crates/redeem/rpc/runtime-api", default-features = false }
module-replace-rpc-runtime-api = { path = "../../../crates/replace/rpc/runtime-api", default-features = false }
Expand Down Expand Up @@ -211,6 +212,7 @@ std = [
"module-btc-relay-rpc-runtime-api/std",
"module-oracle-rpc-runtime-api/std",
"module-vault-registry-rpc-runtime-api/std",
"module-escrow-rpc-runtime-api/std",
"module-issue-rpc-runtime-api/std",
"module-redeem-rpc-runtime-api/std",
"module-replace-rpc-runtime-api/std",
Expand Down
15 changes: 15 additions & 0 deletions parachain/runtime/interlay/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1399,6 +1399,21 @@ impl_runtime_apis! {
}
}

impl module_escrow_rpc_runtime_api::EscrowApi<
Block,
AccountId,
BlockNumber,
Balance
> for Runtime {
fn balance_at(account_id: AccountId, height: Option<BlockNumber>) -> BalanceWrapper<Balance> {
BalanceWrapper{amount: Escrow::balance_at(&account_id, height)}
}

fn total_supply(height: Option<BlockNumber>) -> BalanceWrapper<Balance> {
BalanceWrapper{amount: Escrow::total_supply(height)}
}
}

impl module_issue_rpc_runtime_api::IssueApi<
Block,
AccountId,
Expand Down
2 changes: 2 additions & 0 deletions parachain/runtime/kintsugi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ primitives = { package = "interbtc-primitives", path = "../../../primitives", de
module-btc-relay-rpc-runtime-api = { path = "../../../crates/btc-relay/rpc/runtime-api", default-features = false }
module-oracle-rpc-runtime-api = { path = "../../../crates/oracle/rpc/runtime-api", default-features = false }
module-vault-registry-rpc-runtime-api = { path = "../../../crates/vault-registry/rpc/runtime-api", default-features = false }
module-escrow-rpc-runtime-api = { path = "../../../crates/escrow/rpc/runtime-api", default-features = false }
module-issue-rpc-runtime-api = { path = "../../../crates/issue/rpc/runtime-api", default-features = false }
module-redeem-rpc-runtime-api = { path = "../../../crates/redeem/rpc/runtime-api", default-features = false }
module-replace-rpc-runtime-api = { path = "../../../crates/replace/rpc/runtime-api", default-features = false }
Expand Down Expand Up @@ -215,6 +216,7 @@ std = [
"module-btc-relay-rpc-runtime-api/std",
"module-oracle-rpc-runtime-api/std",
"module-vault-registry-rpc-runtime-api/std",
"module-escrow-rpc-runtime-api/std",
"module-issue-rpc-runtime-api/std",
"module-redeem-rpc-runtime-api/std",
"module-replace-rpc-runtime-api/std",
Expand Down
15 changes: 15 additions & 0 deletions parachain/runtime/kintsugi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1497,6 +1497,21 @@ impl_runtime_apis! {
}
}

impl module_escrow_rpc_runtime_api::EscrowApi<
Block,
AccountId,
BlockNumber,
Balance
> for Runtime {
fn balance_at(account_id: AccountId, height: Option<BlockNumber>) -> BalanceWrapper<Balance> {
BalanceWrapper{amount: Escrow::balance_at(&account_id, height)}
}

fn total_supply(height: Option<BlockNumber>) -> BalanceWrapper<Balance> {
BalanceWrapper{amount: Escrow::total_supply(height)}
}
}

impl module_issue_rpc_runtime_api::IssueApi<
Block,
AccountId,
Expand Down
2 changes: 2 additions & 0 deletions parachain/runtime/testnet-interlay/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ primitives = { package = "interbtc-primitives", path = "../../../primitives", de
module-btc-relay-rpc-runtime-api = { path = "../../../crates/btc-relay/rpc/runtime-api", default-features = false }
module-oracle-rpc-runtime-api = { path = "../../../crates/oracle/rpc/runtime-api", default-features = false }
module-vault-registry-rpc-runtime-api = { path = "../../../crates/vault-registry/rpc/runtime-api", default-features = false }
module-escrow-rpc-runtime-api = { path = "../../../crates/escrow/rpc/runtime-api", default-features = false }
module-issue-rpc-runtime-api = { path = "../../../crates/issue/rpc/runtime-api", default-features = false }
module-redeem-rpc-runtime-api = { path = "../../../crates/redeem/rpc/runtime-api", default-features = false }
module-replace-rpc-runtime-api = { path = "../../../crates/replace/rpc/runtime-api", default-features = false }
Expand Down Expand Up @@ -216,6 +217,7 @@ std = [
"module-btc-relay-rpc-runtime-api/std",
"module-oracle-rpc-runtime-api/std",
"module-vault-registry-rpc-runtime-api/std",
"module-escrow-rpc-runtime-api/std",
"module-issue-rpc-runtime-api/std",
"module-redeem-rpc-runtime-api/std",
"module-replace-rpc-runtime-api/std",
Expand Down
15 changes: 15 additions & 0 deletions parachain/runtime/testnet-interlay/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1371,6 +1371,21 @@ impl_runtime_apis! {
}
}

impl module_escrow_rpc_runtime_api::EscrowApi<
Block,
AccountId,
BlockNumber,
Balance
> for Runtime {
fn balance_at(account_id: AccountId, height: Option<BlockNumber>) -> BalanceWrapper<Balance> {
BalanceWrapper{amount: Escrow::balance_at(&account_id, height)}
}

fn total_supply(height: Option<BlockNumber>) -> BalanceWrapper<Balance> {
BalanceWrapper{amount: Escrow::total_supply(height)}
}
}

impl module_issue_rpc_runtime_api::IssueApi<
Block,
AccountId,
Expand Down
Loading