Skip to content

Commit

Permalink
rebase changes
Browse files Browse the repository at this point in the history
  • Loading branch information
akildemir committed Jul 19, 2024
1 parent d40df53 commit 84b534a
Show file tree
Hide file tree
Showing 15 changed files with 180 additions and 396 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

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
2 changes: 0 additions & 2 deletions substrate/abi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ 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),
Expand All @@ -57,7 +56,6 @@ 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),
Expand Down
4 changes: 4 additions & 0 deletions substrate/client/src/serai/dex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,8 @@ impl<'a> SeraiDex<'a> {
.map_err(|e| SeraiError::ErrorInResponse(e.to_string()))?;
Ok(result.map(|amounts| (Amount(amounts.0), Amount(amounts.1))))
}

pub async fn oracle_value(&self, coin: Coin) -> Result<Option<Amount>, SeraiError> {
self.0.storage(PALLET, "SecurityOracleValue", coin).await
}
}
35 changes: 15 additions & 20 deletions substrate/client/src/serai/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use hex::FromHexError;
use thiserror::Error;

use async_lock::RwLock;
Expand Down Expand Up @@ -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 {
pub async fn call<Req: Serialize, Res: DeserializeOwned>(
&self,
Expand Down Expand Up @@ -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> {
let hash: Option<String> = self.call("chain_getBlockHash", [number]).await?;
let Some(hash) = hash else { return Ok(None) };
hex_decode(hash)
.map_err(|_| SeraiError::InvalidNode("expected hex from node wasn't hex".to_string()))?
Self::hex_decode(hash)?
.try_into()
.map_err(|_| SeraiError::InvalidNode("didn't respond to getBlockHash with hash".to_string()))
.map(Some)
Expand Down Expand Up @@ -212,12 +211,9 @@ impl Serai {

pub async fn latest_finalized_block_hash(&self) -> Result<[u8; 32], SeraiError> {
let hash: String = self.call("chain_getFinalizedHead", ()).await?;
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())
})
Self::hex_decode(hash)?.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> {
Expand All @@ -227,7 +223,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) = hex_decode(block) else {
let Ok(bytes) = Self::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 {
Expand Down Expand Up @@ -373,8 +369,7 @@ 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 = hex_decode(res)
.map_err(|_| SeraiError::InvalidNode("expected hex from node wasn't hex".to_string()))?;
let res = Serai::hex_decode(res)?;
Ok(Some(R::decode(&mut res.as_slice()).map_err(|_| {
SeraiError::InvalidRuntime(format!(
"different type present at storage location, raw value: {}",
Expand Down
Loading

0 comments on commit 84b534a

Please sign in to comment.