Skip to content

Commit 473ee39

Browse files
committed
feat: use config to resolve registry contract id
1 parent 79e694d commit 473ee39

File tree

8 files changed

+119
-144
lines changed

8 files changed

+119
-144
lines changed

crates/stellar-registry-cli/src/bin/main.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
11
use clap::{CommandFactory, Parser};
22

3-
use stellar_registry_cli::{testnet, Root};
3+
use stellar_registry_cli::Root;
44

55
#[tokio::main]
66
async fn main() {
77
let _ = dotenvy::dotenv().unwrap_or_default();
8-
let contract_id = testnet::contract_id_strkey();
9-
std::env::set_var("STELLAR_CONTRACT_ID", contract_id.to_string());
10-
// std::env::set_var("SOROBAN_RPC_URL", testnet::rpc_url());
11-
// std::env::set_var("SOROBAN_NETWORK_PASSPHRASE", testnet::network_passphrase());
12-
// std::env::remove_var("SOROBAN_NETWORK");
138
let mut root = Root::try_parse().unwrap_or_else(|e| {
149
let mut cmd = Root::command();
1510
e.format(&mut cmd).exit();

crates/stellar-registry-cli/src/commands/deploy.rs

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@ use soroban_sdk::xdr::{
1010
Transaction, TransactionExt, Uint256, VecM,
1111
};
1212
use stellar_cli::{
13-
assembled::simulate_and_assemble_transaction, commands::contract::invoke, config, fee,
13+
assembled::simulate_and_assemble_transaction, commands::contract::{arg_parsing, invoke}, config, fee,
1414
utils::rpc::get_remote_wasm_from_hash,
1515
};
1616

1717
use soroban_rpc as rpc;
1818
pub use soroban_spec_tools::contract as contract_spec;
1919

20-
use crate::testnet::{self, contract_address, invoke_registry};
20+
use crate::contract::NetworkContract;
2121

2222
#[derive(Parser, Debug, Clone)]
2323
pub struct Cmd {
@@ -85,18 +85,19 @@ impl Cmd {
8585
}
8686

8787
pub async fn hash(&self) -> Result<xdr::Hash, Error> {
88-
let res = invoke_registry(
89-
&["fetch_hash", "--wasm_name", &self.wasm_name],
90-
&self.config,
91-
&self.fee,
92-
)
93-
.await?;
88+
let res = self
89+
.config
90+
.invoke_registry(
91+
&["fetch_hash", "--wasm_name", &self.wasm_name],
92+
Some(&self.fee),
93+
)
94+
.await?;
9495
let res = res.trim_matches('"');
9596
Ok(res.parse().unwrap())
9697
}
9798

9899
pub async fn wasm(&self) -> Result<Vec<u8>, Error> {
99-
Ok(get_remote_wasm_from_hash(&testnet::client()?, &self.hash().await?).await?)
100+
Ok(get_remote_wasm_from_hash(&self.config.rpc_client()?, &self.hash().await?).await?)
100101
}
101102

102103
pub async fn spec_entries(&self) -> Result<Vec<ScSpecEntry>, Error> {
@@ -106,22 +107,22 @@ impl Cmd {
106107
}
107108

108109
async fn invoke(&self) -> Result<(), Error> {
109-
let client = testnet::client()?;
110+
let client = self.config.rpc_client()?;
110111
let key = self.config.key_pair()?;
111112
let config = &self.config;
112113

113114
// Get the account sequence number
114115
let public_strkey =
115116
stellar_strkey::ed25519::PublicKey(key.verifying_key().to_bytes()).to_string();
116117

117-
let contract_address = contract_address();
118-
let contract_id = &testnet::contract_id_strkey();
118+
let contract_address = self.config.contract_sc_address()?;
119+
let contract_id = &self.config.contract_id()?;
119120
let spec_entries = self.spec_entries().await?;
120121

121122
let (args, signers) = if self.slop.is_empty() {
122123
(ScVal::Void, vec![])
123124
} else {
124-
let res = stellar_cli::commands::contract::arg_parsing::build_host_function_parameters(
125+
let res = arg_parsing::build_host_function_parameters(
125126
contract_id,
126127
&self.slop,
127128
&spec_entries,
@@ -144,7 +145,7 @@ impl Cmd {
144145
));
145146
(args, signers)
146147
}
147-
Err(stellar_cli::commands::contract::arg_parsing::Error::HelpMessage(help)) => {
148+
Err(arg_parsing::Error::HelpMessage(help)) => {
148149
println!("{help}");
149150
return Ok(());
150151
}

crates/stellar-registry-cli/src/commands/install.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,10 @@ use clap::Parser;
33
use stellar_cli::{
44
commands::contract::{fetch, invoke},
55
config,
6-
fee::Args,
76
};
87
use stellar_strkey::Contract;
98

10-
use crate::testnet;
9+
use crate::contract::NetworkContract;
1110

1211
#[derive(Parser, Debug, Clone)]
1312
pub struct Cmd {
@@ -66,12 +65,7 @@ impl Cmd {
6665

6766
// Use this.config directly
6867
eprintln!("Fetching contract ID via registry...");
69-
let raw = testnet::invoke_registry(
70-
&slop,
71-
&self.config,
72-
&Args::default(), // fee::Args::default()
73-
)
74-
.await?;
68+
let raw = self.config.invoke_registry(&slop, None).await?;
7569

7670
let contract_id = raw.trim_matches('"').to_string();
7771
Ok(contract_id.parse()?)

crates/stellar-registry-cli/src/commands/publish.rs

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ use clap::Parser;
44

55
use soroban_sdk::xdr::{ScMetaEntry, ScMetaV0};
66
use stellar_cli::{commands::contract::invoke, config, fee};
7-
87
pub use soroban_spec_tools::contract as contract_spec;
98

10-
use crate::testnet::invoke_registry;
9+
use crate::contract::NetworkContract;
10+
1111

1212
#[derive(Parser, Debug, Clone)]
1313
pub struct Cmd {
@@ -69,22 +69,18 @@ impl Cmd {
6969
}
7070
}
7171
}));
72-
// Use the provided author or the default keypair
73-
let author = self.author.clone().unwrap_or_else(|| {
74-
self.config
75-
.key_pair()
76-
.map(|key| {
77-
stellar_strkey::ed25519::PublicKey(key.verifying_key().to_bytes()).to_string()
78-
})
79-
.unwrap_or_default()
80-
});
72+
// Use the provided author or the source account
73+
let author = if let Some(author) = self.author.clone() {
74+
author
75+
} else {
76+
self.config.source_account().await?.to_string()
77+
};
8178
args.push(format!("--author={author}"));
8279

8380
// Pass config and fee to invoke_registry
84-
invoke_registry(
81+
self.config.invoke_registry(
8582
&args.iter().map(String::as_str).collect::<Vec<_>>(),
86-
&self.config,
87-
&self.fee,
83+
Some(&self.fee),
8884
)
8985
.await?;
9086

crates/stellar-registry-cli/src/contract.rs

Lines changed: 91 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,85 @@
11
use sha2::{Digest, Sha256};
2-
use stellar_cli::xdr::{self, WriteXdr};
2+
3+
use soroban_rpc as rpc;
4+
use stellar_cli::{
5+
commands::{contract::invoke, NetworkRunnable},
6+
config::{self, network::Network, UnresolvedContract},
7+
xdr::{self, WriteXdr},
8+
};
9+
10+
pub trait NetworkContract {
11+
fn contract_id(&self) -> Result<stellar_strkey::Contract, config::Error>;
12+
13+
fn contract_sc_address(&self) -> Result<xdr::ScAddress, config::Error> {
14+
Ok(xdr::ScAddress::Contract(xdr::Hash(self.contract_id()?.0)))
15+
}
16+
17+
fn invoke_registry(
18+
&self,
19+
slop: &[&str],
20+
fee: Option<&stellar_cli::fee::Args>,
21+
) -> impl std::future::Future<Output = Result<String, invoke::Error>> + Send;
22+
23+
fn rpc_client(&self) -> Result<rpc::Client, config::Error>;
24+
}
25+
26+
impl NetworkContract for config::Args {
27+
fn contract_id(&self) -> Result<stellar_strkey::Contract, config::Error> {
28+
let Network {
29+
network_passphrase, ..
30+
} = &self.get_network()?;
31+
let contract: UnresolvedContract = unsafe {
32+
if let Ok(contract_id) = std::env::var("STELLAR_REGISTRY_CONTRACT_ID") {
33+
contract_id.parse()
34+
} else {
35+
contract_id(&network_passphrase).to_string().parse()
36+
}
37+
.unwrap_unchecked()
38+
};
39+
Ok(contract.resolve_contract_id(&self.locator, network_passphrase)?)
40+
}
41+
42+
async fn invoke_registry(
43+
&self,
44+
slop: &[&str],
45+
fee: Option<&stellar_cli::fee::Args>,
46+
) -> Result<String, invoke::Error> {
47+
invoke_registry(slop, self, fee).await
48+
}
49+
50+
fn rpc_client(&self) -> Result<rpc::Client, config::Error> {
51+
Ok(rpc::Client::new(&self.get_network()?.rpc_url)?)
52+
}
53+
}
54+
55+
pub fn build_invoke_cmd(
56+
slop: &[&str],
57+
config: &stellar_cli::config::Args,
58+
fee: Option<&stellar_cli::fee::Args>,
59+
) -> Result<invoke::Cmd, config::Error> {
60+
Ok(invoke::Cmd {
61+
contract_id: UnresolvedContract::Resolved(config.contract_id()?),
62+
slop: slop.iter().map(Into::into).collect(),
63+
config: config.clone(),
64+
fee: fee.cloned().unwrap_or_default(),
65+
..Default::default()
66+
})
67+
}
68+
69+
pub async fn invoke_registry(
70+
slop: &[&str],
71+
config: &stellar_cli::config::Args,
72+
fee: Option<&stellar_cli::fee::Args>,
73+
) -> Result<String, invoke::Error> {
74+
Ok(build_invoke_cmd(slop, config, fee)?
75+
.run_against_rpc_server(Some(&stellar_cli::commands::global::Args::default()), None)
76+
.await?
77+
.into_result()
78+
.expect("Failed to parse JSON"))
79+
}
380

481
pub fn stellar_address() -> stellar_strkey::ed25519::PublicKey {
5-
"GBMJ2WUAZXELW27JLKTGXM5ZQHS4MXVUPQRDQD47J6UKRYPYUFW64LT5"
82+
"GBLTLNPISIK2JFDN42MXET7K7QLFSTPBAL5FLK7QUH2VM5HTCURFQGDK"
683
.parse()
784
.unwrap()
885
}
@@ -25,3 +102,15 @@ pub fn contract_id(network_passphrase: &str) -> stellar_strkey::Contract {
25102
.expect("HashIdPreimage should not fail encoding to xdr");
26103
stellar_strkey::Contract(Sha256::digest(preimage_xdr).into())
27104
}
105+
106+
#[cfg(test)]
107+
mod tests {
108+
#[test]
109+
fn test_contract_id() {
110+
let contract_id = super::contract_id("Test SDF Future Network ; October 2022");
111+
assert_eq!(
112+
contract_id.to_string(),
113+
"CBBL2SVHBQY35LY6IE64RHS5K2M7BXXV72KXS2CKV6MU4L3Y33HANUJ2".to_string()
114+
);
115+
}
116+
}

crates/stellar-registry-cli/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
)]
66
pub mod commands;
77
pub mod contract;
8-
pub mod testnet;
98

109
pub use commands::Root;
1110
use std::path::Path;

crates/stellar-registry-cli/src/testnet/mod.rs

Lines changed: 0 additions & 98 deletions
This file was deleted.

crates/stellar-registry-cli/src/testnet/stellar-registry.json

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)