Struct concordium_smart_contract_testing::Chain
source · 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
impl Chain
sourcepub fn new_with_time_and_rates(
block_time: SlotTime,
micro_ccd_per_euro: ExchangeRate,
euro_per_energy: ExchangeRate
) -> Result<Self, ExchangeRateError>
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
].
sourcepub fn new_with_time(block_time: SlotTime) -> Self
pub fn new_with_time(block_time: SlotTime) -> Self
Create a new Chain
with a specified block_time
where
micro_ccd_per_euro
defaults to50000 / 1
euro_per_energy
defaults to1 / 50000
.
sourcepub fn new() -> Self
pub fn new() -> Self
Create a new Chain
where
block_time
defaults to0
,micro_ccd_per_euro
defaults to50000 / 1
euro_per_energy
defaults to1 / 50000
.
With these exchange rates, one energy costs one microCCD.
sourcepub fn calculate_energy_cost(&self, energy: Energy) -> Amount
pub fn calculate_energy_cost(&self, energy: Energy) -> Amount
Helper function for converting Energy
to Amount
using the two
ExchangeRate
s euro_per_energy
and micro_ccd_per_euro
.
sourcepub fn get_contract(&self, address: ContractAddress) -> Option<&Contract>
pub fn get_contract(&self, address: ContractAddress) -> Option<&Contract>
Get the state of the contract if it exists in the Chain
.
sourcepub fn get_module(&self, module: ModuleReference) -> Option<&ContractModule>
pub fn get_module(&self, module: ModuleReference) -> Option<&ContractModule>
Get the the module if it exists in the Chain
.
sourcepub fn get_account(&self, address: AccountAddress) -> Option<&Account>
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.
sourcepub fn module_deploy_v1(
&mut self,
signer: Signer,
sender: AccountAddress,
wasm_module: WasmModule
) -> Result<ModuleDeploySuccess, ModuleDeployError>
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.
sourcepub fn contract_init(
&mut self,
signer: Signer,
sender: AccountAddress,
energy_reserved: Energy,
payload: InitContractPayload
) -> Result<ContractInitSuccess, ContractInitError>
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 thesender
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.
sourcepub fn contract_update(
&mut self,
signer: Signer,
invoker: AccountAddress,
sender: Address,
energy_reserved: Energy,
payload: UpdateContractPayload
) -> Result<ContractInvokeSuccess, ContractInvokeError>
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, thesender
is always theinvoker
. 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.
sourcepub fn contract_invoke(
&self,
invoker: AccountAddress,
sender: Address,
energy_reserved: Energy,
payload: UpdateContractPayload
) -> Result<ContractInvokeSuccess, ContractInvokeError>
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.
sourcepub fn create_account(&mut self, account: Account) -> Option<Account>
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))
);
sourcepub fn account_balance(&self, address: AccountAddress) -> Option<AccountBalance>
pub fn account_balance(&self, address: AccountAddress) -> Option<AccountBalance>
Returns the balance of an account if it exists.
sourcepub fn account_balance_available(
&self,
address: AccountAddress
) -> Option<Amount>
pub fn account_balance_available( &self, address: AccountAddress ) -> Option<Amount>
Returns the available balance of an account if it exists.
sourcepub fn contract_balance(&self, address: ContractAddress) -> Option<Amount>
pub fn contract_balance(&self, address: ContractAddress) -> Option<Amount>
Returns the balance of an contract if it exists.
sourcepub fn contract_state_lookup(
&self,
address: ContractAddress,
key: &[u8]
) -> Option<Vec<u8>>
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.
sourcepub fn account(
&self,
address: AccountAddress
) -> Result<&Account, AccountDoesNotExist>
pub fn account( &self, address: AccountAddress ) -> Result<&Account, AccountDoesNotExist>
Returns an immutable reference to an Account
.
sourcepub fn account_exists(&self, address: AccountAddress) -> bool
pub fn account_exists(&self, address: AccountAddress) -> bool
Check whether an Account
exists.
sourcepub fn contract_exists(&self, address: ContractAddress) -> bool
pub fn contract_exists(&self, address: ContractAddress) -> bool
Check whether a Contract
exists.
sourcepub fn set_exchange_rates(
&mut self,
micro_ccd_per_euro: ExchangeRate,
euro_per_energy: ExchangeRate
) -> Result<(), ExchangeRateError>
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
.
sourcepub fn micro_ccd_per_euro(&self) -> ExchangeRate
pub fn micro_ccd_per_euro(&self) -> ExchangeRate
Return the current microCCD per euro exchange rate.
sourcepub fn euro_per_energy(&self) -> ExchangeRate
pub fn euro_per_energy(&self) -> ExchangeRate
Return the current euro per energy exchange rate.