-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* skip_unwrap_near * update makefile * build release * add auto_whitelisted_postfix * add sfrax * add SfraxExtraInfo check * update value --------- Co-authored-by: Marco <[email protected]>
- Loading branch information
1 parent
5f6ec6d
commit 5fd4997
Showing
22 changed files
with
1,326 additions
and
108 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
[package] | ||
name = "mock-price-oracle" | ||
version = "0.1.0" | ||
edition = "2018" | ||
publish = false | ||
|
||
[lib] | ||
crate-type = ["cdylib", "rlib"] | ||
|
||
|
||
[dependencies] | ||
near-sdk = "3.1.0" | ||
near-contract-standards = "3.1.0" |
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,123 @@ | ||
use std::collections::HashMap; | ||
|
||
use near_sdk::serde::{Deserialize, Serialize}; | ||
use near_sdk::borsh::{self, BorshDeserialize, BorshSerialize}; | ||
use near_sdk::{near_bindgen, PanicOnDefault}; | ||
use near_sdk::{env, Balance, Timestamp}; | ||
|
||
type AssetId = String; | ||
|
||
#[derive(BorshSerialize, BorshDeserialize, Serialize, Deserialize, Debug, Clone)] | ||
#[serde(crate = "near_sdk::serde")] | ||
pub struct Price { | ||
#[serde(with = "u128_dec_format")] | ||
pub multiplier: Balance, | ||
pub decimals: u8, | ||
} | ||
|
||
#[derive(Serialize, Deserialize, Debug)] | ||
#[serde(crate = "near_sdk::serde")] | ||
pub struct AssetOptionalPrice { | ||
pub asset_id: AssetId, | ||
pub price: Option<Price>, | ||
} | ||
|
||
#[derive(Serialize, Deserialize, Debug)] | ||
#[serde(crate = "near_sdk::serde")] | ||
pub struct PriceData { | ||
#[serde(with = "u64_dec_format")] | ||
pub timestamp: Timestamp, | ||
pub recency_duration_sec: u32, | ||
|
||
pub prices: Vec<AssetOptionalPrice>, | ||
} | ||
|
||
#[near_bindgen] | ||
#[derive(BorshSerialize, BorshDeserialize, PanicOnDefault)] | ||
pub struct Contract { | ||
prices: HashMap<AssetId, Price> | ||
} | ||
|
||
#[near_bindgen] | ||
impl Contract { | ||
#[init] | ||
pub fn new() -> Self { | ||
Self { | ||
prices: HashMap::new(), | ||
} | ||
} | ||
|
||
pub fn set_price_data(&mut self, asset_id: AssetId, price: Price) { | ||
self.prices.insert(asset_id, price); | ||
} | ||
|
||
pub fn get_price_data(&self, asset_ids: Option<Vec<AssetId>>) -> PriceData { | ||
// let asset_ids = asset_ids.unwrap_or(vec![]); | ||
PriceData { | ||
timestamp: env::block_timestamp(), | ||
recency_duration_sec: 90, | ||
prices: { | ||
let mut res = vec![]; | ||
if let Some(asset_ids) = asset_ids { | ||
for asset_id in asset_ids { | ||
res.push(AssetOptionalPrice{ | ||
asset_id: asset_id.clone(), | ||
price: self.prices.get(&asset_id).cloned(), | ||
}); | ||
} | ||
} else { | ||
for (asset_id, price) in self.prices.iter() { | ||
res.push(AssetOptionalPrice{ | ||
asset_id: asset_id.clone(), | ||
price: Some(price.clone()), | ||
}); | ||
} | ||
} | ||
res | ||
} | ||
} | ||
} | ||
|
||
} | ||
|
||
pub(crate) mod u128_dec_format { | ||
use near_sdk::serde::de; | ||
use near_sdk::serde::{Deserialize, Deserializer, Serializer}; | ||
|
||
pub fn serialize<S>(num: &u128, serializer: S) -> Result<S::Ok, S::Error> | ||
where | ||
S: Serializer, | ||
{ | ||
serializer.serialize_str(&num.to_string()) | ||
} | ||
|
||
pub fn deserialize<'de, D>(deserializer: D) -> Result<u128, D::Error> | ||
where | ||
D: Deserializer<'de>, | ||
{ | ||
String::deserialize(deserializer)? | ||
.parse() | ||
.map_err(de::Error::custom) | ||
} | ||
} | ||
|
||
pub(crate) mod u64_dec_format { | ||
use near_sdk::serde::de; | ||
use near_sdk::serde::{Deserialize, Deserializer, Serializer}; | ||
|
||
pub fn serialize<S>(num: &u64, serializer: S) -> Result<S::Ok, S::Error> | ||
where | ||
S: Serializer, | ||
{ | ||
serializer.serialize_str(&num.to_string()) | ||
} | ||
|
||
pub fn deserialize<'de, D>(deserializer: D) -> Result<u64, D::Error> | ||
where | ||
D: Deserializer<'de>, | ||
{ | ||
String::deserialize(deserializer)? | ||
.parse() | ||
.map_err(de::Error::custom) | ||
} | ||
} |
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,13 @@ | ||
[package] | ||
name = "mock-pyth" | ||
version = "0.1.0" | ||
edition = "2018" | ||
publish = false | ||
|
||
[lib] | ||
crate-type = ["cdylib", "rlib"] | ||
|
||
[dependencies] | ||
near-sdk = "3.1.0" | ||
near-contract-standards = "3.1.0" | ||
hex = "0.4.3" |
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,98 @@ | ||
use std::collections::HashMap; | ||
|
||
use near_sdk::borsh::{self, BorshDeserialize, BorshSerialize}; | ||
use near_sdk::json_types::{I64, U64}; | ||
use near_sdk::serde::{Deserialize, Serialize}; | ||
use near_sdk::{near_bindgen, PanicOnDefault}; | ||
|
||
#[derive(BorshDeserialize, BorshSerialize, Debug, Deserialize, Serialize, Clone)] | ||
#[serde(crate = "near_sdk::serde")] | ||
pub struct PythPrice { | ||
pub price: I64, | ||
/// Confidence interval around the price | ||
pub conf: U64, | ||
/// The exponent | ||
pub expo: i32, | ||
/// Unix timestamp of when this price was computed | ||
pub publish_time: i64, | ||
} | ||
|
||
#[derive(BorshDeserialize, BorshSerialize, PartialOrd, PartialEq, Eq, Hash, Clone)] | ||
#[repr(transparent)] | ||
pub struct PriceIdentifier(pub [u8; 32]); | ||
|
||
impl<'de> near_sdk::serde::Deserialize<'de> for PriceIdentifier { | ||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> | ||
where | ||
D: near_sdk::serde::Deserializer<'de>, | ||
{ | ||
/// A visitor that deserializes a hex string into a 32 byte array. | ||
struct IdentifierVisitor; | ||
|
||
impl<'de> near_sdk::serde::de::Visitor<'de> for IdentifierVisitor { | ||
/// Target type for either a hex string or a 32 byte array. | ||
type Value = [u8; 32]; | ||
|
||
fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result { | ||
formatter.write_str("a hex string") | ||
} | ||
|
||
// When given a string, attempt a standard hex decode. | ||
fn visit_str<E>(self, value: &str) -> Result<Self::Value, E> | ||
where | ||
E: near_sdk::serde::de::Error, | ||
{ | ||
if value.len() != 64 { | ||
return Err(E::custom(format!( | ||
"expected a 64 character hex string, got {}", | ||
value.len() | ||
))); | ||
} | ||
let mut bytes = [0u8; 32]; | ||
hex::decode_to_slice(value, &mut bytes).map_err(E::custom)?; | ||
Ok(bytes) | ||
} | ||
} | ||
|
||
deserializer | ||
.deserialize_any(IdentifierVisitor) | ||
.map(PriceIdentifier) | ||
} | ||
} | ||
|
||
impl near_sdk::serde::Serialize for PriceIdentifier { | ||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> | ||
where | ||
S: near_sdk::serde::Serializer, | ||
{ | ||
serializer.serialize_str(&hex::encode(&self.0)) | ||
} | ||
} | ||
|
||
#[near_bindgen] | ||
#[derive(BorshSerialize, BorshDeserialize, PanicOnDefault)] | ||
pub struct Contract { | ||
price_info: HashMap<PriceIdentifier, PythPrice>, | ||
} | ||
|
||
#[near_bindgen] | ||
impl Contract { | ||
#[init] | ||
pub fn new() -> Self { | ||
Self { | ||
price_info: HashMap::new() | ||
} | ||
} | ||
|
||
pub fn set_price(&mut self, price_identifier: PriceIdentifier, pyth_price: PythPrice) { | ||
self.price_info.insert(price_identifier, pyth_price); | ||
} | ||
|
||
pub fn remove_price(&mut self, price_identifier: PriceIdentifier) { | ||
self.price_info.remove(&price_identifier); | ||
} | ||
|
||
pub fn get_price(&self, price_identifier: PriceIdentifier) -> Option<PythPrice> { | ||
self.price_info.get(&price_identifier).cloned() | ||
} | ||
} |
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,6 +1,6 @@ | ||
[package] | ||
name = "ref-exchange" | ||
version = "1.9.0" | ||
version = "1.9.1" | ||
authors = ["Illia Polosukhin <[email protected]>"] | ||
edition = "2018" | ||
publish = false | ||
|
@@ -13,12 +13,15 @@ uint = { version = "0.9.3", default-features = false } | |
near-sdk = "3.1.0" | ||
near-contract-standards = "3.1.0" | ||
once_cell = "=1.8.0" | ||
hex = "0.4.3" | ||
|
||
[dev-dependencies] | ||
near-sdk-sim = "3.1.0" | ||
test-token = { path = "../test-token" } | ||
test-rated-token = { path = "../test-rated-token" } | ||
mock-boost-farming = { path = "../mock-boost-farming" } | ||
mock-wnear = { path = "../mock-wnear" } | ||
mock-price-oracle = { path = "../mock-price-oracle" } | ||
mock-pyth = { path = "../mock-pyth" } | ||
rand = "0.8" | ||
rand_pcg = "0.3" |
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.