Struct odra::ContractEnv

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

Represents the environment accessible in the contract context.

The ContractEnv struct provides methods for interacting with the contract environment, such as accessing storage, calling other contracts, and handling various contract-related operations.

The ContractEnv is available for the user to use in the module code.

Implementations§

source§

impl ContractEnv

source

pub const fn new( index: u32, backend: Rc<RefCell<dyn ContractContext>>, ) -> ContractEnv

Creates a new ContractEnv instance.

source

pub fn get_value<T>(&self, key: &[u8]) -> Option<T>
where T: FromBytes,

Retrieves the value associated with the given key from the contract storage.

§Returns

The value associated with the key, if it exists.

source

pub fn set_value<T>(&self, key: &[u8], value: T)
where T: ToBytes + CLTyped,

Sets the value associated with the given key in the contract storage.

source

pub fn get_named_value<T, U>(&self, name: U) -> Option<T>
where T: FromBytes + CLTyped, U: AsRef<str>,

Retrieves the value associated with the given named key from the contract storage.

source

pub fn set_named_value<T, U>(&self, name: U, value: T)
where T: CLTyped + ToBytes, U: AsRef<str>,

Sets the value associated with the given named key in the contract storage.

source

pub fn get_dictionary_value<T, U>( &self, dictionary_name: U, key: &[u8], ) -> Option<T>
where T: FromBytes + CLTyped, U: AsRef<str>,

Retrieves the value associated with the given named key from the named dictionary in the contract storage.

source

pub fn set_dictionary_value<T, U>( &self, dictionary_name: U, key: &[u8], value: T, )
where T: CLTyped + ToBytes, U: AsRef<str>,

Sets the value associated with the given named key in the named dictionary in the contract storage.

source

pub fn remove_dictionary<U>(&self, dictionary_name: U)
where U: AsRef<str>,

Removes the dictionary from the contract storage.

source

pub fn caller(&self) -> Address

Returns the address of the caller of the contract.

source

pub fn call_contract<T>(&self, address: Address, call: CallDef) -> T
where T: FromBytes,

Calls another contract with the specified address and call definition.

§Returns

The result of the contract call. If any error occurs during the call, the contract will revert.

source

pub fn self_address(&self) -> Address

Returns the address of the current contract.

source

pub fn transfer_tokens(&self, to: &Address, amount: &U512)

Transfers tokens to the specified address.

source

pub fn get_block_time(&self) -> u64

Returns the current block time as u64 value.

source

pub fn attached_value(&self) -> U512

Returns the value attached to the contract call.

source

pub fn self_balance(&self) -> U512

Returns the CSPR balance of the current contract.

source

pub fn revert<E>(&self, error: E) -> !
where E: Into<OdraError>,

Reverts the contract execution with the specified error.

source

pub fn emit_event<T>(&self, event: T)
where T: ToBytes,

Emits an event with the specified data.

source

pub fn verify_signature( &self, message: &Bytes, signature: &Bytes, public_key: &PublicKey, ) -> bool

Verifies the signature of a message using the specified signature, public key, and message.

§Arguments
  • message - The message to verify.
  • signature - The signature to verify.
  • public_key - The public key to use for verification.
§Returns

true if the signature is valid, false otherwise.

source

pub fn hash<T>(&self, value: T) -> [u8; 32]
where T: AsRef<[u8]>,

Hashes the specified value.

§Returns

The hash value as a 32-byte array.

Trait Implementations§

source§

impl Clone for ContractEnv

source§

fn clone(&self) -> ContractEnv

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> DynClone for T
where T: Clone,

source§

fn __clone_box(&self, _: Private) -> *mut ()

source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

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

source§

impl<T> Same for T

§

type Output = T

Should always be Self
source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.