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
impl ContractEnv
Sourcepub const fn new(index: u32, backend: Rc<RefCell<dyn ContractContext>>) -> Self
pub const fn new(index: u32, backend: Rc<RefCell<dyn ContractContext>>) -> Self
Creates a new ContractEnv instance.
Sourcepub fn get_value<T: FromBytes>(&self, key: &[u8]) -> Option<T>
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.
Sourcepub fn set_value<T: ToBytes + CLTyped>(&self, key: &[u8], value: T)
pub fn set_value<T: ToBytes + CLTyped>(&self, key: &[u8], value: T)
Sets the value associated with the given key in the contract storage.
Sourcepub fn get_named_value<T: FromBytes + CLTyped, U: AsRef<str>>(
&self,
name: U,
) -> Option<T>
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.
Sourcepub fn set_named_value<T: CLTyped + ToBytes, U: AsRef<str>>(
&self,
name: U,
value: T,
)
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.
Sourcepub fn get_dictionary_value<T: FromBytes + CLTyped, U: AsRef<str>>(
&self,
dictionary_name: U,
key: &[u8],
) -> Option<T>
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.
Sourcepub fn set_dictionary_value<T: CLTyped + ToBytes, U: AsRef<str>>(
&self,
dictionary_name: U,
key: &[u8],
value: T,
)
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.
Sourcepub fn remove_dictionary<U: AsRef<str>>(&self, dictionary_name: U)
pub fn remove_dictionary<U: AsRef<str>>(&self, dictionary_name: U)
Removes the dictionary from the contract storage.
Sourcepub fn init_dictionary<U: AsRef<str>>(&self, dictionary_name: U)
pub fn init_dictionary<U: AsRef<str>>(&self, dictionary_name: U)
Initializes the empty dictionary with the given name.
Sourcepub fn call_contract<T: FromBytes>(&self, address: Address, call: CallDef) -> T
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.
Sourcepub fn self_address(&self) -> Address
pub fn self_address(&self) -> Address
Returns the address of the current contract.
Sourcepub fn transfer_tokens(&self, to: &Address, amount: &U512)
pub fn transfer_tokens(&self, to: &Address, amount: &U512)
Transfers tokens to the specified address.
Sourcepub fn get_block_time(&self) -> u64
pub fn get_block_time(&self) -> u64
Returns the current block time in milliseconds.
Sourcepub fn get_block_time_millis(&self) -> u64
pub fn get_block_time_millis(&self) -> u64
Returns the current block time in milliseconds.
Sourcepub fn get_block_time_secs(&self) -> u64
pub fn get_block_time_secs(&self) -> u64
Returns the current block time in seconds.
Sourcepub fn attached_value(&self) -> U512
pub fn attached_value(&self) -> U512
Returns the value attached to the contract call.
Sourcepub fn self_balance(&self) -> U512
pub fn self_balance(&self) -> U512
Returns the CSPR balance of the current contract.
Sourcepub fn revert<E: Into<OdraError>>(&self, error: E) -> !
pub fn revert<E: Into<OdraError>>(&self, error: E) -> !
Reverts the contract execution with the specified error.
Sourcepub fn emit_event<T: ToBytes + EventInstance>(&self, event: T)
pub fn emit_event<T: ToBytes + EventInstance>(&self, event: T)
Emits an event with the specified data.
Sourcepub fn emit_native_event<T: ToBytes + EventInstance>(&self, event: T)
pub fn emit_native_event<T: ToBytes + EventInstance>(&self, event: T)
Emits an event with the specified data using the native mechanism.
Sourcepub fn verify_signature(
&self,
message: &Bytes,
signature: &Bytes,
public_key: &PublicKey,
) -> bool
pub fn verify_signature( &self, message: &Bytes, signature: &Bytes, public_key: &PublicKey, ) -> bool
Sourcepub fn delegate(&self, validator: PublicKey, amount: U512)
pub fn delegate(&self, validator: PublicKey, amount: U512)
Delegate tokens to a validator
§Arguments
validator- The validator to delegate toamount- The amount of tokens to delegate
Sourcepub fn undelegate(&self, validator: PublicKey, amount: U512)
pub fn undelegate(&self, validator: PublicKey, amount: U512)
Undelegate tokens from a validator
§Arguments
validator- The validator to undelegate fromamount- The amount of tokens to undelegate
Sourcepub fn delegated_amount(&self, validator: PublicKey) -> U512
pub fn delegated_amount(&self, validator: PublicKey) -> U512
Sourcepub fn get_validator_info(&self, validator: PublicKey) -> Option<ValidatorInfo>
pub fn get_validator_info(&self, validator: PublicKey) -> Option<ValidatorInfo>
Sourcepub fn pseudorandom_bytes(&self, size: usize) -> Vec<u8>
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.
Sourcepub fn pseudorandom_number(&self, high: U512) -> U512
pub fn pseudorandom_number(&self, high: U512) -> U512
Returns a pseudorandom integer.
Trait Implementations§
Source§impl Clone for ContractEnv
impl Clone for ContractEnv
Source§fn clone(&self) -> ContractEnv
fn clone(&self) -> ContractEnv
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Revertible for ContractEnv
impl Revertible for ContractEnv
Auto Trait Implementations§
impl Freeze for ContractEnv
impl !RefUnwindSafe for ContractEnv
impl !Send for ContractEnv
impl !Sync for ContractEnv
impl Unpin for ContractEnv
impl !UnwindSafe for ContractEnv
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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