pub struct Chain { /* private fields */ }
Expand description

Represents the blockchain and supports a number of operations, including creating accounts, deploying modules, initializing contract, updating contracts and invoking contracts.

Implementations§

source§

impl Chain

source

pub fn new_with_time_and_rates( block_time: SlotTime, micro_ccd_per_euro: ExchangeRate, euro_per_energy: ExchangeRate ) -> Result<Self, ExchangeRateError>

Create a new Chain where all the configurable parameters are provided.

Returns an error if the exchange rates provided makes one energy cost more than u64::MAX / [MAX_ALLOWED_INVOKE_ENERGY].

source

pub fn new_with_time(block_time: SlotTime) -> Self

Create a new Chain with a specified block_time where

  • micro_ccd_per_euro defaults to 50000 / 1
  • euro_per_energy defaults to 1 / 50000.
source

pub fn new() -> Self

Create a new Chain where

  • block_time defaults to 0,
  • micro_ccd_per_euro defaults to 50000 / 1
  • euro_per_energy defaults to 1 / 50000.

With these exchange rates, one energy costs one microCCD.

source

pub fn calculate_energy_cost(&self, energy: Energy) -> Amount

Helper function for converting Energy to Amount using the two ExchangeRates euro_per_energy and micro_ccd_per_euro.

source

pub fn get_contract(&self, address: ContractAddress) -> Option<&Contract>

Get the state of the contract if it exists in the Chain.

source

pub fn get_module(&self, module: ModuleReference) -> Option<&ContractModule>

Get the the module if it exists in the Chain.

source

pub fn get_account(&self, address: AccountAddress) -> Option<&Account>

Get the state of the account if it exists in the Chain. Account addresses that are aliases will return the same account.

source

pub fn module_deploy_v1( &mut self, signer: Signer, sender: AccountAddress, wasm_module: WasmModule ) -> Result<ModuleDeploySuccess, ModuleDeployError>

Deploy a smart contract module.

The WasmModule can be loaded from disk with either module_load_v1 or module_load_v1_raw.

Parameters:

  • signer: the signer with a number of keys, which affects the cost.
  • sender: the sender account.
  • module: the v1 wasm module.
source

pub fn contract_init( &mut self, signer: Signer, sender: AccountAddress, energy_reserved: Energy, payload: InitContractPayload ) -> Result<ContractInitSuccess, ContractInitError>

Initialize a contract.

Parameters:

  • signer: the signer with a number of keys, which affects the cost.
  • sender: The account paying for the transaction. Will also become the owner of the contract created.
  • energy_reserved: Amount of energy reserved for executing the init method.
  • payload:
    • amount: The initial balance of the contract. Subtracted from the sender account.
    • mod_ref: The reference to the a module that has already been deployed.
    • init_name: Name of the contract to initialize.
    • param: Parameter provided to the init method.
source

pub fn contract_update( &mut self, signer: Signer, invoker: AccountAddress, sender: Address, energy_reserved: Energy, payload: UpdateContractPayload ) -> Result<ContractInvokeSuccess, ContractInvokeError>

Update a contract by calling one of its entrypoints.

If successful, all changes will be saved.

Parameters:

  • invoker: the account paying for the transaction.
  • sender: the sender of the message, can be an account or contract. For top-level invocations, such as those caused by sending a contract update transaction on the chain, the sender is always the invoker. Here we provide extra freedom for testing invocations where the sender differs.
  • contract_address: the contract to update.
  • entrypoint: the entrypoint to call.
  • parameter: the contract parameter.
  • amount: the amount sent to the contract.
  • energy_reserved: the maximum energy that can be used in the update.
source

pub fn contract_invoke( &self, invoker: AccountAddress, sender: Address, energy_reserved: Energy, payload: UpdateContractPayload ) -> Result<ContractInvokeSuccess, ContractInvokeError>

Invoke a contract by calling an entrypoint.

Similar to Chain::contract_update except that all changes are discarded afterwards. Typically used for “view” functions.

Parameters:

  • invoker: the account paying for the transaction.
  • sender: the sender of the transaction, can also be a contract.
  • contract_address: the contract to update.
  • entrypoint: the entrypoint to call.
  • parameter: the contract parameter.
  • amount: the amount sent to the contract.
  • energy_reserved: the maximum energy that can be used in the update.
source

pub fn create_account(&mut self, account: Account) -> Option<Account>

Create an account.

If an account with a matching address already exists this method will replace it and return the old account.

Note that if the first 29-bytes of an account are identical, then they are considered aliases on each other in all methods. See the example below:

let mut chain = Chain::new();
let acc = AccountAddress([
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0,
]);
let acc_alias = AccountAddress([
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
    2, 3, // Only last three bytes differ.
]);

chain.create_account(Account::new(acc, Amount::from_ccd(123)));
assert_eq!(
    chain.account_balance_available(acc_alias), // Using the alias for lookup.
    Some(Amount::from_ccd(123))
);
source

pub fn account_balance(&self, address: AccountAddress) -> Option<AccountBalance>

Returns the balance of an account if it exists.

source

pub fn account_balance_available( &self, address: AccountAddress ) -> Option<Amount>

Returns the available balance of an account if it exists.

source

pub fn contract_balance(&self, address: ContractAddress) -> Option<Amount>

Returns the balance of an contract if it exists.

source

pub fn contract_state_lookup( &self, address: ContractAddress, key: &[u8] ) -> Option<Vec<u8>>

Helper method for looking up part of the state of a smart contract, which is a key-value store.

source

pub fn account( &self, address: AccountAddress ) -> Result<&Account, AccountDoesNotExist>

Returns an immutable reference to an Account.

source

pub fn account_exists(&self, address: AccountAddress) -> bool

Check whether an Account exists.

source

pub fn contract_exists(&self, address: ContractAddress) -> bool

Check whether a Contract exists.

source

pub fn set_exchange_rates( &mut self, micro_ccd_per_euro: ExchangeRate, euro_per_energy: ExchangeRate ) -> Result<(), ExchangeRateError>

Try to set the exchange rates on the chain.

Will fail if they result in the cost of one energy being larger than u64::MAX / MAX_ALLOWED_INVOKE_ENERGY.

source

pub fn micro_ccd_per_euro(&self) -> ExchangeRate

Return the current microCCD per euro exchange rate.

source

pub fn euro_per_energy(&self) -> ExchangeRate

Return the current euro per energy exchange rate.

Trait Implementations§

source§

impl Debug for Chain

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Default for Chain

source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl RefUnwindSafe for Chain

§

impl Send for Chain

§

impl Sync for Chain

§

impl Unpin for Chain

§

impl UnwindSafe for Chain

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere 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.

§

impl<T> Pointable for T

§

const ALIGN: usize = mem::align_of::<T>()

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
source§

impl<T> Same<T> for T

§

type Output = T

Should always be Self
source§

impl<T, U> TryFrom<U> for Twhere 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 Twhere 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.
§

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

§

fn vzip(self) -> V