Skip to content

Commit

Permalink
Add salp-lite
Browse files Browse the repository at this point in the history
  • Loading branch information
yrong committed Oct 18, 2021
1 parent 405ef64 commit 49ed51c
Show file tree
Hide file tree
Showing 13 changed files with 2,589 additions and 1 deletion.
25 changes: 25 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ members = [
"pallets/vtoken-mint/rpc",
"pallets/token-issuer",
"pallets/lightening-redeem",
"pallets/salp-lite",
"runtime/asgard",
"runtime/bifrost",
"utils/subkey",
Expand Down
3 changes: 3 additions & 0 deletions node/primitives/src/salp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ pub enum ContributionStatus<BalanceOf> {
Refunded,
Unlocked,
Redeemed,
MigratedIdle,
}

#[derive(Encode, Decode, Clone, PartialEq, Eq, Copy, RuntimeDebug)]
Expand All @@ -35,6 +36,7 @@ pub enum RpcContributionStatus {
Refunded,
Unlocked,
Redeemed,
MigratedIdle,
}

impl<BalanceOf> ContributionStatus<BalanceOf>
Expand Down Expand Up @@ -62,6 +64,7 @@ where
Self::Refunded => RpcContributionStatus::Refunded,
Self::Unlocked => RpcContributionStatus::Unlocked,
Self::Redeemed => RpcContributionStatus::Redeemed,
Self::MigratedIdle => RpcContributionStatus::MigratedIdle,
};
rpc_status
}
Expand Down
51 changes: 51 additions & 0 deletions pallets/salp-lite/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
[package]
name = "bifrost-salp-lite"
version = "0.8.0"
authors = ["Ron Yang <yrong1997@gmail.com>"]
edition = "2018"

[dependencies]
codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false, features = ["derive"] }
log = { version = "0.4.14", default-features = false }
node-primitives = { path = "../../node/primitives", default-features = false }
frame-support = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.10", default-features = false }
frame-system = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.10", default-features = false }
frame-benchmarking = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.10", default-features = false, optional = true }
sp-std = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.10", default-features = false }
sp-io = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.10", default-features = false }
sp-runtime = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.10", default-features = false }
sp-arithmetic = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.10", default-features = false }
orml-traits = { version = "0.4.1-dev", default-features = false }

[dev-dependencies]
pallet-multisig = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.10"}
smallvec = "1.6.1"
sp-io = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.10" }
sp-core = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.10" }
sp-runtime = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.10" }
orml-tokens = "0.4.1-dev"
orml-currencies = "0.4.1-dev"
pallet-balances = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.10" }
bifrost-bancor = { path = "../../pallets/bancor" }
bifrost-runtime = { path = "../../runtime/bifrost" }

[features]
default = ["std"]
std = [
"codec/std",
"log/std",
"node-primitives/std",
"frame-support/std",
"frame-system/std",
"sp-std/std",
"sp-io/std",
"sp-runtime/std",
"sp-arithmetic/std",
"orml-traits/std",
]

runtime-benchmarks = [
"frame-benchmarking",
"sp-runtime/runtime-benchmarks",
"frame-system/runtime-benchmarks",
]
19 changes: 19 additions & 0 deletions pallets/salp-lite/rpc/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[package]
name = "bifrost-salp-rpc-api"
version = "0.8.0"
authors = ["Ron Yang <yrong1997@gmail.com>"]
edition = "2018"

[dependencies]
serde = { version = "1.0.124", features = ["derive"] }
codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false, features = ["derive"] }
jsonrpc-core = "15.1.0"
jsonrpc-core-client = "15.1.0"
jsonrpc-derive = "15.1.0"
sp-api = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.10" }
sp-runtime = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.10" }
sp-blockchain = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.10" }
sp-core = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.10" }
sp-rpc = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.10" }
node-primitives = { path = "../../../node/primitives", default-features = false }
bifrost-salp-rpc-runtime-api = { path = "./runtime-api" }
18 changes: 18 additions & 0 deletions pallets/salp-lite/rpc/runtime-api/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[package]
name = "bifrost-salp-rpc-runtime-api"
version = "0.8.0"
authors = ["Ron Yang<yrong1997@gmail.com>"]
edition = "2018"

[dependencies]
codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false, features = ["derive"] }
sp-api = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.10", default-features = false }
node-primitives = { path = "../../../../node/primitives", default-features = false }

[features]
default = ["std"]
std = [
"codec/std",
"sp-api/std",
"node-primitives/std",
]
35 changes: 35 additions & 0 deletions pallets/salp-lite/rpc/runtime-api/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// This file is part of Bifrost.

// Copyright (C) 2019-2021 Liebi Technologies (UK) Ltd.
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0

// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

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

use codec::Codec;
use node_primitives::{Balance, RpcContributionStatus};
use sp_api::decl_runtime_apis;

decl_runtime_apis! {
pub trait SalpRuntimeApi<ParaId,AccountId> where
ParaId: Codec,
AccountId: Codec,
{
fn get_contribution(
index: ParaId,
who: AccountId
) -> (Balance,RpcContributionStatus);
}
}
86 changes: 86 additions & 0 deletions pallets/salp-lite/rpc/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// This file is part of Bifrost.

// Copyright (C) 2019-2021 Liebi Technologies (UK) Ltd.
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0

// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

use std::{marker::PhantomData, sync::Arc};

pub use bifrost_salp_rpc_runtime_api::{self as runtime_api, SalpRuntimeApi};
use codec::Codec;
use jsonrpc_core::{Error as RpcError, ErrorCode, Result as JsonRpcResult};
use jsonrpc_derive::rpc;
use node_primitives::RpcContributionStatus;
use sp_api::ProvideRuntimeApi;
use sp_blockchain::HeaderBackend;
use sp_rpc::number::NumberOrHex;
use sp_runtime::{generic::BlockId, traits::Block as BlockT, SaturatedConversion};

pub use self::gen_client::Client as SalpClient;

#[derive(Clone, Debug)]
pub struct SalpRpcWrapper<C, Block> {
client: Arc<C>,
_marker: PhantomData<Block>,
}

impl<C, Block> SalpRpcWrapper<C, Block> {
pub fn new(client: Arc<C>) -> Self {
Self { client, _marker: PhantomData }
}
}

#[rpc]
pub trait SalpRpcApi<BlockHash, ParaId, AccountId> {
/// rpc method for getting current contribution
#[rpc(name = "salp_getContribution")]
fn get_contribution(
&self,
index: ParaId,
who: AccountId,
at: Option<BlockHash>,
) -> JsonRpcResult<(NumberOrHex, RpcContributionStatus)>;
}

impl<C, Block, ParaId, AccountId> SalpRpcApi<<Block as BlockT>::Hash, ParaId, AccountId>
for SalpRpcWrapper<C, Block>
where
Block: BlockT,
C: Send + Sync + 'static + ProvideRuntimeApi<Block> + HeaderBackend<Block>,
C::Api: SalpRuntimeApi<Block, ParaId, AccountId>,
ParaId: Codec,
AccountId: Codec,
{
fn get_contribution(
&self,
index: ParaId,
account: AccountId,
at: Option<<Block as BlockT>::Hash>,
) -> JsonRpcResult<(NumberOrHex, RpcContributionStatus)> {
let salp_rpc_api = self.client.runtime_api();
let at = BlockId::<Block>::hash(at.unwrap_or_else(|| self.client.info().best_hash));

let rs = salp_rpc_api.get_contribution(&at, index, account);

match rs {
Ok((val, status)) => Ok((NumberOrHex::Number(val.saturated_into()), status)),
Err(e) => Err(RpcError {
code: ErrorCode::InternalError,
message: "Failed to get salp contribution.".to_owned(),
data: Some(format!("{:?}", e).into()),
}),
}
}
}
Loading

0 comments on commit 49ed51c

Please sign in to comment.