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

A0-1614 (don't) hide subxt types #848

Merged
merged 22 commits into from
Jan 23, 2023
Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
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
7 changes: 3 additions & 4 deletions aleph-client/Cargo.lock

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

4 changes: 1 addition & 3 deletions aleph-client/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
[package]
name = "aleph_client"
# TODO bump major version when API stablize
version = "2.10.0"
version = "3.0.0"
kostekIV marked this conversation as resolved.
Show resolved Hide resolved
edition = "2021"
license = "Apache 2.0"

Expand All @@ -21,7 +20,6 @@ futures = "0.3.25"
serde = { version = "1.0", features = ["derive"] }

frame-support = { git = "https://github.com/Cardinal-Cryptography/substrate.git", branch = "aleph-v0.9.32" }
sp-core = { git = "https://github.com/Cardinal-Cryptography/substrate.git", branch = "aleph-v0.9.32", features = ["full_crypto"] }
sp-runtime = { git = "https://github.com/Cardinal-Cryptography/substrate.git", branch = "aleph-v0.9.32" }
pallet-contracts-primitives = { git = "https://github.com/Cardinal-Cryptography/substrate.git", branch = "aleph-v0.9.32" }
primitives = { path = "../primitives" }
Expand Down
21 changes: 6 additions & 15 deletions aleph-client/src/connections.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,12 @@ use subxt::{
metadata::DecodeWithMetadata,
rpc::RpcParams,
storage::{address::Yes, StaticStorageAddress, StorageAddress},
tx::{BaseExtrinsicParamsBuilder, PlainTip, TxPayload},
SubstrateConfig,
tx::TxPayload,
};

use crate::{
api, sp_weights::weight_v2::Weight, AccountId, AlephConfig, BlockHash, Call, KeyPair,
SubxtClient, TxHash, TxStatus,
ParamsBuilder, SubxtClient, TxHash, TxStatus,
};

/// Capable of communicating with a live Aleph chain.
Expand All @@ -26,6 +25,7 @@ pub struct Connection {
}

/// Any connection that is signed by some key.
#[derive(Clone)]
pub struct SignedConnection {
connection: Connection,
signer: KeyPair,
Expand Down Expand Up @@ -158,7 +158,7 @@ pub trait SignedConnectionApi: ConnectionApi {
async fn send_tx_with_params<Call: TxPayload + Send + Sync>(
&self,
tx: Call,
params: BaseExtrinsicParamsBuilder<SubstrateConfig, PlainTip>,
params: ParamsBuilder,
status: TxStatus,
) -> anyhow::Result<TxInfo>;

Expand Down Expand Up @@ -204,15 +204,6 @@ impl SudoCall for RootConnection {
}
}

impl Clone for SignedConnection {
fn clone(&self) -> Self {
SignedConnection {
connection: self.connection.clone(),
signer: KeyPair::new(self.signer.signer().clone()),
}
}
}

impl AsConnection for Connection {
fn as_connection(&self) -> &Connection {
self
Expand Down Expand Up @@ -294,7 +285,7 @@ impl<S: AsSigned + Sync> SignedConnectionApi for S {
async fn send_tx_with_params<Call: TxPayload + Send + Sync>(
&self,
tx: Call,
params: BaseExtrinsicParamsBuilder<SubstrateConfig, PlainTip>,
params: ParamsBuilder,
status: TxStatus,
) -> anyhow::Result<TxInfo> {
if let Some(details) = tx.validation_details() {
Expand All @@ -305,7 +296,7 @@ impl<S: AsSigned + Sync> SignedConnectionApi for S {
.as_connection()
.as_client()
.tx()
.sign_and_submit_then_watch(&tx, self.as_signed().signer(), params)
.sign_and_submit_then_watch(&tx, &self.as_signed().signer().inner, params)
.await
.map_err(|e| anyhow!("Failed to submit transaction: {:?}", e))?;

Expand Down
2 changes: 1 addition & 1 deletion aleph-client/src/contract/convertible_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ mod tests {
let cast: bool = ConvertibleValue(Bool(true))
.try_into()
.expect("Should cast successfully");
assert_eq!(true, cast);
assert!(cast);
}

#[test]
Expand Down
54 changes: 43 additions & 11 deletions aleph-client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@
extern crate core;

pub use contract_transcode;
pub use subxt::{blocks::ExtrinsicEvents, ext::sp_core::Pair};
pub use subxt::ext::{codec, sp_core, sp_core::Pair, sp_runtime};
use subxt::{
ext::sp_core::{ed25519, sr25519, H256},
tx::PairSigner,
OnlineClient, PolkadotConfig,
OnlineClient, PolkadotConfig, SubstrateConfig,
};

use crate::api::runtime_types::aleph_runtime::RuntimeCall as Call;

// generated by running `subxt codegen --derive Clone Debug Eq PartialEq | rustfmt --edition=2021 > src/aleph_zero.rs`
#[allow(clippy::all)]
#[doc(hidden)]
Expand All @@ -39,23 +39,18 @@ pub use ::primitives::{Balance, BlockNumber};
pub use aleph_zero::api;
pub use runtime_types::*;

/// An alias for a configuration of live chain, e.g. block index type, hash type.
pub type AlephConfig = PolkadotConfig;
/// An alias for a pallet aleph keys.
pub type AlephKeyPair = ed25519::Pair;
/// An alias for a type of a key pair that signs chain transactions.
pub type RawKeyPair = sr25519::Pair;
/// An alias for a signer object that constructs [`sr25519::Pair`] from given account id type.
pub type KeyPair = PairSigner<AlephConfig, sr25519::Pair>;
/// An alias for an account id type.
pub type AccountId = subxt::ext::sp_core::crypto::AccountId32;
/// An alias for a client type.
pub type Client = OnlineClient<AlephConfig>;
/// An alias for a hash type.
pub type CodeHash = H256;
/// An alias for a block hash type.
pub type BlockHash = H256;
/// An alias for a transaction hash type.
pub type TxHash = H256;

/// An alias for an RPC client type.
pub type SubxtClient = OnlineClient<AlephConfig>;

Expand All @@ -64,6 +59,41 @@ pub use connections::{
SignedConnectionApi, SudoCall, TxInfo,
};

/// An alias for a configuration of live chain, e.g. block index type, hash type.
type AlephConfig = PolkadotConfig;
type ParamsBuilder = subxt::tx::PolkadotExtrinsicParamsBuilder<SubstrateConfig>;
type PairSigner = subxt::tx::PairSigner<AlephConfig, RawKeyPair>;

/// Used for signing extrinsic payload
pub struct KeyPair {
inner: PairSigner,
}

impl Clone for KeyPair {
fn clone(&self) -> Self {
KeyPair::new(self.inner.signer().clone())
}
}

impl KeyPair {
/// Constructs a new KeyPair from RawKeyPair
pub fn new(keypair: RawKeyPair) -> Self {
KeyPair {
inner: PairSigner::new(keypair),
}
}

/// Returns a reference to the inner RawKeyPair
pub fn signer(&self) -> &RawKeyPair {
self.inner.signer()
}

/// Returns corresponding AccountId
pub fn account_id(&self) -> &AccountId {
self.inner.account_id()
}
}

/// When submitting a transaction, wait for given status before proceeding.
#[derive(Copy, Clone)]
pub enum TxStatus {
Expand All @@ -79,7 +109,9 @@ pub enum TxStatus {
/// * `seed` - a 12 or 24 word seed phrase
pub fn keypair_from_string(seed: &str) -> KeyPair {
let pair = sr25519::Pair::from_string(seed, None).expect("Can't create pair from seed value");
KeyPair::new(pair)
KeyPair {
fixxxedpoint marked this conversation as resolved.
Show resolved Hide resolved
inner: PairSigner::new(pair),
}
}

/// Converts given seed phrase to a sr25519 [`RawKeyPair`] object.
Expand Down
6 changes: 3 additions & 3 deletions aleph-client/src/pallets/balances.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use subxt::{ext::sp_runtime::MultiAddress, tx::PolkadotExtrinsicParamsBuilder};
use subxt::ext::sp_runtime::MultiAddress;

use crate::{
aleph_zero::{self, api, api::runtime_types::pallet_balances::BalanceLock},
Expand All @@ -7,7 +7,7 @@ use crate::{
pallets::utility::UtilityApi,
AccountId, Balance, BlockHash,
Call::Balances,
ConnectionApi, SignedConnectionApi, TxStatus,
ConnectionApi, ParamsBuilder, SignedConnectionApi, TxStatus,
};

/// Pallet balances read-only API.
Expand Down Expand Up @@ -140,7 +140,7 @@ impl<S: SignedConnectionApi> BalanceUserApi for S {
.balances()
.transfer(MultiAddress::Id(dest), amount);

self.send_tx_with_params(tx, PolkadotExtrinsicParamsBuilder::new().tip(tip), status)
self.send_tx_with_params(tx, ParamsBuilder::new().tip(tip), status)
.await
}
}
Expand Down
17 changes: 7 additions & 10 deletions aleph-client/src/pallets/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use pallet_contracts_primitives::ContractExecResult;
use subxt::{ext::sp_core::Bytes, rpc_params};

use crate::{
api, connections::TxInfo, pallet_contracts::wasm::OwnerInfo, sp_weights::weight_v2::Weight,
AccountId, Balance, BlockHash, ConnectionApi, SignedConnectionApi, TxStatus,
api, pallet_contracts::wasm::OwnerInfo, sp_weights::weight_v2::Weight, AccountId, Balance,
BlockHash, CodeHash, ConnectionApi, SignedConnectionApi, TxInfo, TxStatus,
};

/// Arguments to [`ContractRpc::call_and_get`].
Expand All @@ -30,11 +30,8 @@ pub trait ContractsApi {
/// Returns `contracts.owner_info_of` storage for a given code hash.
/// * `code_hash` - a code hash
/// * `at` - optional hash of a block to query state from
async fn get_owner_info(
&self,
code_hash: BlockHash,
at: Option<BlockHash>,
) -> Option<OwnerInfo>;
async fn get_owner_info(&self, code_hash: CodeHash, at: Option<BlockHash>)
-> Option<OwnerInfo>;
}

/// Pallet contracts api.
Expand All @@ -52,7 +49,7 @@ pub trait ContractsUserApi {
#[allow(clippy::too_many_arguments)]
async fn instantiate(
&self,
code_hash: BlockHash,
code_hash: CodeHash,
balance: Balance,
gas_limit: Weight,
storage_limit: Option<Compact<Balance>>,
Expand Down Expand Up @@ -103,7 +100,7 @@ pub trait ContractRpc {
impl<C: ConnectionApi> ContractsApi for C {
async fn get_owner_info(
&self,
code_hash: BlockHash,
code_hash: CodeHash,
at: Option<BlockHash>,
) -> Option<OwnerInfo> {
let addrs = api::storage().contracts().owner_info_of(code_hash);
Expand All @@ -127,7 +124,7 @@ impl<S: SignedConnectionApi> ContractsUserApi for S {

async fn instantiate(
&self,
code_hash: BlockHash,
code_hash: CodeHash,
balance: Balance,
gas_limit: Weight,
storage_limit: Option<Compact<Balance>>,
Expand Down
6 changes: 2 additions & 4 deletions aleph-client/src/pallets/multisig.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@ use std::{collections::HashSet, marker::PhantomData};
use anyhow::{anyhow, ensure};
use codec::{Decode, Encode};
use primitives::{Balance, BlockNumber};
use sp_core::blake2_256;
use sp_runtime::traits::TrailingZeroInput;

use crate::{
account_from_keypair, aleph_runtime::RuntimeCall, api, api::runtime_types, connections::TxInfo,
sp_weights::weight_v2::Weight, AccountId, BlockHash, ConnectionApi, SignedConnectionApi,
TxStatus,
sp_core::blake2_256, sp_runtime::traits::TrailingZeroInput, sp_weights::weight_v2::Weight,
AccountId, BlockHash, ConnectionApi, SignedConnectionApi, TxStatus,
};

/// An alias for a call hash.
Expand Down
Loading