DEV Community

esteblock
esteblock

Posted on

Deterministic Address in Soroban

with Soroban, the Smart Contract Platform on Stellar we can know what the address of an smart contract will be, when we deploy the contract using a Deployer contract or Factory contract.

In fact, from after soroban-sdk version 20.0.0-rc2 we can use the deployed_address method for a DeployerWithAddress struct. For more information please check the documentation here: https://docs.rs/soroban-sdk/20.0.0-rc2/soroban_sdk/deploy/struct.DeployerWithAddress.html#method.deployed_address

We can calculate the contract address like this:

pub fn calculate_address(
    env: Env, 
    deployer: Address,
    salt: BytesN<32>,
) -> Address {

    let deployer_with_address = env.deployer().with_address(deployer.clone(), salt);

    // Calculate deterministic address:
    // This function can be called at anytime, before or after the contract is deployed, because contract addresses are deterministic.
    // https://docs.rs/soroban-sdk/20.0.0-rc2/soroban_sdk/deploy/struct.DeployerWithAddress.html#method.deployed_address
    let deterministic_address = deployer_with_address.deployed_address();
    deterministic_address
}
Enter fullscreen mode Exit fullscreen mode

I wrote a playground repo available here: https://github.com/paltalabs/deterministic-address-soroban

Where we answer the following questions:

1. Same WASM, different salt ✅

Can a deployer deploy 2 contracts with same WASM and different salt?

Answer: ✅ Yes!

Check test_deploy_from_contract_twice_same_wasm_different_salt

2. Same WASM, same salt ❌

Can a deployer deploys 2 contracts with same WASM and same salt?

Answer: ❌ No!

Check test_deploy_from_contract_twice_same_wasm_same_salt_should_panic

3. Different WASM, same salt ❌

Can a deployer deploys 2 different contracts (different WASM) with same salt?

Answer: ❌ No!

Check test_deploy_from_contract_different_wasm_same_salt_should_panic

This proves that in fact the contract addresses does not depend on the WASM, but on the combination of address & salt

4. Two deployers, same WASM, same salt ✅

Can one deployer deploys a (wasm/salt) and another deployer deploys the same (wasm/salt)?

Answer: ✅ Yes!

Check test_deploy_from_two_contract_deployers_same_wasm_same_salt

They have indeed different contract addreess, because, again, the contract address depends on the combination of address&salt.!!

5. Calculate a deterministic address

Can we calculate the address of a contract only knowing the address of the factory that will (or that already did) deploy the contract and the salt used (or to be used)?

Answer: ✅ Yes!

Check test_calculate_address

After soroban-sdk = { version = "20.0.0-rc2" }, we can use the deployed_address method for a DeployerWithAddress struct. For more information please check the documentation here: https://docs.rs/soroban-sdk/20.0.0-rc2/soroban_sdk/deploy/struct.DeployerWithAddress.html#method.deployed_address

Top comments (0)