mirror of
https://github.com/serai-dex/serai.git
synced 2025-12-11 13:39:25 +00:00
add swap-to-staked-sri feature
This commit is contained in:
1
Cargo.lock
generated
1
Cargo.lock
generated
@@ -7908,6 +7908,7 @@ dependencies = [
|
|||||||
"scale-info",
|
"scale-info",
|
||||||
"serai-coins-pallet",
|
"serai-coins-pallet",
|
||||||
"serai-dex-pallet",
|
"serai-dex-pallet",
|
||||||
|
"serai-emissions-pallet",
|
||||||
"serai-genesis-liquidity-pallet",
|
"serai-genesis-liquidity-pallet",
|
||||||
"serai-in-instructions-primitives",
|
"serai-in-instructions-primitives",
|
||||||
"serai-primitives",
|
"serai-primitives",
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ pub mod pallet {
|
|||||||
use frame_system::pallet_prelude::*;
|
use frame_system::pallet_prelude::*;
|
||||||
use frame_support::{pallet_prelude::*, sp_runtime::SaturatedConversion};
|
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 sp_runtime;
|
||||||
|
|
||||||
use coins_pallet::{Config as CoinsConfig, Pallet as Coins, AllowMint};
|
use coins_pallet::{Config as CoinsConfig, Pallet as Coins, AllowMint};
|
||||||
@@ -44,7 +44,8 @@ pub mod pallet {
|
|||||||
|
|
||||||
#[pallet::error]
|
#[pallet::error]
|
||||||
pub enum Error<T> {
|
pub enum Error<T> {
|
||||||
MintFailed,
|
NetworkHasEconomicSecurity,
|
||||||
|
NoValueForCoin,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[pallet::event]
|
#[pallet::event]
|
||||||
@@ -336,13 +337,36 @@ pub mod pallet {
|
|||||||
)
|
)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
Coins::<T>::mint(p, Balance { coin: Coin::Serai, amount: Amount(p_reward) })
|
Coins::<T>::mint(p, Balance { coin: Coin::Serai, amount: Amount(p_reward) })?;
|
||||||
.map_err(|_| Error::<T>::MintFailed)?;
|
|
||||||
ValidatorSets::<T>::deposit_stake(n, p, Amount(p_reward))?;
|
ValidatorSets::<T>::deposit_stake(n, p, Amount(p_reward))?;
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
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(())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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 }
|
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 }
|
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 }
|
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]
|
[features]
|
||||||
std = [
|
std = [
|
||||||
@@ -60,5 +61,6 @@ std = [
|
|||||||
"dex-pallet/std",
|
"dex-pallet/std",
|
||||||
"validator-sets-pallet/std",
|
"validator-sets-pallet/std",
|
||||||
"genesis-liquidity-pallet/std",
|
"genesis-liquidity-pallet/std",
|
||||||
|
"emissions-pallet/std",
|
||||||
]
|
]
|
||||||
default = ["std"]
|
default = ["std"]
|
||||||
|
|||||||
@@ -34,12 +34,18 @@ pub mod pallet {
|
|||||||
};
|
};
|
||||||
|
|
||||||
use genesis_liquidity_pallet::{Pallet as GenesisLiq, Config as GenesisLiqConfig};
|
use genesis_liquidity_pallet::{Pallet as GenesisLiq, Config as GenesisLiqConfig};
|
||||||
|
use emissions_pallet::{Pallet as Emissions, Config as EmissionsConfig};
|
||||||
|
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
#[pallet::config]
|
#[pallet::config]
|
||||||
pub trait 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>;
|
type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
|
||||||
}
|
}
|
||||||
@@ -207,6 +213,9 @@ pub mod pallet {
|
|||||||
InInstruction::GenesisLiquidity(address) => {
|
InInstruction::GenesisLiquidity(address) => {
|
||||||
GenesisLiq::<T>::add_coin_liquidity(address.into(), instruction.balance)?;
|
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(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -79,6 +79,7 @@ pub enum InInstruction {
|
|||||||
Transfer(SeraiAddress),
|
Transfer(SeraiAddress),
|
||||||
Dex(DexCall),
|
Dex(DexCall),
|
||||||
GenesisLiquidity(SeraiAddress),
|
GenesisLiquidity(SeraiAddress),
|
||||||
|
SwapToStakedSRI(SeraiAddress, NetworkId),
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, PartialEq, Eq, Encode, Decode, MaxEncodedLen, TypeInfo, RuntimeDebug)]
|
#[derive(Clone, PartialEq, Eq, Encode, Decode, MaxEncodedLen, TypeInfo, RuntimeDebug)]
|
||||||
|
|||||||
@@ -797,12 +797,19 @@ pub mod pallet {
|
|||||||
total_required
|
total_required
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: make the increase_allocation public instead?
|
||||||
pub fn deposit_stake(
|
pub fn deposit_stake(
|
||||||
network: NetworkId,
|
network: NetworkId,
|
||||||
account: T::AccountId,
|
account: T::AccountId,
|
||||||
amount: Amount,
|
amount: Amount,
|
||||||
) -> DispatchResult {
|
) -> 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)
|
Self::increase_allocation(network, account, amount, true)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user