Skip to content

Commit 8bda61f

Browse files
authored
feat: allow setting different smartdeploy contract id (#29)
* feat: allow setting different smartdeploy contract id Also add dotenvy support * feat: allow generated contract interface to return address
1 parent 70034a1 commit 8bda61f

File tree

7 files changed

+27
-15
lines changed

7 files changed

+27
-15
lines changed

Cargo.lock

Lines changed: 5 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/smartdeploy-cli/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ soroban-spec-tools = "20.0.0-rc3"
4545
heck = "0.4.1"
4646
ed25519-dalek = "2.0.0"
4747
stellar-strkey = "0.0.8"
48+
dotenvy = "0.15.7"
4849

4950
[dev-dependencies]
5051
assert_cmd = "2.0.4"

crates/smartdeploy-cli/src/bin/main.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ use smartdeploy_cli::{testnet, Root};
44

55
#[tokio::main]
66
async fn main() {
7-
std::env::set_var("SOROBAN_CONTRACT_ID", testnet::contract_id());
7+
let _ = dotenvy::dotenv().unwrap_or_default();
8+
let contract_id = testnet::contract_id();
9+
std::env::set_var("SOROBAN_CONTRACT_ID", contract_id);
810
std::env::set_var("SOROBAN_RPC_URL", testnet::rpc_url());
911
std::env::set_var("SOROBAN_NETWORK_PASSPHRASE", testnet::network_passphrase());
1012
std::env::remove_var("SOROBAN_NETWORK");

crates/smartdeploy-cli/src/commands/deploy.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,6 @@ impl Cmd {
104104
// Get the account sequence number
105105
let public_strkey =
106106
stellar_strkey::ed25519::PublicKey(key.verifying_key().to_bytes()).to_string();
107-
let account_details = client.get_account(&public_strkey).await?;
108-
let sequence: i64 = account_details.seq_num.into();
109107

110108
let (function_symbol_arg, final_args) = build_host_function_parameters(
111109
&self.deployed_name,
@@ -139,6 +137,8 @@ impl Cmd {
139137
.unwrap(),
140138
};
141139

140+
let account_details = client.get_account(&public_strkey).await?;
141+
let sequence: i64 = account_details.seq_num.into();
142142
let tx = build_invoke_contract_tx(invoke_contract_args, sequence + 1, self.fee.fee, &key)?;
143143
let (
144144
_,

crates/smartdeploy-cli/src/testnet/mod.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,16 @@ use soroban_cli::{
66

77
const CONTRACT_ID: &str = include_str!("./smartdeploy.json");
88

9-
pub fn contract_id() -> &'static str {
10-
CONTRACT_ID.trim_end().trim_matches('"')
9+
pub fn contract_id() -> String {
10+
if let Ok(contract_id) = std::env::var("SMARTDEPLOY_CONTRACT_ID") {
11+
contract_id
12+
} else {
13+
CONTRACT_ID.trim_end().trim_matches('"').to_owned()
14+
}
1115
}
1216

1317
pub fn contract_id_strkey() -> stellar_strkey::Contract {
14-
stellar_strkey::Contract::from_string(contract_id()).unwrap()
18+
stellar_strkey::Contract::from_string(&contract_id()).unwrap()
1519
}
1620

1721
pub fn contract_address() -> ScAddress {
@@ -28,7 +32,7 @@ pub fn network_passphrase() -> String {
2832

2933
pub fn build_invoke_cmd(slop: &[&str]) -> invoke::Cmd {
3034
invoke::Cmd {
31-
contract_id: contract_id().to_owned(),
35+
contract_id: contract_id(),
3236
wasm: None,
3337
cost: false,
3438
unlimited_budget: false,

crates/smartdeploy-macros/src/lib.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,13 @@ pub fn import_contract(tokens: TokenStream) -> TokenStream {
8484
use soroban_sdk::TryFromVal;
8585
soroban_sdk::contractimport!(file = #file);
8686

87-
pub fn new(env: &Env) -> Client {
87+
pub fn address(env: &Env) -> soroban_sdk::Address {
8888
let bytes: soroban_sdk::BytesN<32> = soroban_sdk::Bytes::from_slice(&env, &[#(#id),*]).try_into().unwrap();
89-
let contract_id = &soroban_sdk::Address::from_contract_id(&bytes);
89+
soroban_sdk::Address::from_contract_id(&bytes)
90+
}
91+
92+
pub fn new(env: &Env) -> Client {
93+
let contract_id = &address(env);
9094
Client::new(env, contract_id)
9195
}
9296
}

examples/increment-init/src/counter.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ pub trait IsRiff {
99
/// Increment increments an internal counter, and returns the value.
1010
fn increment(&mut self) -> u32;
1111

12-
fn init_counter(&mut self, num: u32);
12+
fn init(&mut self, num: u32);
1313
}
1414

1515
#[contracttype]
@@ -23,7 +23,7 @@ impl IsRiff for Impl {
2323
self.0
2424
}
2525

26-
fn init_counter(&mut self, num: u32) {
26+
fn init(&mut self, num: u32) {
2727
self.0 = num;
2828
}
2929

0 commit comments

Comments
 (0)