Skip to content

Commit

Permalink
Improve deploy docs (#1416)
Browse files Browse the repository at this point in the history
### What
Remove the `let _ = deployer.deployed_address();` line from the two
existing deploy examples that are in the rust docs, and add an example
that discusses the `deployer.deployed_address()` in more detail.

<img width="948" alt="Screenshot 2024-12-13 at 11 07 22 pm"
src="https://github.com/user-attachments/assets/14735e0a-a32e-46ff-9e66-ff7ee21bdc6f"
/>

### Why
Plopping the `let _ = deployer.deployed_address();` into the middle of
an example that's about a specific scenario is distracting as to the
main purpose of the example. The docs can show how to use the function
in an example focused on it specifically.
  • Loading branch information
leighmcculloch authored Dec 13, 2024
1 parent 5a3ca3a commit e176daa
Showing 1 changed file with 47 additions and 7 deletions.
54 changes: 47 additions & 7 deletions soroban-sdk/src/deploy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,20 @@
//! use soroban_sdk::{contract, contractimpl, BytesN, Env, Symbol};
//!
//! const DEPLOYED_WASM: &[u8] = include_bytes!("../doctest_fixtures/contract.wasm");
//!
//! #[contract]
//! pub struct Contract;
//!
//! #[contractimpl]
//! impl Contract {
//! pub fn deploy(env: Env, wasm_hash: BytesN<32>) {
//! let salt = [0u8; 32];
//! let deployer = env.deployer().with_current_contract(salt);
//! // Deployed contract address is deterministic and can be accessed
//! // before deploying the contract.
//! let _ = deployer.deployed_address();
//! let contract_address = deployer.deploy_v2(wasm_hash, ());
//! // ...
//! }
//! }
//!
//! #[test]
//! fn test() {
//! # }
Expand All @@ -46,27 +47,29 @@
//! # fn main() { }
//! ```
//!
//! //! #### Deploy a contract with a multi-argument constructor
//! #### Deploy a contract with a multi-argument constructor
//!
//! ```
//! use soroban_sdk::{contract, contractimpl, BytesN, Env, Symbol, IntoVal};
//!
//! const DEPLOYED_WASM_WITH_CTOR: &[u8] = include_bytes!("../doctest_fixtures/contract_with_constructor.wasm");
//!
//! #[contract]
//! pub struct Contract;
//!
//! #[contractimpl]
//! impl Contract {
//! pub fn deploy_with_constructor(env: Env, wasm_hash: BytesN<32>) {
//! let salt = [1u8; 32];
//! let deployer = env.deployer().with_current_contract(salt);
//! // Deployed contract address is deterministic and can be accessed
//! // before deploying the contract.
//! let _ = deployer.deployed_address();
//! let contract_address = deployer.deploy_v2(
//! wasm_hash,
//! (1_u32, 2_i64),
//! );
//! // ...
//! }
//! }
//!
//! #[test]
//! fn test() {
//! # }
Expand All @@ -82,6 +85,43 @@
//! # #[cfg(not(feature = "testutils"))]
//! # fn main() { }
//! ```
//!
//! #### Derive before deployment what the address of a contract will be
//!
//! ```
//! use soroban_sdk::{contract, contractimpl, Address, BytesN, Env, Symbol, IntoVal};
//!
//! #[contract]
//! pub struct Contract;
//!
//! #[contractimpl]
//! impl Contract {
//! pub fn deploy_contract_address(env: Env) -> Address {
//! let salt = [1u8; 32];
//! let deployer = env.deployer().with_current_contract(salt);
//! // Deployed contract address is deterministic and can be accessed
//! // before deploying the contract. It is derived from the deployer
//! // (the current contract's address) and the salt passed in above.
//! deployer.deployed_address()
//! }
//! }
//!
//! #[test]
//! fn test() {
//! # }
//! # #[cfg(feature = "testutils")]
//! # fn main() {
//! let env = Env::default();
//! let contract_address = env.register(Contract, ());
//! let contract = ContractClient::new(&env, &contract_address);
//! assert_eq!(
//! contract.deploy_contract_address(),
//! Address::from_str(&env, "CBESJIMX7J53SWJGJ7WQ6QTLJI4S5LPPJNC2BNVD63GIKAYCDTDOO322"),
//! );
//! }
//! # #[cfg(not(feature = "testutils"))]
//! # fn main() { }
//! ```
use crate::{
env::internal::Env as _, unwrap::UnwrapInfallible, Address, Bytes, BytesN, ConstructorArgs,
Expand Down

0 comments on commit e176daa

Please sign in to comment.