use frame_support::pallet_prelude::*;
use sp_runtime::traits::{AccountIdLookup, StaticLookup};
use sp_std::prelude::*;
use xcm_primitives::{
AvailableStakeCalls, HrmpAvailableCalls, HrmpEncodeCall, StakeEncodeCall,
UtilityAvailableCalls, UtilityEncodeCall,
};
pub use crate::pallet::*;
pub use crate::weights::WeightInfo;
impl<T: Config> UtilityEncodeCall for Pallet<T> {
fn encode_call(self, call: UtilityAvailableCalls) -> Vec<u8> {
match call {
UtilityAvailableCalls::AsDerivative(a, b) => {
let mut encoded_call: Vec<u8> = Vec::new();
encoded_call.push(RelayIndices::<T>::get().utility);
encoded_call.push(RelayIndices::<T>::get().as_derivative);
encoded_call.append(&mut a.encode());
encoded_call.append(&mut b.clone());
encoded_call
}
}
}
}
impl<T: Config> HrmpEncodeCall for Pallet<T> {
fn hrmp_encode_call(call: HrmpAvailableCalls) -> Result<Vec<u8>, xcm::latest::Error> {
match call {
HrmpAvailableCalls::InitOpenChannel(a, b, c) => {
let mut encoded_call: Vec<u8> = Vec::new();
encoded_call.push(RelayIndices::<T>::get().hrmp);
encoded_call.push(RelayIndices::<T>::get().init_open_channel);
encoded_call.append(&mut a.encode());
encoded_call.append(&mut b.encode());
encoded_call.append(&mut c.encode());
Ok(encoded_call)
}
HrmpAvailableCalls::AcceptOpenChannel(a) => {
let mut encoded_call: Vec<u8> = Vec::new();
encoded_call.push(RelayIndices::<T>::get().hrmp);
encoded_call.push(RelayIndices::<T>::get().accept_open_channel);
encoded_call.append(&mut a.encode());
Ok(encoded_call)
}
HrmpAvailableCalls::CloseChannel(a) => {
let mut encoded_call: Vec<u8> = Vec::new();
encoded_call.push(RelayIndices::<T>::get().hrmp);
encoded_call.push(RelayIndices::<T>::get().close_channel);
encoded_call.append(&mut a.encode());
Ok(encoded_call)
}
HrmpAvailableCalls::CancelOpenRequest(a, b) => {
let mut encoded_call: Vec<u8> = Vec::new();
encoded_call.push(RelayIndices::<T>::get().hrmp);
encoded_call.push(RelayIndices::<T>::get().cancel_open_request);
encoded_call.append(&mut a.encode());
encoded_call.append(&mut b.encode());
Ok(encoded_call)
}
}
}
}
fn encode_compact_arg<T: parity_scale_codec::HasCompact>(input: T) -> Vec<u8> {
#[derive(Encode)]
struct CompactWrapper<T: parity_scale_codec::HasCompact> {
#[codec(compact)]
input: T,
}
CompactWrapper { input }.encode()
}
impl<T: Config> StakeEncodeCall for Pallet<T> {
fn encode_call(call: AvailableStakeCalls) -> Vec<u8> {
match call {
AvailableStakeCalls::Bond(b, c) => {
let mut encoded_call: Vec<u8> = Vec::new();
encoded_call.push(RelayIndices::<T>::get().staking);
encoded_call.push(RelayIndices::<T>::get().bond);
encoded_call.append(&mut encode_compact_arg(b));
encoded_call.append(&mut c.encode());
encoded_call
}
AvailableStakeCalls::BondExtra(a) => {
let mut encoded_call: Vec<u8> = Vec::new();
encoded_call.push(RelayIndices::<T>::get().staking);
encoded_call.push(RelayIndices::<T>::get().bond_extra);
encoded_call.append(&mut encode_compact_arg(a));
encoded_call
}
AvailableStakeCalls::Unbond(a) => {
let mut encoded_call: Vec<u8> = Vec::new();
encoded_call.push(RelayIndices::<T>::get().staking);
encoded_call.push(RelayIndices::<T>::get().unbond);
encoded_call.append(&mut encode_compact_arg(a));
encoded_call
}
AvailableStakeCalls::WithdrawUnbonded(a) => {
let mut encoded_call: Vec<u8> = Vec::new();
encoded_call.push(RelayIndices::<T>::get().staking);
encoded_call.push(RelayIndices::<T>::get().withdraw_unbonded);
encoded_call.append(&mut a.encode());
encoded_call
}
AvailableStakeCalls::Validate(a) => {
let mut encoded_call: Vec<u8> = Vec::new();
encoded_call.push(RelayIndices::<T>::get().staking);
encoded_call.push(RelayIndices::<T>::get().validate);
encoded_call.append(&mut a.encode());
encoded_call
}
AvailableStakeCalls::Chill => {
let mut encoded_call: Vec<u8> = Vec::new();
encoded_call.push(RelayIndices::<T>::get().staking);
encoded_call.push(RelayIndices::<T>::get().chill);
encoded_call
}
AvailableStakeCalls::SetPayee(a) => {
let mut encoded_call: Vec<u8> = Vec::new();
encoded_call.push(RelayIndices::<T>::get().staking);
encoded_call.push(RelayIndices::<T>::get().set_payee);
encoded_call.append(&mut a.encode());
encoded_call
}
AvailableStakeCalls::SetController => {
let mut encoded_call: Vec<u8> = Vec::new();
encoded_call.push(RelayIndices::<T>::get().staking);
encoded_call.push(RelayIndices::<T>::get().set_controller);
encoded_call
}
AvailableStakeCalls::Rebond(a) => {
let mut encoded_call: Vec<u8> = Vec::new();
encoded_call.push(RelayIndices::<T>::get().staking);
encoded_call.push(RelayIndices::<T>::get().rebond);
encoded_call.append(&mut encode_compact_arg(a));
encoded_call
}
AvailableStakeCalls::Nominate(a) => {
let mut encoded_call: Vec<u8> = Vec::new();
encoded_call.push(RelayIndices::<T>::get().staking);
encoded_call.push(RelayIndices::<T>::get().nominate);
let nominated: Vec<
<AccountIdLookup<sp_runtime::AccountId32, ()> as StaticLookup>::Source,
> = a.iter().map(|add| (*add).clone().into()).collect();
encoded_call.append(&mut nominated.encode());
encoded_call
}
}
}
}