Skip to content
This repository has been archived by the owner on Aug 2, 2024. It is now read-only.

Commit

Permalink
Merge branch 'main' into feat/invokeV0
Browse files Browse the repository at this point in the history
  • Loading branch information
raizo07 authored Jun 25, 2024
2 parents 92fa87e + 7b40592 commit d140172
Show file tree
Hide file tree
Showing 27 changed files with 60,629 additions and 179 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

## v0.8.0

- feat: Declare V0 RPC call
- feat: add `TransactionFilter<TxType>` to pallet-starknet `Config`
- chore: remove `ignore` from
`storage_changes_should_revert_on_transaction_revert` test
Expand Down Expand Up @@ -348,3 +349,4 @@
- dev : clean contracts and compiled files
- fix: add from_address in calldata of l1 message
- test: add starkgate related testcase
- feat: add pragma api to compute fees
57 changes: 1 addition & 56 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 crates/client/eth-client/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use ethers::types::{Address, H160};
use serde::{Deserialize, Serialize};

use crate::error::Error;
use crate::oracle::OracleConfig;

/// Default Anvil local endpoint
pub const DEFAULT_RPC_ENDPOINT: &str = "http://127.0.0.1:8545";
Expand All @@ -37,6 +38,8 @@ pub struct EthereumClientConfig {
pub wallet: Option<EthereumWalletConfig>,
#[serde(default)]
pub contracts: StarknetContracts,
#[serde(default)]
pub oracle: OracleConfig,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
Expand Down
1 change: 1 addition & 0 deletions crates/client/eth-client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
pub mod config;
pub mod error;
pub mod oracle;

use std::time::Duration;

Expand Down
134 changes: 134 additions & 0 deletions crates/client/eth-client/src/oracle.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
use std::fmt;

use serde::{Deserialize, Serialize};

pub const DEFAULT_API_URL: &str = "https://api.dev.pragma.build/node/v1/data/";

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "oracle_name", content = "config")]
pub enum OracleConfig {
Pragma(PragmaOracle),
}

impl OracleConfig {
pub fn get_fetch_url(&self, base: String, quote: String) -> String {
match self {
OracleConfig::Pragma(pragma_oracle) => pragma_oracle.get_fetch_url(base, quote),
}
}

pub fn get_api_key(&self) -> &String {
match self {
OracleConfig::Pragma(oracle) => &oracle.api_key,
}
}

pub fn is_in_bounds(&self, price: u128) -> bool {
match self {
OracleConfig::Pragma(oracle) => oracle.price_bounds.low <= price && price <= oracle.price_bounds.high,
}
}
}

impl Default for OracleConfig {
fn default() -> Self {
Self::Pragma(PragmaOracle::default())
}
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PragmaOracle {
#[serde(default = "default_oracle_api_url")]
pub api_url: String,
#[serde(default)]
pub api_key: String,
#[serde(default)]
pub aggregation_method: AggregationMethod,
#[serde(default)]
pub interval: Interval,
#[serde(default)]
pub price_bounds: PriceBounds,
}

impl Default for PragmaOracle {
fn default() -> Self {
Self {
api_url: default_oracle_api_url(),
api_key: String::default(),
aggregation_method: AggregationMethod::Median,
interval: Interval::OneMinute,
price_bounds: Default::default(),
}
}
}

impl PragmaOracle {
fn get_fetch_url(&self, base: String, quote: String) -> String {
format!("{}{}/{}?interval={}&aggregation={}", self.api_url, base, quote, self.interval, self.aggregation_method)
}
}

#[derive(Default, Debug, Serialize, Deserialize, Clone)]
/// Supported Aggregation Methods
pub enum AggregationMethod {
#[serde(rename = "median")]
Median,
#[serde(rename = "mean")]
Mean,
#[serde(rename = "twap")]
#[default]
Twap,
}

impl fmt::Display for AggregationMethod {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let name = match self {
AggregationMethod::Median => "median",
AggregationMethod::Mean => "mean",
AggregationMethod::Twap => "twap",
};
write!(f, "{}", name)
}
}

/// Supported Aggregation Intervals
#[derive(Default, Debug, Serialize, Deserialize, Clone)]
pub enum Interval {
#[serde(rename = "1min")]
OneMinute,
#[serde(rename = "15min")]
FifteenMinutes,
#[serde(rename = "1h")]
OneHour,
#[serde(rename = "2h")]
#[default]
TwoHours,
}

impl fmt::Display for Interval {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let name = match self {
Interval::OneMinute => "1min",
Interval::FifteenMinutes => "15min",
Interval::OneHour => "1h",
Interval::TwoHours => "2h",
};
write!(f, "{}", name)
}
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PriceBounds {
pub low: u128,
pub high: u128,
}

impl Default for PriceBounds {
fn default() -> Self {
Self { low: 0, high: u128::MAX }
}
}

fn default_oracle_api_url() -> String {
DEFAULT_API_URL.into()
}
Loading

0 comments on commit d140172

Please sign in to comment.