forked from keep-starknet-strange/madara
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
dev : add pragma api to compute fees in STRK (keep-starknet-strange#1633
) Co-authored-by: 0xevolve <[email protected]>
- Loading branch information
1 parent
f5dce7e
commit 7b40592
Showing
6 changed files
with
197 additions
and
8 deletions.
There are no files selected for viewing
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 |
---|---|---|
|
@@ -9,6 +9,7 @@ | |
|
||
pub mod config; | ||
pub mod error; | ||
pub mod oracle; | ||
|
||
use std::time::Duration; | ||
|
||
|
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,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() | ||
} |
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 |
---|---|---|
@@ -1,9 +1,22 @@ | ||
{ | ||
"provider": { | ||
"rpc_endpoint": "http://127.0.0.1:8545", | ||
"rpc_endpoint": "https://ethereum-rpc.publicnode.com", | ||
"gas_price_poll_ms": 10000 | ||
}, | ||
"contracts": { | ||
"core_contract": "0xe7f1725e7734ce288f8367e1bb143e90bb3f0512" | ||
}, | ||
"oracle": { | ||
"oracle_name": "Pragma", | ||
"config": { | ||
"api_url": "https://api.dev.pragma.build/node/v1/data/", | ||
"api_key": "", | ||
"aggregation_method": "twap", | ||
"interval": "2h", | ||
"price_bounds": { | ||
"low": 3000000000000000000000, | ||
"high": 6000000000000000000000 | ||
} | ||
} | ||
} | ||
} |