Expand description
Deploy contains types for deploying contracts.
Contracts are assigned an ID that is derived from a set of arguments. A contract may choose which set of arguments to use to deploy with:
- Deployer::with_current_contract – A contract deployed by the currently executing contract will have an ID derived from the currently executing contract’s ID.
The deployer can be created using Env::deployer.
§Examples
§Deploy a contract without constructor (or 0-argument constructor)
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);
let contract_address = deployer.deploy_v2(wasm_hash, ());
// ...
}
}
#[test]
fn test() {
let env = Env::default();
let contract_address = env.register(Contract, ());
let contract = ContractClient::new(&env, &contract_address);
// Upload the contract code before deploying its instance.
let wasm_hash = env.deployer().upload_contract_wasm(DEPLOYED_WASM);
contract.deploy(&wasm_hash);
}
§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);
let contract_address = deployer.deploy_v2(
wasm_hash,
(1_u32, 2_i64),
);
// ...
}
}
#[test]
fn test() {
let env = Env::default();
let contract_address = env.register(Contract, ());
let contract = ContractClient::new(&env, &contract_address);
// Upload the contract code before deploying its instance.
let wasm_hash = env.deployer().upload_contract_wasm(DEPLOYED_WASM_WITH_CTOR);
contract.deploy_with_constructor(&wasm_hash);
}
§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() {
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"),
);
}
Structs§
- Deployer
- Deployer provides access to deploying contracts.
- Deployer
With Address - A deployer that deploys a contract that has its ID derived from the provided address and salt.
- Deployer
With Asset