Skip to content

Commit

Permalink
feat: refactoring contract and sdk rust structure (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
martines3000 authored Aug 8, 2024
1 parent 476ddd7 commit ba15f96
Show file tree
Hide file tree
Showing 42 changed files with 571 additions and 466 deletions.
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,8 @@
},
"[json]": {
"editor.defaultFormatter": "biomejs.biome"
},
"[rust]": {
"editor.defaultFormatter": "rust-lang.rust-analyzer"
}
}
71 changes: 51 additions & 20 deletions Cargo.lock

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

23 changes: 19 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,19 +1,34 @@
[workspace]
members = [
"contracts/market",
"libs/src20_sdk",
# Apps
"apps/oracle",
# Pyth mock
# Contracts (types)
"contracts/market",
"contracts/pyth-mock",
"contracts/token",
# Libs
"libs/market_sdk",
"libs/token_sdk",
"libs/pyth_mock_sdk",
# Test
"contracts/test",
]
resolver = "2"

[workspace.dependencies]
# Local SDKs
market_sdk = { path = "libs/market_sdk" }
token_sdk = { path = "libs/token_sdk" }
pyth_mock_sdk = { path = "libs/pyth_mock_sdk" }

# Local contracts (types)
pyth_mock = { path = "contracts/pyth-mock" }
market = { path = "contracts/market" }
token = { path = "contracts/token" }

# External
pyth_sdk = { git = "https://github.com/pyth-network/pyth-crosschain", rev = "f41782f005f4bf99b8f765214b91b1397bc7c39b" }
tokio = { version = "1.12", features = ["full"] }
src20_sdk = { path = "libs/src20_sdk" }
fuels = { version = "0.65.1", features = ["fuel-core-lib"] }
dotenv = "0.15.0"
serde = "1.0.197"
Expand Down
1 change: 1 addition & 0 deletions abis/market_abi/src/structs.sw
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use i256::I256;
pub const BASE_ACCRUAL_SCALE: u256 = 1_000_000; // 1e6
pub const BASE_INDEX_SCALE_15: u256 = 1_000_000_000_000_000; // 1e15
pub const FACTOR_SCALE_18: u256 = 1_000_000_000_000_000_000; // 1e18

pub struct CollateralConfiguration {
pub asset_id: b256,
pub price_feed_id: b256,
Expand Down
2 changes: 2 additions & 0 deletions apps/oracle/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
market_sdk = { workspace = true }
pyth_sdk = { workspace = true }
fuels = { workspace = true }
tokio = { workspace = true }
dotenv = { workspace = true }
reqwest = { workspace = true }
serde = { workspace = true }
serde_json = { workspace = true }
hex = "0.4.3"
41 changes: 39 additions & 2 deletions apps/oracle/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,27 @@
use dotenv::dotenv;
use fuels::accounts::ViewOnlyAccount;
use fuels::prelude::{ContractId, Provider, WalletUnlocked};
use pyth_sdk::pyth_utils::{update_data_bytes, Pyth, PythOracleContract};
use fuels::types::Bytes;
use pyth_sdk::constants::{
BTC_USD_PRICE_FEED_ID, ETH_USD_PRICE_FEED_ID, UNI_USD_PRICE_FEED_ID, USDC_USD_PRICE_FEED_ID,
};
use pyth_sdk::pyth_utils::{Pyth, PythOracleContract};
use reqwest::ClientBuilder;
use serde::Deserialize;
use std::{str::FromStr, thread::sleep, time::Duration};

#[derive(Deserialize)]
struct BinaryData {
encoding: String,
data: Vec<String>,
}

#[derive(Deserialize)]
struct PriceUpdate {
binary: BinaryData,
parsed: serde_json::Value,
}

#[tokio::main]
async fn main() {
dotenv().ok();
Expand All @@ -23,8 +41,27 @@ async fn main() {
let oracle =
Pyth { instance: PythOracleContract::new(id, wallet.clone()), wallet: wallet.clone() };

// Price feed ids to update
let ids = vec![
BTC_USD_PRICE_FEED_ID,
ETH_USD_PRICE_FEED_ID,
UNI_USD_PRICE_FEED_ID,
USDC_USD_PRICE_FEED_ID,
];

let mut params = Vec::new();
for price_id in ids {
params.push(("ids[]", price_id.to_string()));
}

let http_client = ClientBuilder::new().timeout(Duration::from_millis(5000)).build().unwrap();
let url = "https://hermes.pyth.network/v2/updates/price/latest";

loop {
let update_data = update_data_bytes(None).await.unwrap();
let response = http_client.get(url).query(&params).send().await.unwrap();
let price_update_json = response.json::<PriceUpdate>().await.unwrap();
let update_data =
vec![Bytes(hex::decode(price_update_json.binary.data[0].clone()).unwrap())];
let fee = oracle.update_fee(&update_data).await.unwrap().value;
let update_result = oracle.update_price_feeds(fee, &update_data).await;

Expand Down
12 changes: 9 additions & 3 deletions contracts/market/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,25 @@ version = "0.1.0"
edition = "2021"
license = "Apache-2.0"

[lib]
path = "core/lib.rs"

[[test]]
harness = true
name = "integration_tests"
path = "tests/harness.rs"

[dependencies]
pyth_mock = { path = "../pyth-mock" }
fuels = { workspace = true }

[dev-dependencies]
market_sdk = { workspace = true }
token_sdk = { workspace = true }
pyth_mock_sdk = { workspace = true }
pyth_sdk = { workspace = true }
src20_sdk = { workspace = true }
fuels = { workspace = true }
tokio = { workspace = true }
dotenv = { workspace = true }
reqwest = { workspace = true }
serde = { workspace = true }
serde_json = { workspace = true }
rand = { workspace = true }
Expand Down
6 changes: 6 additions & 0 deletions contracts/market/core/contract_types.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
use fuels::macros::abigen;

abigen!(Contract(
name = "Market",
abi = "contracts/market/out/release/market-abi.json"
));
3 changes: 3 additions & 0 deletions contracts/market/core/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
mod contract_types;

pub use contract_types::*;
20 changes: 10 additions & 10 deletions contracts/market/tests/local_tests/functions/pause.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
use crate::utils::contracts_utils::market_utils::{
get_market_config, MarketContract, PauseConfiguration,
};
use crate::utils::contracts_utils::token_utils::deploy_tokens;
use market::PauseConfiguration;
use token_sdk::{TokenAsset, TokenContract};

use crate::utils::init_wallets;
use crate::utils::number_utils::parse_units;
use chrono::Utc;
use fuels::prelude::ViewOnlyAccount;
use fuels::types::{Address, Bits256, ContractId};
use pyth_mock::PythMockContract;
use market_sdk::{get_market_config, parse_units, MarketContract};
use pyth_mock_sdk::PythMockContract;

#[tokio::test]
async fn pause_test() {
Expand All @@ -25,15 +24,17 @@ async fn pause_test() {
let oracle_contract_id = ContractId::from(oracle.instance.contract_id());

//--------------- TOKENS ---------------
let (assets, asset_configs, token_contract) = deploy_tokens(&admin, false).await;
let token_contract = TokenContract::deploy(&admin).await.unwrap();
let (assets, asset_configs) = token_contract.deploy_tokens(&admin).await;

let usdc = assets.get("USDC").unwrap();
let usdc_contract = src20_sdk::token_utils::Asset::new(
let usdc_contract = TokenAsset::new(
admin.clone(),
token_contract.contract_id().into(),
&usdc.symbol,
);
let uni = assets.get("UNI").unwrap();
let uni_contract = src20_sdk::token_utils::Asset::new(
let uni_contract = TokenAsset::new(
admin.clone(),
token_contract.contract_id().into(),
&uni.symbol,
Expand All @@ -48,7 +49,6 @@ async fn pause_test() {
usdc.bits256,
usdc.decimals as u32,
usdc.price_feed_id,
oracle_contract_id,
fuel_eth_base_asset_id,
)
.unwrap();
Expand Down
18 changes: 9 additions & 9 deletions contracts/market/tests/local_tests/main_test_uni.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use crate::utils::contracts_utils::market_utils::{get_market_config, MarketContract};
use crate::utils::contracts_utils::token_utils::deploy_tokens;
use crate::utils::number_utils::parse_units;
use crate::utils::{init_wallets, print_case_title};
use chrono::Utc;
use fuels::prelude::ViewOnlyAccount;
use fuels::types::{Address, Bits256, ContractId};
use pyth_mock::PythMockContract;
use market_sdk::{get_market_config, parse_units, MarketContract};
use pyth_mock_sdk::PythMockContract;
use token_sdk::{TokenAsset, TokenContract};

use crate::utils::{init_wallets, print_case_title};

// Multiplies all values by this number
// It is necessary in order to test how the protocol works with large amounts
Expand All @@ -32,16 +32,17 @@ async fn main_test() {
let oracle_contract_id = ContractId::from(oracle.instance.contract_id());

//--------------- TOKENS ---------------
let (assets, asset_configs, token_contract) = deploy_tokens(&admin, false).await;
let token_contract = TokenContract::deploy(&admin).await.unwrap();
let (assets, asset_configs) = token_contract.deploy_tokens(&admin).await;

let usdc = assets.get("USDC").unwrap();
let usdc_contract = src20_sdk::token_utils::Asset::new(
let usdc_contract = TokenAsset::new(
admin.clone(),
token_contract.contract_id().into(),
&usdc.symbol,
);
let uni = assets.get("UNI").unwrap();
let uni_contract = src20_sdk::token_utils::Asset::new(
let uni_contract = TokenAsset::new(
admin.clone(),
token_contract.contract_id().into(),
&uni.symbol,
Expand All @@ -56,7 +57,6 @@ async fn main_test() {
usdc.bits256,
usdc.decimals as u32,
usdc.price_feed_id,
oracle_contract_id,
fuel_eth_base_asset_id,
)
.unwrap();
Expand Down
Loading

0 comments on commit ba15f96

Please sign in to comment.