add swap-to-staked-sri feature

This commit is contained in:
akildemir
2024-05-16 15:57:03 +03:00
parent 904c6ddbe3
commit 929e66c607
6 changed files with 50 additions and 6 deletions

1
Cargo.lock generated
View File

@@ -7908,6 +7908,7 @@ dependencies = [
"scale-info",
"serai-coins-pallet",
"serai-dex-pallet",
"serai-emissions-pallet",
"serai-genesis-liquidity-pallet",
"serai-in-instructions-primitives",
"serai-primitives",

View File

@@ -7,7 +7,7 @@ pub mod pallet {
use frame_system::pallet_prelude::*;
use frame_support::{pallet_prelude::*, sp_runtime::SaturatedConversion};
use sp_std::{vec, vec::Vec, collections::btree_map::BTreeMap};
use sp_std::{vec, vec::Vec, ops::Mul, collections::btree_map::BTreeMap};
use sp_runtime;
use coins_pallet::{Config as CoinsConfig, Pallet as Coins, AllowMint};
@@ -44,7 +44,8 @@ pub mod pallet {
#[pallet::error]
pub enum Error<T> {
MintFailed,
NetworkHasEconomicSecurity,
NoValueForCoin,
}
#[pallet::event]
@@ -336,13 +337,36 @@ pub mod pallet {
)
.unwrap();
Coins::<T>::mint(p, Balance { coin: Coin::Serai, amount: Amount(p_reward) })
.map_err(|_| Error::<T>::MintFailed)?;
Coins::<T>::mint(p, Balance { coin: Coin::Serai, amount: Amount(p_reward) })?;
ValidatorSets::<T>::deposit_stake(n, p, Amount(p_reward))?;
}
Ok(())
}
pub fn swap_to_staked_sri(
to: PublicKey,
network: NetworkId,
balance: Balance,
) -> DispatchResult {
// check the network didn't reach the economic security yet
if Self::economic_security_reached(network) {
Err(Error::<T>::NetworkHasEconomicSecurity)?;
}
// calculate how much SRI the balance makes
let value =
Dex::<T>::security_oracle_value(balance.coin).ok_or(Error::<T>::NoValueForCoin)?;
// TODO: may panic? It might be best for this math ops to return the result as is instead of
// doing an unwrap so that it can be properly dealt with.
let sri_amount = balance.amount.mul(value);
// Mint & stake the SRI for the network.
Coins::<T>::mint(to, Balance { coin: Coin::Serai, amount: sri_amount })?;
// TODO: deposit_stake lets staking less than per key share. Should we allow that here?
ValidatorSets::<T>::deposit_stake(network, to, sri_amount)?;
Ok(())
}
}
}

View File

@@ -38,6 +38,7 @@ coins-pallet = { package = "serai-coins-pallet", path = "../../coins/pallet", de
dex-pallet = { package = "serai-dex-pallet", path = "../../dex/pallet", default-features = false }
validator-sets-pallet = { package = "serai-validator-sets-pallet", path = "../../validator-sets/pallet", default-features = false }
genesis-liquidity-pallet = { package = "serai-genesis-liquidity-pallet", path = "../../genesis-liquidity/pallet", default-features = false }
emissions-pallet = { package = "serai-emissions-pallet", path = "../../emissions/pallet", default-features = false }
[features]
std = [
@@ -60,5 +61,6 @@ std = [
"dex-pallet/std",
"validator-sets-pallet/std",
"genesis-liquidity-pallet/std",
"emissions-pallet/std",
]
default = ["std"]

View File

@@ -34,12 +34,18 @@ pub mod pallet {
};
use genesis_liquidity_pallet::{Pallet as GenesisLiq, Config as GenesisLiqConfig};
use emissions_pallet::{Pallet as Emissions, Config as EmissionsConfig};
use super::*;
#[pallet::config]
pub trait Config:
frame_system::Config + CoinsConfig + DexConfig + ValidatorSetsConfig + GenesisLiqConfig
frame_system::Config
+ CoinsConfig
+ DexConfig
+ ValidatorSetsConfig
+ GenesisLiqConfig
+ EmissionsConfig
{
type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
}
@@ -207,6 +213,9 @@ pub mod pallet {
InInstruction::GenesisLiquidity(address) => {
GenesisLiq::<T>::add_coin_liquidity(address.into(), instruction.balance)?;
}
InInstruction::SwapToStakedSRI(address, network) => {
Emissions::<T>::swap_to_staked_sri(address.into(), network, instruction.balance)?;
}
}
Ok(())
}

View File

@@ -79,6 +79,7 @@ pub enum InInstruction {
Transfer(SeraiAddress),
Dex(DexCall),
GenesisLiquidity(SeraiAddress),
SwapToStakedSRI(SeraiAddress, NetworkId),
}
#[derive(Clone, PartialEq, Eq, Encode, Decode, MaxEncodedLen, TypeInfo, RuntimeDebug)]

View File

@@ -797,12 +797,19 @@ pub mod pallet {
total_required
}
// TODO: make the increase_allocation public instead?
pub fn deposit_stake(
network: NetworkId,
account: T::AccountId,
amount: Amount,
) -> DispatchResult {
// TODO: make the increase_allocation public instead?
// TODO: Should this call be part of the `increase_allocation` since we have to have it
// before each call to it?
Coins::<T>::transfer_internal(
account,
Self::account(),
Balance { coin: Coin::Serai, amount },
)?;
Self::increase_allocation(network, account, amount, true)
}