mirror of
https://github.com/serai-dex/serai.git
synced 2025-12-13 06:29:25 +00:00
rebase to develop latest
This commit is contained in:
@@ -18,7 +18,6 @@ ignored = ["scale", "scale-info"]
|
||||
[lints]
|
||||
workspace = true
|
||||
|
||||
|
||||
[dependencies]
|
||||
scale = { package = "parity-scale-codec", version = "3", default-features = false, features = ["derive"] }
|
||||
scale-info = { version = "2", default-features = false, features = ["derive"] }
|
||||
@@ -32,6 +31,7 @@ sp-application-crypto = { git = "https://github.com/serai-dex/substrate", defaul
|
||||
|
||||
dex-pallet = { package = "serai-dex-pallet", path = "../../dex/pallet", default-features = false }
|
||||
coins-pallet = { package = "serai-coins-pallet", path = "../../coins/pallet", default-features = false }
|
||||
validator-sets-pallet = { package = "serai-validator-sets-pallet", path = "../../validator-sets/pallet", default-features = false }
|
||||
|
||||
serai-primitives = { path = "../../primitives", default-features = false }
|
||||
genesis-liquidity-primitives = { package = "serai-genesis-liquidity-primitives", path = "../primitives", default-features = false }
|
||||
@@ -51,11 +51,13 @@ std = [
|
||||
|
||||
"coins-pallet/std",
|
||||
"dex-pallet/std",
|
||||
"validator-sets-pallet/std",
|
||||
|
||||
"serai-primitives/std",
|
||||
"genesis-liquidity-primitives/std",
|
||||
"validator-sets-primitives/std",
|
||||
]
|
||||
fast-epoch = ["genesis-liquidity-primitives/fast-epoch"]
|
||||
try-runtime = [] # TODO
|
||||
fast-epoch = []
|
||||
|
||||
default = ["std"]
|
||||
|
||||
@@ -5,46 +5,36 @@
|
||||
pub mod pallet {
|
||||
use super::*;
|
||||
use frame_system::{pallet_prelude::*, RawOrigin};
|
||||
use frame_support::{
|
||||
pallet_prelude::*,
|
||||
sp_runtime::{self, 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};
|
||||
use sp_core::sr25519::Signature;
|
||||
use sp_application_crypto::RuntimePublic;
|
||||
|
||||
use dex_pallet::{Pallet as Dex, Config as DexConfig};
|
||||
use coins_pallet::{Config as CoinsConfig, Pallet as Coins, AllowMint};
|
||||
use validator_sets_pallet::{Config as VsConfig, Pallet as ValidatorSets};
|
||||
|
||||
use serai_primitives::*;
|
||||
use validator_sets_primitives::{ValidatorSet, Session, musig_key};
|
||||
use validator_sets_primitives::{ValidatorSet, musig_key};
|
||||
pub use genesis_liquidity_primitives as primitives;
|
||||
use primitives::*;
|
||||
|
||||
// TODO: Have a more robust way of accessing LiquidityTokens pallet.
|
||||
/// LiquidityTokens Pallet as an instance of coins pallet.
|
||||
pub type LiquidityTokens<T> = coins_pallet::Pallet<T, coins_pallet::Instance1>;
|
||||
|
||||
#[pallet::config]
|
||||
pub trait Config:
|
||||
frame_system::Config + DexConfig + CoinsConfig + coins_pallet::Config<coins_pallet::Instance1>
|
||||
frame_system::Config
|
||||
+ VsConfig
|
||||
+ DexConfig
|
||||
+ CoinsConfig
|
||||
+ coins_pallet::Config<coins_pallet::Instance1>
|
||||
{
|
||||
type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
|
||||
}
|
||||
|
||||
#[pallet::genesis_config]
|
||||
#[derive(Clone, PartialEq, Eq, Debug, Encode, Decode)]
|
||||
pub struct GenesisConfig<T: Config> {
|
||||
/// List of participants to place in the initial validator sets.
|
||||
pub participants: Vec<T::AccountId>,
|
||||
}
|
||||
|
||||
impl<T: Config> Default for GenesisConfig<T> {
|
||||
fn default() -> Self {
|
||||
GenesisConfig { participants: Default::default() }
|
||||
}
|
||||
}
|
||||
|
||||
#[pallet::error]
|
||||
pub enum Error<T> {
|
||||
GenesisPeriodEnded,
|
||||
@@ -58,44 +48,46 @@ pub mod pallet {
|
||||
pub enum Event<T: Config> {
|
||||
GenesisLiquidityAdded { by: SeraiAddress, balance: Balance },
|
||||
GenesisLiquidityRemoved { by: SeraiAddress, balance: Balance },
|
||||
GenesisLiquidityAddedToPool { coin1: Balance, coin2: Balance },
|
||||
GenesisLiquidityAddedToPool { coin1: Balance, sri: Amount },
|
||||
EconomicSecurityReached { network: NetworkId },
|
||||
}
|
||||
|
||||
#[pallet::pallet]
|
||||
pub struct Pallet<T>(PhantomData<T>);
|
||||
|
||||
/// Keeps shares and the amount of coins per account.
|
||||
#[pallet::storage]
|
||||
pub(crate) type Liquidity<T: Config> =
|
||||
StorageDoubleMap<_, Identity, Coin, Blake2_128Concat, PublicKey, SubstrateAmount, OptionQuery>;
|
||||
StorageDoubleMap<_, Identity, Coin, Blake2_128Concat, PublicKey, LiquidityAmount, OptionQuery>;
|
||||
|
||||
/// Keeps the total shares and the total amount of coins per coin.
|
||||
#[pallet::storage]
|
||||
pub(crate) type LiquidityTokensPerAddress<T: Config> =
|
||||
StorageDoubleMap<_, Identity, Coin, Blake2_128Concat, PublicKey, SubstrateAmount, OptionQuery>;
|
||||
pub(crate) type Supply<T: Config> = StorageMap<_, Identity, Coin, LiquidityAmount, OptionQuery>;
|
||||
|
||||
#[pallet::storage]
|
||||
pub(crate) type EconomicSecurityReached<T: Config> =
|
||||
StorageMap<_, Identity, NetworkId, BlockNumberFor<T>, ValueQuery>;
|
||||
StorageMap<_, Identity, NetworkId, BlockNumberFor<T>, OptionQuery>;
|
||||
|
||||
#[pallet::storage]
|
||||
pub(crate) type Participants<T: Config> =
|
||||
StorageMap<_, Identity, NetworkId, BoundedVec<PublicKey, ConstU32<150>>, ValueQuery>;
|
||||
pub(crate) type Oracle<T: Config> = StorageMap<_, Identity, Coin, u64, OptionQuery>;
|
||||
|
||||
#[pallet::storage]
|
||||
pub(crate) type Oracle<T: Config> = StorageMap<_, Identity, Coin, u64, ValueQuery>;
|
||||
|
||||
#[pallet::genesis_build]
|
||||
impl<T: Config> BuildGenesisConfig for GenesisConfig<T> {
|
||||
fn build(&self) {
|
||||
Participants::<T>::set(NetworkId::Serai, self.participants.clone().try_into().unwrap());
|
||||
}
|
||||
}
|
||||
pub(crate) type GenesisComplete<T: Config> = StorageValue<_, (), OptionQuery>;
|
||||
|
||||
#[pallet::hooks]
|
||||
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
|
||||
fn on_finalize(n: BlockNumberFor<T>) {
|
||||
fn on_initialize(n: BlockNumberFor<T>) -> Weight {
|
||||
#[cfg(feature = "fast-epoch")]
|
||||
let final_block = 10u64;
|
||||
|
||||
#[cfg(not(feature = "fast-epoch"))]
|
||||
let final_block = MONTHS;
|
||||
|
||||
// Distribute the genesis sri to pools after a month
|
||||
if n == GENESIS_PERIOD_BLOCKS.into() {
|
||||
if (n.saturated_into::<u64>() >= final_block) &&
|
||||
Self::oraclization_is_done() &&
|
||||
GenesisComplete::<T>::get().is_none()
|
||||
{
|
||||
// mint the SRI
|
||||
Coins::<T>::mint(
|
||||
GENESIS_LIQUIDITY_ACCOUNT.into(),
|
||||
@@ -103,8 +95,7 @@ pub mod pallet {
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
// get coin values & total
|
||||
let mut account_values = BTreeMap::new();
|
||||
// get pool & total values
|
||||
let mut pool_values = vec![];
|
||||
let mut total_value: u128 = 0;
|
||||
for coin in COINS {
|
||||
@@ -113,22 +104,18 @@ pub mod pallet {
|
||||
}
|
||||
|
||||
// initial coin value in terms of btc
|
||||
let value = Oracle::<T>::get(coin);
|
||||
let Some(value) = Oracle::<T>::get(coin) else {
|
||||
continue;
|
||||
};
|
||||
|
||||
// get the pool & individual address values
|
||||
account_values.insert(coin, vec![]);
|
||||
let mut pool_amount: u128 = 0;
|
||||
for (account, amount) in Liquidity::<T>::iter_prefix(coin) {
|
||||
pool_amount = pool_amount.saturating_add(amount.into());
|
||||
let value_this_addr =
|
||||
u128::from(amount).saturating_mul(value.into()) / 10u128.pow(coin.decimals());
|
||||
account_values.get_mut(&coin).unwrap().push((account, value_this_addr))
|
||||
}
|
||||
// sort, so that everyone has a consistent accounts vector per coin
|
||||
account_values.get_mut(&coin).unwrap().sort();
|
||||
|
||||
let pool_value = pool_amount.saturating_mul(value.into()) / 10u128.pow(coin.decimals());
|
||||
total_value = total_value.saturating_add(pool_value);
|
||||
let pool_amount =
|
||||
u128::from(Supply::<T>::get(coin).unwrap_or(LiquidityAmount::zero()).coins);
|
||||
let pool_value = pool_amount
|
||||
.checked_mul(value.into())
|
||||
.unwrap()
|
||||
.checked_div(10u128.pow(coin.decimals()))
|
||||
.unwrap();
|
||||
total_value = total_value.checked_add(pool_value).unwrap();
|
||||
pool_values.push((coin, pool_amount, pool_value));
|
||||
}
|
||||
|
||||
@@ -138,20 +125,22 @@ pub mod pallet {
|
||||
for (i, (coin, pool_amount, pool_value)) in pool_values.into_iter().enumerate() {
|
||||
// whatever sri left for the last coin should be ~= it's ratio
|
||||
let sri_amount = if i == (pool_values_len - 1) {
|
||||
GENESIS_SRI - total_sri_distributed
|
||||
GENESIS_SRI.checked_sub(total_sri_distributed).unwrap()
|
||||
} else {
|
||||
u64::try_from(u128::from(GENESIS_SRI).saturating_mul(pool_value) / total_value).unwrap()
|
||||
u64::try_from(
|
||||
u128::from(GENESIS_SRI)
|
||||
.checked_mul(pool_value)
|
||||
.unwrap()
|
||||
.checked_div(total_value)
|
||||
.unwrap(),
|
||||
)
|
||||
.unwrap()
|
||||
};
|
||||
total_sri_distributed += sri_amount;
|
||||
|
||||
// we can't add 0 liquidity
|
||||
if !(pool_amount > 0 && sri_amount > 0) {
|
||||
continue;
|
||||
}
|
||||
total_sri_distributed = total_sri_distributed.checked_add(sri_amount).unwrap();
|
||||
|
||||
// actually add the liquidity to dex
|
||||
let origin = RawOrigin::Signed(GENESIS_LIQUIDITY_ACCOUNT.into());
|
||||
Dex::<T>::add_liquidity(
|
||||
let Ok(()) = Dex::<T>::add_liquidity(
|
||||
origin.into(),
|
||||
coin,
|
||||
u64::try_from(pool_amount).unwrap(),
|
||||
@@ -159,38 +148,15 @@ pub mod pallet {
|
||||
u64::try_from(pool_amount).unwrap(),
|
||||
sri_amount,
|
||||
GENESIS_LIQUIDITY_ACCOUNT.into(),
|
||||
)
|
||||
.unwrap();
|
||||
) else {
|
||||
continue;
|
||||
};
|
||||
|
||||
// let everyone know about the event
|
||||
Self::deposit_event(Event::GenesisLiquidityAddedToPool {
|
||||
coin1: Balance { coin, amount: Amount(u64::try_from(pool_amount).unwrap()) },
|
||||
coin2: Balance { coin: Coin::Serai, amount: Amount(sri_amount) },
|
||||
sri: Amount(sri_amount),
|
||||
});
|
||||
|
||||
// set liquidity tokens per account
|
||||
let tokens =
|
||||
u128::from(LiquidityTokens::<T>::balance(GENESIS_LIQUIDITY_ACCOUNT.into(), coin).0);
|
||||
let mut total_tokens_this_coin: u128 = 0;
|
||||
|
||||
let accounts = account_values.get(&coin).unwrap();
|
||||
for (i, (acc, acc_value)) in accounts.iter().enumerate() {
|
||||
// give whatever left to the last account not to have rounding errors.
|
||||
let liq_tokens_this_acc = if i == accounts.len() - 1 {
|
||||
tokens - total_tokens_this_coin
|
||||
} else {
|
||||
tokens.saturating_mul(*acc_value) / pool_value
|
||||
};
|
||||
|
||||
total_tokens_this_coin = total_tokens_this_coin.saturating_add(liq_tokens_this_acc);
|
||||
|
||||
LiquidityTokensPerAddress::<T>::set(
|
||||
coin,
|
||||
acc,
|
||||
Some(u64::try_from(liq_tokens_this_acc).unwrap()),
|
||||
);
|
||||
}
|
||||
assert_eq!(tokens, total_tokens_this_coin);
|
||||
}
|
||||
assert_eq!(total_sri_distributed, GENESIS_SRI);
|
||||
|
||||
@@ -199,18 +165,23 @@ pub mod pallet {
|
||||
for coin in COINS {
|
||||
assert_eq!(Coins::<T>::balance(GENESIS_LIQUIDITY_ACCOUNT.into(), coin), Amount(0));
|
||||
}
|
||||
|
||||
GenesisComplete::<T>::set(Some(()));
|
||||
}
|
||||
|
||||
// we accept we reached economic security once we can mint smallest amount of a network's coin
|
||||
// TODO: move EconomicSecurity to a separate pallet
|
||||
for coin in COINS {
|
||||
let existing = EconomicSecurityReached::<T>::get(coin.network());
|
||||
if existing == 0u32.into() &&
|
||||
if existing.is_none() &&
|
||||
<T as CoinsConfig>::AllowMint::is_allowed(&Balance { coin, amount: Amount(1) })
|
||||
{
|
||||
EconomicSecurityReached::<T>::set(coin.network(), n);
|
||||
EconomicSecurityReached::<T>::set(coin.network(), Some(n));
|
||||
Self::deposit_event(Event::EconomicSecurityReached { network: coin.network() });
|
||||
}
|
||||
}
|
||||
|
||||
Weight::zero() // TODO
|
||||
}
|
||||
}
|
||||
|
||||
@@ -223,39 +194,87 @@ pub mod pallet {
|
||||
Err(Error::<T>::GenesisPeriodEnded)?;
|
||||
}
|
||||
|
||||
// mint the coins
|
||||
Coins::<T>::mint(GENESIS_LIQUIDITY_ACCOUNT.into(), balance)?;
|
||||
// calculate new shares & supply
|
||||
let (new_liquidity, new_supply) = if let Some(supply) = Supply::<T>::get(balance.coin) {
|
||||
// calculate amount of shares for this amount
|
||||
let shares = Self::mul_div(supply.shares, balance.amount.0, supply.coins)?;
|
||||
|
||||
// get new shares for this account
|
||||
let existing =
|
||||
Liquidity::<T>::get(balance.coin, account).unwrap_or(LiquidityAmount::zero());
|
||||
(
|
||||
LiquidityAmount {
|
||||
shares: existing.shares.checked_add(shares).ok_or(Error::<T>::AmountOverflowed)?,
|
||||
coins: existing
|
||||
.coins
|
||||
.checked_add(balance.amount.0)
|
||||
.ok_or(Error::<T>::AmountOverflowed)?,
|
||||
},
|
||||
LiquidityAmount {
|
||||
shares: supply.shares.checked_add(shares).ok_or(Error::<T>::AmountOverflowed)?,
|
||||
coins: supply
|
||||
.coins
|
||||
.checked_add(balance.amount.0)
|
||||
.ok_or(Error::<T>::AmountOverflowed)?,
|
||||
},
|
||||
)
|
||||
} else {
|
||||
let first_amount =
|
||||
LiquidityAmount { shares: INITIAL_GENESIS_LP_SHARES, coins: balance.amount.0 };
|
||||
(first_amount, first_amount)
|
||||
};
|
||||
|
||||
// save
|
||||
let existing = Liquidity::<T>::get(balance.coin, account).unwrap_or(0);
|
||||
let new = existing.checked_add(balance.amount.0).ok_or(Error::<T>::AmountOverflowed)?;
|
||||
Liquidity::<T>::set(balance.coin, account, Some(new));
|
||||
|
||||
Liquidity::<T>::set(balance.coin, account, Some(new_liquidity));
|
||||
Supply::<T>::set(balance.coin, Some(new_supply));
|
||||
Self::deposit_event(Event::GenesisLiquidityAdded { by: account.into(), balance });
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Returns the number of blocks since the coin's network reached economic security first time.
|
||||
/// If the network is yet to be reached that threshold, 0 is returned. And maximum of
|
||||
/// `GENESIS_SRI_TRICKLE_FEED` returned.
|
||||
fn blocks_since_ec_security(coin: &Coin) -> u64 {
|
||||
let ec_security_block =
|
||||
EconomicSecurityReached::<T>::get(coin.network()).saturated_into::<u64>();
|
||||
let current = <frame_system::Pallet<T>>::block_number().saturated_into::<u64>();
|
||||
if ec_security_block > 0 {
|
||||
let diff = current - ec_security_block;
|
||||
if diff > GENESIS_SRI_TRICKLE_FEED {
|
||||
return GENESIS_SRI_TRICKLE_FEED;
|
||||
}
|
||||
|
||||
return diff;
|
||||
/// Returns the number of blocks since the all networks reached economic security first time.
|
||||
/// If networks is yet to be reached that threshold, None is returned.
|
||||
fn blocks_since_ec_security() -> Option<u64> {
|
||||
let mut min = u64::MAX;
|
||||
for n in NETWORKS {
|
||||
let ec_security_block = EconomicSecurityReached::<T>::get(n)?.saturated_into::<u64>();
|
||||
let current = <frame_system::Pallet<T>>::block_number().saturated_into::<u64>();
|
||||
let diff = current.saturating_sub(ec_security_block);
|
||||
min = diff.min(min);
|
||||
}
|
||||
|
||||
0
|
||||
Some(min)
|
||||
}
|
||||
|
||||
fn genesis_ended() -> bool {
|
||||
<frame_system::Pallet<T>>::block_number() >= GENESIS_PERIOD_BLOCKS.into()
|
||||
Self::oraclization_is_done() &&
|
||||
<frame_system::Pallet<T>>::block_number().saturated_into::<u64>() >= MONTHS
|
||||
}
|
||||
|
||||
fn oraclization_is_done() -> bool {
|
||||
for c in COINS {
|
||||
if c == Coin::Serai {
|
||||
continue;
|
||||
}
|
||||
|
||||
if Oracle::<T>::get(c).is_none() {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
true
|
||||
}
|
||||
|
||||
fn mul_div(a: u64, b: u64, c: u64) -> Result<u64, Error<T>> {
|
||||
let a = u128::from(a);
|
||||
let b = u128::from(b);
|
||||
let c = u128::from(c);
|
||||
|
||||
let result = a
|
||||
.checked_mul(b)
|
||||
.ok_or(Error::<T>::AmountOverflowed)?
|
||||
.checked_div(c)
|
||||
.ok_or(Error::<T>::AmountOverflowed)?;
|
||||
|
||||
result.try_into().map_err(|_| Error::<T>::AmountOverflowed)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -267,14 +286,21 @@ pub mod pallet {
|
||||
pub fn remove_coin_liquidity(origin: OriginFor<T>, balance: Balance) -> DispatchResult {
|
||||
let account = ensure_signed(origin)?;
|
||||
let origin = RawOrigin::Signed(GENESIS_LIQUIDITY_ACCOUNT.into());
|
||||
let supply = Supply::<T>::get(balance.coin).ok_or(Error::<T>::NotEnoughLiquidity)?;
|
||||
|
||||
// check we are still in genesis period
|
||||
if Self::genesis_ended() {
|
||||
// check user have enough to remove
|
||||
let existing = LiquidityTokensPerAddress::<T>::get(balance.coin, account).unwrap_or(0);
|
||||
if balance.amount.0 > existing {
|
||||
Err(Error::<T>::NotEnoughLiquidity)?;
|
||||
}
|
||||
let (new_liquidity, new_supply) = if Self::genesis_ended() {
|
||||
// see how much liq tokens we have
|
||||
let total_liq_tokens =
|
||||
LiquidityTokens::<T>::balance(GENESIS_LIQUIDITY_ACCOUNT.into(), Coin::Serai).0;
|
||||
|
||||
// get how much user wants to remove
|
||||
let LiquidityAmount { shares, coins } =
|
||||
Liquidity::<T>::get(balance.coin, account).unwrap_or(LiquidityAmount::zero());
|
||||
let total_shares = Supply::<T>::get(balance.coin).unwrap_or(LiquidityAmount::zero()).shares;
|
||||
let user_liq_tokens = Self::mul_div(total_liq_tokens, shares, total_shares)?;
|
||||
let amount_to_remove =
|
||||
Self::mul_div(user_liq_tokens, balance.amount.0, INITIAL_GENESIS_LP_SHARES)?;
|
||||
|
||||
// remove liquidity from pool
|
||||
let prev_sri = Coins::<T>::balance(GENESIS_LIQUIDITY_ACCOUNT.into(), Coin::Serai);
|
||||
@@ -282,7 +308,7 @@ pub mod pallet {
|
||||
Dex::<T>::remove_liquidity(
|
||||
origin.clone().into(),
|
||||
balance.coin,
|
||||
balance.amount.0,
|
||||
amount_to_remove,
|
||||
1,
|
||||
1,
|
||||
GENESIS_LIQUIDITY_ACCOUNT.into(),
|
||||
@@ -291,18 +317,26 @@ pub mod pallet {
|
||||
let current_coin = Coins::<T>::balance(GENESIS_LIQUIDITY_ACCOUNT.into(), balance.coin);
|
||||
|
||||
// burn the SRI if necessary
|
||||
let mut sri = current_sri.0.saturating_sub(prev_sri.0);
|
||||
// TODO: take into consideration movement between pools.
|
||||
let mut sri: u64 = current_sri.0.saturating_sub(prev_sri.0);
|
||||
let distance_to_full_pay =
|
||||
GENESIS_SRI_TRICKLE_FEED - Self::blocks_since_ec_security(&balance.coin);
|
||||
let burn_sri_amount = sri.saturating_mul(distance_to_full_pay) / GENESIS_SRI_TRICKLE_FEED;
|
||||
GENESIS_SRI_TRICKLE_FEED.saturating_sub(Self::blocks_since_ec_security().unwrap_or(0));
|
||||
let burn_sri_amount = u64::try_from(
|
||||
u128::from(sri)
|
||||
.checked_mul(u128::from(distance_to_full_pay))
|
||||
.ok_or(Error::<T>::AmountOverflowed)?
|
||||
.checked_div(u128::from(GENESIS_SRI_TRICKLE_FEED))
|
||||
.ok_or(Error::<T>::AmountOverflowed)?,
|
||||
)
|
||||
.map_err(|_| Error::<T>::AmountOverflowed)?;
|
||||
Coins::<T>::burn(
|
||||
origin.clone().into(),
|
||||
Balance { coin: Coin::Serai, amount: Amount(burn_sri_amount) },
|
||||
)?;
|
||||
sri -= burn_sri_amount;
|
||||
sri = sri.checked_sub(burn_sri_amount).ok_or(Error::<T>::AmountOverflowed)?;
|
||||
|
||||
// transfer to owner
|
||||
let coin_out = current_coin.0 - prev_coin.0;
|
||||
let coin_out = current_coin.0.saturating_sub(prev_coin.0);
|
||||
Coins::<T>::transfer(
|
||||
origin.clone().into(),
|
||||
account,
|
||||
@@ -314,46 +348,73 @@ pub mod pallet {
|
||||
Balance { coin: Coin::Serai, amount: Amount(sri) },
|
||||
)?;
|
||||
|
||||
// save
|
||||
let existing = LiquidityTokensPerAddress::<T>::get(balance.coin, account).unwrap_or(0);
|
||||
let new = existing.checked_sub(balance.amount.0).ok_or(Error::<T>::AmountOverflowed)?;
|
||||
LiquidityTokensPerAddress::<T>::set(balance.coin, account, Some(new));
|
||||
// return new amounts
|
||||
(
|
||||
LiquidityAmount {
|
||||
shares: shares.checked_sub(amount_to_remove).ok_or(Error::<T>::AmountOverflowed)?,
|
||||
coins: coins.checked_sub(coin_out).ok_or(Error::<T>::AmountOverflowed)?,
|
||||
},
|
||||
LiquidityAmount {
|
||||
shares: supply
|
||||
.shares
|
||||
.checked_sub(amount_to_remove)
|
||||
.ok_or(Error::<T>::AmountOverflowed)?,
|
||||
coins: supply.coins.checked_sub(coin_out).ok_or(Error::<T>::AmountOverflowed)?,
|
||||
},
|
||||
)
|
||||
} else {
|
||||
let existing = Liquidity::<T>::get(balance.coin, account).unwrap_or(0);
|
||||
if balance.amount.0 > existing || balance.amount.0 == 0 {
|
||||
Err(Error::<T>::NotEnoughLiquidity)?;
|
||||
}
|
||||
if balance.amount.0 < existing {
|
||||
if balance.amount.0 != INITIAL_GENESIS_LP_SHARES {
|
||||
Err(Error::<T>::CanOnlyRemoveFullAmount)?;
|
||||
}
|
||||
let existing =
|
||||
Liquidity::<T>::get(balance.coin, account).ok_or(Error::<T>::NotEnoughLiquidity)?;
|
||||
|
||||
// TODO: do external transfer instead for making it easier for the user?
|
||||
// or do we even want to make it easier?
|
||||
Coins::<T>::transfer(origin.into(), account, balance)?;
|
||||
// transfer to the user
|
||||
Coins::<T>::transfer(
|
||||
origin.into(),
|
||||
account,
|
||||
Balance { coin: balance.coin, amount: Amount(existing.coins) },
|
||||
)?;
|
||||
|
||||
// save
|
||||
(
|
||||
LiquidityAmount::zero(),
|
||||
LiquidityAmount {
|
||||
shares: supply
|
||||
.shares
|
||||
.checked_sub(existing.shares)
|
||||
.ok_or(Error::<T>::AmountOverflowed)?,
|
||||
coins: supply.coins.checked_sub(existing.coins).ok_or(Error::<T>::AmountOverflowed)?,
|
||||
},
|
||||
)
|
||||
};
|
||||
|
||||
// save
|
||||
if new_liquidity == LiquidityAmount::zero() {
|
||||
Liquidity::<T>::set(balance.coin, account, None);
|
||||
} else {
|
||||
Liquidity::<T>::set(balance.coin, account, Some(new_liquidity));
|
||||
}
|
||||
Supply::<T>::set(balance.coin, Some(new_supply));
|
||||
|
||||
Self::deposit_event(Event::GenesisLiquidityRemoved { by: account.into(), balance });
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// A call to submit the initial coi values.
|
||||
/// A call to submit the initial coin values in terms of BTC.
|
||||
#[pallet::call_index(1)]
|
||||
#[pallet::weight((0, DispatchClass::Operational))] // TODO
|
||||
pub fn set_initial_price(
|
||||
pub fn oraclize_values(
|
||||
origin: OriginFor<T>,
|
||||
prices: Prices,
|
||||
values: Values,
|
||||
_signature: Signature,
|
||||
) -> DispatchResult {
|
||||
ensure_none(origin)?;
|
||||
|
||||
// set the prices
|
||||
Oracle::<T>::set(Coin::Bitcoin, prices.bitcoin);
|
||||
Oracle::<T>::set(Coin::Monero, prices.monero);
|
||||
Oracle::<T>::set(Coin::Ether, prices.ethereum);
|
||||
Oracle::<T>::set(Coin::Dai, prices.dai);
|
||||
// set their relative values
|
||||
Oracle::<T>::set(Coin::Bitcoin, Some(10u64.pow(Coin::Bitcoin.decimals())));
|
||||
Oracle::<T>::set(Coin::Monero, Some(values.monero));
|
||||
Oracle::<T>::set(Coin::Ether, Some(values.ether));
|
||||
Oracle::<T>::set(Coin::Dai, Some(values.dai));
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
@@ -364,11 +425,32 @@ pub mod pallet {
|
||||
|
||||
fn validate_unsigned(_: TransactionSource, call: &Self::Call) -> TransactionValidity {
|
||||
match call {
|
||||
Call::set_initial_price { ref prices, ref signature } => {
|
||||
let set = ValidatorSet { network: NetworkId::Serai, session: Session(0) };
|
||||
let signers = Participants::<T>::get(NetworkId::Serai);
|
||||
Call::oraclize_values { ref values, ref signature } => {
|
||||
let network = NetworkId::Serai;
|
||||
let Some(session) = ValidatorSets::<T>::session(network) else {
|
||||
return Err(TransactionValidityError::from(InvalidTransaction::Custom(0)));
|
||||
};
|
||||
|
||||
if !musig_key(set, &signers).verify(&set_initial_price_message(&set, prices), signature) {
|
||||
let set = ValidatorSet { network, session };
|
||||
let signers = ValidatorSets::<T>::participants_for_latest_decided_set(network)
|
||||
.expect("no participant in the current set")
|
||||
.into_iter()
|
||||
.map(|(p, _)| p)
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
// check this didn't get called before
|
||||
if Self::oraclization_is_done() {
|
||||
Err(InvalidTransaction::Custom(1))?;
|
||||
}
|
||||
|
||||
// make sure signers settings the value at the end of the genesis period.
|
||||
// we don't need this check for tests.
|
||||
#[cfg(not(feature = "fast-epoch"))]
|
||||
if <frame_system::Pallet<T>>::block_number().saturated_into::<u64>() < MONTHS {
|
||||
Err(InvalidTransaction::Custom(2))?;
|
||||
}
|
||||
|
||||
if !musig_key(set, &signers).verify(&oraclize_values_message(&set, values), signature) {
|
||||
Err(InvalidTransaction::BadProof)?;
|
||||
}
|
||||
|
||||
|
||||
@@ -18,34 +18,37 @@ use scale_info::TypeInfo;
|
||||
use serai_primitives::*;
|
||||
use validator_sets_primitives::ValidatorSet;
|
||||
|
||||
// amount of blocks in 30 days for 6s per block.
|
||||
#[cfg(not(feature = "fast-epoch"))]
|
||||
pub const GENESIS_PERIOD_BLOCKS: u32 = 10 * 60 * 24 * 30;
|
||||
|
||||
#[cfg(feature = "fast-epoch")]
|
||||
pub const GENESIS_PERIOD_BLOCKS: u32 = 25;
|
||||
|
||||
/// 180 days of blocks
|
||||
pub const GENESIS_SRI_TRICKLE_FEED: u64 = 10 * 60 * 24 * 180;
|
||||
|
||||
// 100 Million SRI
|
||||
pub const GENESIS_SRI: u64 = 100_000_000 * 10_u64.pow(8);
|
||||
pub const INITIAL_GENESIS_LP_SHARES: u64 = 10_000;
|
||||
|
||||
// This is the account to hold and manage the genesis liquidity.
|
||||
pub const GENESIS_LIQUIDITY_ACCOUNT: SeraiAddress = system_address(b"Genesis-liquidity-account");
|
||||
pub const GENESIS_LIQUIDITY_ACCOUNT: SeraiAddress = system_address(b"GenesisLiquidity-account");
|
||||
|
||||
#[derive(Clone, Copy, PartialEq, Eq, Debug, Encode, Decode, MaxEncodedLen, TypeInfo)]
|
||||
#[cfg_attr(feature = "std", derive(Zeroize))]
|
||||
#[cfg_attr(feature = "borsh", derive(BorshSerialize, BorshDeserialize))]
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
pub struct Prices {
|
||||
pub bitcoin: u64,
|
||||
pub struct Values {
|
||||
pub monero: u64,
|
||||
pub ethereum: u64,
|
||||
pub ether: u64,
|
||||
pub dai: u64,
|
||||
}
|
||||
|
||||
/// The message for the set_initial_price signature.
|
||||
pub fn set_initial_price_message(set: &ValidatorSet, prices: &Prices) -> Vec<u8> {
|
||||
(b"GenesisLiquidity-set_initial_price", set, prices).encode()
|
||||
#[derive(Clone, Copy, PartialEq, Eq, Debug, Encode, Decode, MaxEncodedLen, TypeInfo)]
|
||||
#[cfg_attr(feature = "std", derive(Zeroize))]
|
||||
#[cfg_attr(feature = "borsh", derive(BorshSerialize, BorshDeserialize))]
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
pub struct LiquidityAmount {
|
||||
pub shares: u64,
|
||||
pub coins: u64,
|
||||
}
|
||||
|
||||
impl LiquidityAmount {
|
||||
pub fn zero() -> Self {
|
||||
LiquidityAmount { shares: 0, coins: 0 }
|
||||
}
|
||||
}
|
||||
|
||||
/// The message for the oraclize_values signature.
|
||||
pub fn oraclize_values_message(set: &ValidatorSet, values: &Values) -> Vec<u8> {
|
||||
(b"GenesisLiquidity-oraclize_values", set, values).encode()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user