Skip to content

Commit

Permalink
hide RpcParams
Browse files Browse the repository at this point in the history
  • Loading branch information
krzysztofziobro committed Jan 9, 2023
1 parent ff00519 commit 072eff4
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 11 deletions.
17 changes: 12 additions & 5 deletions aleph-client/src/connections.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@ use codec::Decode;
use log::info;
use subxt::{
metadata::DecodeWithMetadata,
rpc::RpcParams,
storage::{address::Yes, StaticStorageAddress, StorageAddress},
tx::TxPayload,
};

use crate::{
api, sp_core::Bytes, sp_weights::weight_v2::Weight, AccountId, BlockHash, Call, KeyPair,
ParamsBuilder, SubxtClient, TxStatus,
ParamsBuilder, RpcCallParams, SubxtClient, TxStatus,
};

#[derive(Clone)]
Expand Down Expand Up @@ -56,7 +55,11 @@ pub trait ConnectionApi: Sync {
at: Option<BlockHash>,
) -> Option<T::Target>;

async fn rpc_call<R: Decode>(&self, func_name: String, params: RpcParams) -> anyhow::Result<R>;
async fn rpc_call<R: Decode>(
&self,
func_name: String,
params: RpcCallParams,
) -> anyhow::Result<R>;
}

#[async_trait::async_trait]
Expand Down Expand Up @@ -171,13 +174,17 @@ impl<C: AsConnection + Sync> ConnectionApi for C {
.expect("Should access storage")
}

async fn rpc_call<R: Decode>(&self, func_name: String, params: RpcParams) -> anyhow::Result<R> {
async fn rpc_call<R: Decode>(
&self,
func_name: String,
params: RpcCallParams,
) -> anyhow::Result<R> {
info!(target: "aleph-client", "submitting rpc call `{}`, with params {:?}", func_name, params);
let bytes: Bytes = self
.as_connection()
.as_client()
.rpc()
.request(&func_name, params)
.request(&func_name, params.inner)
.await?;

Ok(R::decode(&mut bytes.as_ref())?)
Expand Down
30 changes: 28 additions & 2 deletions aleph-client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@

extern crate core;

use anyhow::anyhow;
pub use contract_transcode;
use serde::Serialize;
use serde_json::value::RawValue;
pub use subxt::ext::{codec, sp_core, sp_runtime};
use subxt::{OnlineClient, PolkadotConfig, SubstrateConfig};
use subxt::{rpc::RpcParams, OnlineClient, PolkadotConfig, SubstrateConfig};

pub use crate::sp_core::Pair;
use crate::{
Expand All @@ -27,7 +30,7 @@ pub use runtime_types::*;

pub type AlephKeyPair = ed25519::Pair;
pub type RawKeyPair = sr25519::Pair;
pub type AccountId = subxt::ext::sp_core::crypto::AccountId32;
pub type AccountId = crate::sp_core::crypto::AccountId32;
pub type BlockHash = H256;
pub type CodeHash = H256;

Expand Down Expand Up @@ -60,6 +63,29 @@ impl KeyPair {
}
}

#[derive(Debug, Clone, Default)]
pub struct RpcCallParams {
inner: RpcParams,
}

impl RpcCallParams {
pub fn new() -> Self {
Self {
inner: RpcParams::new(),
}
}

pub fn push<P: Serialize>(&mut self, param: P) -> anyhow::Result<()> {
self.inner
.push(param)
.map_err(|e| anyhow!("Failed to push rpc param: {:?}", e))
}

pub fn build(self) -> Option<Box<RawValue>> {
self.inner.build()
}
}

#[derive(Copy, Clone)]
pub enum TxStatus {
InBlock,
Expand Down
6 changes: 4 additions & 2 deletions aleph-client/src/pallets/aleph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::{
pallet_aleph::pallet::Call::schedule_finality_version_change,
AccountId, AlephKeyPair, BlockHash,
Call::Aleph,
ConnectionApi, Pair, RootConnection, SudoCall, TxStatus,
ConnectionApi, Pair, RootConnection, RpcCallParams, SudoCall, TxStatus,
};

#[async_trait::async_trait]
Expand Down Expand Up @@ -78,7 +78,9 @@ impl<C: ConnectionApi> AlephRpc for C {
let method = "alephNode_emergencyFinalize";
let signature = key_pair.sign(&hash.encode());
let raw_signature: &[u8] = signature.as_ref();
let params = rpc_params![raw_signature, hash, number];
let params = RpcCallParams {
inner: rpc_params![raw_signature, hash, number],
};

let _: () = self.rpc_call(method.to_string(), params).await?;

Expand Down
6 changes: 4 additions & 2 deletions aleph-client/src/pallets/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use subxt::rpc_params;

use crate::{
api, pallet_contracts::wasm::OwnerInfo, sp_core::Bytes, sp_weights::weight_v2::Weight,
AccountId, BlockHash, CodeHash, ConnectionApi, SignedConnectionApi, TxStatus,
AccountId, BlockHash, CodeHash, ConnectionApi, RpcCallParams, SignedConnectionApi, TxStatus,
};

#[derive(Encode)]
Expand Down Expand Up @@ -178,7 +178,9 @@ impl<C: ConnectionApi> ContractRpc for C {
&self,
args: ContractCallArgs,
) -> anyhow::Result<ContractExecResult<Balance>> {
let params = rpc_params!["ContractsApi_call", Bytes(args.encode())];
let params = RpcCallParams {
inner: rpc_params!["ContractsApi_call", Bytes(args.encode())],
};
self.rpc_call("state_call".to_string(), params).await
}
}

0 comments on commit 072eff4

Please sign in to comment.