pub type TestReceiveContext<'a> = TestContext<'a, TestReceiveOnlyData>;
👎Deprecated since 8.1.0: Deprecated in favor of concordium-smart-contract-testing.
Expand description

Placeholder for the receiving context. All the fields can be set optionally and the getting an unset field will result in calling fail!. Use only in tests!

§Setters

Every field have a setter function prefixed with set_.

§Example

Creating an empty context and setting the init_origin.

let owner = AccountAddress([0u8; 32]);
let mut ctx = TestReceiveContext::empty();
ctx.set_owner(owner);
ctx.set_sender(Address::Account(owner));

§Set chain meta data

Chain meta data is set using setters on the context or by setters on a mutable reference of TestChainMeta.

§Example

Creating an empty context and setting the slot_time metadata.

let mut ctx = TestReceiveContext::empty();
ctx.set_metadata_slot_time(Timestamp::from_timestamp_millis(1609459200));

or

let mut ctx = TestReceiveContext::empty();
ctx.metadata_mut().set_slot_time(Timestamp::from_timestamp_millis(1609459200));

§Use case example

Creating a context for running unit tests

#[receive(contract = "mycontract", name = "receive", mutable)]
fn contract_receive<S: HasStateApi>(
    ctx: &impl HasReceiveContext,
    host: &mut impl HasHost<State, StateApiType = S>,
) -> ReceiveResult<()> {
    ensure!(ctx.sender().matches_account(&ctx.owner()));
    // ...
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;
    use concordium_sc_base::test_infrastructure::*;
    #[test]
    fn test() {
        let owner = AccountAddress([0u8; 32]);
        let mut ctx = TestReceiveContext::empty();
        ctx.set_owner(owner);
        ctx.set_sender(Address::Account(owner));
        // ...
        let result: ReceiveResult<ActionsTree> = contract_receive(&ctx, 0, &mut logger, state);
    }
}

Aliased Type§

struct TestReceiveContext<'a> { /* private fields */ }

Implementations§

source§

impl<'a> TestReceiveContext<'a>

source

pub fn empty() -> Self

👎Deprecated since 8.1.0: Deprecated in favor of concordium-smart-contract-testing.

Create a TestReceiveContext where every field is unset, and getting any of the fields will result in fail!.

source

pub fn set_invoker(&mut self, value: AccountAddress) -> &mut Self

👎Deprecated since 8.1.0: Deprecated in favor of concordium-smart-contract-testing.
source

pub fn set_self_address(&mut self, value: ContractAddress) -> &mut Self

👎Deprecated since 8.1.0: Deprecated in favor of concordium-smart-contract-testing.
source

pub fn set_sender(&mut self, value: Address) -> &mut Self

👎Deprecated since 8.1.0: Deprecated in favor of concordium-smart-contract-testing.
source

pub fn set_owner(&mut self, value: AccountAddress) -> &mut Self

👎Deprecated since 8.1.0: Deprecated in favor of concordium-smart-contract-testing.
source

pub fn set_named_entrypoint(&mut self, value: OwnedEntrypointName) -> &mut Self

👎Deprecated since 8.1.0: Deprecated in favor of concordium-smart-contract-testing.

Trait Implementations§

source§

impl<'a> HasReceiveContext for TestReceiveContext<'a>

§

type ReceiveData = ()

source§

fn open(_data: Self::ReceiveData) -> Self

Open the receive context for reading and accessing values.
source§

fn invoker(&self) -> AccountAddress

Who is the account that initiated the top-level transaction this invocation is a part of.
source§

fn self_address(&self) -> ContractAddress

The address of the contract being invoked.
source§

fn sender(&self) -> Address

The immediate sender of the message. In general different from the invoker.
source§

fn owner(&self) -> AccountAddress

Account which created the contract instance.
source§

fn named_entrypoint(&self) -> OwnedEntrypointName

Get the name of the entrypoint that was named. In case a default entrypoint is invoked this can be different from the name of the entrypoint that is being executed.