Skip to content
This repository was archived by the owner on Sep 28, 2023. It is now read-only.
Draft
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 Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,7 @@ members = [
"precompiles/sr25519",
"precompiles/utils",
"precompiles/xcm",
"chainextensions/traits",
"chainextensions/balances",
"chainextensions/dapps-staking",
]
45 changes: 45 additions & 0 deletions chainextensions/balances/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
[package]
name = "chain-extension-balances"
version = "4.0.0"
authors = ["Stake Technologies <devops@stake.co.jp>"]
edition = "2021"
license = "Apache-2.0"
homepage = "https://astar.network"
repository = "https://github.com/AstarNetwork/astar-frame"
description = "dApps Staking chain extension for WASM contracts"

[dependencies]
codec = { package = "parity-scale-codec", version = "3.0.0", default-features = false }
num-traits = { version = "0.2", default-features = false }
scale-info = { version = "2.1.0", default-features = false, features = ["derive"] }
sp-std = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.24", default-features = false }
sp-core = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.24", default-features = false }
frame-support = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.24", default-features = false }
frame-system = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.24", default-features = false }
pallet-contracts = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.24", default-features = false, features = ["unstable-interface"] }
pallet-contracts-primitives = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.24", default-features = false }
pallet-contracts-rpc-runtime-api = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.24", default-features = false }
sp-runtime = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.24", default-features = false }
log = "0.4.16"

# Astar
chain-extension-traits = { path = "../traits", default-features = false }
pallet-balances = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.24", default-features = false }

[features]
default = ["std"]
std = [
"codec/std",
"frame-support/std",
"frame-system/std",
"num-traits/std",
"pallet-contracts/std",
"pallet-contracts-primitives/std",
"pallet-contracts-rpc-runtime-api/std",
"pallet-balances/std",
"scale-info/std",
"sp-std/std",
"sp-core/std",
"sp-runtime/std",
]

60 changes: 60 additions & 0 deletions chainextensions/balances/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#![cfg_attr(not(feature = "std"), no_std)]
use chain_extension_traits::ChainExtensionExec;
use codec::Encode;
use frame_support::log::{error, trace};
use pallet_contracts::chain_extension::{Environment, Ext, InitState, SysConfig, UncheckedFrom};
use sp_runtime::DispatchError;

pub struct BalancesExtension;

impl<T: pallet_balances::Config> ChainExtensionExec<T> for BalancesExtension {
fn execute_func<E>(func_id: u32, env: Environment<E, InitState>) -> Result<(), DispatchError>
where
E: Ext<T = T>,
<E::T as SysConfig>::AccountId: UncheckedFrom<<E::T as SysConfig>::Hash> + AsRef<[u8]>,
{
let mut env = env.buf_in_buf_out();
match func_id {
// Balances - free_balance()
1 => {
let arg: <E::T as SysConfig>::AccountId = env.read_as()?;
let free_balance = pallet_balances::Pallet::<T>::free_balance(arg.clone());
let free_balance_encoded = free_balance.encode();
trace!(
target: "runtime",
"[ChainExtension]|call|func_id:{:} free_balance_encoded:{:?}, arg:{:?}",
func_id,
free_balance_encoded,
arg
);
env.write(&free_balance_encoded, false, None).map_err(|_| {
DispatchError::Other("ChainExtension failed to call free_balance")
})?;
}
// balances - usable_balance()
2 => {
let arg: <E::T as SysConfig>::AccountId = env.read_as()?;
let usable_balance = pallet_balances::Pallet::<T>::usable_balance(arg.clone());
let usable_balance_encoded = usable_balance.encode();
trace!(
target: "runtime",
"[ChainExtension]|call|func_id:{:} usable_balance_encoded:{:?}, arg:{:?}",
func_id,
usable_balance_encoded,
arg
);
env.write(&usable_balance_encoded, false, None)
.map_err(|_| {
DispatchError::Other("ChainExtension failed to call usable_balance")
})?;
}
_ => {
error!("Called an unregistered `func_id`: {:}", func_id);
return Err(DispatchError::Other(
"BalancesExtension: Unimplemented func_id",
));
}
}
Ok(())
}
}
45 changes: 45 additions & 0 deletions chainextensions/dapps-staking/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
[package]
name = "pallet-chain-extension-dapps-staking"
version = "4.0.0"
authors = ["Stake Technologies <devops@stake.co.jp>"]
edition = "2021"
license = "Apache-2.0"
homepage = "https://astar.network"
repository = "https://github.com/AstarNetwork/astar-frame"
description = "dApps Staking chain extension for WASM contracts"

[dependencies]
codec = { package = "parity-scale-codec", version = "3.0.0", default-features = false }
num-traits = { version = "0.2", default-features = false }
scale-info = { version = "2.1.0", default-features = false, features = ["derive"] }
sp-std = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.24", default-features = false }
sp-core = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.24", default-features = false }
frame-support = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.24", default-features = false }
frame-system = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.24", default-features = false }
pallet-contracts = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.24", default-features = false, features = ["unstable-interface"] }
pallet-contracts-primitives = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.24", default-features = false }
pallet-contracts-rpc-runtime-api = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.24", default-features = false }
sp-runtime = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.24", default-features = false }
log = "0.4.16"

# Astar
chain-extension-traits = { path = "../traits", default-features = false }
pallet-dapps-staking = { path = "../../frame/dapps-staking", default-features = false }

[features]
default = ["std"]
std = [
"codec/std",
"frame-support/std",
"frame-system/std",
"num-traits/std",
"pallet-contracts/std",
"pallet-contracts-primitives/std",
"pallet-contracts-rpc-runtime-api/std",
"pallet-dapps-staking/std",
"scale-info/std",
"sp-std/std",
"sp-core/std",
"sp-runtime/std",
]

65 changes: 65 additions & 0 deletions chainextensions/dapps-staking/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#![cfg_attr(not(feature = "std"), no_std)]

use chain_extension_traits::ChainExtensionExec;
use codec::Encode;
use frame_support::log::{error, trace};
use pallet_contracts::chain_extension::{Environment, Ext, InitState, SysConfig, UncheckedFrom};
use sp_runtime::DispatchError;

pub struct DappsStakingExtension;

impl<T: pallet_dapps_staking::Config> ChainExtensionExec<T> for DappsStakingExtension {
fn execute_func<E>(func_id: u32, env: Environment<E, InitState>) -> Result<(), DispatchError>
where
E: Ext<T = T>,
<E::T as SysConfig>::AccountId: UncheckedFrom<<E::T as SysConfig>::Hash> + AsRef<[u8]>,
{
let mut env = env.buf_in_buf_out();
match func_id {
// DappsStaking - current_era()
1 => {
let current_era = pallet_dapps_staking::CurrentEra::<T>::get();
let current_era_encoded = current_era.encode();
trace!(
target: "runtime",
"[ChainExtension]|call|func_id:{:} current_era:{:?}",
func_id,
&current_era_encoded
);

env.write(&current_era_encoded, false, None).map_err(|_| {
DispatchError::Other(
"ChainExtension DappsStakingExtension failed to write result",
)
})?;
}

// DappsStaking - general_era_info()
2 => {
let arg: u32 = env.read_as()?;
let era_info = pallet_dapps_staking::GeneralEraInfo::<T>::get(arg)
.ok_or(DispatchError::Other("general_era_info call failed"));
let era_info_encoded = era_info.encode();
trace!(
target: "runtime",
"[ChainExtension]|call|func_id:{:} era_info_encoded:{:?}",
func_id,
&era_info_encoded
);

env.write(&era_info_encoded, false, None).map_err(|_| {
DispatchError::Other(
"ChainExtension DappsStakingExtension failed to write result",
)
})?;
}
_ => {
error!("Called an unregistered `func_id`: {:}", func_id);
return Err(DispatchError::Other(
"DappsStakingExtension: Unimplemented func_id",
));
}
}
Ok(())
}
}
21 changes: 21 additions & 0 deletions chainextensions/traits/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[package]
name = "chain-extension-traits"
version = "4.0.0"
authors = ["Stake Technologies <devops@stake.co.jp>"]
edition = "2021"
license = "Apache-2.0"
homepage = "https://astar.network"
repository = "https://github.com/AstarNetwork/astar-frame"
description = "dApps Staking chain extension for WASM contracts"

[dependencies]
pallet-contracts = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.24", default-features = false, features = ["unstable-interface"] }
sp-runtime = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.24", default-features = false }

[features]
default = ["std"]
std = [
"pallet-contracts/std",
"sp-runtime/std",
]

10 changes: 10 additions & 0 deletions chainextensions/traits/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#![cfg_attr(not(feature = "std"), no_std)]
use pallet_contracts::chain_extension::{Environment, Ext, InitState, SysConfig, UncheckedFrom};
use sp_runtime::DispatchError;

pub trait ChainExtensionExec<T: SysConfig> {
fn execute_func<E>(func_id: u32, env: Environment<E, InitState>) -> Result<(), DispatchError>
where
E: Ext<T = T>,
<E::T as SysConfig>::AccountId: UncheckedFrom<<E::T as SysConfig>::Hash> + AsRef<[u8]>;
}