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/optimize salp #1433

Merged
merged 8 commits into from
Sep 23, 2024
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
78 changes: 78 additions & 0 deletions pallets/deprecated/salp/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
[package]
name = "bifrost-salp"
version = "0.8.0"
authors = ["Edwin Wang <[email protected]>", "Ron yang<[email protected]>"]
edition = "2021"

[dependencies]
parity-scale-codec = { workspace = true, features = ["derive"] }
scale-info = { workspace = true, features = ["derive"] }
log = { workspace = true }
bifrost-primitives = { workspace = true }
frame-support = { workspace = true }
frame-system = { workspace = true }
frame-benchmarking = { workspace = true, optional = true }
sp-std = { workspace = true }
sp-io = { workspace = true }
sp-runtime = { workspace = true }
sp-arithmetic = { workspace = true }
orml-traits = { workspace = true }
orml-tokens = { workspace = true }
bifrost-xcm-interface = { workspace = true }
zenlink-protocol = { workspace = true }
cumulus-primitives-core = { workspace = true }
pallet-xcm = { workspace = true }
xcm = { workspace = true }
xcm-builder = { workspace = true }
bifrost-stable-pool = { workspace = true }
bifrost-stable-asset = { workspace = true }
bifrost-vtoken-minting = { workspace = true }

[dev-dependencies]
pallet-multisig = { workspace = true }
pallet-sudo = { workspace = true }
pallet-collective = { workspace = true }
sp-core = { workspace = true }
bifrost-currencies = { workspace = true }
orml-xtokens = { workspace = true }
pallet-balances = { workspace = true }
bifrost-asset-registry = { workspace = true }
xcm-executor = { workspace = true }


[features]
default = ["std"]
std = [
"parity-scale-codec/std",
"scale-info/std",
"log/std",
"bifrost-primitives/std",
"frame-support/std",
"frame-system/std",
"sp-std/std",
"sp-io/std",
"sp-runtime/std",
"sp-arithmetic/std",
"orml-traits/std",
"bifrost-xcm-interface/std",
"xcm-builder/std",
"zenlink-protocol/std",
"bifrost-asset-registry/std",
"cumulus-primitives-core/std",
"pallet-xcm/std",
"pallet-sudo/std",
"xcm/std",
"pallet-multisig/std",
"pallet-collective/std",
"orml-xtokens/std",
]

runtime-benchmarks = [
"frame-benchmarking",
"frame-support/runtime-benchmarks",
"frame-system/runtime-benchmarks",
"xcm-builder/runtime-benchmarks",
"pallet-xcm/runtime-benchmarks",
"pallet-collective/runtime-benchmarks",
]
try-runtime = [ "frame-support/try-runtime" ]
16 changes: 16 additions & 0 deletions pallets/deprecated/salp/rpc/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[package]
name = "bifrost-salp-rpc"
version = "0.8.0"
authors = ["Ron Yang <[email protected]>"]
edition = "2021"

[dependencies]
parity-scale-codec = { workspace = true, features = ["derive"] }
jsonrpsee = { workspace = true, features = ["server", "macros"] }
sp-api = { workspace = true }
sp-runtime = { workspace = true }
sp-blockchain = { workspace = true }
sp-core = { workspace = true }
sp-rpc = { workspace = true }
bifrost-primitives = { workspace = true }
bifrost-salp-rpc-runtime-api = { workspace = true }
18 changes: 18 additions & 0 deletions pallets/deprecated/salp/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<[email protected]>"]
edition = "2021"

[dependencies]
parity-scale-codec = { workspace = true, features = ["derive"] }
sp-api = { workspace = true }
bifrost-primitives = { workspace = true }

[features]
default = ["std"]
std = [
"parity-scale-codec/std",
"sp-api/std",
"bifrost-primitives/std",
]
35 changes: 35 additions & 0 deletions pallets/deprecated/salp/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) Liebi Technologies PTE. 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 bifrost_primitives::{Balance, RpcContributionStatus};
use parity_scale_codec::Codec;
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);
}
}
101 changes: 101 additions & 0 deletions pallets/deprecated/salp/rpc/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
// This file is part of Bifrost.

// Copyright (C) Liebi Technologies PTE. 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};

use bifrost_primitives::{Balance, RpcContributionStatus};
pub use bifrost_salp_rpc_runtime_api::{self as runtime_api, SalpRuntimeApi};
use jsonrpsee::{
core::{async_trait, RpcResult},
proc_macros::rpc,
types::error::{ErrorCode, ErrorObject},
};
use parity_scale_codec::Codec;
use sp_api::ProvideRuntimeApi;
use sp_blockchain::HeaderBackend;
use sp_rpc::number::NumberOrHex;
use sp_runtime::{sp_std::convert::TryInto, traits::Block as BlockT};

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

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

fn convert_rpc_params(value: Balance) -> RpcResult<NumberOrHex> {
value.try_into().map_err(|e| {
ErrorObject::owned(
ErrorCode::InvalidParams.code(),
format!("{} doesn't fit in NumberOrHex representation", value),
Some(format!("{:?}", e)),
)
})
}

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

#[async_trait]
impl<C, Block, ParaId, AccountId> SalpRpcApiServer<<Block as BlockT>::Hash, ParaId, AccountId>
for SalpRpc<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>,
) -> RpcResult<(NumberOrHex, RpcContributionStatus)> {
let salp_rpc_api = self.client.runtime_api();
let at = at.unwrap_or_else(|| self.client.info().best_hash);

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

match rs {
Ok((val, status)) => match convert_rpc_params(val) {
Ok(value) => Ok((value, status)),
Err(e) => Err(e),
},
Err(e) => Err(ErrorObject::owned(
ErrorCode::InternalError.code(),
"Failed to get salp contribution.",
Some(format!("{:?}", e)),
)),
}
}
}
Loading