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

call-runtime: use subxt #1776

Closed
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
3 changes: 3 additions & 0 deletions integration-tests/call-runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ ink = { path = "../../crates/ink", default-features = false, features = ["call-r
scale = { package = "parity-scale-codec", version = "3", default-features = false, features = ["derive"] }
scale-info = { version = "2.6", default-features = false, features = ["derive"], optional = true }

subxt-macro = { git = "https://github.com/pmikolajczyk41/subxt", branch = "pmikolajczyk41/no-std-imports" }
primitive-types = { version = "0.12.1", default-features = false }

# Substrate
#
# We need to explicitly turn off some of the `sp-io` features, to avoid conflicts
Expand Down
1 change: 0 additions & 1 deletion integration-tests/call-runtime/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ To integrate this example into Substrate you need to adjust pallet contracts con
// `Everything` or anything that will allow for the `Balances::transfer` extrinsic.
type CallFilter = frame_support::traits::Everything;
type UnsafeUnstableInterface = ConstBool<true>;
}
```
Expand Down
68 changes: 27 additions & 41 deletions integration-tests/call-runtime/lib.rs
Original file line number Diff line number Diff line change
@@ -1,51 +1,35 @@
#![cfg_attr(not(feature = "std"), no_std, no_main)]

use ink::primitives::AccountId;
use sp_runtime::MultiAddress;

/// A part of the runtime dispatchable API.
///
/// For now, `ink!` doesn't provide any support for exposing the real `RuntimeCall` enum,
/// which fully describes the composed API of all the pallets present in runtime. Hence,
/// in order to use `call-runtime` functionality, we have to provide at least a partial
/// object, which correctly encodes the target extrinsic.
///
/// You can investigate the full `RuntimeCall` definition by either expanding
/// `construct_runtime!` macro application or by using secondary tools for reading chain
/// metadata, like `subxt`.
#[derive(scale::Encode)]
enum RuntimeCall {
/// This index can be found by investigating runtime configuration. You can check the
/// pallet order inside `construct_runtime!` block and read the position of your
/// pallet (0-based).
///
///
/// [See here for more.](https://substrate.stackexchange.com/questions/778/how-to-get-pallet-index-u8-of-a-pallet-in-runtime)
#[codec(index = 4)]
Balances(BalancesCall),
}
pub mod subxt {
pub mod ext {
pub use scale as codec;
}

#[derive(scale::Encode)]
enum BalancesCall {
/// This index can be found by investigating the pallet dispatchable API. In your
/// pallet code, look for `#[pallet::call]` section and check
/// `#[pallet::call_index(x)]` attribute of the call. If these attributes are
/// missing, use source-code order (0-based).
#[codec(index = 0)]
Transfer {
dest: MultiAddress<AccountId, ()>,
#[codec(compact)]
value: u128,
},
pub mod utils {
pub use primitive_types::H256;
pub use sp_runtime::{
AccountId32,
MultiAddress,
};
}
}

#[subxt_macro::subxt(
runtime_metadata_url = "ws://localhost:9944",
runtime_types_only,
no_default_derives,
derive_for_all_types = "crate::subxt::ext::codec::Encode",
attributes_for_all_types = "#[codec(crate = crate::subxt::ext::codec)]",
crate = "crate::subxt"
)]
mod substrate {}

#[ink::contract]
mod runtime_call {
use crate::{
BalancesCall,
RuntimeCall,
use crate::substrate::runtime_types::{
contracts_node_runtime::RuntimeCall,
pallet_balances::pallet::Call as BalanceCall,
};

use ink::env::Error as EnvError;

/// A trivial contract with a single message, that uses `call-runtime` API for
Expand Down Expand Up @@ -94,8 +78,10 @@ mod runtime_call {
receiver: AccountId,
value: Balance,
) -> Result<(), RuntimeError> {
let receiver: [u8; 32] = *receiver.as_ref();
let receiver: sp_runtime::AccountId32 = receiver.into();
self.env()
.call_runtime(&RuntimeCall::Balances(BalancesCall::Transfer {
.call_runtime(&RuntimeCall::Balances(BalanceCall::transfer {
dest: receiver.into(),
value,
}))
Expand Down