From 8bda61f72cd9bb74729414d81b2d3132a9ee567d Mon Sep 17 00:00:00 2001 From: Willem Wyndham Date: Fri, 17 Nov 2023 12:09:15 -0500 Subject: [PATCH] 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 --- Cargo.lock | 9 +++++---- crates/smartdeploy-cli/Cargo.toml | 1 + crates/smartdeploy-cli/src/bin/main.rs | 4 +++- crates/smartdeploy-cli/src/commands/deploy.rs | 4 ++-- crates/smartdeploy-cli/src/testnet/mod.rs | 12 ++++++++---- crates/smartdeploy-macros/src/lib.rs | 8 ++++++-- examples/increment-init/src/counter.rs | 4 ++-- 7 files changed, 27 insertions(+), 15 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index a6bfdb7..834df93 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2557,6 +2557,7 @@ dependencies = [ "assert_cmd", "assert_fs", "clap", + "dotenvy", "ed25519-dalek 2.0.0", "heck", "loam-sdk", @@ -2752,9 +2753,9 @@ dependencies = [ [[package]] name = "soroban-spec-json" -version = "20.0.0-rc3" +version = "20.0.0-rc4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a653b46a0c8075483efa85312d7053e6291cff8f99fdc8ef1d39d7b09a75e037" +checksum = "8f562402230eed7948609d6b7c2de1937127070ab0c2f4dde62980e32c37aa51" dependencies = [ "serde", "serde_derive", @@ -2801,9 +2802,9 @@ dependencies = [ [[package]] name = "soroban-spec-typescript" -version = "20.0.0-rc3" +version = "20.0.0-rc4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "27b9e4683a20da28d0b3a73db8fbbf0e58cc17d5758c813a036dc7ff82e70d7f" +checksum = "01891cd62cb54a4fc7c6cc72e64cd109187b4ad5d028470ff8d0654643dbe3f2" dependencies = [ "base64 0.21.4", "heck", diff --git a/crates/smartdeploy-cli/Cargo.toml b/crates/smartdeploy-cli/Cargo.toml index 63f2476..3b63a17 100644 --- a/crates/smartdeploy-cli/Cargo.toml +++ b/crates/smartdeploy-cli/Cargo.toml @@ -45,6 +45,7 @@ soroban-spec-tools = "20.0.0-rc3" heck = "0.4.1" ed25519-dalek = "2.0.0" stellar-strkey = "0.0.8" +dotenvy = "0.15.7" [dev-dependencies] assert_cmd = "2.0.4" diff --git a/crates/smartdeploy-cli/src/bin/main.rs b/crates/smartdeploy-cli/src/bin/main.rs index 30e3318..2628812 100644 --- a/crates/smartdeploy-cli/src/bin/main.rs +++ b/crates/smartdeploy-cli/src/bin/main.rs @@ -4,7 +4,9 @@ use smartdeploy_cli::{testnet, Root}; #[tokio::main] async fn main() { - std::env::set_var("SOROBAN_CONTRACT_ID", testnet::contract_id()); + let _ = dotenvy::dotenv().unwrap_or_default(); + let contract_id = testnet::contract_id(); + std::env::set_var("SOROBAN_CONTRACT_ID", contract_id); std::env::set_var("SOROBAN_RPC_URL", testnet::rpc_url()); std::env::set_var("SOROBAN_NETWORK_PASSPHRASE", testnet::network_passphrase()); std::env::remove_var("SOROBAN_NETWORK"); diff --git a/crates/smartdeploy-cli/src/commands/deploy.rs b/crates/smartdeploy-cli/src/commands/deploy.rs index 17a0c78..377c177 100644 --- a/crates/smartdeploy-cli/src/commands/deploy.rs +++ b/crates/smartdeploy-cli/src/commands/deploy.rs @@ -104,8 +104,6 @@ impl Cmd { // Get the account sequence number let public_strkey = stellar_strkey::ed25519::PublicKey(key.verifying_key().to_bytes()).to_string(); - let account_details = client.get_account(&public_strkey).await?; - let sequence: i64 = account_details.seq_num.into(); let (function_symbol_arg, final_args) = build_host_function_parameters( &self.deployed_name, @@ -139,6 +137,8 @@ impl Cmd { .unwrap(), }; + let account_details = client.get_account(&public_strkey).await?; + let sequence: i64 = account_details.seq_num.into(); let tx = build_invoke_contract_tx(invoke_contract_args, sequence + 1, self.fee.fee, &key)?; let ( _, diff --git a/crates/smartdeploy-cli/src/testnet/mod.rs b/crates/smartdeploy-cli/src/testnet/mod.rs index 0fd3fca..6d21194 100644 --- a/crates/smartdeploy-cli/src/testnet/mod.rs +++ b/crates/smartdeploy-cli/src/testnet/mod.rs @@ -6,12 +6,16 @@ use soroban_cli::{ const CONTRACT_ID: &str = include_str!("./smartdeploy.json"); -pub fn contract_id() -> &'static str { - CONTRACT_ID.trim_end().trim_matches('"') +pub fn contract_id() -> String { + if let Ok(contract_id) = std::env::var("SMARTDEPLOY_CONTRACT_ID") { + contract_id + } else { + CONTRACT_ID.trim_end().trim_matches('"').to_owned() + } } pub fn contract_id_strkey() -> stellar_strkey::Contract { - stellar_strkey::Contract::from_string(contract_id()).unwrap() + stellar_strkey::Contract::from_string(&contract_id()).unwrap() } pub fn contract_address() -> ScAddress { @@ -28,7 +32,7 @@ pub fn network_passphrase() -> String { pub fn build_invoke_cmd(slop: &[&str]) -> invoke::Cmd { invoke::Cmd { - contract_id: contract_id().to_owned(), + contract_id: contract_id(), wasm: None, cost: false, unlimited_budget: false, diff --git a/crates/smartdeploy-macros/src/lib.rs b/crates/smartdeploy-macros/src/lib.rs index 3df2f69..33ae5f2 100644 --- a/crates/smartdeploy-macros/src/lib.rs +++ b/crates/smartdeploy-macros/src/lib.rs @@ -84,9 +84,13 @@ pub fn import_contract(tokens: TokenStream) -> TokenStream { use soroban_sdk::TryFromVal; soroban_sdk::contractimport!(file = #file); - pub fn new(env: &Env) -> Client { + pub fn address(env: &Env) -> soroban_sdk::Address { let bytes: soroban_sdk::BytesN<32> = soroban_sdk::Bytes::from_slice(&env, &[#(#id),*]).try_into().unwrap(); - let contract_id = &soroban_sdk::Address::from_contract_id(&bytes); + soroban_sdk::Address::from_contract_id(&bytes) + } + + pub fn new(env: &Env) -> Client { + let contract_id = &address(env); Client::new(env, contract_id) } } diff --git a/examples/increment-init/src/counter.rs b/examples/increment-init/src/counter.rs index 2b93c71..1f114d0 100644 --- a/examples/increment-init/src/counter.rs +++ b/examples/increment-init/src/counter.rs @@ -9,7 +9,7 @@ pub trait IsRiff { /// Increment increments an internal counter, and returns the value. fn increment(&mut self) -> u32; - fn init_counter(&mut self, num: u32); + fn init(&mut self, num: u32); } #[contracttype] @@ -23,7 +23,7 @@ impl IsRiff for Impl { self.0 } - fn init_counter(&mut self, num: u32) { + fn init(&mut self, num: u32) { self.0 = num; }