Skip to content

Commit

Permalink
feat: first it demo
Browse files Browse the repository at this point in the history
- Replaced `setup_client` with `setup_testnet_client` in various CLI modules
- Simplified client initialization and removed some unused imports
- Updated command implementations to use new client setup method
- Commented out some verbose logging in sync command
- Added `pm-demo` crate to workspace configuration
  • Loading branch information
JordyRo1 committed Feb 21, 2025
1 parent 6c344e3 commit f29d08a
Show file tree
Hide file tree
Showing 24 changed files with 947 additions and 52 deletions.
19 changes: 19 additions & 0 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 Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ members = [
"crates/cli/oracle",
"crates/cli/publisher",
"crates/cli/utils",
"crates/demo",
]
resolver = "1"
default-members = [
Expand All @@ -13,6 +14,7 @@ default-members = [
"crates/cli/oracle",
"crates/cli/publisher",
"crates/cli/utils",
"crates/demo"
]

[workspace.lints.rust]
Expand All @@ -32,6 +34,7 @@ pm-accounts = { path = "crates/accounts", default-features = false }
pm-oracle-cli = { path = "crates/cli/oracle", default-features = false }
pm-publisher-cli = { path = "crates/cli/publisher", default-features = false }
pm-utils-cli = { path = "crates/cli/utils", default-features = false }
pm-demo = { path = "crates/demo", default-features = false }

anyhow = "1.0.93"
async-trait = "0.1.83"
Expand Down
9 changes: 5 additions & 4 deletions crates/cli/oracle/src/commands/get_entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use pm_utils_cli::{
#[clap(about = "Gets entry")]
pub struct GetEntryCmd {
// Input pair (format example: "BTC/USD")
publisher_id: String, // TO REMOVE
pair: String,
}

Expand All @@ -28,14 +29,14 @@ impl GetEntryCmd {
let oracle_id = pragma_storage.get_key(ORACLE_ACCOUNT_COLUMN).unwrap();
let oracle_id = AccountId::from_hex(oracle_id).unwrap();

let publisher_id = pragma_storage.get_key(PUBLISHER_ACCOUNT_COLUMN).unwrap();
let publisher_id = AccountId::from_hex(publisher_id).unwrap();
// let publisher_id: &String = pragma_storage.get_key(PUBLISHER_ACCOUNT_COLUMN).unwrap();
let publisher_id = AccountId::from_hex(&self.publisher_id).unwrap();
let publisher = client
.get_account(publisher_id)
.await
.unwrap()
.expect("Publisher account not found");
let oracle = client
let _ = client
.get_account(oracle_id)
.await
.unwrap()
Expand Down Expand Up @@ -83,7 +84,7 @@ impl GetEntryCmd {
.with_custom_script(get_entry_script)
.unwrap()
.build();
let tx_result = client
let _ = client
.new_transaction(oracle_id, transaction_request)
.await
.map_err(|e| anyhow::anyhow!("Error while creating a transaction: {e:?}"))?;
Expand Down
10 changes: 5 additions & 5 deletions crates/cli/oracle/src/commands/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,11 @@ impl InitCmd {
println!(
"{}",
r#"
╭────────────────────────────────────────────────────────────╮
│ pm-oracle-cli register-publisher 0x12345678
│ pm-oracle-cli entry 0x12345678 BTC/USD │
│ pm-oracle-cli median BTC/USD │
╰────────────────────────────────────────────────────────────╯
╭────────────────────────────────────────────────────────────────────
│ pm-oracle-cli register-publisher 0x64cbfe4bc88cfe00000556901757eb
│ pm-oracle-cli entry 96310150000 BTC/USD
│ pm-oracle-cli median BTC/USD
╰────────────────────────────────────────────────────────────────────
"#
.bright_blue()
);
Expand Down
7 changes: 2 additions & 5 deletions crates/cli/oracle/src/commands/median.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ use pm_accounts::oracle::get_oracle_component_library;
use pm_accounts::utils::word_to_masm;
use pm_types::Pair;
use pm_utils_cli::{JsonStorage, ORACLE_ACCOUNT_COLUMN, PRAGMA_ACCOUNTS_STORAGE_FILE};
use std::fmt::Write; // Changed from io::Write to fmt::Write
use std::str::FromStr;
use std::sync::{Arc, Mutex};

#[derive(clap::Parser, Debug, Clone)]
#[clap(about = "Compute the median for a given pair")]
Expand All @@ -27,7 +25,7 @@ impl MedianCmd {

let oracle_id = pragma_storage.get_key(ORACLE_ACCOUNT_COLUMN).unwrap();
let oracle_id = AccountId::from_hex(oracle_id).unwrap();

client.sync_state().await.unwrap();
let oracle = client
.get_account(oracle_id)
.await
Expand All @@ -54,7 +52,6 @@ impl MedianCmd {
})
.collect::<Result<_, _>>()
.context("Failed to collect publisher array")?;

let mut foreign_accounts: Vec<ForeignAccount> = vec![];
for publisher_id in publisher_array {
let publisher = client
Expand Down Expand Up @@ -108,7 +105,7 @@ impl MedianCmd {

let transaction_request = transaction_request.build();

let result = client
let _ = client
.new_transaction(oracle_id, transaction_request)
.await
.map_err(|e| anyhow::anyhow!("Error while creating a transaction: {e:?}"))?;
Expand Down
6 changes: 3 additions & 3 deletions crates/cli/oracle/src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use publishers::PublishersCmd;
use register_publisher::RegisterPublisherCmd;
use sync::SyncCmd;

use pm_utils_cli::{setup_client, STORE_FILENAME};
use pm_utils_cli::{setup_client, setup_testnet_client, STORE_FILENAME};

#[derive(Debug, Parser, Clone)]
pub enum SubCommand {
Expand Down Expand Up @@ -48,8 +48,8 @@ pub enum SubCommand {

impl SubCommand {
pub async fn call(&self) -> anyhow::Result<()> {
let exec_dir = PathBuf::new();
let store_config = exec_dir.join(STORE_FILENAME);
let crate_path = PathBuf::new();
let store_config = crate_path.join(STORE_FILENAME);
let mut client = setup_client(store_config).await.unwrap();

match self {
Expand Down
36 changes: 18 additions & 18 deletions crates/cli/oracle/src/commands/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,24 @@ impl SyncCmd {

println!("🔁 Sync successful!\n");

println!("State synced to block {}", new_details.block_num);
println!("New public notes: {}", new_details.received_notes.len());
println!(
"Tracked notes updated: {}",
new_details.committed_notes.len()
);
println!(
"Tracked notes consumed: {}",
new_details.consumed_notes.len()
);
println!(
"Tracked accounts updated: {}",
new_details.updated_accounts.len()
);
println!(
"Commited transactions: {}",
new_details.committed_transactions.len()
);
// println!("State synced to block {}", new_details.block_num);
// println!("New public notes: {}", new_details.received_notes.len());
// println!(
// "Tracked notes updated: {}",
// new_details.committed_notes.len()
// );
// println!(
// "Tracked notes consumed: {}",
// new_details.consumed_notes.len()
// );
// println!(
// "Tracked accounts updated: {}",
// new_details.updated_accounts.len()
// );
// println!(
// "Commited transactions: {}",
// new_details.committed_transactions.len()
// );
Ok(())
}
}
5 changes: 5 additions & 0 deletions crates/cli/publisher/pragma_miden.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"data": {
"publisher_account_id": "0x2fc8bcad8a205780000507e88e8332"
}
}
1 change: 0 additions & 1 deletion crates/cli/publisher/src/commands/entry.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use chrono::{DateTime, Utc};
use miden_client::{account::AccountId, crypto::FeltRng, Client};
use pm_types::{Entry, Pair};
use pm_utils_cli::{JsonStorage, PRAGMA_ACCOUNTS_STORAGE_FILE, PUBLISHER_ACCOUNT_COLUMN};
use prettytable::{Cell, Row, Table};
use std::str::FromStr;

Expand Down
1 change: 0 additions & 1 deletion crates/cli/publisher/src/commands/get_entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use miden_client::{account::AccountId, crypto::FeltRng};
use pm_accounts::publisher::get_publisher_component_library;
use pm_accounts::utils::word_to_masm;
use pm_types::Pair;
use pm_utils_cli::{JsonStorage, PRAGMA_ACCOUNTS_STORAGE_FILE, PUBLISHER_ACCOUNT_COLUMN};
use std::str::FromStr;

#[derive(clap::Parser, Debug, Clone)]
Expand Down
6 changes: 3 additions & 3 deletions crates/cli/publisher/src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use clap::Parser;
use entry::EntryCmd;
use get_entry::GetEntryCmd;
use init::InitCmd;
use pm_utils_cli::{setup_client, STORE_FILENAME};
use pm_utils_cli::{setup_client, setup_testnet_client, STORE_FILENAME};
use publish::PublishCmd;
use sync::SyncCmd;

Expand All @@ -35,8 +35,8 @@ pub enum SubCommand {

impl SubCommand {
pub async fn call(&self) -> anyhow::Result<()> {
let exec_dir = PathBuf::new();
let store_config = exec_dir.join(STORE_FILENAME);
let crate_path = PathBuf::new();
let store_config = crate_path.join(STORE_FILENAME);
let mut client = setup_client(store_config).await.unwrap();

match self {
Expand Down
3 changes: 1 addition & 2 deletions crates/cli/publisher/src/commands/publish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use miden_client::{

use pm_accounts::{publisher::get_publisher_component_library, utils::word_to_masm};
use pm_types::{Entry, Pair};
use pm_utils_cli::{JsonStorage, PRAGMA_ACCOUNTS_STORAGE_FILE};

#[derive(clap::Parser, Debug, Clone)]
#[clap(about = "Publish an entry(Callable by the publisher itself)")]
Expand All @@ -24,7 +23,7 @@ pub struct PublishCmd {

impl PublishCmd {
pub async fn call(&self, client: &mut Client<impl FeltRng>) -> anyhow::Result<()> {
let pragma_storage = JsonStorage::new(PRAGMA_ACCOUNTS_STORAGE_FILE)?;
// let pragma_storage = JsonStorage::new(PRAGMA_ACCOUNTS_STORAGE_FILE)?;
// let publisher_id = pragma_storage.get_key(PUBLISHER_ACCOUNT_COLUMN).unwrap();
let publisher_id = &self.publisher;
let publisher_id = AccountId::from_hex(publisher_id).unwrap();
Expand Down
14 changes: 7 additions & 7 deletions crates/cli/publisher/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ mod commands;
use crate::commands::{
entry::EntryCmd, get_entry::GetEntryCmd, init::InitCmd, publish::PublishCmd, sync::SyncCmd,
};
use pm_utils_cli::setup_client;
use pm_utils_cli::setup_testnet_client;

pub const STORE_SIMPLE_FILENAME: &str = "store.sqlite3";

Expand All @@ -19,7 +19,7 @@ fn py_init(oracle_id: Option<String>) -> PyResult<String> {
let exec_dir = PathBuf::new();
let store_config = exec_dir.join(STORE_SIMPLE_FILENAME);
println!("store_config: {:?}", store_config);
let mut client = setup_client(store_config).await.unwrap();
let mut client = setup_testnet_client().await.unwrap();

let cmd = InitCmd { oracle_id };
cmd.call(&mut client)
Expand All @@ -45,7 +45,7 @@ fn py_publish(
rt.block_on(async {
let exec_dir = PathBuf::new();
let store_config = exec_dir.join(STORE_SIMPLE_FILENAME);
let mut client = setup_client(store_config).await.unwrap();
let mut client = setup_testnet_client().await.unwrap();

let cmd = PublishCmd {
publisher,
Expand All @@ -71,7 +71,7 @@ fn py_get_entry(publisher_id: String, pair: String) -> PyResult<String> {
rt.block_on(async {
let exec_dir = PathBuf::new();
let store_config = exec_dir.join(STORE_SIMPLE_FILENAME);
let mut client = setup_client(store_config).await.unwrap();
let mut client = setup_testnet_client().await.unwrap();

let cmd = GetEntryCmd { publisher_id, pair };
cmd.call(&mut client)
Expand All @@ -91,7 +91,7 @@ fn py_entry(publisher_id: String, pair: String) -> PyResult<String> {
rt.block_on(async {
let exec_dir = PathBuf::new();
let store_config = exec_dir.join(STORE_SIMPLE_FILENAME);
let mut client = setup_client(store_config).await.unwrap();
let mut client = setup_testnet_client().await.unwrap();

let cmd = EntryCmd { publisher_id, pair };
cmd.call(&mut client)
Expand All @@ -111,7 +111,7 @@ fn py_sync() -> PyResult<String> {
rt.block_on(async {
let exec_dir = PathBuf::new();
let store_config = exec_dir.join(STORE_SIMPLE_FILENAME);
let mut client = setup_client(store_config).await.unwrap();
let mut client = setup_testnet_client().await.unwrap();

let cmd = SyncCmd {};
cmd.call(&mut client)
Expand All @@ -124,7 +124,7 @@ fn py_sync() -> PyResult<String> {

/// Python module
#[pymodule]
fn pm_publisher(py: Python<'_>, m: &PyModule) -> PyResult<()> {
fn pm_publisher(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
m.add_wrapped(wrap_pyfunction!(py_init))?;
m.add_wrapped(wrap_pyfunction!(py_publish))?;
m.add_wrapped(wrap_pyfunction!(py_get_entry))?;
Expand Down
2 changes: 1 addition & 1 deletion crates/cli/utils/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ pub async fn setup_client(path: PathBuf) -> Result<Client<RpoRandomCoin>, Client
Ok(client)
}

pub async fn initialize_testnet_client() -> Result<Client<RpoRandomCoin>, ClientError> {
pub async fn setup_testnet_client() -> Result<Client<RpoRandomCoin>, ClientError> {
// RPC endpoint and timeout
let endpoint = Endpoint::new(
"https".to_string(),
Expand Down
35 changes: 35 additions & 0 deletions crates/demo/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
[package]
name = "pm-demo"
description = "Pragma-Miden demo"
authors.workspace = true
homepage.workspace = true
edition.workspace = true
repository.workspace = true
version.workspace = true
license.workspace = true

[lib]
name = "pm_demo"
path = "src/lib.rs"

[[bin]]
name = "pm-demo"
path = "src/main.rs"

[lints]
workspace = true

[dependencies]
miden-assembly.workspace = true
miden-client.workspace = true
miden-crypto.workspace = true
miden-lib.workspace = true
miden-objects.workspace = true
miden-tx.workspace = true
pm-types.workspace = true
tokio.workspace = true
pm-utils-cli.workspace = true
anyhow.workspace = true
rand.workspace = true
rand_chacha.workspace = true
pm-accounts.workspace = true
Loading

0 comments on commit f29d08a

Please sign in to comment.