Skip to content

Implement block emissions #551

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 35 commits into from
Aug 15, 2024
Merged
Show file tree
Hide file tree
Changes from 31 commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
c1bcb0f
add genesis liquidity implementation
Mar 27, 2024
da51543
Merge branch 'develop' of https://github.com/serai-dex/serai into emi…
Mar 27, 2024
b9df73e
add missing deposit event
Mar 27, 2024
2b32fe9
fix CI issues
Mar 28, 2024
207f2bd
minor fixes
Mar 29, 2024
c320402
make math safer
Apr 8, 2024
8be0538
fix fmt
Apr 9, 2024
1cd9868
implement block emissions
Apr 9, 2024
df774d1
Merge branch 'develop' of https://github.com/serai-dex/serai into emi…
Apr 17, 2024
7041dbe
make remove liquidity an authorized call
Apr 17, 2024
826f998
implement setting initial values for coins
Apr 20, 2024
926ddd0
add genesis liquidity test & misc fixes
Apr 29, 2024
7ecbfde
Merge branch 'develop' of https://github.com/serai-dex/serai into emi…
Apr 30, 2024
20cf4c9
updato develop latest
Apr 30, 2024
90a5232
Merge branch 'develop' of https://github.com/serai-dex/serai into emi…
May 3, 2024
04fcb2b
fix rotation test
May 3, 2024
33fcd27
Merge branch 'emissions' of https://github.com/akildemir/serai into b…
May 3, 2024
3ed6bd3
fix licencing
May 5, 2024
1d5a53d
add fast-epoch feature
May 5, 2024
971b93b
only create the pool when adding liquidity first time
May 10, 2024
30df837
add initial reward era test
May 10, 2024
317b929
test whole pre ec security emissions
May 10, 2024
b8a9d20
fix clippy
May 10, 2024
929d0bf
Merge branch 'develop' of https://github.com/akildemir/serai into emi…
May 16, 2024
904c6dd
Merge branch 'emissions' of https://github.com/akildemir/serai into b…
May 16, 2024
929e66c
add swap-to-staked-sri feature
May 16, 2024
d40df53
rebase to develop latest
Jul 19, 2024
84b534a
rebase changes
Jul 19, 2024
8eb7e66
fix tests
Jul 26, 2024
a23e7f6
Merge branch 'develop' into HEAD
kayabaNerve Jul 29, 2024
3cb7386
Remove accidentally commited ETH ABI files
kayabaNerve Jul 29, 2024
e1b4a9a
fix some pr comments
Jul 30, 2024
bb5fd37
Finish up fixing pr comments
Aug 1, 2024
dd9aeb1
exclude SRI from is_allowed check
Aug 2, 2024
c620d8f
Misc changes
kayabaNerve Aug 15, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions deny.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ exceptions = [

{ allow = ["AGPL-3.0"], name = "serai-coins-pallet" },
{ allow = ["AGPL-3.0"], name = "serai-dex-pallet" },

{ allow = ["AGPL-3.0"], name = "serai-genesis-liquidity-pallet" },
{ allow = ["AGPL-3.0"], name = "serai-emissions-pallet" },

{ allow = ["AGPL-3.0"], name = "serai-genesis-liquidity-pallet" },

Expand Down
2 changes: 2 additions & 0 deletions substrate/abi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ serai-primitives = { path = "../primitives", version = "0.1", default-features =
serai-coins-primitives = { path = "../coins/primitives", version = "0.1", default-features = false }
serai-validator-sets-primitives = { path = "../validator-sets/primitives", version = "0.1", default-features = false }
serai-genesis-liquidity-primitives = { path = "../genesis-liquidity/primitives", version = "0.1", default-features = false }
serai-emissions-primitives = { path = "../emissions/primitives", version = "0.1", default-features = false }
serai-in-instructions-primitives = { path = "../in-instructions/primitives", version = "0.1", default-features = false }
serai-signals-primitives = { path = "../signals/primitives", version = "0.1", default-features = false }

Expand All @@ -57,6 +58,7 @@ std = [
"serai-coins-primitives/std",
"serai-validator-sets-primitives/std",
"serai-genesis-liquidity-primitives/std",
"serai-emissions-primitives/std",
"serai-in-instructions-primitives/std",
"serai-signals-primitives/std",
]
Expand Down
16 changes: 16 additions & 0 deletions substrate/abi/src/emissions.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
pub use serai_emissions_primitives as 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,
}

#[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 {
empty_event,
}
7 changes: 5 additions & 2 deletions substrate/abi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ pub mod in_instructions;
pub mod signals;

pub mod genesis_liquidity;
pub mod emissions;

pub mod babe;
pub mod grandpa;
Expand All @@ -32,8 +33,9 @@ pub enum Call {
Coins(coins::Call),
LiquidityTokens(liquidity_tokens::Call),
Dex(dex::Call),
GenesisLiquidity(genesis_liquidity::Call),
ValidatorSets(validator_sets::Call),
GenesisLiquidity(genesis_liquidity::Call),
Emissions(emissions::Call),
InInstructions(in_instructions::Call),
Signals(signals::Call),
Babe(babe::Call),
Expand All @@ -54,8 +56,9 @@ pub enum Event {
Coins(coins::Event),
LiquidityTokens(liquidity_tokens::Event),
Dex(dex::Event),
GenesisLiquidity(genesis_liquidity::Event),
ValidatorSets(validator_sets::Event),
GenesisLiquidity(genesis_liquidity::Event),
Emissions(emissions::Event),
InInstructions(in_instructions::Event),
Signals(signals::Event),
Babe,
Expand Down
25 changes: 9 additions & 16 deletions substrate/client/src/serai/dex.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use sp_core::bounded_vec::BoundedVec;
use serai_abi::primitives::{SeraiAddress, Amount, Coin};

use scale::{decode_from_bytes, Encode};

use crate::{Serai, SeraiError, TemporalSerai};
use crate::{SeraiError, TemporalSerai};

pub type DexEvent = serai_abi::dex::Event;

const PALLET: &str = "Dex";

#[derive(Clone, Copy)]
pub struct SeraiDex<'a>(pub(crate) &'a TemporalSerai<'a>);
impl<'a> SeraiDex<'a> {
Expand Down Expand Up @@ -61,18 +61,11 @@ impl<'a> SeraiDex<'a> {
}

/// Returns the reserves of `coin:SRI` pool.
pub async fn get_reserves(&self, coin: Coin) -> Result<Option<(Amount, Amount)>, SeraiError> {
let reserves = self
.0
.serai
.call(
"state_call",
["DexApi_get_reserves".to_string(), hex::encode((coin, Coin::Serai).encode())],
)
.await?;
let bytes = Serai::hex_decode(reserves)?;
let result = decode_from_bytes::<Option<(u64, u64)>>(bytes.into())
.map_err(|e| SeraiError::ErrorInResponse(e.to_string()))?;
Ok(result.map(|amounts| (Amount(amounts.0), Amount(amounts.1))))
pub async fn get_reserves(&self, coin: Coin) -> Result<Option<(u64, u64)>, SeraiError> {
self.0.runtime_api("DexApi_get_reserves", (coin, Coin::Serai)).await
}

pub async fn oracle_value(&self, coin: Coin) -> Result<Option<Amount>, SeraiError> {
self.0.storage(PALLET, "SecurityOracleValue", coin).await
}
}
4 changes: 4 additions & 0 deletions substrate/client/src/serai/genesis_liquidity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,8 @@ impl<'a> SeraiGenesisLiquidity<'a> {
pub async fn supply(&self, coin: Coin) -> Result<LiquidityAmount, SeraiError> {
Ok(self.0.storage(PALLET, "Supply", coin).await?.unwrap_or(LiquidityAmount::zero()))
}

pub async fn genesis_complete(&self) -> Result<Option<()>, SeraiError> {
self.0.storage(PALLET, "GenesisComplete", ()).await
}
}
33 changes: 22 additions & 11 deletions substrate/client/src/serai/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,17 +198,6 @@ 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 validators: String = self
.call("state_call", ["SeraiRuntimeApi_validators".to_string(), hex::encode(network.encode())])
.await?;
let bytes = Self::hex_decode(validators)?;
let r = Vec::<Public>::decode(&mut bytes.as_slice())
.map_err(|e| SeraiError::ErrorInResponse(e.to_string()))?;
Ok(r)
}

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(|_| {
Expand Down Expand Up @@ -378,6 +367,28 @@ impl<'a> TemporalSerai<'a> {
})?))
}

async fn runtime_api<P: Encode, R: Decode>(
&self,
method: &'static str,
params: P,
) -> Result<R, SeraiError> {
let result: String = self
.serai
.call(
"state_call",
[method.to_string(), hex::encode(params.encode()), hex::encode(self.block)],
)
.await?;

let bytes = Serai::hex_decode(result.clone())?;
R::decode(&mut bytes.as_slice()).map_err(|_| {
SeraiError::InvalidRuntime(format!(
"different type than what is expected returned, raw value: {}",
hex::encode(result)
))
})
}

pub fn coins(&'a self) -> SeraiCoins<'a> {
SeraiCoins(self)
}
Expand Down
10 changes: 9 additions & 1 deletion substrate/client/src/serai/validator_sets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ impl<'a> SeraiValidatorSets<'a> {
&self,
network: NetworkId,
) -> Result<Vec<Public>, SeraiError> {
self.0.serai.active_network_validators(network).await
self.0.runtime_api("SeraiRuntimeApi_validators", network).await
}

// TODO: Store these separately since we almost never need both at once?
Expand All @@ -178,6 +178,14 @@ impl<'a> SeraiValidatorSets<'a> {
self.0.storage(PALLET, "PendingSlashReport", network).await
}

pub async fn session_begin_block(
&self,
network: NetworkId,
session: Session,
) -> Result<Option<u64>, SeraiError> {
self.0.storage(PALLET, "SessionBeginBlock", (network, session)).await
}

pub fn set_keys(
network: NetworkId,
removed_participants: sp_runtime::BoundedVec<
Expand Down
Loading
Loading