Merge branch 'emissions' of https://github.com/akildemir/serai into block-emissions

This commit is contained in:
akildemir
2024-05-03 13:50:52 +03:00
157 changed files with 8806 additions and 2349 deletions

View File

@@ -6,7 +6,7 @@ license = "MIT"
repository = "https://github.com/serai-dex/serai/tree/develop/substrate/abi"
authors = ["Luke Parker <lukeparker5132@gmail.com>"]
edition = "2021"
rust-version = "1.69"
rust-version = "1.74"
[package.metadata.docs.rs]
all-features = true
@@ -31,6 +31,7 @@ sp-consensus-grandpa = { git = "https://github.com/serai-dex/substrate" }
serai-primitives = { path = "../primitives", version = "0.1" }
serai-coins-primitives = { path = "../coins/primitives", version = "0.1" }
serai-validator-sets-primitives = { path = "../validator-sets/primitives", version = "0.1" }
serai-genesis-liquidity-primitives = { path = "../genesis-liquidity/primitives", version = "0.1" }
serai-in-instructions-primitives = { path = "../in-instructions/primitives", version = "0.1" }
serai-signals-primitives = { path = "../signals/primitives", version = "0.1" }
@@ -42,6 +43,7 @@ borsh = [
"serai-primitives/borsh",
"serai-coins-primitives/borsh",
"serai-validator-sets-primitives/borsh",
"serai-genesis-liquidity-primitives/borsh",
"serai-in-instructions-primitives/borsh",
"serai-signals-primitives/borsh",
]
@@ -50,6 +52,7 @@ serde = [
"serai-primitives/serde",
"serai-coins-primitives/serde",
"serai-validator-sets-primitives/serde",
"serai-genesis-liquidity-primitives/serde",
"serai-in-instructions-primitives/serde",
"serai-signals-primitives/serde",
]

View File

@@ -1,11 +1,13 @@
pub use serai_genesis_liquidity_primitives as primitives;
use serai_primitives::*;
use primitives::*;
#[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, serde::Deserialize))]
pub enum Call {
// This call is just a place holder so that abi works as expected.
empty_call,
remove_coin_liquidity { balance: Balance },
set_initial_price { prices: Prices, signature: Signature },
}
#[derive(Clone, PartialEq, Eq, Debug, scale::Encode, scale::Decode, scale_info::TypeInfo)]

View File

@@ -0,0 +1,18 @@
use serai_primitives::{Balance, SeraiAddress};
#[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, serde::Deserialize))]
pub enum Call {
burn { balance: Balance },
transfer { to: SeraiAddress, balance: Balance },
}
#[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, serde::Deserialize))]
pub enum Event {
Mint { to: SeraiAddress, balance: Balance },
Burn { from: SeraiAddress, balance: Balance },
Transfer { from: SeraiAddress, to: SeraiAddress, balance: Balance },
}

View File

@@ -1,4 +1,4 @@
use core::str::FromStr;
use core::{str::FromStr, fmt};
use scale::{Encode, Decode};
@@ -35,9 +35,9 @@ impl FromStr for Address {
}
}
impl ToString for Address {
fn to_string(&self) -> String {
self.0.to_string()
impl fmt::Display for Address {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt(f)
}
}

View File

@@ -1,4 +1,4 @@
use core::str::FromStr;
use core::{str::FromStr, fmt};
use scale::{Encode, Decode};
@@ -24,9 +24,9 @@ impl FromStr for Address {
}
}
impl ToString for Address {
fn to_string(&self) -> String {
self.0.to_string()
impl fmt::Display for Address {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt(f)
}
}

View File

@@ -1,7 +1,9 @@
use sp_core::bounded_vec::BoundedVec;
use serai_abi::primitives::{SeraiAddress, Amount, Coin};
use crate::{SeraiError, TemporalSerai};
use scale::{decode_from_bytes, Encode};
use crate::{SeraiError, hex_decode, TemporalSerai};
pub type DexEvent = serai_abi::dex::Event;
@@ -57,4 +59,21 @@ impl<'a> SeraiDex<'a> {
send_to: address,
})
}
pub async fn get_reserves(
&self,
coin1: Coin,
coin2: Coin,
) -> Result<Option<(Amount, Amount)>, SeraiError> {
let hash = self
.0
.serai
.call("state_call", ["DexApi_get_reserves".to_string(), hex::encode((coin1, coin2).encode())])
.await?;
let bytes = hex_decode(hash)
.map_err(|_| SeraiError::InvalidNode("expected hex from node wasn't hex".to_string()))?;
let resut = decode_from_bytes::<Option<(u64, u64)>>(bytes.into())
.map_err(|e| SeraiError::ErrorInResponse(e.to_string()))?;
Ok(resut.map(|amounts| (Amount(amounts.0), Amount(amounts.1))))
}
}

View File

@@ -0,0 +1,73 @@
pub use serai_abi::genesis_liquidity::primitives;
use primitives::Prices;
use serai_abi::primitives::*;
use sp_core::sr25519::Signature;
use scale::Encode;
use crate::{Serai, SeraiError, TemporalSerai, Transaction};
pub type GenesisLiquidityEvent = serai_abi::genesis_liquidity::Event;
const PALLET: &str = "GenesisLiquidity";
#[derive(Clone, Copy)]
pub struct SeraiGenesisLiquidity<'a>(pub(crate) &'a TemporalSerai<'a>);
impl<'a> SeraiGenesisLiquidity<'a> {
pub async fn events(&self) -> Result<Vec<GenesisLiquidityEvent>, SeraiError> {
self
.0
.events(|event| {
if let serai_abi::Event::GenesisLiquidity(event) = event {
Some(event.clone())
} else {
None
}
})
.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 {
Serai::unsigned(serai_abi::Call::GenesisLiquidity(
serai_abi::genesis_liquidity::Call::set_initial_price { prices, signature },
))
}
pub fn remove_coin_liquidity(balance: Balance) -> serai_abi::Call {
serai_abi::Call::GenesisLiquidity(serai_abi::genesis_liquidity::Call::remove_coin_liquidity {
balance,
})
}
pub async fn liquidity(&self, address: &SeraiAddress, coin: Coin) -> Option<Amount> {
self
.0
.storage(
PALLET,
"Liquidity",
(coin, sp_core::hashing::blake2_128(&address.encode()), &address.0),
)
.await
.unwrap()
}
}

View File

@@ -0,0 +1,41 @@
use scale::Encode;
use serai_abi::primitives::{SeraiAddress, Amount, Coin, Balance};
use crate::{TemporalSerai, SeraiError};
const PALLET: &str = "LiquidityTokens";
#[derive(Clone, Copy)]
pub struct SeraiLiquidityTokens<'a>(pub(crate) &'a TemporalSerai<'a>);
impl<'a> SeraiLiquidityTokens<'a> {
pub async fn token_supply(&self, coin: Coin) -> Result<Amount, SeraiError> {
Ok(self.0.storage(PALLET, "Supply", coin).await?.unwrap_or(Amount(0)))
}
pub async fn token_balance(
&self,
coin: Coin,
address: SeraiAddress,
) -> Result<Amount, SeraiError> {
Ok(
self
.0
.storage(
PALLET,
"Balances",
(sp_core::hashing::blake2_128(&address.encode()), &address.0, coin),
)
.await?
.unwrap_or(Amount(0)),
)
}
pub fn transfer(to: SeraiAddress, balance: Balance) -> serai_abi::Call {
serai_abi::Call::Coins(serai_abi::coins::Call::transfer { to, balance })
}
pub fn burn(balance: Balance) -> serai_abi::Call {
serai_abi::Call::Coins(serai_abi::coins::Call::burn { balance })
}
}

View File

@@ -1,3 +1,4 @@
use hex::FromHexError;
use thiserror::Error;
use async_lock::RwLock;
@@ -26,6 +27,10 @@ pub mod in_instructions;
pub use in_instructions::SeraiInInstructions;
pub mod validator_sets;
pub use validator_sets::SeraiValidatorSets;
pub mod genesis_liquidity;
pub use genesis_liquidity::SeraiGenesisLiquidity;
pub mod liquidity_tokens;
pub use liquidity_tokens::SeraiLiquidityTokens;
#[derive(Clone, PartialEq, Eq, Debug, scale::Encode, scale::Decode)]
pub struct Block {
@@ -82,6 +87,14 @@ 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 {
pub async fn call<Req: Serialize, Res: DeserializeOwned>(
&self,
@@ -134,19 +147,11 @@ 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> {
let hash: Option<String> = self.call("chain_getBlockHash", [number]).await?;
let Some(hash) = hash else { return Ok(None) };
Self::hex_decode(hash)?
hex_decode(hash)
.map_err(|_| SeraiError::InvalidNode("expected hex from node wasn't hex".to_string()))?
.try_into()
.map_err(|_| SeraiError::InvalidNode("didn't respond to getBlockHash with hash".to_string()))
.map(Some)
@@ -195,11 +200,13 @@ impl Serai {
Ok(())
}
// TODO: move this into substrate/client/src/validator_sets.rs
async fn active_network_validators(&self, network: NetworkId) -> Result<Vec<Public>, SeraiError> {
let hash: String = self
.call("state_call", ["SeraiRuntimeApi_validators".to_string(), hex::encode(network.encode())])
.await?;
let bytes = Self::hex_decode(hash)?;
let bytes = hex_decode(hash)
.map_err(|_| SeraiError::InvalidNode("expected hex from node wasn't hex".to_string()))?;
let r = Vec::<Public>::decode(&mut bytes.as_slice())
.map_err(|e| SeraiError::ErrorInResponse(e.to_string()))?;
Ok(r)
@@ -207,9 +214,12 @@ impl Serai {
pub async fn latest_finalized_block_hash(&self) -> Result<[u8; 32], SeraiError> {
let hash: String = self.call("chain_getFinalizedHead", ()).await?;
Self::hex_decode(hash)?.try_into().map_err(|_| {
SeraiError::InvalidNode("didn't respond to getFinalizedHead with hash".to_string())
})
hex_decode(hash)
.map_err(|_| SeraiError::InvalidNode("expected hex from node wasn't hex".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> {
@@ -219,7 +229,7 @@ impl Serai {
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 Some(block) = block else { return Ok(None) };
let Ok(bytes) = Self::hex_decode(block) else {
let Ok(bytes) = hex_decode(block) else {
Err(SeraiError::InvalidNode("didn't return a hex-encoded block".to_string()))?
};
let Ok(block) = Block::decode(&mut bytes.as_slice()) else {
@@ -365,7 +375,8 @@ impl<'a> TemporalSerai<'a> {
let res: Option<String> =
self.serai.call("state_getStorage", [hex::encode(full_key), hex::encode(self.block)]).await?;
let Some(res) = res else { return Ok(None) };
let res = Serai::hex_decode(res)?;
let res = 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(|_| {
SeraiError::InvalidRuntime("different type present at storage location".to_string())
})?))
@@ -386,4 +397,12 @@ impl<'a> TemporalSerai<'a> {
pub fn validator_sets(&'a self) -> SeraiValidatorSets<'a> {
SeraiValidatorSets(self)
}
pub fn genesis_liquidity(&'a self) -> SeraiGenesisLiquidity {
SeraiGenesisLiquidity(self)
}
pub fn liquidity_tokens(&'a self) -> SeraiLiquidityTokens {
SeraiLiquidityTokens(self)
}
}

View File

@@ -66,3 +66,67 @@ macro_rules! serai_test {
)*
}
}
#[macro_export]
macro_rules! serai_test_fast_epoch {
($($name: ident: $test: expr)*) => {
$(
#[tokio::test]
async fn $name() {
use std::collections::HashMap;
use dockertest::{
PullPolicy, StartPolicy, LogOptions, LogAction, LogPolicy, LogSource, Image,
TestBodySpecification, DockerTest,
};
serai_docker_tests::build("serai-fast-epoch".to_string());
let handle = concat!("serai_client-serai_node-", stringify!($name));
let composition = TestBodySpecification::with_image(
Image::with_repository("serai-dev-serai-fast-epoch").pull_policy(PullPolicy::Never),
)
.replace_cmd(vec![
"serai-node".to_string(),
"--dev".to_string(),
"--unsafe-rpc-external".to_string(),
"--rpc-cors".to_string(),
"all".to_string(),
])
.replace_env(
HashMap::from([
("RUST_LOG".to_string(), "runtime=debug".to_string()),
("KEY".to_string(), " ".to_string()),
])
)
.set_publish_all_ports(true)
.set_handle(handle)
.set_start_policy(StartPolicy::Strict)
.set_log_options(Some(LogOptions {
action: LogAction::Forward,
policy: LogPolicy::Always,
source: LogSource::Both,
}));
let mut test = DockerTest::new().with_network(dockertest::Network::Isolated);
test.provide_container(composition);
test.run_async(|ops| async move {
// Sleep until the Substrate RPC starts
let serai_rpc = ops.handle(handle).host_port(9944).unwrap();
let serai_rpc = format!("http://{}:{}", serai_rpc.0, serai_rpc.1);
// Bound execution to 60 seconds
for _ in 0 .. 60 {
tokio::time::sleep(core::time::Duration::from_secs(1)).await;
let Ok(client) = Serai::new(serai_rpc.clone()).await else { continue };
if client.latest_finalized_block_hash().await.is_err() {
continue;
}
break;
}
#[allow(clippy::redundant_closure_call)]
$test(Serai::new(serai_rpc).await.unwrap()).await;
}).await;
}
)*
}
}

View File

@@ -0,0 +1,232 @@
use std::{time::Duration, collections::HashMap};
use rand_core::{RngCore, OsRng};
use zeroize::Zeroizing;
use ciphersuite::{Ciphersuite, Ristretto};
use frost::dkg::musig::musig;
use schnorrkel::Schnorrkel;
use serai_client::{
genesis_liquidity::{
primitives::{GENESIS_LIQUIDITY_ACCOUNT, GENESIS_SRI},
SeraiGenesisLiquidity,
},
validator_sets::primitives::{musig_context, Session, ValidatorSet},
};
use serai_abi::{
genesis_liquidity::primitives::{set_initial_price_message, Prices},
primitives::COINS,
};
use sp_core::{sr25519::Signature, Pair as PairTrait};
use serai_client::{
primitives::{
Amount, NetworkId, Coin, Balance, BlockHash, SeraiAddress, insecure_pair_from_name,
},
in_instructions::primitives::{InInstruction, InInstructionWithBalance, Batch},
Serai,
};
mod common;
use common::{in_instructions::provide_batch, tx::publish_tx};
serai_test_fast_epoch!(
genesis_liquidity: (|serai: Serai| async move {
test_genesis_liquidity(serai).await;
})
);
async fn test_genesis_liquidity(serai: Serai) {
// amounts
let amounts = vec![
Amount(5_53246991),
Amount(3_14562819),
Amount(9_33648912),
Amount(150_873639000000),
Amount(248_665228000000),
Amount(451_765529000000),
];
// addresses
let mut btc_addresses = vec![];
let mut xmr_addresses = vec![];
let addr_count = amounts.len();
for (i, amount) in amounts.into_iter().enumerate() {
let mut address = SeraiAddress::new([0; 32]);
OsRng.fill_bytes(&mut address.0);
if i < addr_count / 2 {
btc_addresses.push((address, amount));
} else {
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
let mut block_hash = BlockHash([0; 32]);
OsRng.fill_bytes(&mut block_hash.0);
let btc_ins = btc_addresses
.iter()
.map(|(addr, amount)| InInstructionWithBalance {
instruction: InInstruction::GenesisLiquidity(*addr),
balance: Balance { coin: Coin::Bitcoin, amount: *amount },
})
.collect::<Vec<_>>();
let batch =
Batch { network: NetworkId::Bitcoin, id: 0, block: block_hash, instructions: btc_ins };
provide_batch(&serai, batch).await;
// xmr batch
let mut block_hash = BlockHash([0; 32]);
OsRng.fill_bytes(&mut block_hash.0);
let xmr_ins = xmr_addresses
.iter()
.map(|(addr, amount)| InInstructionWithBalance {
instruction: InInstruction::GenesisLiquidity(*addr),
balance: Balance { coin: Coin::Monero, amount: *amount },
})
.collect::<Vec<_>>();
let batch = Batch { network: NetworkId::Monero, id: 0, block: block_hash, instructions: xmr_ins };
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..
tokio::time::timeout(tokio::time::Duration::from_secs(300), async {
while serai.latest_finalized_block().await.unwrap().number() < 25 {
tokio::time::sleep(Duration::from_secs(6)).await;
}
})
.await
.unwrap();
// check total SRI supply is +100M
let last_block = serai.latest_finalized_block().await.unwrap().hash();
let serai = serai.as_of(last_block);
// Check balance instead of supply
let sri = serai.coins().coin_supply(Coin::Serai).await.unwrap();
// there are 6 endowed accounts in dev-net. Take this into consideration when checking
// for the total sri minted at this time.
let endowed_amount: u64 = 1 << 60;
let total_sri = (6 * endowed_amount) + GENESIS_SRI;
assert_eq!(sri, Amount(total_sri));
// check genesis account has no coins, all transferred to pools.
for coin in COINS {
let amount = serai.coins().coin_balance(coin, GENESIS_LIQUIDITY_ACCOUNT).await.unwrap();
assert_eq!(amount.0, 0);
}
// check pools has proper liquidity
let pool_btc = btc_addresses.iter().fold(0u128, |acc, value| acc + u128::from(value.1 .0));
let pool_xmr = xmr_addresses.iter().fold(0u128, |acc, value| acc + u128::from(value.1 .0));
let pool_btc_value = (pool_btc * u128::from(prices.bitcoin)) / 10u128.pow(8);
let pool_xmr_value = (pool_xmr * u128::from(prices.monero)) / 10u128.pow(12);
let total_value = pool_btc_value + pool_xmr_value;
// calculated distributed SRI. We know that xmr is at the end of COINS array
// so it will be approximated to roof instead of floor after integer division.
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 btc_reserves = serai.dex().get_reserves(Coin::Bitcoin, Coin::Serai).await.unwrap().unwrap();
assert_eq!(u128::from(btc_reserves.0 .0), pool_btc);
assert_eq!(u128::from(btc_reserves.1 .0), btc_sri);
let xmr_reserves = serai.dex().get_reserves(Coin::Monero, Coin::Serai).await.unwrap().unwrap();
assert_eq!(u128::from(xmr_reserves.0 .0), pool_xmr);
assert_eq!(u128::from(xmr_reserves.1 .0), xmr_sri);
// check each btc liq provider got liq tokens proportional to their value
let btc_liq_token_supply = u128::from(
serai
.liquidity_tokens()
.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 =
serai.genesis_liquidity().liquidity_tokens(addr, Coin::Bitcoin).await.unwrap();
assert_eq!(addr_liq_tokens, addr_actual_token_amount.0.into());
total_tokens_this_coin += addr_liq_tokens;
}
// check each xmr liq provider got liq tokens proportional to their value
let xmr_liq_token_supply = u128::from(
serai
.liquidity_tokens()
.token_balance(Coin::Monero, GENESIS_LIQUIDITY_ACCOUNT)
.await
.unwrap()
.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.
}
async fn set_prices(serai: &Serai, prices: &Prices) {
// prepare a Musig tx to set the initial prices
let pair = insecure_pair_from_name("Alice");
let public = pair.public();
let set = ValidatorSet { session: Session(0), network: NetworkId::Serai };
let public_key = <Ristretto as Ciphersuite>::read_G::<&[u8]>(&mut public.0.as_ref()).unwrap();
let secret_key = <Ristretto as Ciphersuite>::read_F::<&[u8]>(
&mut pair.as_ref().secret.to_bytes()[.. 32].as_ref(),
)
.unwrap();
assert_eq!(Ristretto::generator() * secret_key, public_key);
let threshold_keys =
musig::<Ristretto>(&musig_context(set), &Zeroizing::new(secret_key), &[public_key]).unwrap();
let sig = frost::tests::sign_without_caching(
&mut OsRng,
frost::tests::algorithm_machines(
&mut OsRng,
&Schnorrkel::new(b"substrate"),
&HashMap::from([(threshold_keys.params().i(), threshold_keys.into())]),
),
&set_initial_price_message(&set, prices),
);
// set initial prices
let _ = publish_tx(
serai,
&SeraiGenesisLiquidity::set_initial_price(*prices, Signature(sig.to_bytes())),
)
.await;
}

View File

@@ -14,7 +14,7 @@ use serai_client::{
mod common;
use common::validator_sets::{set_keys, allocate_stake, deallocate_stake};
const EPOCH_INTERVAL: u64 = 5;
const EPOCH_INTERVAL: u64 = 300;
serai_test!(
set_keys_test: (|serai: Serai| async move {

View File

@@ -6,7 +6,7 @@ license = "AGPL-3.0-only"
repository = "https://github.com/serai-dex/serai/tree/develop/substrate/coins/pallet"
authors = ["Akil Demir <aeg_asd@hotmail.com>"]
edition = "2021"
rust-version = "1.70"
rust-version = "1.74"
[package.metadata.docs.rs]
all-features = true

View File

@@ -5,7 +5,7 @@ description = "Serai coins primitives"
license = "MIT"
authors = ["Luke Parker <lukeparker5132@gmail.com>"]
edition = "2021"
rust-version = "1.69"
rust-version = "1.74"
[package.metadata.docs.rs]
all-features = true

View File

@@ -6,7 +6,7 @@ license = "AGPL-3.0-only"
repository = "https://github.com/serai-dex/serai/tree/develop/substrate/dex/pallet"
authors = ["Parity Technologies <admin@parity.io>, Akil Demir <aeg_asd@hotmail.com>"]
edition = "2021"
rust-version = "1.70"
rust-version = "1.74"
[package.metadata.docs.rs]
all-features = true

View File

@@ -43,7 +43,7 @@ fn create_coin<T: Config>(coin: &Coin) -> (T::AccountId, AccountIdLookupOf<T>) {
let caller_lookup = T::Lookup::unlookup(caller);
assert_ok!(Coins::<T>::mint(
caller,
Balance { coin: Coin::native(), amount: Amount(SubstrateAmount::max_value().div(1000u64)) }
Balance { coin: Coin::native(), amount: Amount(SubstrateAmount::MAX.div(1000u64)) }
));
assert_ok!(Coins::<T>::mint(
caller,

View File

@@ -27,12 +27,15 @@ frame-system = { git = "https://github.com/serai-dex/substrate", default-feature
frame-support = { git = "https://github.com/serai-dex/substrate", default-features = false }
sp-std = { git = "https://github.com/serai-dex/substrate", default-features = false }
sp-core = { git = "https://github.com/serai-dex/substrate", default-features = false }
sp-application-crypto = { git = "https://github.com/serai-dex/substrate", 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 }
serai-primitives = { path = "../../primitives", default-features = false }
genesis-liquidity-primitives = { package = "serai-genesis-liquidity-primitives", path = "../primitives", default-features = false }
validator-sets-primitives = { package = "serai-validator-sets-primitives", path = "../../validator-sets/primitives", default-features = false }
[features]
std = [
@@ -43,12 +46,16 @@ std = [
"frame-support/std",
"sp-std/std",
"sp-core/std",
"sp-application-crypto/std",
"coins-pallet/std",
"dex-pallet/std",
"serai-primitives/std",
"genesis-liquidity-primitives/std",
"validator-sets-primitives/std",
]
fast-epoch = []
default = ["std"]
default = ["std"]

View File

@@ -5,17 +5,20 @@
pub mod pallet {
use super::*;
use frame_system::{pallet_prelude::*, RawOrigin};
use frame_support::{pallet_prelude::*, sp_runtime::SaturatedConversion};
use sp_std::{vec, collections::btree_map::BTreeMap};
use dex_pallet::{Pallet as Dex, Config as DexConfig};
use coins_pallet::{
primitives::{OutInstructionWithBalance, OutInstruction},
Config as CoinsConfig, Pallet as Coins, AllowMint,
use frame_support::{
pallet_prelude::*,
sp_runtime::{self, SaturatedConversion},
};
use sp_std::{vec, vec::Vec, collections::btree_map::BTreeMap};
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 serai_primitives::*;
use validator_sets_primitives::{ValidatorSet, Session, musig_key};
pub use genesis_liquidity_primitives as primitives;
use primitives::*;
@@ -29,6 +32,19 @@ pub mod pallet {
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,
@@ -61,11 +77,31 @@ pub mod pallet {
pub(crate) type EconomicSecurityReached<T: Config> =
StorageMap<_, Identity, NetworkId, BlockNumberFor<T>, ValueQuery>;
#[pallet::storage]
pub(crate) type Participants<T: Config> =
StorageMap<_, Identity, NetworkId, BoundedVec<PublicKey, ConstU32<150>>, ValueQuery>;
#[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());
}
}
#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
fn on_finalize(n: BlockNumberFor<T>) {
#[cfg(feature = "fast-epoch")]
let final_block = 25u32;
#[cfg(not(feature = "fast-epoch"))]
let final_block = BLOCKS_PER_MONTH;
// Distribute the genesis sri to pools after a month
if n == BLOCKS_PER_MONTH.into() {
if n == final_block.into() {
// mint the SRI
Coins::<T>::mint(
GENESIS_LIQUIDITY_ACCOUNT.into(),
@@ -75,57 +111,97 @@ pub mod pallet {
// get coin values & total
let mut account_values = BTreeMap::new();
let mut pool_values = BTreeMap::new();
let mut total_value: u64 = 0;
let mut pool_values = vec![];
let mut total_value: u128 = 0;
for coin in COINS {
// TODO: following line is just a place holder till we get the actual coin value
// in terms of btc.
let value = Dex::<T>::security_oracle_value(coin).unwrap_or(Amount(0)).0;
account_values.insert(coin, vec![]);
let mut pool_amount: u64 = 0;
for (account, amount) in Liquidity::<T>::iter_prefix(coin) {
pool_amount = pool_amount.saturating_add(amount);
let value_this_addr = amount.saturating_mul(value);
account_values.get_mut(&coin).unwrap().push((account, value_this_addr))
if coin == Coin::Serai {
continue;
}
let pool_value = pool_amount.saturating_mul(value);
// initial coin value in terms of btc
let value = Oracle::<T>::get(coin);
// 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);
pool_values.insert(coin, (pool_amount, pool_value));
pool_values.push((coin, pool_amount, pool_value));
}
// add the liquidity per pool
for (coin, (amount, value)) in &pool_values {
let sri_amount = GENESIS_SRI.saturating_mul(*value) / total_value;
let mut total_sri_distributed = 0;
let pool_values_len = pool_values.len();
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
} else {
u64::try_from(u128::from(GENESIS_SRI).saturating_mul(pool_value) / total_value).unwrap()
};
total_sri_distributed += sri_amount;
// we can't add 0 liquidity
if !(pool_amount > 0 && sri_amount > 0) {
continue;
}
// actually add the liquidity to dex
let origin = RawOrigin::Signed(GENESIS_LIQUIDITY_ACCOUNT.into());
Dex::<T>::add_liquidity(
origin.into(),
*coin,
*amount,
coin,
u64::try_from(pool_amount).unwrap(),
sri_amount,
*amount,
u64::try_from(pool_amount).unwrap(),
sri_amount,
GENESIS_LIQUIDITY_ACCOUNT.into(),
)
.unwrap();
// let everyone know about the event
Self::deposit_event(Event::GenesisLiquidityAddedToPool {
coin1: Balance { coin: *coin, amount: Amount(*amount) },
coin1: Balance { coin, amount: Amount(u64::try_from(pool_amount).unwrap()) },
coin2: Balance { coin: Coin::Serai, amount: Amount(sri_amount) },
});
// set liquidity tokens per account
let tokens = LiquidityTokens::<T>::balance(GENESIS_LIQUIDITY_ACCOUNT.into(), *coin).0;
let mut total_tokens_this_coin: u64 = 0;
for (acc, value) in account_values.get(coin).unwrap() {
let liq_tokens_this_acc =
tokens.saturating_mul(*value) / pool_values.get(coin).unwrap().1;
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(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);
// we shouldn't have any coin left in our account at this moment, including SRI.
// we shouldn't have left any coin in genesis account at this moment, including SRI.
// All transferred to the pools.
for coin in COINS {
assert_eq!(Coins::<T>::balance(GENESIS_LIQUIDITY_ACCOUNT.into(), coin), Amount(0));
}
@@ -165,12 +241,37 @@ pub mod pallet {
Ok(())
}
/// Remove the provided genesis liquidity for an account. If called pre-economic security era,
pub fn remove_coin_liquidity(
account: PublicKey,
balance: Balance,
out_address: ExternalAddress,
) -> DispatchResult {
/// 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;
}
0
}
fn genesis_ended() -> bool {
<frame_system::Pallet<T>>::block_number() >= BLOCKS_PER_MONTH.into()
}
}
#[pallet::call]
impl<T: Config> Pallet<T> {
/// Remove the provided genesis liquidity for an account.
#[pallet::call_index(0)]
#[pallet::weight((0, DispatchClass::Operational))] // TODO
pub fn remove_coin_liquidity(origin: OriginFor<T>, balance: Balance) -> DispatchResult {
let account = ensure_signed(origin)?;
let origin = RawOrigin::Signed(GENESIS_LIQUIDITY_ACCOUNT.into());
// check we are still in genesis period
@@ -232,13 +333,9 @@ pub mod pallet {
Err(Error::<T>::CanOnlyRemoveFullAmount)?;
}
// TODO: do internal transfer instead?
let origin = RawOrigin::Signed(GENESIS_LIQUIDITY_ACCOUNT.into());
let instruction = OutInstructionWithBalance {
instruction: OutInstruction { address: out_address, data: None },
balance,
};
Coins::<T>::burn_with_instruction(origin.into(), instruction)?;
// 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)?;
// save
Liquidity::<T>::set(balance.coin, account, None);
@@ -248,27 +345,48 @@ pub mod pallet {
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;
}
/// A call to submit the initial coi values.
#[pallet::call_index(1)]
#[pallet::weight((0, DispatchClass::Operational))] // TODO
pub fn set_initial_price(
origin: OriginFor<T>,
prices: Prices,
_signature: Signature,
) -> DispatchResult {
ensure_none(origin)?;
return diff;
}
0
// 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);
Ok(())
}
}
fn genesis_ended() -> bool {
<frame_system::Pallet<T>>::block_number() >= BLOCKS_PER_MONTH.into()
#[pallet::validate_unsigned]
impl<T: Config> ValidateUnsigned for Pallet<T> {
type Call = Call<T>;
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);
if !musig_key(set, &signers).verify(&set_initial_price_message(&set, prices), signature) {
Err(InvalidTransaction::BadProof)?;
}
ValidTransaction::with_tag_prefix("GenesisLiquidity")
.and_provides((0, set))
.longevity(u64::MAX)
.propagate(true)
.build()
}
Call::remove_coin_liquidity { .. } => Err(InvalidTransaction::Call)?,
Call::__Ignore(_, _) => unreachable!(),
}
}
}
}

View File

@@ -16,10 +16,30 @@ rustdoc-args = ["--cfg", "docsrs"]
workspace = true
[dependencies]
zeroize = { version = "^1.5", features = ["derive"], optional = true }
borsh = { version = "1", default-features = false, features = ["derive", "de_strict_order"], optional = true }
serde = { version = "1", default-features = false, features = ["derive", "alloc"], optional = true }
scale = { package = "parity-scale-codec", version = "3", default-features = false, features = ["derive"] }
scale-info = { version = "2", default-features = false, features = ["derive"] }
sp-std = { git = "https://github.com/serai-dex/substrate", default-features = false }
serai-primitives = { path = "../../primitives", default-features = false }
validator-sets-primitives = { package = "serai-validator-sets-primitives", path = "../../validator-sets/primitives", default-features = false }
[features]
std = [
"zeroize",
"scale/std",
"borsh?/std",
"serde?/std",
"scale-info/std",
"serai-primitives/std",
"validator-sets-primitives/std",
"sp-std/std"
]
default = ["std"]

View File

@@ -2,7 +2,21 @@
#![cfg_attr(docsrs, feature(doc_auto_cfg))]
#![cfg_attr(not(feature = "std"), no_std)]
#[cfg(feature = "std")]
use zeroize::Zeroize;
#[cfg(feature = "borsh")]
use borsh::{BorshSerialize, BorshDeserialize};
#[cfg(feature = "serde")]
use serde::{Serialize, Deserialize};
use sp_std::vec::Vec;
use scale::{Encode, Decode, MaxEncodedLen};
use scale_info::TypeInfo;
use serai_primitives::*;
use validator_sets_primitives::ValidatorSet;
// amount of blocks in 30 days for 6s per block.
pub const BLOCKS_PER_MONTH: u32 = 10 * 60 * 24 * 30;
@@ -15,3 +29,19 @@ pub const GENESIS_SRI: u64 = 100_000_000 * 10_u64.pow(8);
// This is the account to hold and manage the genesis liquidity.
pub const GENESIS_LIQUIDITY_ACCOUNT: SeraiAddress = system_address(b"Genesis-liquidity-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 monero: u64,
pub ethereum: 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()
}

View File

@@ -10,7 +10,7 @@ pub use in_instructions_primitives as primitives;
use primitives::*;
// TODO: Investigate why Substrate generates these
#[allow(clippy::cast_possible_truncation, clippy::no_effect_underscore_binding)]
#[allow(clippy::cast_possible_truncation, clippy::no_effect_underscore_binding, clippy::empty_docs)]
#[frame_support::pallet]
pub mod pallet {
use sp_std::vec;
@@ -204,14 +204,9 @@ pub mod pallet {
}
}
}
InInstruction::GenesisLiquidity(ops) => match ops {
GenesisLiquidityOperation::Add(address, balance) => {
GenesisLiq::<T>::add_coin_liquidity(address.into(), balance)?;
}
GenesisLiquidityOperation::Remove(address, balance, out_address) => {
GenesisLiq::<T>::remove_coin_liquidity(address.into(), balance, out_address)?;
}
},
InInstruction::GenesisLiquidity(address) => {
GenesisLiq::<T>::add_coin_liquidity(address.into(), instruction.balance)?;
}
}
Ok(())
}

View File

@@ -5,7 +5,7 @@ description = "Serai instructions library, enabling encoding and decoding"
license = "MIT"
authors = ["Luke Parker <lukeparker5132@gmail.com>"]
edition = "2021"
rust-version = "1.69"
rust-version = "1.74"
[package.metadata.docs.rs]
all-features = true

View File

@@ -71,15 +71,6 @@ pub enum DexCall {
Swap(Balance, OutAddress),
}
#[derive(Clone, 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 enum GenesisLiquidityOperation {
Add(SeraiAddress, Balance),
Remove(SeraiAddress, Balance, ExternalAddress),
}
#[derive(Clone, PartialEq, Eq, Debug, Encode, Decode, MaxEncodedLen, TypeInfo)]
#[cfg_attr(feature = "std", derive(Zeroize))]
#[cfg_attr(feature = "borsh", derive(BorshSerialize, BorshDeserialize))]
@@ -87,7 +78,7 @@ pub enum GenesisLiquidityOperation {
pub enum InInstruction {
Transfer(SeraiAddress),
Dex(DexCall),
GenesisLiquidity(GenesisLiquidityOperation),
GenesisLiquidity(SeraiAddress),
}
#[derive(Clone, PartialEq, Eq, Encode, Decode, MaxEncodedLen, TypeInfo, RuntimeDebug)]

View File

@@ -26,6 +26,8 @@ hex = "0.4"
rand_core = "0.6"
schnorrkel = "0.11"
libp2p = "0.52"
sp-core = { git = "https://github.com/serai-dex/substrate" }
sp-keystore = { git = "https://github.com/serai-dex/substrate" }
sp-timestamp = { git = "https://github.com/serai-dex/substrate" }

View File

@@ -1,13 +1,14 @@
use core::marker::PhantomData;
use std::collections::HashSet;
use sp_core::Pair as PairTrait;
use sp_core::{Decode, Pair as PairTrait, sr25519::Public};
use sc_service::ChainType;
use serai_runtime::{
primitives::*, WASM_BINARY, BABE_GENESIS_EPOCH_CONFIG, RuntimeGenesisConfig, SystemConfig,
CoinsConfig, DexConfig, ValidatorSetsConfig, SignalsConfig, BabeConfig, GrandpaConfig,
EmissionsConfig,
EmissionsConfig, GenesisLiquidityConfig,
};
pub type ChainSpec = sc_service::GenericChainSpec<RuntimeGenesisConfig>;
@@ -24,7 +25,7 @@ fn wasm_binary() -> Vec<u8> {
WASM_BINARY.ok_or("compiled in wasm not available").unwrap().to_vec()
}
fn testnet_genesis(
fn devnet_genesis(
wasm_binary: &[u8],
validators: &[&'static str],
endowed_accounts: Vec<PublicKey>,
@@ -60,6 +61,63 @@ fn testnet_genesis(
.collect(),
participants: validators.clone(),
},
genesis_liquidity: GenesisLiquidityConfig { participants: validators.clone() },
emissions: EmissionsConfig {
networks: serai_runtime::primitives::NETWORKS.to_vec(),
participants: validators.clone(),
},
signals: SignalsConfig::default(),
babe: BabeConfig {
authorities: validators.iter().map(|validator| ((*validator).into(), 1)).collect(),
epoch_config: Some(BABE_GENESIS_EPOCH_CONFIG),
_config: PhantomData,
},
grandpa: GrandpaConfig {
authorities: validators.into_iter().map(|validator| (validator.into(), 1)).collect(),
_config: PhantomData,
},
}
}
fn testnet_genesis(wasm_binary: &[u8], validators: Vec<&'static str>) -> RuntimeGenesisConfig {
let validators = validators
.into_iter()
.map(|validator| Public::decode(&mut hex::decode(validator).unwrap().as_slice()).unwrap())
.collect::<Vec<_>>();
assert_eq!(validators.iter().collect::<HashSet<_>>().len(), validators.len());
RuntimeGenesisConfig {
system: SystemConfig { code: wasm_binary.to_vec(), _config: PhantomData },
transaction_payment: Default::default(),
coins: CoinsConfig {
accounts: validators
.iter()
.map(|a| (*a, Balance { coin: Coin::Serai, amount: Amount(5_000_000 * 10_u64.pow(8)) }))
.collect(),
_ignore: Default::default(),
},
dex: DexConfig {
pools: vec![Coin::Bitcoin, Coin::Ether, Coin::Dai, Coin::Monero],
_ignore: Default::default(),
},
validator_sets: ValidatorSetsConfig {
networks: serai_runtime::primitives::NETWORKS
.iter()
.map(|network| match network {
NetworkId::Serai => (NetworkId::Serai, Amount(50_000 * 10_u64.pow(8))),
NetworkId::Bitcoin => (NetworkId::Bitcoin, Amount(1_000_000 * 10_u64.pow(8))),
NetworkId::Ethereum => (NetworkId::Ethereum, Amount(1_000_000 * 10_u64.pow(8))),
NetworkId::Monero => (NetworkId::Monero, Amount(100_000 * 10_u64.pow(8))),
})
.collect(),
participants: validators.clone(),
},
genesis_liquidity: GenesisLiquidityConfig { participants: validators.clone() },
emissions: EmissionsConfig {
networks: serai_runtime::primitives::NETWORKS.to_vec(),
participants: validators.clone(),
@@ -87,7 +145,7 @@ pub fn development_config() -> ChainSpec {
"devnet",
ChainType::Development,
move || {
testnet_genesis(
devnet_genesis(
&wasm_binary,
&["Alice"],
vec![
@@ -105,7 +163,7 @@ pub fn development_config() -> ChainSpec {
// Telemetry
None,
// Protocol ID
Some("serai"),
Some("serai-devnet"),
// Fork ID
None,
// Properties
@@ -115,7 +173,7 @@ pub fn development_config() -> ChainSpec {
)
}
pub fn testnet_config() -> ChainSpec {
pub fn local_config() -> ChainSpec {
let wasm_binary = wasm_binary();
ChainSpec::from_genesis(
@@ -125,7 +183,7 @@ pub fn testnet_config() -> ChainSpec {
"local",
ChainType::Local,
move || {
testnet_genesis(
devnet_genesis(
&wasm_binary,
&["Alice", "Bob", "Charlie", "Dave"],
vec![
@@ -143,7 +201,7 @@ pub fn testnet_config() -> ChainSpec {
// Telemetry
None,
// Protocol ID
Some("serai"),
Some("serai-local"),
// Fork ID
None,
// Properties
@@ -152,3 +210,39 @@ pub fn testnet_config() -> ChainSpec {
None,
)
}
pub fn testnet_config() -> ChainSpec {
let wasm_binary = wasm_binary();
ChainSpec::from_genesis(
// Name
"Test Network 2",
// ID
"testnet-2",
ChainType::Live,
move || {
let _ = testnet_genesis(&wasm_binary, vec![]);
todo!()
},
// Bootnodes
vec![],
// Telemetry
None,
// Protocol ID
Some("serai-testnet-2"),
// Fork ID
None,
// Properties
None,
// Extensions
None,
)
}
pub fn bootnode_multiaddrs(id: &str) -> Vec<libp2p::Multiaddr> {
match id {
"devnet" | "local" => vec![],
"testnet-2" => todo!(),
_ => panic!("unrecognized network ID"),
}
}

View File

@@ -40,7 +40,8 @@ impl SubstrateCli for Cli {
fn load_spec(&self, id: &str) -> Result<Box<dyn sc_service::ChainSpec>, String> {
match id {
"dev" | "devnet" => Ok(Box::new(chain_spec::development_config())),
"local" => Ok(Box::new(chain_spec::testnet_config())),
"local" => Ok(Box::new(chain_spec::local_config())),
"testnet" => Ok(Box::new(chain_spec::testnet_config())),
_ => panic!("Unknown network ID"),
}
}

View File

@@ -19,6 +19,7 @@ pub use sc_rpc_api::DenyUnsafe;
use sc_transaction_pool_api::TransactionPool;
pub struct FullDeps<C, P> {
pub id: String,
pub client: Arc<C>,
pub pool: Arc<P>,
pub deny_unsafe: DenyUnsafe,
@@ -46,18 +47,19 @@ where
use pallet_transaction_payment_rpc::{TransactionPayment, TransactionPaymentApiServer};
let mut module = RpcModule::new(());
let FullDeps { client, pool, deny_unsafe, authority_discovery } = deps;
let FullDeps { id, client, pool, deny_unsafe, authority_discovery } = deps;
module.merge(System::new(client.clone(), pool, deny_unsafe).into_rpc())?;
module.merge(TransactionPayment::new(client.clone()).into_rpc())?;
if let Some(authority_discovery) = authority_discovery {
let mut authority_discovery_module = RpcModule::new((client, RwLock::new(authority_discovery)));
let mut authority_discovery_module =
RpcModule::new((id, client, RwLock::new(authority_discovery)));
authority_discovery_module.register_async_method(
"p2p_validators",
|params, context| async move {
let network: NetworkId = params.parse()?;
let (client, authority_discovery) = &*context;
let (id, client, authority_discovery) = &*context;
let latest_block = client.info().best_hash;
let validators = client.runtime_api().validators(latest_block, network).map_err(|_| {
@@ -66,7 +68,9 @@ where
"please report this at https://github.com/serai-dex/serai",
)))
})?;
let mut all_p2p_addresses = vec![];
// Always return the protocol's bootnodes
let mut all_p2p_addresses = crate::chain_spec::bootnode_multiaddrs(id);
// Additionally returns validators found over the DHT
for validator in validators {
let mut returned_addresses = authority_discovery
.write()

View File

@@ -161,7 +161,7 @@ pub fn new_partial(
))
}
pub fn new_full(config: Configuration) -> Result<TaskManager, ServiceError> {
pub fn new_full(mut config: Configuration) -> Result<TaskManager, ServiceError> {
let (
sc_service::PartialComponents {
client,
@@ -176,6 +176,11 @@ pub fn new_full(config: Configuration) -> Result<TaskManager, ServiceError> {
keystore_container,
) = new_partial(&config)?;
config.network.node_name = "serai".to_string();
config.network.client_version = "0.1.0".to_string();
config.network.listen_addresses =
vec!["/ip4/0.0.0.0/tcp/30333".parse().unwrap(), "/ip6/::/tcp/30333".parse().unwrap()];
let mut net_config = sc_network::config::FullNetworkConfiguration::new(&config.network);
let grandpa_protocol_name =
grandpa::protocol_standard_name(&client.block_hash(0).unwrap().unwrap(), &config.chain_spec);
@@ -203,6 +208,59 @@ pub fn new_full(config: Configuration) -> Result<TaskManager, ServiceError> {
warp_sync_params: Some(WarpSyncParams::WithProvider(warp_sync)),
})?;
task_manager.spawn_handle().spawn("bootnodes", "bootnodes", {
let network = network.clone();
let id = config.chain_spec.id().to_string();
async move {
// Transforms the above Multiaddrs into MultiaddrWithPeerIds
// While the PeerIds *should* be known in advance and hardcoded, that data wasn't collected in
// time and this fine for a testnet
let bootnodes = || async {
use libp2p::{Transport as TransportTrait, tcp::tokio::Transport, noise::Config};
let bootnode_multiaddrs = crate::chain_spec::bootnode_multiaddrs(&id);
let mut tasks = vec![];
for multiaddr in bootnode_multiaddrs {
tasks.push(tokio::time::timeout(
core::time::Duration::from_secs(10),
tokio::task::spawn(async move {
let Ok(noise) = Config::new(&sc_network::Keypair::generate_ed25519()) else { None? };
let mut transport = Transport::default()
.upgrade(libp2p::core::upgrade::Version::V1)
.authenticate(noise)
.multiplex(libp2p::yamux::Config::default());
let Ok(transport) = transport.dial(multiaddr.clone()) else { None? };
let Ok((peer_id, _)) = transport.await else { None? };
Some(sc_network::config::MultiaddrWithPeerId { multiaddr, peer_id })
}),
));
}
let mut res = vec![];
for task in tasks {
if let Ok(Ok(Some(bootnode))) = task.await {
res.push(bootnode);
}
}
res
};
use sc_network::{NetworkStatusProvider, NetworkPeers};
loop {
if let Ok(status) = network.status().await {
if status.num_connected_peers < 3 {
for bootnode in bootnodes().await {
let _ = network.add_reserved_peer(bootnode);
}
}
}
tokio::time::sleep(core::time::Duration::from_secs(60)).await;
}
}
});
if config.offchain_worker.enabled {
task_manager.spawn_handle().spawn(
"offchain-workers-runner",
@@ -258,11 +316,13 @@ pub fn new_full(config: Configuration) -> Result<TaskManager, ServiceError> {
};
let rpc_builder = {
let id = config.chain_spec.id().to_string();
let client = client.clone();
let pool = transaction_pool.clone();
Box::new(move |deny_unsafe, _| {
crate::rpc::create_full(crate::rpc::FullDeps {
id: id.clone(),
client: client.clone(),
pool: pool.clone(),
deny_unsafe,

View File

@@ -6,7 +6,7 @@ license = "MIT"
repository = "https://github.com/serai-dex/serai/tree/develop/substrate/primitives"
authors = ["Luke Parker <lukeparker5132@gmail.com>"]
edition = "2021"
rust-version = "1.69"
rust-version = "1.74"
[package.metadata.docs.rs]
all-features = true

View File

@@ -128,7 +128,7 @@ std = [
"pallet-transaction-payment-rpc-runtime-api/std",
]
fast-epoch = []
fast-epoch = ["genesis-liquidity-pallet/fast-epoch"]
runtime-benchmarks = [
"sp-runtime/runtime-benchmarks",

View File

@@ -11,7 +11,6 @@ use core::marker::PhantomData;
// Re-export all components
pub use serai_primitives as primitives;
pub use primitives::{BlockNumber, Header};
use primitives::{NetworkId, NETWORKS};
pub use frame_system as system;
pub use frame_support as support;
@@ -50,7 +49,7 @@ use sp_runtime::{
BoundedVec, Perbill, ApplyExtrinsicResult,
};
use primitives::{PublicKey, AccountLookup, SubstrateAmount};
use primitives::{NetworkId, PublicKey, AccountLookup, SubstrateAmount, Coin, NETWORKS};
use support::{
traits::{ConstU8, ConstU16, ConstU32, ConstU64, Contains},
@@ -178,6 +177,9 @@ impl Contains<RuntimeCall> for CallFilter {
},
RuntimeCall::Dex(call) => !matches!(call, dex::Call::__Ignore(_, _)),
RuntimeCall::ValidatorSets(call) => !matches!(call, validator_sets::Call::__Ignore(_, _)),
RuntimeCall::GenesisLiquidity(call) => {
!matches!(call, genesis_liquidity::Call::__Ignore(_, _))
}
RuntimeCall::InInstructions(call) => !matches!(call, in_instructions::Call::__Ignore(_, _)),
RuntimeCall::Signals(call) => !matches!(call, signals::Call::__Ignore(_, _)),
@@ -325,7 +327,7 @@ pub type ReportLongevity = <Runtime as pallet_babe::Config>::EpochDuration;
impl babe::Config for Runtime {
#[cfg(feature = "fast-epoch")]
type EpochDuration = ConstU64<{ MINUTES / 2 }>; // 30 seconds
type EpochDuration = ConstU64<{ HOURS / 2 }>; // 30 minutes
#[cfg(not(feature = "fast-epoch"))]
type EpochDuration = ConstU64<{ 4 * 7 * DAYS }>;
@@ -641,4 +643,28 @@ sp_api::impl_runtime_apis! {
}
}
}
impl dex::DexApi<Block> for Runtime {
fn quote_price_exact_tokens_for_tokens(
asset1: Coin,
asset2: Coin,
amount: SubstrateAmount,
include_fee: bool
) -> Option<SubstrateAmount> {
Dex::quote_price_exact_tokens_for_tokens(asset1, asset2, amount, include_fee)
}
fn quote_price_tokens_for_exact_tokens(
asset1: Coin,
asset2: Coin,
amount: SubstrateAmount,
include_fee: bool
) -> Option<SubstrateAmount> {
Dex::quote_price_tokens_for_exact_tokens(asset1, asset2, amount, include_fee)
}
fn get_reserves(asset1: Coin, asset2: Coin) -> Option<(SubstrateAmount, SubstrateAmount)> {
Dex::get_reserves(&asset1, &asset2).ok()
}
}
}

View File

@@ -477,7 +477,7 @@ pub mod pallet {
let Some(top) = top else { return false };
// key_shares may be over MAX_KEY_SHARES_PER_SET, which will cause an off-chain reduction of
// key_shares may be over MAX_KEY_SHARES_PER_SET, which will cause a round robin reduction of
// each validator's key shares until their sum is MAX_KEY_SHARES_PER_SET
// post_amortization_key_shares_for_top_validator yields what the top validator's key shares
// would be after such a reduction, letting us evaluate this correctly