Struct soroban_sdk::Env

source ·
pub struct Env { /* private fields */ }
Expand description

The Env type provides access to the environment the contract is executing within.

The Env provides access to information about the currently executing contract, who invoked it, contract data, functions for signing, hashing, etc.

Most types require access to an Env to be constructed or converted.

Implementations§

Get the invoking Address of the current executing contract.

Get a Data for accessing and update contract data that has been stored by the currently executing contract.

Get Events for publishing events associated with the currently executing contract.

Get a Ledger for accessing the current ledger.

Get an Accounts for accessing accounts in the current ledger.

Get a deployer for deploying contracts.

Get the 32-byte hash identifier of the current executing contract.

Returns the contract call stack as a Vec of (contract_id, function_name).

Examples
use soroban_sdk::{contractimpl, BytesN, Env, Symbol, symbol};

pub struct Contract;

#[contractimpl]
impl Contract {
    pub fn hello(env: Env) {
        let stack = env.call_stack();
        assert_eq!(stack.len(), 1);

        let outer = stack.get(0).unwrap().unwrap();
        assert_eq!(outer.0, BytesN::from_array(&env, &[0; 32]));
        assert_eq!(outer.1, symbol!("hello"));
    }
}
let env = Env::default();
let contract_id = BytesN::from_array(&env, &[0; 32]);
env.register_contract(&contract_id, Contract);
let client = ContractClient::new(&env, &contract_id);
client.hello();

Computes a SHA-256 hash.

Verifies an ed25519 signature.

The ed25519 signature (sig) is verified as a valid signature of the message (msg) by the ed25519 public key (pk).

Panics

Will panic if the signature verification fails.

TODO

Return a Result instead of panicking.

Invokes a function of a contract that is registered in the Env.

Panics

Will panic if the contract_id does not match a registered contract, func does not match a function of the referenced contract, or the number of args do not match the argument count of the referenced contract function.

Will panic if the contract that is invoked fails or aborts in anyway.

Will panic if the value returned from the contract cannot be converted into the type T.

Invokes a function of a contract that is registered in the Env, returns an error if the invocation fails for any reason.

Get the Logger for logging debug events.

Available on crate feature testutils only.

Creates a new Env loaded with the LedgerSnapshot.

The ledger info and state in the snapshot are aloaded into the Env.

Available on crate feature testutils only.

Creates a new Env loaded with the ledger snapshot loaded from the file.

Panics

If there is any error writing the file.

Available on crate feature testutils only.

Create a snapshot from the Env’s current state.

Available on crate feature testutils only.

Create a snapshot file from the Env’s current state.

Panics

If there is any error writing the file.

Available on crate feature testutils only.

Sets the source account in the Env.

The source account will be accessible via Env::invoker when a contract is directly invoked.

Available on crate feature testutils only.

Gets the source account set in the Env.

Available on crate feature testutils only.

Run the closure as if executed by the given contract ID.

Used to write or read contract data, or take other actions in tests for setting up tests or asserting on internal state.

Available on crate feature testutils only.

Register a contract with the Env for testing.

Passing a contract ID for the first arguments registers the contract with that contract ID. Providing None causes a random ID to be assigned to the contract.

Registering a contract that is already registered replaces it.

Returns the contract ID of the registered contract.

Examples
use soroban_sdk::{contractimpl, BytesN, Env, Symbol};

pub struct HelloContract;

#[contractimpl]
impl HelloContract {
    pub fn hello(env: Env, recipient: soroban_sdk::Symbol) -> soroban_sdk::Symbol {
        todo!()
    }
}

let env = Env::default();
let contract_id = BytesN::from_array(&env, &[0; 32]);
env.register_contract(&contract_id, HelloContract);
Available on crate feature testutils only.

Install the contract WASM code to the Env for testing.

Returns the hash of the installed code that can be then used for the contract deployment.

Useful for contract factory testing, otherwise use register_contract_wasm function that installs and deploys the contract in a single call.

Examples
use soroban_sdk::{BytesN, Env};

const WASM: &[u8] = include_bytes!("../doctest_fixtures/contract.wasm");

let env = Env::default();
env.install_contract_wasm(WASM);
Available on crate feature testutils only.

Register a contract in a WASM file with the Env for testing.

Passing a contract ID for the first arguments registers the contract with that contract ID. Providing None causes a random ID to be assigned to the contract.

Registering a contract that is already registered replaces it.

Returns the contract ID of the registered contract.

Examples
use soroban_sdk::{BytesN, Env};

const WASM: &[u8] = include_bytes!("../doctest_fixtures/contract.wasm");

let env = Env::default();
env.register_contract_wasm(None, WASM);
Available on crate feature testutils only.

Register the built-in token contract with the Env for testing.

Passing a contract ID for the first arguments registers the contract with that contract ID. Providing None causes a random ID to be assigned to the contract.

Registering a contract that is already registered replaces it.

Returns the contract ID of the registered contract.

Examples
use soroban_sdk::{BytesN, Env};

let env = Env::default();
env.register_contract_token(None);

Trait Implementations§

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more
Convert Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can then be further downcast into Box<ConcreteType> where ConcreteType implements Trait. Read more
Convert Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait. Read more
Convert &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s. Read more
Convert &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Should always be Self
The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.