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

Add traits and remove excessive type definition in extrinsic helpers #434

Merged
merged 25 commits into from
Jan 19, 2023
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
4 changes: 2 additions & 2 deletions examples/examples/contract_instantiate_with_code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ use codec::Decode;
use kitchensink_runtime::{AccountId, Runtime, Signature};
use sp_keyring::AccountKeyring;
use substrate_api_client::{
rpc::JsonrpseeClient, Api, ExtrinsicSigner, PlainTipExtrinsicParams, StaticEvent,
SubmitAndWatch, SubmitAndWatchUntilSuccess, XtStatus,
extrinsic::ContractsExtrinsics, rpc::JsonrpseeClient, Api, ExtrinsicSigner,
PlainTipExtrinsicParams, StaticEvent, SubmitAndWatch, SubmitAndWatchUntilSuccess, XtStatus,
};

#[allow(unused)]
Expand Down
4 changes: 2 additions & 2 deletions examples/examples/event_error_details.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ use kitchensink_runtime::{Runtime, Signature};
use sp_keyring::AccountKeyring;
use sp_runtime::{AccountId32 as AccountId, MultiAddress};
use substrate_api_client::{
rpc::JsonrpseeClient, Api, AssetTipExtrinsicParams, ExtrinsicSigner, GetAccountInformation,
StaticEvent, SubmitAndWatchUntilSuccess,
extrinsic::BalancesExtrinsics, rpc::JsonrpseeClient, Api, AssetTipExtrinsicParams,
ExtrinsicSigner, GetAccountInformation, StaticEvent, SubmitAndWatchUntilSuccess,
};

#[derive(Decode)]
Expand Down
6 changes: 4 additions & 2 deletions examples/examples/staking_batch_payout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ use pallet_staking::{ActiveEraInfo, Exposure};
use sp_keyring::AccountKeyring;
use sp_runtime::{app_crypto::Ss58Codec, AccountId32};
use substrate_api_client::{
rpc::JsonrpseeClient, Api, AssetTipExtrinsicParams, ExtrinsicSigner, GetStorage,
SubmitAndWatch, SubmitAndWatchUntilSuccess, XtStatus,
extrinsic::{StakingExtrinsics, UtilityExtrinsics},
rpc::JsonrpseeClient,
Api, AssetTipExtrinsicParams, ExtrinsicSigner, GetStorage, SubmitAndWatch,
SubmitAndWatchUntilSuccess, XtStatus,
};

const MAX_BATCHED_TRANSACTION: u32 = 9;
Expand Down
4 changes: 2 additions & 2 deletions examples/examples/transfer_with_tungstenite_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ use sp_core::{
};
use sp_runtime::MultiAddress;
use substrate_api_client::{
rpc::TungsteniteRpcClient, Api, AssetTipExtrinsicParams, ExtrinsicSigner,
GetAccountInformation, SubmitAndWatch, XtStatus,
extrinsic::BalancesExtrinsics, rpc::TungsteniteRpcClient, Api, AssetTipExtrinsicParams,
ExtrinsicSigner, GetAccountInformation, SubmitAndWatch, XtStatus,
};

fn main() {
Expand Down
4 changes: 2 additions & 2 deletions examples/examples/transfer_with_ws_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ use sp_core::{
};
use sp_runtime::MultiAddress;
use substrate_api_client::{
rpc::WsRpcClient, Api, AssetTipExtrinsicParams, ExtrinsicSigner, GetAccountInformation,
SubmitAndWatch, XtStatus,
extrinsic::BalancesExtrinsics, rpc::WsRpcClient, Api, AssetTipExtrinsicParams, ExtrinsicSigner,
GetAccountInformation, SubmitAndWatch, XtStatus,
};

fn main() {
Expand Down
81 changes: 46 additions & 35 deletions src/extrinsic/balances.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/

//! Extrinsics for `pallet-balances`.
//! https://polkadot.js.org/docs/substrate/extrinsics/#balances

use crate::{api::Api, rpc::Request};
use ac_compose_macros::compose_extrinsic;
Expand All @@ -24,60 +25,70 @@ use ac_primitives::{
};
use alloc::borrow::ToOwned;
use codec::{Compact, Encode};
use serde::de::DeserializeOwned;
use sp_runtime::traits::GetRuntimeBlockType;

pub const BALANCES_MODULE: &str = "Balances";
pub const BALANCES_TRANSFER: &str = "transfer";
pub const BALANCES_SET_BALANCE: &str = "set_balance";
pub const TRANSFER: &str = "transfer";
pub const SET_BALANCE: &str = "set_balance";

pub type BalanceTransferFn<Address, Balance> = (CallIndex, Address, Compact<Balance>);
pub type BalanceSetBalanceFn<Address, Balance> =
/// Call for a balance transfer.
pub type TransferCall<Address, Balance> = (CallIndex, Address, Compact<Balance>);

/// Call to the balance of an account.
pub type SetBalanceCall<Address, Balance> =
(CallIndex, Address, Compact<Balance>, Compact<Balance>);

pub type BalanceTransferXt<Address, Balance, Signature, SignedExtra> =
UncheckedExtrinsicV4<Address, BalanceTransferFn<Address, Balance>, Signature, SignedExtra>;
pub type BalanceSetBalanceXt<Address, Balance, Signature, SignedExtra> =
UncheckedExtrinsicV4<Address, BalanceSetBalanceFn<Address, Balance>, Signature, SignedExtra>;
pub trait BalancesExtrinsics {
type Balance;
type Address;
type Extrinsic<Call>;

/// Transfer some liquid free balance to another account.
fn balance_transfer(
&self,
to: Self::Address,
amount: Self::Balance,
) -> Self::Extrinsic<TransferCall<Self::Address, Self::Balance>>;

impl<Signer, Client, Params, Runtime> Api<Signer, Client, Params, Runtime>
/// Set the balances of a given account.
fn balance_set_balance(
&self,
who: Self::Address,
free_balance: Self::Balance,
reserved_balance: Self::Balance,
) -> Self::Extrinsic<SetBalanceCall<Self::Address, Self::Balance>>;
}

impl<Signer, Client, Params, Runtime> BalancesExtrinsics for Api<Signer, Client, Params, Runtime>
where
Signer: SignExtrinsic<Runtime::AccountId>,
Client: Request,
Runtime: GetRuntimeBlockType + BalancesConfig,
Runtime: BalancesConfig,
Params: ExtrinsicParams<Runtime::Index, Runtime::Hash>,
Compact<Runtime::Balance>: Encode,
Runtime::Header: DeserializeOwned,
Runtime::RuntimeBlock: DeserializeOwned,
{
pub fn balance_transfer(
type Balance = Runtime::Balance;
type Address = Signer::ExtrinsicAddress;
type Extrinsic<Call> =
UncheckedExtrinsicV4<Self::Address, Call, Signer::Signature, Params::SignedExtra>;

fn balance_transfer(
&self,
to: Signer::ExtrinsicAddress,
amount: Runtime::Balance,
) -> BalanceTransferXt<
Signer::ExtrinsicAddress,
Runtime::Balance,
Signer::Signature,
Params::SignedExtra,
> {
compose_extrinsic!(self, BALANCES_MODULE, BALANCES_TRANSFER, to, Compact(amount))
to: Self::Address,
amount: Self::Balance,
) -> Self::Extrinsic<TransferCall<Self::Address, Self::Balance>> {
compose_extrinsic!(self, BALANCES_MODULE, TRANSFER, to, Compact(amount))
}

pub fn balance_set_balance(
fn balance_set_balance(
&self,
who: Signer::ExtrinsicAddress,
free_balance: Runtime::Balance,
reserved_balance: Runtime::Balance,
) -> BalanceSetBalanceXt<
Signer::ExtrinsicAddress,
Runtime::Balance,
Signer::Signature,
Params::SignedExtra,
> {
who: Self::Address,
free_balance: Self::Balance,
reserved_balance: Self::Balance,
) -> Self::Extrinsic<SetBalanceCall<Self::Address, Self::Balance>> {
compose_extrinsic!(
self,
BALANCES_MODULE,
BALANCES_SET_BALANCE,
SET_BALANCE,
who,
Compact(free_balance),
Compact(reserved_balance)
Expand Down
34 changes: 0 additions & 34 deletions src/extrinsic/common.rs

This file was deleted.

Loading