mirror of
https://github.com/serai-dex/serai.git
synced 2025-12-11 13:39:25 +00:00
fix pr comments
This commit is contained in:
1
Cargo.lock
generated
1
Cargo.lock
generated
@@ -7966,6 +7966,7 @@ dependencies = [
|
|||||||
"serai-dex-pallet",
|
"serai-dex-pallet",
|
||||||
"serai-genesis-liquidity-primitives",
|
"serai-genesis-liquidity-primitives",
|
||||||
"serai-primitives",
|
"serai-primitives",
|
||||||
|
"serai-validator-sets-pallet",
|
||||||
"serai-validator-sets-primitives",
|
"serai-validator-sets-primitives",
|
||||||
"sp-application-crypto",
|
"sp-application-crypto",
|
||||||
"sp-core",
|
"sp-core",
|
||||||
|
|||||||
@@ -13,15 +13,6 @@ pub enum Call {
|
|||||||
burn_with_instruction { instruction: OutInstructionWithBalance },
|
burn_with_instruction { instruction: OutInstructionWithBalance },
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, PartialEq, Eq, Debug, scale::Encode, scale::Decode, scale_info::TypeInfo)]
|
|
||||||
#[cfg_attr(feature = "borsh", derive(borsh::BorshSerialize, borsh::BorshDeserialize))]
|
|
||||||
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
|
|
||||||
#[cfg_attr(all(feature = "std", feature = "serde"), derive(serde::Deserialize))]
|
|
||||||
pub enum LiquidityTokensCall {
|
|
||||||
transfer { to: SeraiAddress, balance: Balance },
|
|
||||||
burn { balance: Balance },
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone, PartialEq, Eq, Debug, scale::Encode, scale::Decode, scale_info::TypeInfo)]
|
#[derive(Clone, PartialEq, Eq, Debug, scale::Encode, scale::Decode, scale_info::TypeInfo)]
|
||||||
#[cfg_attr(feature = "borsh", derive(borsh::BorshSerialize, borsh::BorshDeserialize))]
|
#[cfg_attr(feature = "borsh", derive(borsh::BorshSerialize, borsh::BorshDeserialize))]
|
||||||
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
|
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ pub mod system;
|
|||||||
pub mod timestamp;
|
pub mod timestamp;
|
||||||
|
|
||||||
pub mod coins;
|
pub mod coins;
|
||||||
|
pub mod liquidity_tokens;
|
||||||
pub mod dex;
|
pub mod dex;
|
||||||
|
|
||||||
pub mod validator_sets;
|
pub mod validator_sets;
|
||||||
@@ -29,7 +30,7 @@ pub mod tx;
|
|||||||
pub enum Call {
|
pub enum Call {
|
||||||
Timestamp(timestamp::Call),
|
Timestamp(timestamp::Call),
|
||||||
Coins(coins::Call),
|
Coins(coins::Call),
|
||||||
LiquidityTokens(coins::LiquidityTokensCall),
|
LiquidityTokens(liquidity_tokens::Call),
|
||||||
Dex(dex::Call),
|
Dex(dex::Call),
|
||||||
GenesisLiquidity(genesis_liquidity::Call),
|
GenesisLiquidity(genesis_liquidity::Call),
|
||||||
ValidatorSets(validator_sets::Call),
|
ValidatorSets(validator_sets::Call),
|
||||||
@@ -51,7 +52,7 @@ pub enum Event {
|
|||||||
Timestamp,
|
Timestamp,
|
||||||
TransactionPayment(TransactionPaymentEvent),
|
TransactionPayment(TransactionPaymentEvent),
|
||||||
Coins(coins::Event),
|
Coins(coins::Event),
|
||||||
LiquidityTokens(coins::Event),
|
LiquidityTokens(liquidity_tokens::Event),
|
||||||
Dex(dex::Event),
|
Dex(dex::Event),
|
||||||
GenesisLiquidity(genesis_liquidity::Event),
|
GenesisLiquidity(genesis_liquidity::Event),
|
||||||
ValidatorSets(validator_sets::Event),
|
ValidatorSets(validator_sets::Event),
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ use serai_abi::primitives::{SeraiAddress, Amount, Coin};
|
|||||||
|
|
||||||
use scale::{decode_from_bytes, Encode};
|
use scale::{decode_from_bytes, Encode};
|
||||||
|
|
||||||
use crate::{SeraiError, hex_decode, TemporalSerai};
|
use crate::{Serai, SeraiError, TemporalSerai};
|
||||||
|
|
||||||
pub type DexEvent = serai_abi::dex::Event;
|
pub type DexEvent = serai_abi::dex::Event;
|
||||||
|
|
||||||
@@ -60,20 +60,19 @@ impl<'a> SeraiDex<'a> {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn get_reserves(
|
/// Returns the reserves of `coin:SRI` pool.
|
||||||
&self,
|
pub async fn get_reserves(&self, coin: Coin) -> Result<Option<(Amount, Amount)>, SeraiError> {
|
||||||
coin1: Coin,
|
let reserves = self
|
||||||
coin2: Coin,
|
|
||||||
) -> Result<Option<(Amount, Amount)>, SeraiError> {
|
|
||||||
let hash = self
|
|
||||||
.0
|
.0
|
||||||
.serai
|
.serai
|
||||||
.call("state_call", ["DexApi_get_reserves".to_string(), hex::encode((coin1, coin2).encode())])
|
.call(
|
||||||
|
"state_call",
|
||||||
|
["DexApi_get_reserves".to_string(), hex::encode((coin, Coin::Serai).encode())],
|
||||||
|
)
|
||||||
.await?;
|
.await?;
|
||||||
let bytes = hex_decode(hash)
|
let bytes = Serai::hex_decode(reserves)?;
|
||||||
.map_err(|_| SeraiError::InvalidNode("expected hex from node wasn't hex".to_string()))?;
|
let result = decode_from_bytes::<Option<(u64, u64)>>(bytes.into())
|
||||||
let resut = decode_from_bytes::<Option<(u64, u64)>>(bytes.into())
|
|
||||||
.map_err(|e| SeraiError::ErrorInResponse(e.to_string()))?;
|
.map_err(|e| SeraiError::ErrorInResponse(e.to_string()))?;
|
||||||
Ok(resut.map(|amounts| (Amount(amounts.0), Amount(amounts.1))))
|
Ok(result.map(|amounts| (Amount(amounts.0), Amount(amounts.1))))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -29,24 +29,6 @@ impl<'a> SeraiGenesisLiquidity<'a> {
|
|||||||
.await
|
.await
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn liquidity_tokens(
|
|
||||||
&self,
|
|
||||||
address: &SeraiAddress,
|
|
||||||
coin: Coin,
|
|
||||||
) -> Result<Amount, SeraiError> {
|
|
||||||
Ok(
|
|
||||||
self
|
|
||||||
.0
|
|
||||||
.storage(
|
|
||||||
PALLET,
|
|
||||||
"LiquidityTokensPerAddress",
|
|
||||||
(coin, sp_core::hashing::blake2_128(&address.encode()), &address.0),
|
|
||||||
)
|
|
||||||
.await?
|
|
||||||
.unwrap_or(Amount(0)),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn set_initial_price(prices: Prices, signature: Signature) -> Transaction {
|
pub fn set_initial_price(prices: Prices, signature: Signature) -> Transaction {
|
||||||
Serai::unsigned(serai_abi::Call::GenesisLiquidity(
|
Serai::unsigned(serai_abi::Call::GenesisLiquidity(
|
||||||
serai_abi::genesis_liquidity::Call::set_initial_price { prices, signature },
|
serai_abi::genesis_liquidity::Call::set_initial_price { prices, signature },
|
||||||
@@ -59,15 +41,21 @@ impl<'a> SeraiGenesisLiquidity<'a> {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn liquidity(&self, address: &SeraiAddress, coin: Coin) -> Option<Amount> {
|
pub async fn liquidity(&self, address: &SeraiAddress, coin: Coin) -> Result<Amount, SeraiError> {
|
||||||
self
|
Ok(
|
||||||
.0
|
self
|
||||||
.storage(
|
.0
|
||||||
PALLET,
|
.storage(
|
||||||
"Liquidity",
|
PALLET,
|
||||||
(coin, sp_core::hashing::blake2_128(&address.encode()), &address.0),
|
"Liquidity",
|
||||||
)
|
(coin, sp_core::hashing::blake2_128(&address.encode()), &address.0),
|
||||||
.await
|
)
|
||||||
.unwrap()
|
.await?
|
||||||
|
.unwrap_or(Amount(0)),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn supply(&self, coin: Coin) -> Result<(Amount, Amount), SeraiError> {
|
||||||
|
Ok(self.0.storage(PALLET, "Supply", coin).await?.unwrap_or((Amount(0), Amount(0))))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -32,10 +32,10 @@ impl<'a> SeraiLiquidityTokens<'a> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn transfer(to: SeraiAddress, balance: Balance) -> serai_abi::Call {
|
pub fn transfer(to: SeraiAddress, balance: Balance) -> serai_abi::Call {
|
||||||
serai_abi::Call::Coins(serai_abi::coins::Call::transfer { to, balance })
|
serai_abi::Call::LiquidityTokens(serai_abi::liquidity_tokens::Call::transfer { to, balance })
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn burn(balance: Balance) -> serai_abi::Call {
|
pub fn burn(balance: Balance) -> serai_abi::Call {
|
||||||
serai_abi::Call::Coins(serai_abi::coins::Call::burn { balance })
|
serai_abi::Call::LiquidityTokens(serai_abi::liquidity_tokens::Call::burn { balance })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
use hex::FromHexError;
|
|
||||||
use thiserror::Error;
|
use thiserror::Error;
|
||||||
|
|
||||||
use async_lock::RwLock;
|
use async_lock::RwLock;
|
||||||
@@ -87,14 +86,6 @@ impl<'a> Clone for TemporalSerai<'a> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn hex_decode(str: String) -> Result<Vec<u8>, FromHexError> {
|
|
||||||
if let Some(stripped) = str.strip_prefix("0x") {
|
|
||||||
hex::decode(stripped)
|
|
||||||
} else {
|
|
||||||
hex::decode(str)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Serai {
|
impl Serai {
|
||||||
pub async fn call<Req: Serialize, Res: DeserializeOwned>(
|
pub async fn call<Req: Serialize, Res: DeserializeOwned>(
|
||||||
&self,
|
&self,
|
||||||
@@ -147,11 +138,19 @@ impl Serai {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn hex_decode(str: String) -> Result<Vec<u8>, SeraiError> {
|
||||||
|
(if let Some(stripped) = str.strip_prefix("0x") {
|
||||||
|
hex::decode(stripped)
|
||||||
|
} else {
|
||||||
|
hex::decode(str)
|
||||||
|
})
|
||||||
|
.map_err(|_| SeraiError::InvalidNode("expected hex from node wasn't hex".to_string()))
|
||||||
|
}
|
||||||
|
|
||||||
pub async fn block_hash(&self, number: u64) -> Result<Option<[u8; 32]>, SeraiError> {
|
pub async fn block_hash(&self, number: u64) -> Result<Option<[u8; 32]>, SeraiError> {
|
||||||
let hash: Option<String> = self.call("chain_getBlockHash", [number]).await?;
|
let hash: Option<String> = self.call("chain_getBlockHash", [number]).await?;
|
||||||
let Some(hash) = hash else { return Ok(None) };
|
let Some(hash) = hash else { return Ok(None) };
|
||||||
hex_decode(hash)
|
Self::hex_decode(hash)?
|
||||||
.map_err(|_| SeraiError::InvalidNode("expected hex from node wasn't hex".to_string()))?
|
|
||||||
.try_into()
|
.try_into()
|
||||||
.map_err(|_| SeraiError::InvalidNode("didn't respond to getBlockHash with hash".to_string()))
|
.map_err(|_| SeraiError::InvalidNode("didn't respond to getBlockHash with hash".to_string()))
|
||||||
.map(Some)
|
.map(Some)
|
||||||
@@ -204,8 +203,7 @@ impl Serai {
|
|||||||
let validators: String = self
|
let validators: String = self
|
||||||
.call("state_call", ["SeraiRuntimeApi_validators".to_string(), hex::encode(network.encode())])
|
.call("state_call", ["SeraiRuntimeApi_validators".to_string(), hex::encode(network.encode())])
|
||||||
.await?;
|
.await?;
|
||||||
let bytes = hex_decode(validators)
|
let bytes = Self::hex_decode(validators)?;
|
||||||
.map_err(|_| SeraiError::InvalidNode("expected hex from node wasn't hex".to_string()))?;
|
|
||||||
let r = Vec::<Public>::decode(&mut bytes.as_slice())
|
let r = Vec::<Public>::decode(&mut bytes.as_slice())
|
||||||
.map_err(|e| SeraiError::ErrorInResponse(e.to_string()))?;
|
.map_err(|e| SeraiError::ErrorInResponse(e.to_string()))?;
|
||||||
Ok(r)
|
Ok(r)
|
||||||
@@ -213,12 +211,9 @@ impl Serai {
|
|||||||
|
|
||||||
pub async fn latest_finalized_block_hash(&self) -> Result<[u8; 32], SeraiError> {
|
pub async fn latest_finalized_block_hash(&self) -> Result<[u8; 32], SeraiError> {
|
||||||
let hash: String = self.call("chain_getFinalizedHead", ()).await?;
|
let hash: String = self.call("chain_getFinalizedHead", ()).await?;
|
||||||
hex_decode(hash)
|
Self::hex_decode(hash)?.try_into().map_err(|_| {
|
||||||
.map_err(|_| SeraiError::InvalidNode("expected hex from node wasn't hex".to_string()))?
|
SeraiError::InvalidNode("didn't respond to getFinalizedHead with hash".to_string())
|
||||||
.try_into()
|
})
|
||||||
.map_err(|_| {
|
|
||||||
SeraiError::InvalidNode("didn't respond to getFinalizedHead with hash".to_string())
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn header(&self, hash: [u8; 32]) -> Result<Option<Header>, SeraiError> {
|
pub async fn header(&self, hash: [u8; 32]) -> Result<Option<Header>, SeraiError> {
|
||||||
@@ -228,7 +223,7 @@ impl Serai {
|
|||||||
pub async fn block(&self, hash: [u8; 32]) -> Result<Option<Block>, SeraiError> {
|
pub async fn block(&self, hash: [u8; 32]) -> Result<Option<Block>, SeraiError> {
|
||||||
let block: Option<String> = self.call("chain_getBlockBin", [hex::encode(hash)]).await?;
|
let block: Option<String> = self.call("chain_getBlockBin", [hex::encode(hash)]).await?;
|
||||||
let Some(block) = block else { return Ok(None) };
|
let Some(block) = block else { return Ok(None) };
|
||||||
let Ok(bytes) = hex_decode(block) else {
|
let Ok(bytes) = Self::hex_decode(block) else {
|
||||||
Err(SeraiError::InvalidNode("didn't return a hex-encoded block".to_string()))?
|
Err(SeraiError::InvalidNode("didn't return a hex-encoded block".to_string()))?
|
||||||
};
|
};
|
||||||
let Ok(block) = Block::decode(&mut bytes.as_slice()) else {
|
let Ok(block) = Block::decode(&mut bytes.as_slice()) else {
|
||||||
@@ -374,8 +369,7 @@ impl<'a> TemporalSerai<'a> {
|
|||||||
let res: Option<String> =
|
let res: Option<String> =
|
||||||
self.serai.call("state_getStorage", [hex::encode(full_key), hex::encode(self.block)]).await?;
|
self.serai.call("state_getStorage", [hex::encode(full_key), hex::encode(self.block)]).await?;
|
||||||
let Some(res) = res else { return Ok(None) };
|
let Some(res) = res else { return Ok(None) };
|
||||||
let res = hex_decode(res)
|
let res = Serai::hex_decode(res)?;
|
||||||
.map_err(|_| SeraiError::InvalidNode("expected hex from node wasn't hex".to_string()))?;
|
|
||||||
Ok(Some(R::decode(&mut res.as_slice()).map_err(|_| {
|
Ok(Some(R::decode(&mut res.as_slice()).map_err(|_| {
|
||||||
SeraiError::InvalidRuntime(format!(
|
SeraiError::InvalidRuntime(format!(
|
||||||
"different type present at storage location, raw value: {}",
|
"different type present at storage location, raw value: {}",
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ use schnorrkel::Schnorrkel;
|
|||||||
|
|
||||||
use serai_client::{
|
use serai_client::{
|
||||||
genesis_liquidity::{
|
genesis_liquidity::{
|
||||||
primitives::{GENESIS_LIQUIDITY_ACCOUNT, GENESIS_SRI},
|
primitives::{GENESIS_LIQUIDITY_ACCOUNT, GENESIS_LP_SHARES},
|
||||||
SeraiGenesisLiquidity,
|
SeraiGenesisLiquidity,
|
||||||
},
|
},
|
||||||
validator_sets::primitives::{musig_context, Session, ValidatorSet},
|
validator_sets::primitives::{musig_context, Session, ValidatorSet},
|
||||||
@@ -24,7 +24,7 @@ use sp_core::{sr25519::Signature, Pair as PairTrait};
|
|||||||
|
|
||||||
use serai_client::{
|
use serai_client::{
|
||||||
primitives::{
|
primitives::{
|
||||||
Amount, NetworkId, Coin, Balance, BlockHash, SeraiAddress, insecure_pair_from_name,
|
Amount, NetworkId, Coin, Balance, BlockHash, SeraiAddress, insecure_pair_from_name, GENESIS_SRI,
|
||||||
},
|
},
|
||||||
in_instructions::primitives::{InInstruction, InInstructionWithBalance, Batch},
|
in_instructions::primitives::{InInstruction, InInstructionWithBalance, Batch},
|
||||||
Serai,
|
Serai,
|
||||||
@@ -63,8 +63,6 @@ async fn test_genesis_liquidity(serai: Serai) {
|
|||||||
xmr_addresses.push((address, amount));
|
xmr_addresses.push((address, amount));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
btc_addresses.sort_by(|a1, a2| a1.0.cmp(&a2.0));
|
|
||||||
xmr_addresses.sort_by(|a1, a2| a1.0.cmp(&a2.0));
|
|
||||||
|
|
||||||
// btc batch
|
// btc batch
|
||||||
let mut block_hash = BlockHash([0; 32]);
|
let mut block_hash = BlockHash([0; 32]);
|
||||||
@@ -93,23 +91,25 @@ async fn test_genesis_liquidity(serai: Serai) {
|
|||||||
let batch = Batch { network: NetworkId::Monero, id: 0, block: block_hash, instructions: xmr_ins };
|
let batch = Batch { network: NetworkId::Monero, id: 0, block: block_hash, instructions: xmr_ins };
|
||||||
provide_batch(&serai, batch).await;
|
provide_batch(&serai, batch).await;
|
||||||
|
|
||||||
// set prices
|
|
||||||
let prices = Prices { bitcoin: 10u64.pow(8), monero: 184100, ethereum: 4785000, dai: 1500 };
|
|
||||||
set_prices(&serai, &prices).await;
|
|
||||||
|
|
||||||
// wait until genesis ends..
|
// wait until genesis ends..
|
||||||
tokio::time::timeout(tokio::time::Duration::from_secs(300), async {
|
tokio::time::timeout(tokio::time::Duration::from_secs(300), async {
|
||||||
while serai.latest_finalized_block().await.unwrap().number() < 25 {
|
while serai.latest_finalized_block().await.unwrap().number() < 10 {
|
||||||
tokio::time::sleep(Duration::from_secs(6)).await;
|
tokio::time::sleep(Duration::from_secs(6)).await;
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
|
// set prices
|
||||||
|
let prices = Prices { bitcoin: 10u64.pow(8), monero: 184100, ethereum: 4785000, dai: 1500 };
|
||||||
|
set_prices(&serai, &prices).await;
|
||||||
|
|
||||||
|
// wait little bit..
|
||||||
|
tokio::time::sleep(Duration::from_secs(12)).await;
|
||||||
|
|
||||||
// check total SRI supply is +100M
|
// check total SRI supply is +100M
|
||||||
let last_block = serai.latest_finalized_block().await.unwrap().hash();
|
let last_block = serai.latest_finalized_block().await.unwrap().hash();
|
||||||
let serai = serai.as_of(last_block);
|
let serai = serai.as_of(last_block);
|
||||||
// Check balance instead of supply
|
|
||||||
let sri = serai.coins().coin_supply(Coin::Serai).await.unwrap();
|
let sri = serai.coins().coin_supply(Coin::Serai).await.unwrap();
|
||||||
// there are 6 endowed accounts in dev-net. Take this into consideration when checking
|
// there are 6 endowed accounts in dev-net. Take this into consideration when checking
|
||||||
// for the total sri minted at this time.
|
// for the total sri minted at this time.
|
||||||
@@ -136,65 +136,37 @@ async fn test_genesis_liquidity(serai: Serai) {
|
|||||||
let btc_sri = (pool_btc_value * u128::from(GENESIS_SRI)) / total_value;
|
let btc_sri = (pool_btc_value * u128::from(GENESIS_SRI)) / total_value;
|
||||||
let xmr_sri = ((pool_xmr_value * u128::from(GENESIS_SRI)) / total_value) + 1;
|
let xmr_sri = ((pool_xmr_value * u128::from(GENESIS_SRI)) / total_value) + 1;
|
||||||
|
|
||||||
let btc_reserves = serai.dex().get_reserves(Coin::Bitcoin, Coin::Serai).await.unwrap().unwrap();
|
let btc_reserves = serai.dex().get_reserves(Coin::Bitcoin).await.unwrap().unwrap();
|
||||||
assert_eq!(u128::from(btc_reserves.0 .0), pool_btc);
|
assert_eq!(u128::from(btc_reserves.0 .0), pool_btc);
|
||||||
assert_eq!(u128::from(btc_reserves.1 .0), btc_sri);
|
assert_eq!(u128::from(btc_reserves.1 .0), btc_sri);
|
||||||
|
|
||||||
let xmr_reserves = serai.dex().get_reserves(Coin::Monero, Coin::Serai).await.unwrap().unwrap();
|
let xmr_reserves = serai.dex().get_reserves(Coin::Monero).await.unwrap().unwrap();
|
||||||
assert_eq!(u128::from(xmr_reserves.0 .0), pool_xmr);
|
assert_eq!(u128::from(xmr_reserves.0 .0), pool_xmr);
|
||||||
assert_eq!(u128::from(xmr_reserves.1 .0), xmr_sri);
|
assert_eq!(u128::from(xmr_reserves.1 .0), xmr_sri);
|
||||||
|
|
||||||
// check each btc liq provider got liq tokens proportional to their value
|
// check each btc liq provider got liq tokens proportional to their value
|
||||||
let btc_liq_token_supply = u128::from(
|
let btc_liq_supply = serai.genesis_liquidity().supply(Coin::Bitcoin).await.unwrap();
|
||||||
serai
|
for (acc, amount) in btc_addresses {
|
||||||
.liquidity_tokens()
|
let acc_liq_shares = serai.genesis_liquidity().liquidity(&acc, Coin::Bitcoin).await.unwrap().0;
|
||||||
.token_balance(Coin::Bitcoin, GENESIS_LIQUIDITY_ACCOUNT)
|
|
||||||
.await
|
|
||||||
.unwrap()
|
|
||||||
.0,
|
|
||||||
);
|
|
||||||
let mut total_tokens_this_coin: u128 = 0;
|
|
||||||
for (i, (addr, amount)) in btc_addresses.iter().enumerate() {
|
|
||||||
let addr_value = (u128::from(amount.0) * u128::from(prices.bitcoin)) / 10u128.pow(8);
|
|
||||||
let addr_liq_tokens = if i == btc_addresses.len() - 1 {
|
|
||||||
btc_liq_token_supply - total_tokens_this_coin
|
|
||||||
} else {
|
|
||||||
(addr_value * btc_liq_token_supply) / pool_btc_value
|
|
||||||
};
|
|
||||||
|
|
||||||
let addr_actual_token_amount =
|
// since we can't test the ratios directly(due to integer division giving 0)
|
||||||
serai.genesis_liquidity().liquidity_tokens(addr, Coin::Bitcoin).await.unwrap();
|
// we test whether they give the same result when multiplied by another constant.
|
||||||
|
// Following test ensures the account in fact has the right amount of shares.
|
||||||
assert_eq!(addr_liq_tokens, addr_actual_token_amount.0.into());
|
let shares_ratio = (GENESIS_LP_SHARES * acc_liq_shares) / btc_liq_supply.0 .0;
|
||||||
total_tokens_this_coin += addr_liq_tokens;
|
let amounts_ratio = (GENESIS_LP_SHARES * amount.0) / u64::try_from(pool_btc).unwrap();
|
||||||
|
assert_eq!(shares_ratio, amounts_ratio);
|
||||||
}
|
}
|
||||||
|
|
||||||
// check each xmr liq provider got liq tokens proportional to their value
|
// check each xmr liq provider got liq tokens proportional to their value
|
||||||
let xmr_liq_token_supply = u128::from(
|
let xmr_liq_supply = serai.genesis_liquidity().supply(Coin::Monero).await.unwrap();
|
||||||
serai
|
for (acc, amount) in xmr_addresses {
|
||||||
.liquidity_tokens()
|
let acc_liq_shares = serai.genesis_liquidity().liquidity(&acc, Coin::Monero).await.unwrap().0;
|
||||||
.token_balance(Coin::Monero, GENESIS_LIQUIDITY_ACCOUNT)
|
let shares_ratio = (GENESIS_LP_SHARES * acc_liq_shares) / xmr_liq_supply.0 .0;
|
||||||
.await
|
let amounts_ratio = (GENESIS_LP_SHARES * amount.0) / u64::try_from(pool_xmr).unwrap();
|
||||||
.unwrap()
|
assert_eq!(shares_ratio, amounts_ratio);
|
||||||
.0,
|
|
||||||
);
|
|
||||||
total_tokens_this_coin = 0;
|
|
||||||
for (i, (addr, amount)) in xmr_addresses.iter().enumerate() {
|
|
||||||
let addr_value = (u128::from(amount.0) * u128::from(prices.monero)) / 10u128.pow(12);
|
|
||||||
let addr_liq_tokens = if i == xmr_addresses.len() - 1 {
|
|
||||||
xmr_liq_token_supply - total_tokens_this_coin
|
|
||||||
} else {
|
|
||||||
(addr_value * xmr_liq_token_supply) / pool_xmr_value
|
|
||||||
};
|
|
||||||
|
|
||||||
let addr_actual_token_amount =
|
|
||||||
serai.genesis_liquidity().liquidity_tokens(addr, Coin::Monero).await.unwrap();
|
|
||||||
|
|
||||||
assert_eq!(addr_liq_tokens, addr_actual_token_amount.0.into());
|
|
||||||
total_tokens_this_coin += addr_liq_tokens;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: remove the liq before/after genesis ended.
|
// TODO: test remove the liq before/after genesis ended.
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn set_prices(serai: &Serai, prices: &Prices) {
|
async fn set_prices(serai: &Serai, prices: &Prices) {
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ pub use coins_pallet as coins;
|
|||||||
|
|
||||||
use coins::Pallet as CoinsPallet;
|
use coins::Pallet as CoinsPallet;
|
||||||
|
|
||||||
use serai_primitives::*;
|
use serai_primitives::{Balance, COINS, PublicKey, system_address, Amount};
|
||||||
|
|
||||||
type LiquidityTokens<T> = coins_pallet::Pallet<T, coins::Instance1>;
|
type LiquidityTokens<T> = coins_pallet::Pallet<T, coins::Instance1>;
|
||||||
type LiquidityTokensError<T> = coins_pallet::Error<T, coins::Instance1>;
|
type LiquidityTokensError<T> = coins_pallet::Error<T, coins::Instance1>;
|
||||||
|
|||||||
@@ -32,6 +32,7 @@ sp-application-crypto = { git = "https://github.com/serai-dex/substrate", defaul
|
|||||||
|
|
||||||
dex-pallet = { package = "serai-dex-pallet", path = "../../dex/pallet", default-features = false }
|
dex-pallet = { package = "serai-dex-pallet", path = "../../dex/pallet", default-features = false }
|
||||||
coins-pallet = { package = "serai-coins-pallet", path = "../../coins/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 }
|
serai-primitives = { path = "../../primitives", default-features = false }
|
||||||
genesis-liquidity-primitives = { package = "serai-genesis-liquidity-primitives", path = "../primitives", default-features = false }
|
genesis-liquidity-primitives = { package = "serai-genesis-liquidity-primitives", path = "../primitives", default-features = false }
|
||||||
@@ -51,6 +52,7 @@ std = [
|
|||||||
|
|
||||||
"coins-pallet/std",
|
"coins-pallet/std",
|
||||||
"dex-pallet/std",
|
"dex-pallet/std",
|
||||||
|
"validator-sets-pallet/std",
|
||||||
|
|
||||||
"serai-primitives/std",
|
"serai-primitives/std",
|
||||||
"genesis-liquidity-primitives/std",
|
"genesis-liquidity-primitives/std",
|
||||||
|
|||||||
@@ -5,19 +5,17 @@
|
|||||||
pub mod pallet {
|
pub mod pallet {
|
||||||
use super::*;
|
use super::*;
|
||||||
use frame_system::{pallet_prelude::*, RawOrigin};
|
use frame_system::{pallet_prelude::*, RawOrigin};
|
||||||
use frame_support::{
|
use frame_support::{pallet_prelude::*, sp_runtime::SaturatedConversion};
|
||||||
pallet_prelude::*,
|
|
||||||
sp_runtime::{self, SaturatedConversion},
|
|
||||||
};
|
|
||||||
|
|
||||||
use sp_std::{vec, vec::Vec, collections::btree_map::BTreeMap};
|
use sp_std::{vec, vec::Vec};
|
||||||
use sp_core::sr25519::Signature;
|
use sp_core::sr25519::Signature;
|
||||||
use sp_application_crypto::RuntimePublic;
|
use sp_application_crypto::RuntimePublic;
|
||||||
|
|
||||||
use dex_pallet::{Pallet as Dex, Config as DexConfig};
|
use dex_pallet::{Pallet as Dex, Config as DexConfig};
|
||||||
use coins_pallet::{Config as CoinsConfig, Pallet as Coins, AllowMint};
|
use coins_pallet::{Config as CoinsConfig, Pallet as Coins, AllowMint};
|
||||||
|
use validator_sets_pallet::{Config as VsConfig, Pallet as ValidatorSets};
|
||||||
|
|
||||||
use serai_primitives::*;
|
use serai_primitives::{Coin, COINS, *};
|
||||||
use validator_sets_primitives::{ValidatorSet, Session, musig_key};
|
use validator_sets_primitives::{ValidatorSet, Session, musig_key};
|
||||||
pub use genesis_liquidity_primitives as primitives;
|
pub use genesis_liquidity_primitives as primitives;
|
||||||
use primitives::*;
|
use primitives::*;
|
||||||
@@ -27,24 +25,15 @@ pub mod pallet {
|
|||||||
|
|
||||||
#[pallet::config]
|
#[pallet::config]
|
||||||
pub trait 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>;
|
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]
|
#[pallet::error]
|
||||||
pub enum Error<T> {
|
pub enum Error<T> {
|
||||||
GenesisPeriodEnded,
|
GenesisPeriodEnded,
|
||||||
@@ -69,39 +58,34 @@ pub mod pallet {
|
|||||||
pub(crate) type Liquidity<T: Config> =
|
pub(crate) type Liquidity<T: Config> =
|
||||||
StorageDoubleMap<_, Identity, Coin, Blake2_128Concat, PublicKey, SubstrateAmount, OptionQuery>;
|
StorageDoubleMap<_, Identity, Coin, Blake2_128Concat, PublicKey, SubstrateAmount, OptionQuery>;
|
||||||
|
|
||||||
|
/// Keeps the total shares and the total amount of coins per coin.
|
||||||
#[pallet::storage]
|
#[pallet::storage]
|
||||||
pub(crate) type LiquidityTokensPerAddress<T: Config> =
|
pub(crate) type Supply<T: Config> = StorageMap<_, Identity, Coin, (u64, u64), OptionQuery>;
|
||||||
StorageDoubleMap<_, Identity, Coin, Blake2_128Concat, PublicKey, SubstrateAmount, OptionQuery>;
|
|
||||||
|
|
||||||
#[pallet::storage]
|
#[pallet::storage]
|
||||||
pub(crate) type EconomicSecurityReached<T: Config> =
|
pub(crate) type EconomicSecurityReached<T: Config> =
|
||||||
StorageMap<_, Identity, NetworkId, BlockNumberFor<T>, ValueQuery>;
|
StorageMap<_, Identity, NetworkId, BlockNumberFor<T>, OptionQuery>;
|
||||||
|
|
||||||
#[pallet::storage]
|
#[pallet::storage]
|
||||||
pub(crate) type Participants<T: Config> =
|
pub(crate) type Oracle<T: Config> = StorageMap<_, Identity, Coin, u64, OptionQuery>;
|
||||||
StorageMap<_, Identity, NetworkId, BoundedVec<PublicKey, ConstU32<150>>, ValueQuery>;
|
|
||||||
|
|
||||||
#[pallet::storage]
|
#[pallet::storage]
|
||||||
pub(crate) type Oracle<T: Config> = StorageMap<_, Identity, Coin, u64, ValueQuery>;
|
pub(crate) type GenesisComplete<T: Config> = StorageValue<_, bool, OptionQuery>;
|
||||||
|
|
||||||
#[pallet::genesis_build]
|
|
||||||
impl<T: Config> BuildGenesisConfig for GenesisConfig<T> {
|
|
||||||
fn build(&self) {
|
|
||||||
Participants::<T>::set(NetworkId::Serai, self.participants.clone().try_into().unwrap());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[pallet::hooks]
|
#[pallet::hooks]
|
||||||
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
|
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
|
||||||
fn on_finalize(n: BlockNumberFor<T>) {
|
fn on_finalize(n: BlockNumberFor<T>) {
|
||||||
#[cfg(feature = "fast-epoch")]
|
#[cfg(feature = "fast-epoch")]
|
||||||
let final_block = 25u32;
|
let final_block = 10u64;
|
||||||
|
|
||||||
#[cfg(not(feature = "fast-epoch"))]
|
#[cfg(not(feature = "fast-epoch"))]
|
||||||
let final_block = BLOCKS_PER_MONTH;
|
let final_block = MONTHS;
|
||||||
|
|
||||||
// Distribute the genesis sri to pools after a month
|
// Distribute the genesis sri to pools after a month
|
||||||
if n == final_block.into() {
|
if n.saturated_into::<u64>() >= final_block &&
|
||||||
|
Self::oraclization_is_done() &&
|
||||||
|
GenesisComplete::<T>::get().is_none()
|
||||||
|
{
|
||||||
// mint the SRI
|
// mint the SRI
|
||||||
Coins::<T>::mint(
|
Coins::<T>::mint(
|
||||||
GENESIS_LIQUIDITY_ACCOUNT.into(),
|
GENESIS_LIQUIDITY_ACCOUNT.into(),
|
||||||
@@ -109,8 +93,7 @@ pub mod pallet {
|
|||||||
)
|
)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
// get coin values & total
|
// get pool & total values
|
||||||
let mut account_values = BTreeMap::new();
|
|
||||||
let mut pool_values = vec![];
|
let mut pool_values = vec![];
|
||||||
let mut total_value: u128 = 0;
|
let mut total_value: u128 = 0;
|
||||||
for coin in COINS {
|
for coin in COINS {
|
||||||
@@ -119,20 +102,11 @@ pub mod pallet {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// initial coin value in terms of btc
|
// 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_amount = u128::from(Supply::<T>::get(coin).unwrap_or((0, 0)).1);
|
||||||
let pool_value = pool_amount.saturating_mul(value.into()) / 10u128.pow(coin.decimals());
|
let pool_value = pool_amount.saturating_mul(value.into()) / 10u128.pow(coin.decimals());
|
||||||
total_value = total_value.saturating_add(pool_value);
|
total_value = total_value.saturating_add(pool_value);
|
||||||
pool_values.push((coin, pool_amount, pool_value));
|
pool_values.push((coin, pool_amount, pool_value));
|
||||||
@@ -173,30 +147,6 @@ pub mod pallet {
|
|||||||
coin1: Balance { coin, amount: Amount(u64::try_from(pool_amount).unwrap()) },
|
coin1: Balance { coin, amount: Amount(u64::try_from(pool_amount).unwrap()) },
|
||||||
coin2: Balance { coin: Coin::Serai, amount: Amount(sri_amount) },
|
coin2: Balance { coin: Coin::Serai, amount: 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);
|
assert_eq!(total_sri_distributed, GENESIS_SRI);
|
||||||
|
|
||||||
@@ -205,15 +155,17 @@ pub mod pallet {
|
|||||||
for coin in COINS {
|
for coin in COINS {
|
||||||
assert_eq!(Coins::<T>::balance(GENESIS_LIQUIDITY_ACCOUNT.into(), coin), Amount(0));
|
assert_eq!(Coins::<T>::balance(GENESIS_LIQUIDITY_ACCOUNT.into(), coin), Amount(0));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
GenesisComplete::<T>::set(Some(true));
|
||||||
}
|
}
|
||||||
|
|
||||||
// we accept we reached economic security once we can mint smallest amount of a network's coin
|
// we accept we reached economic security once we can mint smallest amount of a network's coin
|
||||||
for coin in COINS {
|
for coin in COINS {
|
||||||
let existing = EconomicSecurityReached::<T>::get(coin.network());
|
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) })
|
<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() });
|
Self::deposit_event(Event::EconomicSecurityReached { network: coin.network() });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -232,36 +184,77 @@ pub mod pallet {
|
|||||||
// mint the coins
|
// mint the coins
|
||||||
Coins::<T>::mint(GENESIS_LIQUIDITY_ACCOUNT.into(), balance)?;
|
Coins::<T>::mint(GENESIS_LIQUIDITY_ACCOUNT.into(), balance)?;
|
||||||
|
|
||||||
// save
|
// calculate new shares & supply
|
||||||
let existing = Liquidity::<T>::get(balance.coin, account).unwrap_or(0);
|
let (new_shares, new_supply) = if let Some(supply) = Supply::<T>::get(balance.coin) {
|
||||||
let new = existing.checked_add(balance.amount.0).ok_or(Error::<T>::AmountOverflowed)?;
|
// calculate amount of shares for this amount
|
||||||
Liquidity::<T>::set(balance.coin, account, Some(new));
|
let shares = Self::mul_div(supply.0, balance.amount.0, supply.1)?;
|
||||||
|
|
||||||
|
// get new shares for this account
|
||||||
|
let existing = Liquidity::<T>::get(balance.coin, account).unwrap_or(0);
|
||||||
|
let new = existing.checked_add(shares).ok_or(Error::<T>::AmountOverflowed)?;
|
||||||
|
|
||||||
|
(
|
||||||
|
new,
|
||||||
|
(
|
||||||
|
supply.0.checked_add(shares).ok_or(Error::<T>::AmountOverflowed)?,
|
||||||
|
supply.1.checked_add(balance.amount.0).ok_or(Error::<T>::AmountOverflowed)?,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
(GENESIS_LP_SHARES, (GENESIS_LP_SHARES, balance.amount.0))
|
||||||
|
};
|
||||||
|
|
||||||
|
// save
|
||||||
|
Liquidity::<T>::set(balance.coin, account, Some(new_shares));
|
||||||
|
Supply::<T>::set(balance.coin, Some(new_supply));
|
||||||
Self::deposit_event(Event::GenesisLiquidityAdded { by: account.into(), balance });
|
Self::deposit_event(Event::GenesisLiquidityAdded { by: account.into(), balance });
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the number of blocks since the coin's network reached economic security first time.
|
/// Returns the number of blocks since the all networks reached economic security first time.
|
||||||
/// If the network is yet to be reached that threshold, 0 is returned. And maximum of
|
/// If networks is yet to be reached that threshold, None is returned.
|
||||||
/// `GENESIS_SRI_TRICKLE_FEED` returned.
|
fn blocks_since_ec_security() -> Option<u64> {
|
||||||
fn blocks_since_ec_security(coin: &Coin) -> u64 {
|
let mut min = u64::MAX;
|
||||||
let ec_security_block =
|
for n in NETWORKS {
|
||||||
EconomicSecurityReached::<T>::get(coin.network()).saturated_into::<u64>();
|
let ec_security_block = EconomicSecurityReached::<T>::get(n)?.saturated_into::<u64>();
|
||||||
let current = <frame_system::Pallet<T>>::block_number().saturated_into::<u64>();
|
let current = <frame_system::Pallet<T>>::block_number().saturated_into::<u64>();
|
||||||
if ec_security_block > 0 {
|
let diff = current.saturating_sub(ec_security_block);
|
||||||
let diff = current - ec_security_block;
|
min = diff.min(min);
|
||||||
if diff > GENESIS_SRI_TRICKLE_FEED {
|
|
||||||
return GENESIS_SRI_TRICKLE_FEED;
|
|
||||||
}
|
|
||||||
|
|
||||||
return diff;
|
|
||||||
}
|
}
|
||||||
|
Some(min)
|
||||||
0
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn genesis_ended() -> bool {
|
fn genesis_ended() -> bool {
|
||||||
<frame_system::Pallet<T>>::block_number() >= BLOCKS_PER_MONTH.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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -276,11 +269,15 @@ pub mod pallet {
|
|||||||
|
|
||||||
// check we are still in genesis period
|
// check we are still in genesis period
|
||||||
if Self::genesis_ended() {
|
if Self::genesis_ended() {
|
||||||
// check user have enough to remove
|
// see how much liq tokens we have
|
||||||
let existing = LiquidityTokensPerAddress::<T>::get(balance.coin, account).unwrap_or(0);
|
let total_liq_tokens =
|
||||||
if balance.amount.0 > existing {
|
LiquidityTokens::<T>::balance(GENESIS_LIQUIDITY_ACCOUNT.into(), Coin::Serai).0;
|
||||||
Err(Error::<T>::NotEnoughLiquidity)?;
|
|
||||||
}
|
// get how much user wants to remove
|
||||||
|
let user_shares = Liquidity::<T>::get(balance.coin, account).unwrap_or(0);
|
||||||
|
let total_shares = Supply::<T>::get(balance.coin).unwrap_or((0, 0)).0;
|
||||||
|
let user_liq_tokens = Self::mul_div(total_liq_tokens, user_shares, total_shares)?;
|
||||||
|
let amount_to_remove = Self::mul_div(user_liq_tokens, balance.amount.0, GENESIS_LP_SHARES)?;
|
||||||
|
|
||||||
// remove liquidity from pool
|
// remove liquidity from pool
|
||||||
let prev_sri = Coins::<T>::balance(GENESIS_LIQUIDITY_ACCOUNT.into(), Coin::Serai);
|
let prev_sri = Coins::<T>::balance(GENESIS_LIQUIDITY_ACCOUNT.into(), Coin::Serai);
|
||||||
@@ -288,7 +285,7 @@ pub mod pallet {
|
|||||||
Dex::<T>::remove_liquidity(
|
Dex::<T>::remove_liquidity(
|
||||||
origin.clone().into(),
|
origin.clone().into(),
|
||||||
balance.coin,
|
balance.coin,
|
||||||
balance.amount.0,
|
amount_to_remove,
|
||||||
1,
|
1,
|
||||||
1,
|
1,
|
||||||
GENESIS_LIQUIDITY_ACCOUNT.into(),
|
GENESIS_LIQUIDITY_ACCOUNT.into(),
|
||||||
@@ -297,9 +294,10 @@ pub mod pallet {
|
|||||||
let current_coin = Coins::<T>::balance(GENESIS_LIQUIDITY_ACCOUNT.into(), balance.coin);
|
let current_coin = Coins::<T>::balance(GENESIS_LIQUIDITY_ACCOUNT.into(), balance.coin);
|
||||||
|
|
||||||
// burn the SRI if necessary
|
// burn the SRI if necessary
|
||||||
|
// TODO: take into consideration movement between pools.
|
||||||
let mut sri = current_sri.0.saturating_sub(prev_sri.0);
|
let mut sri = current_sri.0.saturating_sub(prev_sri.0);
|
||||||
let distance_to_full_pay =
|
let distance_to_full_pay =
|
||||||
GENESIS_SRI_TRICKLE_FEED - Self::blocks_since_ec_security(&balance.coin);
|
GENESIS_SRI_TRICKLE_FEED.saturating_sub(Self::blocks_since_ec_security().unwrap_or(0));
|
||||||
let burn_sri_amount = sri.saturating_mul(distance_to_full_pay) / GENESIS_SRI_TRICKLE_FEED;
|
let burn_sri_amount = sri.saturating_mul(distance_to_full_pay) / GENESIS_SRI_TRICKLE_FEED;
|
||||||
Coins::<T>::burn(
|
Coins::<T>::burn(
|
||||||
origin.clone().into(),
|
origin.clone().into(),
|
||||||
@@ -321,9 +319,13 @@ pub mod pallet {
|
|||||||
)?;
|
)?;
|
||||||
|
|
||||||
// save
|
// save
|
||||||
let existing = LiquidityTokensPerAddress::<T>::get(balance.coin, account).unwrap_or(0);
|
let new_shares =
|
||||||
let new = existing.checked_sub(balance.amount.0).ok_or(Error::<T>::AmountOverflowed)?;
|
user_shares.checked_sub(amount_to_remove).ok_or(Error::<T>::AmountOverflowed)?;
|
||||||
LiquidityTokensPerAddress::<T>::set(balance.coin, account, Some(new));
|
if new_shares == 0 {
|
||||||
|
Liquidity::<T>::set(balance.coin, account, None);
|
||||||
|
} else {
|
||||||
|
Liquidity::<T>::set(balance.coin, account, Some(new_shares));
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
let existing = Liquidity::<T>::get(balance.coin, account).unwrap_or(0);
|
let existing = Liquidity::<T>::get(balance.coin, account).unwrap_or(0);
|
||||||
if balance.amount.0 > existing || balance.amount.0 == 0 {
|
if balance.amount.0 > existing || balance.amount.0 == 0 {
|
||||||
@@ -345,7 +347,7 @@ pub mod pallet {
|
|||||||
Ok(())
|
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::call_index(1)]
|
||||||
#[pallet::weight((0, DispatchClass::Operational))] // TODO
|
#[pallet::weight((0, DispatchClass::Operational))] // TODO
|
||||||
pub fn set_initial_price(
|
pub fn set_initial_price(
|
||||||
@@ -356,10 +358,10 @@ pub mod pallet {
|
|||||||
ensure_none(origin)?;
|
ensure_none(origin)?;
|
||||||
|
|
||||||
// set the prices
|
// set the prices
|
||||||
Oracle::<T>::set(Coin::Bitcoin, prices.bitcoin);
|
Oracle::<T>::set(Coin::Bitcoin, Some(prices.bitcoin));
|
||||||
Oracle::<T>::set(Coin::Monero, prices.monero);
|
Oracle::<T>::set(Coin::Monero, Some(prices.monero));
|
||||||
Oracle::<T>::set(Coin::Ether, prices.ethereum);
|
Oracle::<T>::set(Coin::Ether, Some(prices.ethereum));
|
||||||
Oracle::<T>::set(Coin::Dai, prices.dai);
|
Oracle::<T>::set(Coin::Dai, Some(prices.dai));
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -371,8 +373,26 @@ pub mod pallet {
|
|||||||
fn validate_unsigned(_: TransactionSource, call: &Self::Call) -> TransactionValidity {
|
fn validate_unsigned(_: TransactionSource, call: &Self::Call) -> TransactionValidity {
|
||||||
match call {
|
match call {
|
||||||
Call::set_initial_price { ref prices, ref signature } => {
|
Call::set_initial_price { ref prices, ref signature } => {
|
||||||
|
// TODO: if this is supposed to be called after a month, serai set won't still be
|
||||||
|
// in the session 0? Ideally this should pull the session from Vs pallet?
|
||||||
let set = ValidatorSet { network: NetworkId::Serai, session: Session(0) };
|
let set = ValidatorSet { network: NetworkId::Serai, session: Session(0) };
|
||||||
let signers = Participants::<T>::get(NetworkId::Serai);
|
let signers = ValidatorSets::<T>::participants_for_latest_decided_set(NetworkId::Serai)
|
||||||
|
.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(0))?;
|
||||||
|
}
|
||||||
|
|
||||||
|
// make sure signers settings the price 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(1))?;
|
||||||
|
}
|
||||||
|
|
||||||
if !musig_key(set, &signers).verify(&set_initial_price_message(&set, prices), signature) {
|
if !musig_key(set, &signers).verify(&set_initial_price_message(&set, prices), signature) {
|
||||||
Err(InvalidTransaction::BadProof)?;
|
Err(InvalidTransaction::BadProof)?;
|
||||||
|
|||||||
@@ -18,14 +18,7 @@ use scale_info::TypeInfo;
|
|||||||
use serai_primitives::*;
|
use serai_primitives::*;
|
||||||
use validator_sets_primitives::ValidatorSet;
|
use validator_sets_primitives::ValidatorSet;
|
||||||
|
|
||||||
// amount of blocks in 30 days for 6s per block.
|
pub const GENESIS_LP_SHARES: u64 = 10_000;
|
||||||
pub const BLOCKS_PER_MONTH: u32 = 10 * 60 * 24 * 30;
|
|
||||||
|
|
||||||
/// 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);
|
|
||||||
|
|
||||||
// This is the account to hold and manage the genesis liquidity.
|
// 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"Genesis-liquidity-account");
|
||||||
|
|||||||
@@ -8,7 +8,6 @@ use sc_service::ChainType;
|
|||||||
use serai_runtime::{
|
use serai_runtime::{
|
||||||
primitives::*, WASM_BINARY, BABE_GENESIS_EPOCH_CONFIG, RuntimeGenesisConfig, SystemConfig,
|
primitives::*, WASM_BINARY, BABE_GENESIS_EPOCH_CONFIG, RuntimeGenesisConfig, SystemConfig,
|
||||||
CoinsConfig, DexConfig, ValidatorSetsConfig, SignalsConfig, BabeConfig, GrandpaConfig,
|
CoinsConfig, DexConfig, ValidatorSetsConfig, SignalsConfig, BabeConfig, GrandpaConfig,
|
||||||
GenesisLiquidityConfig,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
pub type ChainSpec = sc_service::GenericChainSpec<RuntimeGenesisConfig>;
|
pub type ChainSpec = sc_service::GenericChainSpec<RuntimeGenesisConfig>;
|
||||||
@@ -64,7 +63,6 @@ fn devnet_genesis(
|
|||||||
.collect(),
|
.collect(),
|
||||||
participants: validators.clone(),
|
participants: validators.clone(),
|
||||||
},
|
},
|
||||||
genesis_liquidity: GenesisLiquidityConfig { participants: validators.clone() },
|
|
||||||
signals: SignalsConfig::default(),
|
signals: SignalsConfig::default(),
|
||||||
babe: BabeConfig {
|
babe: BabeConfig {
|
||||||
authorities: validators.iter().map(|validator| ((*validator).into(), 1)).collect(),
|
authorities: validators.iter().map(|validator| ((*validator).into(), 1)).collect(),
|
||||||
@@ -116,7 +114,6 @@ fn testnet_genesis(wasm_binary: &[u8], validators: Vec<&'static str>) -> Runtime
|
|||||||
.collect(),
|
.collect(),
|
||||||
participants: validators.clone(),
|
participants: validators.clone(),
|
||||||
},
|
},
|
||||||
genesis_liquidity: GenesisLiquidityConfig { participants: validators.clone() },
|
|
||||||
signals: SignalsConfig::default(),
|
signals: SignalsConfig::default(),
|
||||||
babe: BabeConfig {
|
babe: BabeConfig {
|
||||||
authorities: validators.iter().map(|validator| ((*validator).into(), 1)).collect(),
|
authorities: validators.iter().map(|validator| ((*validator).into(), 1)).collect(),
|
||||||
|
|||||||
29
substrate/primitives/src/constants.rs
Normal file
29
substrate/primitives/src/constants.rs
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
use crate::BlockNumber;
|
||||||
|
|
||||||
|
// 1 MB
|
||||||
|
pub const BLOCK_SIZE: u32 = 1024 * 1024;
|
||||||
|
// 6 seconds
|
||||||
|
pub const TARGET_BLOCK_TIME: u64 = 6;
|
||||||
|
|
||||||
|
/// Measured in blocks.
|
||||||
|
pub const MINUTES: BlockNumber = 60 / TARGET_BLOCK_TIME;
|
||||||
|
pub const HOURS: BlockNumber = MINUTES * 60;
|
||||||
|
pub const DAYS: BlockNumber = HOURS * 24;
|
||||||
|
pub const WEEKS: BlockNumber = DAYS * 7;
|
||||||
|
pub const MONTHS: BlockNumber = WEEKS * 4;
|
||||||
|
|
||||||
|
/// 6 months of blocks
|
||||||
|
pub const GENESIS_SRI_TRICKLE_FEED: u64 = MONTHS * 6;
|
||||||
|
|
||||||
|
// 100 Million SRI
|
||||||
|
pub const GENESIS_SRI: u64 = 100_000_000 * 10_u64.pow(8);
|
||||||
|
|
||||||
|
/// This needs to be long enough for arbitrage to occur and make holding any fake price up
|
||||||
|
/// sufficiently unrealistic.
|
||||||
|
#[allow(clippy::cast_possible_truncation)]
|
||||||
|
pub const ARBITRAGE_TIME: u16 = (2 * HOURS) as u16;
|
||||||
|
|
||||||
|
/// Since we use the median price, double the window length.
|
||||||
|
///
|
||||||
|
/// We additionally +1 so there is a true median.
|
||||||
|
pub const MEDIAN_PRICE_WINDOW_LENGTH: u16 = (2 * ARBITRAGE_TIME) + 1;
|
||||||
@@ -37,6 +37,9 @@ pub use balance::*;
|
|||||||
mod account;
|
mod account;
|
||||||
pub use account::*;
|
pub use account::*;
|
||||||
|
|
||||||
|
mod constants;
|
||||||
|
pub use constants::*;
|
||||||
|
|
||||||
pub type BlockNumber = u64;
|
pub type BlockNumber = u64;
|
||||||
pub type Header = sp_runtime::generic::Header<BlockNumber, sp_runtime::traits::BlakeTwo256>;
|
pub type Header = sp_runtime::generic::Header<BlockNumber, sp_runtime::traits::BlakeTwo256>;
|
||||||
|
|
||||||
|
|||||||
@@ -30,10 +30,10 @@ impl From<Call> for RuntimeCall {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
Call::LiquidityTokens(lt) => match lt {
|
Call::LiquidityTokens(lt) => match lt {
|
||||||
serai_abi::coins::LiquidityTokensCall::transfer { to, balance } => {
|
serai_abi::liquidity_tokens::Call::transfer { to, balance } => {
|
||||||
RuntimeCall::LiquidityTokens(coins::Call::transfer { to: to.into(), balance })
|
RuntimeCall::LiquidityTokens(coins::Call::transfer { to: to.into(), balance })
|
||||||
}
|
}
|
||||||
serai_abi::coins::LiquidityTokensCall::burn { balance } => {
|
serai_abi::liquidity_tokens::Call::burn { balance } => {
|
||||||
RuntimeCall::LiquidityTokens(coins::Call::burn { balance })
|
RuntimeCall::LiquidityTokens(coins::Call::burn { balance })
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@@ -220,9 +220,9 @@ impl TryInto<Call> for RuntimeCall {
|
|||||||
}),
|
}),
|
||||||
RuntimeCall::LiquidityTokens(call) => Call::LiquidityTokens(match call {
|
RuntimeCall::LiquidityTokens(call) => Call::LiquidityTokens(match call {
|
||||||
coins::Call::transfer { to, balance } => {
|
coins::Call::transfer { to, balance } => {
|
||||||
serai_abi::coins::LiquidityTokensCall::transfer { to: to.into(), balance }
|
serai_abi::liquidity_tokens::Call::transfer { to: to.into(), balance }
|
||||||
}
|
}
|
||||||
coins::Call::burn { balance } => serai_abi::coins::LiquidityTokensCall::burn { balance },
|
coins::Call::burn { balance } => serai_abi::liquidity_tokens::Call::burn { balance },
|
||||||
_ => Err(())?,
|
_ => Err(())?,
|
||||||
}),
|
}),
|
||||||
RuntimeCall::Dex(call) => Call::Dex(match call {
|
RuntimeCall::Dex(call) => Call::Dex(match call {
|
||||||
|
|||||||
@@ -48,7 +48,11 @@ use sp_runtime::{
|
|||||||
BoundedVec, Perbill, ApplyExtrinsicResult,
|
BoundedVec, Perbill, ApplyExtrinsicResult,
|
||||||
};
|
};
|
||||||
|
|
||||||
use primitives::{NetworkId, PublicKey, AccountLookup, SubstrateAmount, Coin, NETWORKS};
|
#[allow(unused_imports)]
|
||||||
|
use primitives::{
|
||||||
|
NetworkId, PublicKey, AccountLookup, SubstrateAmount, Coin, NETWORKS, MEDIAN_PRICE_WINDOW_LENGTH,
|
||||||
|
HOURS, DAYS, MINUTES, TARGET_BLOCK_TIME, BLOCK_SIZE,
|
||||||
|
};
|
||||||
|
|
||||||
use support::{
|
use support::{
|
||||||
traits::{ConstU8, ConstU16, ConstU32, ConstU64, Contains},
|
traits::{ConstU8, ConstU16, ConstU32, ConstU64, Contains},
|
||||||
@@ -115,28 +119,7 @@ pub fn native_version() -> NativeVersion {
|
|||||||
NativeVersion { runtime_version: VERSION, can_author_with: Default::default() }
|
NativeVersion { runtime_version: VERSION, can_author_with: Default::default() }
|
||||||
}
|
}
|
||||||
|
|
||||||
// 1 MB
|
|
||||||
pub const BLOCK_SIZE: u32 = 1024 * 1024;
|
|
||||||
// 6 seconds
|
|
||||||
pub const TARGET_BLOCK_TIME: u64 = 6;
|
|
||||||
|
|
||||||
/// Measured in blocks.
|
|
||||||
pub const MINUTES: BlockNumber = 60 / TARGET_BLOCK_TIME;
|
|
||||||
pub const HOURS: BlockNumber = MINUTES * 60;
|
|
||||||
pub const DAYS: BlockNumber = HOURS * 24;
|
|
||||||
|
|
||||||
pub const PRIMARY_PROBABILITY: (u64, u64) = (1, 4);
|
pub const PRIMARY_PROBABILITY: (u64, u64) = (1, 4);
|
||||||
|
|
||||||
/// This needs to be long enough for arbitrage to occur and make holding any fake price up
|
|
||||||
/// sufficiently unrealistic.
|
|
||||||
#[allow(clippy::cast_possible_truncation)]
|
|
||||||
pub const ARBITRAGE_TIME: u16 = (2 * HOURS) as u16;
|
|
||||||
|
|
||||||
/// Since we use the median price, double the window length.
|
|
||||||
///
|
|
||||||
/// We additionally +1 so there is a true median.
|
|
||||||
pub const MEDIAN_PRICE_WINDOW_LENGTH: u16 = (2 * ARBITRAGE_TIME) + 1;
|
|
||||||
|
|
||||||
pub const BABE_GENESIS_EPOCH_CONFIG: sp_consensus_babe::BabeEpochConfiguration =
|
pub const BABE_GENESIS_EPOCH_CONFIG: sp_consensus_babe::BabeEpochConfiguration =
|
||||||
sp_consensus_babe::BabeEpochConfiguration {
|
sp_consensus_babe::BabeEpochConfiguration {
|
||||||
c: PRIMARY_PROBABILITY,
|
c: PRIMARY_PROBABILITY,
|
||||||
@@ -295,7 +278,7 @@ pub type ReportLongevity = <Runtime as pallet_babe::Config>::EpochDuration;
|
|||||||
|
|
||||||
impl babe::Config for Runtime {
|
impl babe::Config for Runtime {
|
||||||
#[cfg(feature = "fast-epoch")]
|
#[cfg(feature = "fast-epoch")]
|
||||||
type EpochDuration = ConstU64<{ HOURS / 2 }>; // 30 minutes
|
type EpochDuration = ConstU64<{ MINUTES / 2 }>; // 30 seconds
|
||||||
|
|
||||||
#[cfg(not(feature = "fast-epoch"))]
|
#[cfg(not(feature = "fast-epoch"))]
|
||||||
type EpochDuration = ConstU64<{ 4 * 7 * DAYS }>;
|
type EpochDuration = ConstU64<{ 4 * 7 * DAYS }>;
|
||||||
|
|||||||
Reference in New Issue
Block a user