Skip to content
Open
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
155 changes: 155 additions & 0 deletions chain-extensions/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub mod types;

use crate::types::{FunctionId, Output};
use codec::{Decode, Encode, MaxEncodedLen};
use frame_support::storage::{TransactionOutcome, transactional};
use frame_support::{DebugNoBound, traits::Get};
use frame_system::RawOrigin;
use pallet_contracts::chain_extension::{
Expand Down Expand Up @@ -523,6 +524,160 @@ where

Ok(RetVal::Converging(Output::Success as u32))
}
FunctionId::RecycleAlphaV1 => {
let weight = Weight::from_parts(113_400_000, 0)
.saturating_add(T::DbWeight::get().reads(10))
.saturating_add(T::DbWeight::get().writes(4));

env.charge_weight(weight)?;

let (hotkey, amount, netuid): (T::AccountId, AlphaBalance, NetUid) =
env.read_as()?;

let caller = env.caller();

let call_result = pallet_subtensor::Pallet::<T>::do_recycle_alpha(
RawOrigin::Signed(caller).into(),
hotkey,
amount,
netuid,
);

match call_result {
Ok(real_amount) => {
env.write_output(&real_amount.encode())
.map_err(|_| DispatchError::Other("Failed to write output"))?;
Ok(RetVal::Converging(Output::Success as u32))
}
Err(e) => {
let error_code = Output::from(e) as u32;
Ok(RetVal::Converging(error_code))
}
}
}
FunctionId::BurnAlphaV1 => {
let weight = Weight::from_parts(112_200_000, 0)
.saturating_add(T::DbWeight::get().reads(10))
.saturating_add(T::DbWeight::get().writes(3));

env.charge_weight(weight)?;

let (hotkey, amount, netuid): (T::AccountId, AlphaBalance, NetUid) =
env.read_as()?;

let caller = env.caller();

let call_result = pallet_subtensor::Pallet::<T>::do_burn_alpha(
RawOrigin::Signed(caller).into(),
hotkey,
amount,
netuid,
);

match call_result {
Ok(real_amount) => {
env.write_output(&real_amount.encode())
.map_err(|_| DispatchError::Other("Failed to write output"))?;
Ok(RetVal::Converging(Output::Success as u32))
}
Err(e) => {
let error_code = Output::from(e) as u32;
Ok(RetVal::Converging(error_code))
}
}
}
FunctionId::AddStakeRecycleV1 => {
let weight = Weight::from_parts(454_200_000, 0)
.saturating_add(T::DbWeight::get().reads(33))
.saturating_add(T::DbWeight::get().writes(19));

env.charge_weight(weight)?;

let (hotkey, netuid, tao_amount): (T::AccountId, NetUid, TaoBalance) =
env.read_as()?;

let caller = env.caller();

let result = transactional::with_transaction(|| {
let alpha = match pallet_subtensor::Pallet::<T>::do_add_stake(
RawOrigin::Signed(caller.clone()).into(),
hotkey.clone(),
netuid,
tao_amount,
) {
Ok(a) => a,
Err(e) => return TransactionOutcome::Rollback(Err(e)),
};

match pallet_subtensor::Pallet::<T>::do_recycle_alpha(
RawOrigin::Signed(caller).into(),
hotkey,
alpha,
netuid,
) {
Ok(real_alpha) => TransactionOutcome::Commit(Ok(real_alpha)),
Err(e) => TransactionOutcome::Rollback(Err(e)),
}
});

match result {
Ok(alpha) => {
env.write_output(&alpha.encode())
.map_err(|_| DispatchError::Other("Failed to write output"))?;
Ok(RetVal::Converging(Output::Success as u32))
}
Err(e) => {
let error_code = Output::from(e) as u32;
Ok(RetVal::Converging(error_code))
}
}
}
FunctionId::AddStakeBurnV1 => {
let weight = Weight::from_parts(453_000_000, 0)
.saturating_add(T::DbWeight::get().reads(33))
.saturating_add(T::DbWeight::get().writes(18));

env.charge_weight(weight)?;

let (hotkey, netuid, tao_amount): (T::AccountId, NetUid, TaoBalance) =
env.read_as()?;

let caller = env.caller();

let result = transactional::with_transaction(|| {
let alpha = match pallet_subtensor::Pallet::<T>::do_add_stake(
RawOrigin::Signed(caller.clone()).into(),
hotkey.clone(),
netuid,
tao_amount,
) {
Ok(a) => a,
Err(e) => return TransactionOutcome::Rollback(Err(e)),
};

match pallet_subtensor::Pallet::<T>::do_burn_alpha(
RawOrigin::Signed(caller).into(),
hotkey,
alpha,
netuid,
) {
Ok(real_alpha) => TransactionOutcome::Commit(Ok(real_alpha)),
Err(e) => TransactionOutcome::Rollback(Err(e)),
}
});

match result {
Ok(alpha) => {
env.write_output(&alpha.encode())
.map_err(|_| DispatchError::Other("Failed to write output"))?;
Ok(RetVal::Converging(Output::Success as u32))
}
Err(e) => {
let error_code = Output::from(e) as u32;
Ok(RetVal::Converging(error_code))
}
}
}
}
}
}
Expand Down
Loading