Skip to content
Merged
Show file tree
Hide file tree
Changes from 24 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
4e2e06e
upgrade to v0.9.37
nbaztec Feb 3, 2023
1359267
fix tests
nbaztec Feb 3, 2023
a42bab5
fix tests, update frontier
nbaztec Feb 3, 2023
dbd06b0
update frontier
nbaztec Feb 3, 2023
8e02406
update frontier
nbaztec Feb 3, 2023
acdfe12
fix batch precompile tests
nbaztec Feb 6, 2023
2091dd4
fix lint
nbaztec Feb 6, 2023
1a25606
fix tests
nbaztec Feb 6, 2023
cf5d088
add root-testing pallet
nbaztec Feb 7, 2023
e7d7887
fix length fees test
nbaztec Feb 7, 2023
99f29f3
fix length fees test
nbaztec Feb 7, 2023
d472156
fix ts tests
nbaztec Feb 9, 2023
3a9e01a
fmt
nbaztec Feb 9, 2023
352320a
fix typo
nbaztec Feb 9, 2023
bcb7622
fix statemine tests
nbaztec Feb 9, 2023
8b18f5b
fix ts test
nbaztec Feb 9, 2023
c36d9be
fix tests
nbaztec Feb 9, 2023
8f84984
fix test
nbaztec Feb 9, 2023
1820b9d
change test name
nbaztec Feb 10, 2023
e7fe53e
remove dev_mode, add call_index
nbaztec Feb 10, 2023
e8bb693
bump
nbaztec Feb 10, 2023
363bf1d
init
amarsinghcodes Feb 11, 2023
9edecb6
into master
amarsinghcodes Feb 18, 2023
99db641
closed referendum info getter
amarsinghcodes Feb 20, 2023
50b1c46
make closed referendum info into a struct
amarsinghcodes Feb 20, 2023
da9df04
into master
amarsinghcodes Feb 20, 2023
79c2ffb
init ongoing referendum info
amarsinghcodes Feb 20, 2023
90f7cce
everything but schedule address works
amarsinghcodes Feb 20, 2023
b812fc2
fix schedule address which does implement encode even if it is not tr…
amarsinghcodes Feb 20, 2023
e23d35c
fix comment
amarsinghcodes Feb 20, 2023
907a954
into master
amarsinghcodes Feb 21, 2023
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
42 changes: 42 additions & 0 deletions precompiles/referenda/Referenda.sol
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@ Referenda constant REFERENDA_CONTRACT = Referenda(REFERENDA_ADDRESS);
/// @title The interface through which solidity contracts will interact with the Referenda pallet
/// @custom:address 0x0000000000000000000000000000000000000811
interface Referenda {
/// @dev Defines the referendum status.
/// The values start at `0` (most permissive) and are represented as `uint8`
enum ReferendumStatus {
Ongoing,
Approved,
Rejected,
Cancelled,
TimedOut,
Killed
}
struct TrackInfo {
string name;
uint256 maxDeciding;
Expand Down Expand Up @@ -47,6 +57,38 @@ interface Referenda {
/// @custom:selector 34038146
function trackInfo(uint16 trackId) external view returns (TrackInfo memory);

/// Return the ReferendumStatus for the input referendumIndex
/// @param referendumIndex The index of the referendum
/// @custom:selector 8d407c0b
function referendumStatus(uint32 referendumIndex)
external
view
returns (ReferendumStatus);

/// Return the referendumInfo for a closed referendum
/// @param referendumIndex The index of the referendum
/// @custom:selector 078e5678
// todo: is this return struct too long, how long is too long, how to test and enforce this
function closedReferendumInfo(uint32 referendumIndex)
external
view
returns (
ReferendumStatus,
uint256,
address,
uint256,
address,
uint256
);

/// Return the block the referendum was killed
/// @param referendumIndex The index of the referendum
/// @custom:selector 6414ddc5
function killedReferendumBlock(uint32 referendumIndex)
external
view
returns (uint256);

/// @dev Submit a referenda
/// @custom:selector 95f9ed68
/// @param trackId The trackId corresponding to the origin from which the proposal is to be
Expand Down
111 changes: 109 additions & 2 deletions precompiles/referenda/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,13 @@ use frame_support::traits::{
schedule::DispatchTime, Bounded, ConstU32, Currency, Get, OriginTrait,
};
use pallet_evm::AddressMapping;
use pallet_referenda::{Call as ReferendaCall, DecidingCount, ReferendumCount, TracksInfo};
use pallet_referenda::{
Call as ReferendaCall, DecidingCount, Deposit, ReferendumCount, ReferendumInfo,
ReferendumInfoFor, TracksInfo,
};
use parity_scale_codec::Encode;
use precompile_utils::{data::String, prelude::*};
use sp_core::{Hasher, H256, U256};
use sp_core::{Hasher, H160, H256, U256};
use sp_std::{boxed::Box, marker::PhantomData, str::FromStr, vec::Vec};

#[cfg(test)]
Expand Down Expand Up @@ -86,6 +89,7 @@ impl<Runtime, GovOrigin> ReferendaPrecompile<Runtime, GovOrigin>
where
Runtime: pallet_referenda::Config + pallet_evm::Config + frame_system::Config,
OriginOf<Runtime>: From<GovOrigin>,
Runtime::AccountId: Into<H160>,
<Runtime as frame_system::Config>::RuntimeCall:
Dispatchable<PostInfo = PostDispatchInfo> + GetDispatchInfo,
<<Runtime as frame_system::Config>::RuntimeCall as Dispatchable>::RuntimeOrigin:
Expand Down Expand Up @@ -241,6 +245,109 @@ where
Ok(referendum_index)
}

#[precompile::public("referendumStatus(uint32)")]
#[precompile::view]
fn referendum_status(
handle: &mut impl PrecompileHandle,
referendum_index: u32,
) -> EvmResult<u8> {
// Fetch data from pallet
handle.record_cost(RuntimeHelper::<Runtime>::db_read_gas_cost())?;

let status = match ReferendumInfoFor::<Runtime>::get(referendum_index).ok_or(
RevertReason::custom("Referendum does not exist for index")
.in_field("referendum_index"),
)? {
ReferendumInfo::Ongoing(..) => 0,
ReferendumInfo::Approved(..) => 1,
ReferendumInfo::Rejected(..) => 2,
ReferendumInfo::Cancelled(..) => 3,
ReferendumInfo::TimedOut(..) => 4,
ReferendumInfo::Killed(..) => 5,
};

Ok(status)
}

// TODO: ongoing_referendum_info

#[precompile::public("closedReferendumInfo(uint32)")]
#[precompile::view]
fn closed_referendum_info(
handle: &mut impl PrecompileHandle,
referendum_index: u32,
) -> EvmResult<(u8, U256, Address, U256, Address, U256)> {
// Fetch data from pallet
handle.record_cost(RuntimeHelper::<Runtime>::db_read_gas_cost())?;

let get_closed_ref_info =
|status,
moment: Runtime::BlockNumber,
submission_deposit: Option<Deposit<Runtime::AccountId, BalanceOf<Runtime>>>,
decision_deposit: Option<Deposit<Runtime::AccountId, BalanceOf<Runtime>>>|
-> (u8, U256, Address, U256, Address, U256) {
let (submission_depositor, submission_deposit_amount): (Address, U256) =
if let Some(Deposit { who, amount }) = submission_deposit {
(Address(who.into()), amount.into())
} else {
(Address(H160::zero()), U256::zero())
};
let (decision_depositor, decision_deposit_amount) =
if let Some(Deposit { who, amount }) = decision_deposit {
(Address(who.into()), amount.into())
} else {
(Address(H160::zero()), U256::zero())
};
(
status,
moment.into(),
submission_depositor,
submission_deposit_amount,
decision_depositor,
decision_deposit_amount,
)
};

match ReferendumInfoFor::<Runtime>::get(referendum_index).ok_or(
RevertReason::custom("Referendum does not exist for index")
.in_field("referendum_index"),
)? {
ReferendumInfo::Approved(moment, submission_deposit, decision_deposit) => Ok(
get_closed_ref_info(1, moment, submission_deposit, decision_deposit),
),
ReferendumInfo::Rejected(moment, submission_deposit, decision_deposit) => Ok(
get_closed_ref_info(2, moment, submission_deposit, decision_deposit),
),
ReferendumInfo::Cancelled(moment, submission_deposit, decision_deposit) => Ok(
get_closed_ref_info(3, moment, submission_deposit, decision_deposit),
),
ReferendumInfo::TimedOut(moment, submission_deposit, decision_deposit) => Ok(
get_closed_ref_info(4, moment, submission_deposit, decision_deposit),
),
_ => Err(RevertReason::custom("Referendum not closed").into()),
}
}

#[precompile::public("killedReferendumBlock(uint32)")]
#[precompile::view]
fn killed_referendum_block(
handle: &mut impl PrecompileHandle,
referendum_index: u32,
) -> EvmResult<U256> {
// Fetch data from pallet
handle.record_cost(RuntimeHelper::<Runtime>::db_read_gas_cost())?;

let block = match ReferendumInfoFor::<Runtime>::get(referendum_index).ok_or(
RevertReason::custom("Referendum does not exist for index")
.in_field("referendum_index"),
)? {
ReferendumInfo::Killed(b) => b,
_ => return Err(RevertReason::custom("Referendum not killed").into()),
};

Ok(block.into())
}

/// Propose a referendum on a privileged action.
///
/// Parameters:
Expand Down