Skip to content

Commit

Permalink
feat: allow setting different smartdeploy contract id (#29)
Browse files Browse the repository at this point in the history
* feat: allow setting different smartdeploy contract id

Also add dotenvy support

* feat: allow generated contract interface to return address
  • Loading branch information
willemneal authored Nov 17, 2023
1 parent 70034a1 commit 8bda61f
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 15 deletions.
9 changes: 5 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/smartdeploy-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
4 changes: 3 additions & 1 deletion crates/smartdeploy-cli/src/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
4 changes: 2 additions & 2 deletions crates/smartdeploy-cli/src/commands/deploy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 (
_,
Expand Down
12 changes: 8 additions & 4 deletions crates/smartdeploy-cli/src/testnet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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,
Expand Down
8 changes: 6 additions & 2 deletions crates/smartdeploy-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down
4 changes: 2 additions & 2 deletions examples/increment-init/src/counter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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;
}

Expand Down

0 comments on commit 8bda61f

Please sign in to comment.