-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement genesis liquidity protocol (#545)
* add genesis liquidity implementation * add missing deposit event * fix CI issues * minor fixes * make math safer * fix fmt * make remove liquidity an authorized call * implement setting initial values for coins * add genesis liquidity test & misc fixes * updato develop latest * fix rotation test * Finish merging develop * Remove accidentally committed ETH files * fix pr comments * further bug fixes * fix last pr comments * tidy up * Misc --------- Co-authored-by: Luke Parker <[email protected]>
- Loading branch information
1 parent
2ccb0cd
commit 1493f49
Showing
28 changed files
with
1,287 additions
and
43 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
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 = "serde", derive(serde::Serialize, serde::Deserialize))] | ||
pub enum Call { | ||
remove_coin_liquidity { balance: Balance }, | ||
oraclize_values { values: Values, signature: Signature }, | ||
} | ||
|
||
#[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 { | ||
GenesisLiquidityAdded { by: SeraiAddress, balance: Balance }, | ||
GenesisLiquidityRemoved { by: SeraiAddress, balance: Balance }, | ||
GenesisLiquidityAddedToPool { coin1: Balance, coin2: Balance }, | ||
EconomicSecurityReached { network: NetworkId }, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 }, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
pub use serai_abi::genesis_liquidity::primitives; | ||
use primitives::{Values, LiquidityAmount}; | ||
|
||
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 fn oraclize_values(values: Values, signature: Signature) -> Transaction { | ||
Serai::unsigned(serai_abi::Call::GenesisLiquidity( | ||
serai_abi::genesis_liquidity::Call::oraclize_values { values, 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, | ||
) -> Result<LiquidityAmount, SeraiError> { | ||
Ok( | ||
self | ||
.0 | ||
.storage( | ||
PALLET, | ||
"Liquidity", | ||
(coin, sp_core::hashing::blake2_128(&address.encode()), &address.0), | ||
) | ||
.await? | ||
.unwrap_or(LiquidityAmount::zero()), | ||
) | ||
} | ||
|
||
pub async fn supply(&self, coin: Coin) -> Result<LiquidityAmount, SeraiError> { | ||
Ok(self.0.storage(PALLET, "Supply", coin).await?.unwrap_or(LiquidityAmount::zero())) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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::LiquidityTokens(serai_abi::liquidity_tokens::Call::transfer { to, balance }) | ||
} | ||
|
||
pub fn burn(balance: Balance) -> serai_abi::Call { | ||
serai_abi::Call::LiquidityTokens(serai_abi::liquidity_tokens::Call::burn { balance }) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.