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

Precompile: Governance flow #126

Merged
merged 9 commits into from
Aug 13, 2024
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
433 changes: 245 additions & 188 deletions Cargo.lock

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ pallet-evm-precompile-staking = { path = "precompiles/staking", default-features
pallet-evm-precompile-faucet = { path = "precompiles/faucet", default-features = false }
pallet-evm-precompile-nomination-pools = { path = "precompiles/nomination-pools", default-features = false }
pallet-evm-precompile-babe = { path = "precompiles/babe", default-features = false }
pallet-evm-precompile-governance = { path = "precompiles/governance", default-features = false }
pallet-evm-precompile-treasury = { path = "precompiles/treasury", default-features = false }
pallet-evm-precompile-preimage = { path = "precompiles/preimage", default-features = false }

async-trait = "0.1"
bn = { package = "substrate-bn", version = "0.6", default-features = false }
Expand Down
42 changes: 42 additions & 0 deletions precompiles/governance/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
[package]
name = "pallet-evm-precompile-governance"
authors = { workspace = true }
edition = "2021"
version = "0.0.1"

[dependencies]
precompile-utils = { workspace = true }
pallet-democracy = { workspace = true }

evm = { workspace = true }
fp-evm = { workspace = true }
pallet-evm = { workspace = true }

frame-support = { workspace = true }
frame-system = { workspace = true }

sp-std = { workspace = true }
sp-core = { workspace = true }
sp-io = { workspace = true }
sp-runtime = { workspace = true }


environmental = { workspace = true }

[features]
default = ["std"]
std = [
"precompile-utils/std",
"pallet-democracy/std",

"evm/std",
"fp-evm/std",
"pallet-evm/std",

"frame-support/std",
"frame-system/std",

"sp-std/std",
"sp-core/std",
"sp-io/std",
]
125 changes: 125 additions & 0 deletions precompiles/governance/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
#![cfg_attr(not(feature = "std"), no_std)]
#![allow(missing_docs)]

use fp_evm::PrecompileHandle;
use frame_support::{
dispatch::{GetDispatchInfo, PostDispatchInfo},
traits::{Bounded, BoundedInline, Currency, IsType},
};
use pallet_evm::{AddressMapping, PrecompileFailure};
use precompile_utils::prelude::*;
use sp_core::{H160, H256, U256};
use sp_runtime::traits::{Dispatchable, StaticLookup};
use sp_std::{marker::PhantomData, vec::Vec};

type BalanceOf<Runtime> = <<Runtime as pallet_democracy::Config>::Currency as Currency<
<Runtime as frame_system::Config>::AccountId,
>>::Balance;

pub struct GovernancePrecompile<Runtime>(PhantomData<Runtime>);

#[precompile_utils::precompile]
impl<Runtime> GovernancePrecompile<Runtime>
where
Runtime: pallet_evm::Config + pallet_democracy::Config,
Runtime::AccountId: Into<H160>,
Runtime::Hash: IsType<H256>,
BalanceOf<Runtime>: TryFrom<U256> + Into<U256> + solidity::Codec,
Runtime::Lookup: StaticLookup<Source = Runtime::AccountId>,
Runtime::RuntimeCall: From<pallet_democracy::Call<Runtime>>,
<Runtime::RuntimeCall as Dispatchable>::RuntimeOrigin: From<Option<Runtime::AccountId>>,
Runtime::RuntimeCall: Dispatchable<PostInfo = PostDispatchInfo> + GetDispatchInfo,
{
#[precompile::public("propose(uint8[],uint256)")]
fn propose_inline(
h: &mut impl PrecompileHandle,
bounded_call: Vec<u8>,
value: U256,
) -> EvmResult<()> {
let bounded_call = BoundedInline::try_from(bounded_call)
.map_err(|_| Self::custom_err("Unable to parse bounded call"))?;
let value = Self::u256_to_amount(value)?;
Self::_propose(h, Bounded::Inline(bounded_call), value)
}

#[precompile::public("propose(bytes32,uint256)")]
fn propose_lookup(
h: &mut impl PrecompileHandle,
proposal_hash: H256,
value: U256,
) -> EvmResult<()> {
let hash = proposal_hash;
let len = hash.0.len() as u32;
let value = Self::u256_to_amount(value)?;
Self::_propose(h, Bounded::Lookup { hash: hash.into(), len }, value)
}

fn _propose(
h: &mut impl PrecompileHandle,
proposal: pallet_democracy::BoundedCallOf<Runtime>,
value: BalanceOf<Runtime>,
) -> EvmResult<()> {
let call = pallet_democracy::Call::<Runtime>::propose { proposal, value };
let origin = Some(Runtime::AddressMapping::into_account_id(h.context().caller));
RuntimeHelper::<Runtime>::try_dispatch(h, origin.into(), call)?;
Ok(())
}

#[precompile::public("vote(uint32,bool,uint8,uint256)")]
fn vote_standard(
h: &mut impl PrecompileHandle,
ref_index: u32,
aye: bool,
conviction: u8,
balance: U256,
) -> EvmResult<()> {
let conviction = pallet_democracy::Conviction::try_from(conviction)
.map_err(|_| Self::custom_err("Unable to parse conviction"))?;
let vote = pallet_democracy::Vote { aye, conviction };
let balance = Self::u256_to_amount(balance)?;
let vote = pallet_democracy::AccountVote::Standard { vote, balance };
Self::_vote(h, ref_index, vote)
}

#[precompile::public("vote(uint32,uint256,uint256)")]
fn vote_split(
h: &mut impl PrecompileHandle,
ref_index: u32,
aye: U256,
nay: U256,
) -> EvmResult<()> {
let aye = Self::u256_to_amount(aye)?;
let nay = Self::u256_to_amount(nay)?;
let vote = pallet_democracy::AccountVote::Split { aye, nay };
Self::_vote(h, ref_index, vote)
}

fn _vote(
h: &mut impl PrecompileHandle,
ref_index: pallet_democracy::ReferendumIndex,
vote: pallet_democracy::AccountVote<BalanceOf<Runtime>>,
) -> EvmResult<()> {
let call = pallet_democracy::Call::<Runtime>::vote { ref_index, vote };
let origin = Some(Runtime::AddressMapping::into_account_id(h.context().caller));
RuntimeHelper::<Runtime>::try_dispatch(h, origin.into(), call)?;
Ok(())
}

#[precompile::public("removeVote(uint32)")]
fn remove_vote(h: &mut impl PrecompileHandle, index: u32) -> EvmResult<()> {
let call = pallet_democracy::Call::<Runtime>::remove_vote { index };
let origin = Some(Runtime::AddressMapping::into_account_id(h.context().caller));
RuntimeHelper::<Runtime>::try_dispatch(h, origin.into(), call)?;
Ok(())
}

fn u256_to_amount(value: U256) -> MayRevert<BalanceOf<Runtime>> {
value
.try_into()
.map_err(|_| RevertReason::value_is_too_large("amount type").into())
}

fn custom_err(reason: &'static str) -> PrecompileFailure {
PrecompileFailure::Error { exit_status: evm::ExitError::Other(reason.into()) }
}
}
42 changes: 42 additions & 0 deletions precompiles/preimage/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
[package]
name = "pallet-evm-precompile-preimage"
authors = { workspace = true }
edition = "2021"
version = "0.0.1"

[dependencies]
precompile-utils = { workspace = true }
pallet-preimage = { workspace = true }

evm = { workspace = true }
fp-evm = { workspace = true }
pallet-evm = { workspace = true }

frame-support = { workspace = true }
frame-system = { workspace = true }

sp-std = { workspace = true }
sp-core = { workspace = true }
sp-io = { workspace = true }
sp-runtime = { workspace = true }


environmental = { workspace = true }

[features]
default = ["std"]
std = [
"precompile-utils/std",
"pallet-preimage/std",

"evm/std",
"fp-evm/std",
"pallet-evm/std",

"frame-support/std",
"frame-system/std",

"sp-std/std",
"sp-core/std",
"sp-io/std",
]
35 changes: 35 additions & 0 deletions precompiles/preimage/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#![cfg_attr(not(feature = "std"), no_std)]
#![allow(missing_docs)]

use fp_evm::PrecompileHandle;
use frame_support::{
dispatch::{GetDispatchInfo, PostDispatchInfo},
traits::IsType,
};
use pallet_evm::AddressMapping;
use precompile_utils::prelude::*;
use sp_core::{H160, H256};
use sp_runtime::traits::{Dispatchable, StaticLookup};
use sp_std::{marker::PhantomData, vec::Vec};

pub struct PreimagePrecompile<Runtime>(PhantomData<Runtime>);

#[precompile_utils::precompile]
impl<Runtime> PreimagePrecompile<Runtime>
where
Runtime: pallet_evm::Config + pallet_preimage::Config,
Runtime::AccountId: Into<H160>,
Runtime::Hash: IsType<H256>,
Runtime::Lookup: StaticLookup<Source = Runtime::AccountId>,
Runtime::RuntimeCall: From<pallet_preimage::Call<Runtime>>,
<Runtime::RuntimeCall as Dispatchable>::RuntimeOrigin: From<Option<Runtime::AccountId>>,
Runtime::RuntimeCall: Dispatchable<PostInfo = PostDispatchInfo> + GetDispatchInfo,
{
#[precompile::public("notePreimage(uint8[])")]
fn note_preimage(h: &mut impl PrecompileHandle, bytes: Vec<u8>) -> EvmResult<()> {
let call = pallet_preimage::Call::<Runtime>::note_preimage { bytes };
let origin = Some(Runtime::AddressMapping::into_account_id(h.context().caller));
RuntimeHelper::<Runtime>::try_dispatch(h, origin.into(), call)?;
Ok(())
}
}
42 changes: 42 additions & 0 deletions precompiles/treasury/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
[package]
name = "pallet-evm-precompile-treasury"
authors = { workspace = true }
edition = "2021"
version = "0.0.1"

[dependencies]
precompile-utils = { workspace = true }
pallet-treasury = { workspace = true }

evm = { workspace = true }
fp-evm = { workspace = true }
pallet-evm = { workspace = true }

frame-support = { workspace = true }
frame-system = { workspace = true }

sp-std = { workspace = true }
sp-core = { workspace = true }
sp-io = { workspace = true }
sp-runtime = { workspace = true }


environmental = { workspace = true }

[features]
default = ["std"]
std = [
"precompile-utils/std",
"pallet-treasury/std",

"evm/std",
"fp-evm/std",
"pallet-evm/std",

"frame-support/std",
"frame-system/std",

"sp-std/std",
"sp-core/std",
"sp-io/std",
]
54 changes: 54 additions & 0 deletions precompiles/treasury/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#![cfg_attr(not(feature = "std"), no_std)]
#![allow(missing_docs)]

use fp_evm::PrecompileHandle;
use frame_support::{
dispatch::{GetDispatchInfo, PostDispatchInfo},
traits::{Currency, IsType},
};
use pallet_evm::{AddressMapping, PrecompileFailure};
use precompile_utils::prelude::*;
use sp_core::{H160, H256, U256};
use sp_runtime::traits::{Dispatchable, StaticLookup};
use sp_std::marker::PhantomData;

type BalanceOf<Runtime> = <<Runtime as pallet_treasury::Config>::Currency as Currency<
<Runtime as frame_system::Config>::AccountId,
>>::Balance;

pub struct TreasuryPrecompile<Runtime>(PhantomData<Runtime>);

#[precompile_utils::precompile]
impl<Runtime> TreasuryPrecompile<Runtime>
where
Runtime: pallet_evm::Config + pallet_treasury::Config,
Runtime::AccountId: Into<H160>,
Runtime::Hash: IsType<H256>,
BalanceOf<Runtime>: TryFrom<U256> + Into<U256> + solidity::Codec,
Runtime::Lookup: StaticLookup<Source = Runtime::AccountId>,
Runtime::RuntimeCall: From<pallet_treasury::Call<Runtime>>,
<Runtime::RuntimeCall as Dispatchable>::RuntimeOrigin: From<Option<Runtime::AccountId>>,
Runtime::RuntimeCall: Dispatchable<PostInfo = PostDispatchInfo> + GetDispatchInfo,
{
#[precompile::public("proposeSpend(uint256,address)")]
fn propose_spend(
h: &mut impl PrecompileHandle,
value: U256,
beneficiary: Address,
) -> EvmResult<()> {
let value =
value.try_into().map_err(|_| RevertReason::value_is_too_large("amount type"))?;
let beneficiary =
Runtime::Lookup::lookup(Runtime::AddressMapping::into_account_id(beneficiary.0))
.map_err(|_| Self::custom_err("Unable to lookup address"))?;

let call = pallet_treasury::Call::<Runtime>::propose_spend { value, beneficiary };
let origin = Some(Runtime::AddressMapping::into_account_id(h.context().caller));
RuntimeHelper::<Runtime>::try_dispatch(h, origin.into(), call)?;
Ok(())
}

fn custom_err(reason: &'static str) -> PrecompileFailure {
PrecompileFailure::Error { exit_status: evm::ExitError::Other(reason.into()) }
}
}
7 changes: 6 additions & 1 deletion runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,9 @@ pallet-evm-precompile-staking = { workspace = true }
pallet-evm-precompile-faucet = { workspace = true }
pallet-evm-precompile-nomination-pools = { workspace = true }
pallet-evm-precompile-babe = { workspace = true }

pallet-evm-precompile-governance = { workspace = true }
pallet-evm-precompile-treasury = { workspace = true }
pallet-evm-precompile-preimage = { workspace = true }

[build-dependencies]
substrate-wasm-builder = { workspace = true, optional = true }
Expand Down Expand Up @@ -186,6 +188,9 @@ std = [
"pallet-evm-precompile-faucet/std",
"pallet-evm-precompile-nomination-pools/std",
"pallet-evm-precompile-babe/std",
"pallet-evm-precompile-governance/std",
"pallet-evm-precompile-treasury/std",
"pallet-evm-precompile-preimage/std",
]
runtime-benchmarks = [
"frame-benchmarking/runtime-benchmarks",
Expand Down
Loading