Skip to content

Commit

Permalink
feat: initial pass on refactoring contract and sdk rust structure
Browse files Browse the repository at this point in the history
  • Loading branch information
martines3000 committed Aug 7, 2024
1 parent 476ea36 commit 80abb6c
Show file tree
Hide file tree
Showing 30 changed files with 260 additions and 83 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"
}
}
50 changes: 47 additions & 3 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",
# Libs
"libs/src20_sdk",
"libs/market_sdk",
"libs/token_sdk",
"libs/pyth_mock_sdk",
# Test
"contracts/test",
]
resolver = "2"

[workspace.dependencies]
# Local SDKs
src20_sdk = { path = "libs/src20_sdk" }
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" }

# 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
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"
53 changes: 51 additions & 2 deletions apps/oracle/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,29 @@
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::programs::calls::CallParameters;
use fuels::types::{AssetId, Bytes};
use market_sdk::MarketContract;
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 @@ -13,18 +33,47 @@ async fn main() {
let provider = Provider::connect(rpc).await.unwrap();
let secret = std::env::var("SECRET").unwrap();
let oracle_address = std::env::var("ORACLE_ADDRESS").unwrap();
let market_address = std::env::var("MARKET_ADDRESS").unwrap();

// Wallet
let wallet =
WalletUnlocked::new_from_private_key(secret.parse().unwrap(), Some(provider.clone()));

// Market contract
let market_contract_id = ContractId::from_str(&market_address).unwrap();
let market_contract = MarketContract::new(market_contract_id, wallet.clone()).await;

// Pyth contract
let id = ContractId::from_str(&oracle_address).unwrap();
let oracle =
Pyth { instance: PythOracleContract::new(id, wallet.clone()), wallet: wallet.clone() };

// Base asset id
let base_asset_id =
AssetId::from_str("0xf8f8b6283d7fa5b672b530cbb84fcccb4ff8dc40f8176ef4544ddb1f1952ad07")
.unwrap();

// 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
10 changes: 9 additions & 1 deletion contracts/market/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,21 @@ 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 }
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::*;
12 changes: 5 additions & 7 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 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;
use token_sdk::deploy_tokens;

#[tokio::test]
async fn pause_test() {
Expand Down Expand Up @@ -48,7 +47,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
11 changes: 5 additions & 6 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::deploy_tokens;

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 Down Expand Up @@ -56,7 +56,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
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
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::deploy_tokens;

// Multiplies all values by this number
// It is necessary in order to test how the protocol works with large amounts
Expand Down Expand Up @@ -55,7 +54,6 @@ async fn main_test_no_debug() {
usdc.bits256,
usdc.decimals as u32,
usdc.price_feed_id,
oracle_contract_id,
fuel_eth_base_asset_id,
)
.unwrap();
Expand Down
13 changes: 6 additions & 7 deletions contracts/market/tests/scripts/deploy.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
use crate::utils::contracts_utils::{
market_utils::{get_market_config, MarketContract},
token_utils::load_tokens,
};
use dotenv::dotenv;
use fuels::{
prelude::{Provider, WalletUnlocked},
types::{Bits256, ContractId},
types::{AssetId, Bits256, ContractId},
};
use market_sdk::{get_market_config, MarketContract};
use pyth_sdk::constants::USDC_USD_PRICE_FEED_ID;
use std::{path::PathBuf, str::FromStr};
use token_sdk::load_tokens;

#[tokio::test]
async fn deploy() {
Expand Down Expand Up @@ -39,15 +37,16 @@ async fn deploy() {

//--------------- MARKET ---------------
let usdc_price_feed = Bits256::from_hex_str(USDC_USD_PRICE_FEED_ID).unwrap();
let fuel_eth_base_asset_id = Bits256::zeroed(); // TODO[Martin]: Change to ETH asset id (everywhere)
let fuel_eth_base_asset_id =
Bits256::from_hex_str("0xf8f8b6283d7fa5b672b530cbb84fcccb4ff8dc40f8176ef4544ddb1f1952ad07")
.unwrap();

let market_config = get_market_config(
wallet.address().into(),
wallet.address().into(),
usdc.bits256,
usdc.decimals as u32,
usdc_price_feed,
oracle_id,
fuel_eth_base_asset_id,
)
.unwrap();
Expand Down
2 changes: 1 addition & 1 deletion contracts/market/tests/scripts/deploy_tokens.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::utils::contracts_utils::token_utils::deploy_tokens;
use dotenv::dotenv;
use fuels::accounts::{provider::Provider, wallet::WalletUnlocked};
use token_sdk::deploy_tokens;

#[tokio::test]
async fn deploy() {
Expand Down
2 changes: 1 addition & 1 deletion contracts/market/tests/scripts/mint_tokens.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use crate::utils::contracts_utils::token_utils::load_tokens;
use dotenv::dotenv;
use fuels::{
accounts::{provider::Provider, wallet::WalletUnlocked},
types::ContractId,
};
use std::{path::PathBuf, str::FromStr};
use token_sdk::load_tokens;

#[tokio::test]
async fn mint() {
Expand Down
Loading

0 comments on commit 80abb6c

Please sign in to comment.