ContractEnv

Struct 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>>) -> Self

Creates a new ContractEnv instance.

Source

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

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: ToBytes + CLTyped>(&self, key: &[u8], value: T)

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

Source

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

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

Source

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

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

Source

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

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

Source

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

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

Source

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

Removes the dictionary from the contract storage.

Source

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

Initializes the empty dictionary with the given name.

Source

pub fn caller(&self) -> Address

Returns the address of the caller of the contract.

Source

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

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 in milliseconds.

Source

pub fn get_block_time_millis(&self) -> u64

Returns the current block time in milliseconds.

Source

pub fn get_block_time_secs(&self) -> u64

Returns the current block time in seconds.

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: Into<OdraError>>(&self, error: E) -> !

Reverts the contract execution with the specified error.

Source

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

Emits an event with the specified data.

Source

pub fn emit_native_event<T: ToBytes + EventInstance>(&self, event: T)

Emits an event with the specified data using the native mechanism.

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: AsRef<[u8]>>(&self, value: T) -> [u8; 32]

Hashes the specified value.

§Returns

The hash value as a 32-byte array.

Source

pub fn delegate(&self, validator: PublicKey, amount: U512)

Delegate tokens to a validator

§Arguments
  • validator - The validator to delegate to
  • amount - The amount of tokens to delegate
Source

pub fn undelegate(&self, validator: PublicKey, amount: U512)

Undelegate tokens from a validator

§Arguments
  • validator - The validator to undelegate from
  • amount - The amount of tokens to undelegate
Source

pub fn delegated_amount(&self, validator: PublicKey) -> U512

Returns the amount of tokens delegated to a validator

§Arguments
  • validator - The validator to get the delegated amount from
§Returns

The amount of tokens delegated to the validator

Source

pub fn get_validator_info(&self, validator: PublicKey) -> Option<ValidatorInfo>

Returns information about the validator

§Arguments
  • validator - The validator to query
§Returns

Option

Source

pub fn pseudorandom_bytes(&self, size: usize) -> Vec<u8>

Returns a vector of pseudorandom bytes of the specified size. There is no guarantee that the returned bytes are in any way cryptographically secure.

Source

pub fn pseudorandom_number(&self, high: U512) -> U512

Returns a pseudorandom integer.

Trait Implementations§

Source§

impl Clone for ContractEnv

Source§

fn clone(&self) -> ContractEnv

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Revertible for ContractEnv

Source§

fn revert<E: Into<OdraError>>(&self, e: E) -> !

Reverts the contract execution with the given error.

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

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

Source§

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>,

Source§

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>,

Source§

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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more