Skip to content

Commit

Permalink
Fix dia chain extension (#228)
Browse files Browse the repository at this point in the history
fix-dia-chain-extension
  • Loading branch information
ashneverdawn authored May 11, 2023
1 parent b444b5b commit 0b4dc2e
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 16 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

4 changes: 3 additions & 1 deletion runtime/common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ sp-runtime = { git = "https://github.com/paritytech/substrate", default-features
sp-consensus-aura = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "polkadot-v0.9.37" }
sp-core = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "polkadot-v0.9.37" }
spacewalk-primitives = { git = "https://github.com/pendulum-chain/spacewalk", default-features = false, rev = "c4bdbb8f5ce74023e06898ef7576d1ce93882ac1"}
dia-oracle = { git = "https://github.com/pendulum-chain/oracle-pallet", default-features = false, branch = "polkadot-v0.9.37" }

[features]
default = [
Expand All @@ -30,7 +31,8 @@ std = [
"sp-core/std",
"spacewalk-primitives/std",
"parity-scale-codec/std",
"scale-info/std"
"scale-info/std",
"dia-oracle/std",
]

runtime-benchmarks = [
Expand Down
55 changes: 54 additions & 1 deletion runtime/common/src/chain_ext.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use crate::*;
use sp_core::{Decode, Encode, MaxEncodedLen};
use sp_runtime::{ArithmeticError, TokenError};
use sp_runtime::{ArithmeticError, TokenError, codec};
use spacewalk_primitives::{Asset, CurrencyId};
use scale_info::prelude::vec::Vec;
use dia_oracle::dia;

#[derive(Debug, Copy, Clone, PartialEq, Eq, Encode, Decode, MaxEncodedLen)]
pub enum OriginType {
Caller,
Expand All @@ -23,6 +26,7 @@ struct PalletAssetBalanceRequest {
}

#[derive(Debug, Copy, Clone, PartialEq, Eq, Encode, Decode, MaxEncodedLen)]
#[cfg_attr(feature = "std", derive(scale_info::TypeInfo))]
pub enum ChainExtensionError {
/// Some error occurred.
Other,
Expand All @@ -47,6 +51,7 @@ pub enum ChainExtensionError {
}

#[derive(Debug, Copy, Clone, PartialEq, Eq, Encode, Decode, MaxEncodedLen)]
#[cfg_attr(feature = "std", derive(scale_info::TypeInfo))]
pub enum PalletAssetArithmeticError {
/// Underflow.
Underflow,
Expand All @@ -59,6 +64,7 @@ pub enum PalletAssetArithmeticError {
}

#[derive(Debug, Copy, Clone, PartialEq, Eq, Encode, Decode, MaxEncodedLen)]
#[cfg_attr(feature = "std", derive(scale_info::TypeInfo))]
pub enum PalletAssetTokenError {
/// Funds are unavailable.
NoFunds,
Expand Down Expand Up @@ -141,3 +147,50 @@ pub fn try_get_currency_id_from(
_ => Err(()),
}
}

pub type Blockchain = [u8; 32];
pub type Symbol = [u8; 32];

pub trait ToTrimmedVec {
fn to_trimmed_vec(&self) -> Vec<u8>;
}
impl ToTrimmedVec for [u8; 32] {
fn to_trimmed_vec(&self) -> Vec<u8> {
trim_trailing_zeros(self).to_vec()
}
}

fn trim_trailing_zeros(slice: &[u8]) -> &[u8] {
let mut trim_amount = 0;
for el in slice.iter().rev() {
if *el == 0 {
trim_amount += 1;
} else {
break
}
}
&slice[..slice.len() - trim_amount]
}

#[derive(Debug, Clone, PartialEq, Eq, codec::Encode, codec::Decode)]
#[cfg_attr(feature = "std", derive(scale_info::TypeInfo))]
pub struct CoinInfo {
pub symbol: Vec<u8>,
pub name: Vec<u8>,
pub blockchain: Vec<u8>,
pub supply: u128,
pub last_update_timestamp: u64,
pub price: u128,
}
impl From<dia::CoinInfo> for CoinInfo {
fn from(coin_info: dia::CoinInfo) -> Self {
Self {
symbol: coin_info.symbol,
name: coin_info.name,
blockchain: coin_info.blockchain,
supply: coin_info.supply,
last_update_timestamp: coin_info.last_update_timestamp,
price: coin_info.price,
}
}
}
30 changes: 16 additions & 14 deletions runtime/foucoco/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1283,20 +1283,22 @@ where
//dia price feed
7777 => {
let mut env = env.buf_in_buf_out();
let price_feed_request: ([u8; 32], [u8; 32]) = env.read_as()?;

let blockchain = price_feed_request.0;
let symbol = price_feed_request.1;
let price_feed = <dia_oracle::Pallet<T> as DiaOracle>::get_coin_info(
blockchain.to_vec(),
symbol.to_vec(),
);

warn!("price_feed_request : {:#?}", price_feed_request);
warn!("price_feed : {:#?}", price_feed);

env.write(&price_feed.encode(), false, None).map_err(|_| {
DispatchError::Other("ChainExtension failed to call price feed")
let (blockchain, symbol): (Blockchain, Symbol) = env.read_as()?;

let result =
<dia_oracle::Pallet<T> as DiaOracle>::get_coin_info(blockchain.to_trimmed_vec(), symbol.to_trimmed_vec());

warn!("blockchain: {:#?}, symbol: {:#?}", blockchain, symbol);
warn!("price_feed: {:#?}", result);

let result = match result {
Ok(coin_info) =>
Result::<CoinInfo, ChainExtensionError>::Ok(CoinInfo::from(coin_info)),
Err(e) =>
Result::<CoinInfo, ChainExtensionError>::Err(ChainExtensionError::from(e)),
};
env.write(&result.encode(), false, None).map_err(|_| {
DispatchError::Other("ChainExtension failed to call 'price feed'")
})?;
},
_ => {
Expand Down

0 comments on commit 0b4dc2e

Please sign in to comment.