Skip to content
This repository has been archived by the owner on Sep 28, 2023. It is now read-only.

Commit

Permalink
Remove chain-extension-trait package (#109)
Browse files Browse the repository at this point in the history
* Remove chain-extension-trait package

* Dapps Staking CE optimizations

* Dapps Staking CE optimizations

* Rename dapps-staking CE in general way

* Derive Default for chain extension structs

* Implement Default trait for CEs

* Fix typo
  • Loading branch information
akru committed Oct 25, 2022
1 parent 5340817 commit 902fcbf
Show file tree
Hide file tree
Showing 12 changed files with 74 additions and 144 deletions.
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ members = [
"chain-extensions/dapps-staking",
"chain-extensions/rmrk",
"chain-extensions/xvm",
"chain-extensions/trait",
"chain-extensions/types/*",
"frame/block-reward",
"frame/collator-selection",
Expand Down
7 changes: 3 additions & 4 deletions chain-extensions/dapps-staking/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "dapps-staking-chain-extension"
version = "1.0.4"
name = "pallet-chain-extension-dapps-staking"
version = "1.1.0"
authors = ["Stake Technologies <devops@stake.co.jp>"]
edition = "2021"
license = "Apache-2.0"
Expand All @@ -23,9 +23,8 @@ sp-runtime = { git = "https://github.com/paritytech/substrate", branch = "polkad
sp-std = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.29", default-features = false }

# Astar
chain-extension-trait = { path = "../trait", default-features = false }
dapps-staking-chain-extension-types = { path = "../types/dapps-staking", default-features = false }
pallet-dapps-staking = { path = "../../frame/dapps-staking", default-features = false }
pallet-dapps-staking = { git = "https://github.com/AstarNetwork/astar-frame", branch = "polkadot-v0.9.29", default-features = false }

[features]
default = ["std"]
Expand Down
91 changes: 35 additions & 56 deletions chain-extensions/dapps-staking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,15 @@ use sp_runtime::{
DispatchError,
};

use chain_extension_trait::ChainExtensionExec;
use codec::{Decode, Encode};
use codec::Encode;
use dapps_staking_chain_extension_types::{
Contract, DSError, DappsStakingAccountInput, DappsStakingEraInput, DappsStakingNominationInput,
DSError, DappsStakingAccountInput, DappsStakingEraInput, DappsStakingNominationInput,
DappsStakingValueInput,
};
use frame_support::traits::{Currency, Get};
use frame_system::RawOrigin;
use pallet_contracts::chain_extension::{
Environment, Ext, InitState, RetVal, SysConfig, UncheckedFrom,
ChainExtension, Environment, Ext, InitState, RetVal, SysConfig, UncheckedFrom,
};
use pallet_dapps_staking::{RewardDestination, WeightInfo};
use sp_std::marker::PhantomData;
Expand All @@ -39,10 +38,10 @@ enum DappsStakingFunc {
NominationTransfer,
}

impl TryFrom<u32> for DappsStakingFunc {
impl TryFrom<u16> for DappsStakingFunc {
type Error = DispatchError;

fn try_from(value: u32) -> Result<Self, Self::Error> {
fn try_from(value: u16) -> Result<Self, Self::Error> {
match value {
1 => Ok(DappsStakingFunc::CurrentEra),
2 => Ok(DappsStakingFunc::UnbondingPeriod),
Expand All @@ -65,19 +64,27 @@ impl TryFrom<u32> for DappsStakingFunc {
}
}

pub struct DappsStakingExtension<R>(PhantomData<R>);
/// Dapps Staking chain extension.
pub struct DappsStakingExtension<T>(PhantomData<T>);

impl<T: pallet_dapps_staking::Config> ChainExtensionExec<T> for DappsStakingExtension<T> {
fn execute_func<E>(
func_id: u32,
env: Environment<E, InitState>,
) -> Result<RetVal, DispatchError>
impl<T> Default for DappsStakingExtension<T> {
fn default() -> Self {
DappsStakingExtension(PhantomData)
}
}

impl<T> ChainExtension<T> for DappsStakingExtension<T>
where
T: pallet_dapps_staking::Config + pallet_contracts::Config,
<T as pallet_dapps_staking::Config>::SmartContract: From<[u8; 32]>,
<T as SysConfig>::AccountId: From<[u8; 32]>,
{
fn call<E: Ext>(&mut self, env: Environment<E, InitState>) -> Result<RetVal, DispatchError>
where
E: Ext<T = T>,
<E::T as SysConfig>::AccountId:
UncheckedFrom<<E::T as SysConfig>::Hash> + AsRef<[u8]> + From<[u8; 32]>,
<E::T as SysConfig>::AccountId: UncheckedFrom<<E::T as SysConfig>::Hash> + AsRef<[u8]>,
{
let func_id = DappsStakingFunc::try_from(func_id)?;
let func_id = env.func_id().try_into()?;
let mut env = env.buf_in_buf_out();

match func_id {
Expand Down Expand Up @@ -133,8 +140,9 @@ impl<T: pallet_dapps_staking::Config> ChainExtensionExec<T> for DappsStakingExte

DappsStakingFunc::StakedAmountOnContract => {
let args: DappsStakingAccountInput = env.read_as()?;
let staker = T::AccountId::decode(&mut args.staker.as_ref()).unwrap();
let contract = Self::decode_smart_contract(args.contract)?;
let staker: T::AccountId = args.staker.into();
let contract: <T as pallet_dapps_staking::Config>::SmartContract =
args.contract.into();

let base_weight = <T as frame_system::Config>::DbWeight::get().reads(1);
env.charge_weight(base_weight)?;
Expand All @@ -147,7 +155,8 @@ impl<T: pallet_dapps_staking::Config> ChainExtensionExec<T> for DappsStakingExte

DappsStakingFunc::ReadContractStake => {
let contract_bytes: [u8; 32] = env.read_as()?;
let contract = Self::decode_smart_contract(contract_bytes)?;
let contract: <T as pallet_dapps_staking::Config>::SmartContract =
contract_bytes.into();

let base_weight = <T as frame_system::Config>::DbWeight::get().reads(1);
env.charge_weight(base_weight.saturating_add(base_weight))?;
Expand All @@ -162,7 +171,7 @@ impl<T: pallet_dapps_staking::Config> ChainExtensionExec<T> for DappsStakingExte

DappsStakingFunc::BondAndStake => {
let args: DappsStakingValueInput<BalanceOf<T>> = env.read_as()?;
let contract = Self::decode_smart_contract(args.contract)?;
let contract = args.contract.into();
let value: BalanceOf<T> = args.value;

let base_weight = <T as pallet_dapps_staking::Config>::WeightInfo::bond_and_stake();
Expand All @@ -185,7 +194,7 @@ impl<T: pallet_dapps_staking::Config> ChainExtensionExec<T> for DappsStakingExte

DappsStakingFunc::UnbondAndUnstake => {
let args: DappsStakingValueInput<BalanceOf<T>> = env.read_as()?;
let contract = Self::decode_smart_contract(args.contract)?;
let contract = args.contract.into();
let value: BalanceOf<T> = args.value;

let base_weight =
Expand Down Expand Up @@ -228,10 +237,10 @@ impl<T: pallet_dapps_staking::Config> ChainExtensionExec<T> for DappsStakingExte

DappsStakingFunc::ClaimStaker => {
let contract_bytes: [u8; 32] = env.read_as()?;
let contract = Self::decode_smart_contract(contract_bytes)?;
let contract = contract_bytes.into();

let base_weight = T::WeightInfo::claim_staker_with_restake()
.max(T::WeightInfo::claim_staker_without_restake());
let base_weight = <T as pallet_dapps_staking::Config>::WeightInfo::claim_staker_with_restake()
.max(<T as pallet_dapps_staking::Config>::WeightInfo::claim_staker_without_restake());
let charged_weight = env.charge_weight(base_weight)?;

let caller = env.ext().address().clone();
Expand Down Expand Up @@ -259,7 +268,7 @@ impl<T: pallet_dapps_staking::Config> ChainExtensionExec<T> for DappsStakingExte

DappsStakingFunc::ClaimDapp => {
let args: DappsStakingEraInput = env.read_as()?;
let contract = Self::decode_smart_contract(args.contract)?;
let contract = args.contract.into();
let era: u32 = args.era;

let base_weight = <T as pallet_dapps_staking::Config>::WeightInfo::claim_dapp();
Expand Down Expand Up @@ -313,8 +322,8 @@ impl<T: pallet_dapps_staking::Config> ChainExtensionExec<T> for DappsStakingExte

DappsStakingFunc::NominationTransfer => {
let args: DappsStakingNominationInput<BalanceOf<T>> = env.read_as()?;
let origin_smart_contract = Self::decode_smart_contract(args.origin_contract)?;
let target_smart_contract = Self::decode_smart_contract(args.target_contract)?;
let origin_smart_contract = args.origin_contract.into();
let target_smart_contract = args.target_contract.into();
let value: BalanceOf<T> = args.value;

let base_weight =
Expand All @@ -341,33 +350,3 @@ impl<T: pallet_dapps_staking::Config> ChainExtensionExec<T> for DappsStakingExte
Ok(RetVal::Converging(DSError::Success as u32))
}
}

impl<R> DappsStakingExtension<R> {
/// Helper method to decode type SmartContract enum
pub fn decode_smart_contract(
contract_bytes: [u8; 32],
) -> Result<<R as pallet_dapps_staking::Config>::SmartContract, DispatchError>
where
R: pallet_dapps_staking::Config,
R::AccountId: From<[u8; 32]>,
{
let account: R::AccountId = contract_bytes.into();
// Encode contract address to fit SmartContract enum.
// Since the SmartContract enum type can't be accessed from this chain extension,
// use locally defined enum clone (see Contract enum)
let contract_enum_encoded = Contract::<R::AccountId>::Wasm(account).encode();

// encoded enum will add one byte before the contract's address
// therefore we need to decode len(u32) + 1 byte = 33
let smart_contract = <R as pallet_dapps_staking::Config>::SmartContract::decode(
&mut &contract_enum_encoded[..33],
)
.map_err(|_| {
DispatchError::Other(
"[ChainExtension] Error while decoding SmartContract in ChainExtension",
)
})?;

Ok(smart_contract)
}
}
2 changes: 0 additions & 2 deletions chain-extensions/rmrk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ sp-runtime = { git = "https://github.com/paritytech/substrate", branch = "polkad
sp-std = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.29", default-features = false }

# RMRK
chain-extension-trait = { path = "../trait", default-features = false }
pallet-rmrk-core = { git = "https://github.com/AstarNetwork/rmrk-substrate", branch = "polkadot-v0.9.29", default-features = false }
rmrk-chain-extension-types = { path = "../types/rmrk", default-features = false }
rmrk-traits = { git = "https://github.com/AstarNetwork/rmrk-substrate", branch = "polkadot-v0.9.29", default-features = false }
Expand All @@ -33,7 +32,6 @@ rmrk-traits = { git = "https://github.com/AstarNetwork/rmrk-substrate", branch =
default = ["std"]
std = [
"codec/std",
"chain-extension-trait/std",
"frame-support/std",
"frame-system/std",
"num-traits/std",
Expand Down
31 changes: 17 additions & 14 deletions chain-extensions/rmrk/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@

use sp_runtime::{traits::StaticLookup, DispatchError, Permill};

use chain_extension_trait::ChainExtensionExec;

use codec::Encode;
use frame_support::{log, weights::Weight, BoundedVec};
use frame_system::RawOrigin;
use pallet_contracts::chain_extension::{
Environment, Ext, InitState, RetVal, RetVal::Converging, SysConfig, UncheckedFrom,
ChainExtension, Environment, Ext, InitState, RetVal, RetVal::Converging, SysConfig,
UncheckedFrom,
};
use pallet_rmrk_core::{BoundedResourceInfoTypeOf, StringLimitOf};
use rmrk_chain_extension_types::{RmrkError, RmrkFunc};
Expand All @@ -18,23 +17,27 @@ use rmrk_traits::{
};
use sp_std::{marker::PhantomData, vec::Vec};

pub struct RmrkExtension<R>(PhantomData<R>);
/// RMRK chain extension.
pub struct RmrkExtension<T>(PhantomData<T>);

impl<
T: pallet_rmrk_core::Config
+ pallet_uniques::Config<CollectionId = CollectionId, ItemId = NftId>,
> ChainExtensionExec<T> for RmrkExtension<T>
impl<T> Default for RmrkExtension<T> {
fn default() -> Self {
RmrkExtension(PhantomData)
}
}

impl<T> ChainExtension<T> for RmrkExtension<T>
where
T: pallet_contracts::Config
+ pallet_rmrk_core::Config
+ pallet_uniques::Config<CollectionId = CollectionId, ItemId = NftId>,
{
fn execute_func<E>(
func_id: u32,
env: Environment<E, InitState>,
) -> Result<RetVal, DispatchError>
fn call<E: Ext>(&mut self, env: Environment<E, InitState>) -> Result<RetVal, DispatchError>
where
E: Ext<T = T>,
<E::T as SysConfig>::AccountId: UncheckedFrom<<E::T as SysConfig>::Hash> + AsRef<[u8]>,
{
let func_id = RmrkFunc::try_from(func_id)?;

let func_id = env.func_id().try_into()?;
match func_id {
// READ functions
RmrkFunc::CollectionIndex => {
Expand Down
20 changes: 0 additions & 20 deletions chain-extensions/trait/Cargo.toml

This file was deleted.

21 changes: 0 additions & 21 deletions chain-extensions/trait/src/lib.rs

This file was deleted.

2 changes: 1 addition & 1 deletion chain-extensions/types/dapps-staking/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "dapps-staking-chain-extension-types"
version = "1.0.2"
version = "1.1.0"
authors = ["Stake Technologies <devops@stake.co.jp>"]
edition = "2021"
license = "Apache-2.0"
Expand Down
10 changes: 0 additions & 10 deletions chain-extensions/types/dapps-staking/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#![cfg_attr(not(feature = "std"), no_std)]
use codec::{Decode, Encode};
use frame_support::pallet_prelude::MaxEncodedLen;
use sp_core::H160;
use sp_runtime::{DispatchError, ModuleError};

#[cfg_attr(feature = "std", derive(scale_info::TypeInfo))]
Expand Down Expand Up @@ -111,15 +110,6 @@ impl TryFrom<DispatchError> for DSError {
}
}

/// This is only used to encode SmartContract enum
#[derive(PartialEq, Eq, Copy, Clone, Encode, Decode, Debug)]
pub enum Contract<Account> {
// EVM smart contract instance.
Evm(H160),
// Wasm smart contract instance. Not used in this precompile
Wasm(Account),
}

#[derive(Debug, Copy, Clone, PartialEq, Eq, Encode, Decode, MaxEncodedLen)]
pub struct DappsStakingValueInput<Balance> {
pub contract: [u8; 32],
Expand Down
4 changes: 2 additions & 2 deletions chain-extensions/types/rmrk/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ pub enum RmrkFunc {
SetPriority,
}

impl TryFrom<u32> for RmrkFunc {
impl TryFrom<u16> for RmrkFunc {
type Error = DispatchError;

fn try_from(value: u32) -> Result<Self, Self::Error> {
fn try_from(value: u16) -> Result<Self, Self::Error> {
return match value {
// getters
// 0x0001 => Ok(RmrkFunc::NextNftId),
Expand Down
3 changes: 1 addition & 2 deletions chain-extensions/xvm/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = "xvm-chain-extension"
name = "pallet-chain-extension-xvm"
version = "0.1.0"
authors = ["Stake Technologies <devops@stake.co.jp>"]
edition = "2021"
Expand All @@ -23,7 +23,6 @@ sp-runtime = { git = "https://github.com/paritytech/substrate", branch = "polkad
sp-std = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.29", default-features = false }

# Astar
chain-extension-trait = { path = "../trait", default-features = false }
pallet-xvm = { path = "../../frame/pallet-xvm", default-features = false }
xvm-chain-extension-types = { path = "../types/xvm", default-features = false }

Expand Down
Loading

0 comments on commit 902fcbf

Please sign in to comment.