Skip to content

Port messages API for new Virtual Machine#5122

Merged
mpapierski merged 20 commits intocasper-network:devfrom
mpapierski:gh-4993-port-messages-api
Mar 11, 2025
Merged

Port messages API for new Virtual Machine#5122
mpapierski merged 20 commits intocasper-network:devfrom
mpapierski:gh-4993-port-messages-api

Conversation

@mpapierski
Copy link
Copy Markdown
Collaborator

This PR introduces the ability for new VM smart contracts to emit events as according to the specification described in CEP88 casper-network/ceps#88

The only difference between the implementation in 2.0 in VM1 runtime and the implementation in 2.0 is that under VM2 runtime, the topic is registered lazily in the global state upon first emitting of given message. This is a quality-of-life improvement that also simplifies the code and has potential gas savings (i.e., you don't need to register a topic upfront that may never be emitted). Another difference is that VM2 runtime only allows "Bytes" variant of a payload (compared to String/Bytes option in VM1 runtime).

Usage

use casper_sdk::prelude::*;

#[casper(message)]
pub struct Transfer {
    pub from: Option<Entity>,
    pub to: Entity,
    pub amount: u64,
}

Contract code:

    #[casper(revert_on_error)]
    fn transfer(&mut self, recipient: Entity, amount: u64) -> Result<(), Cep18Error> {
        let sender = casper::get_caller();
        if sender == recipient {
            return Err(Cep18Error::CannotTargetSelfUser);
        }
        self.state_mut()
            .transfer_balance(&sender, &recipient, amount)?;

        casper::emit(Transfer {
            from: Some(sender),
            to: recipient,
            amount,
        })
        .expect("failed to emit message");

        Ok(())
    }

The schema obtained from the contract code contains details regarding the decoding of given message:

{
  ...,
  "definitions": {
    "vm2_cep18::messages::Transfer": {
      "type": "Struct",
      "items": [
        {
          "name": "from",
          "decl": "Option<Entity>"
        },
        {
          "name": "to",
          "decl": "Entity"
        },
        {
          "name": "amount",
          "decl": "U64"
        }
      ]
    },
  "messages": [
    {
      "name": "Transfer",
      "decl": "vm2_cep18::messages::Transfer"
    },
    {
      "name": "Approve",
      "decl": "vm2_cep18::messages::Approve"
    }
  ]

Closes #4993

All host function interactions are contained within `casper_sdk::casper`
module now, and also, there are improvements to the prelude module:
macros are re-exported so smart contract developers can just add the SDK
as dependency.
Comment thread executor/wasm/src/lib.rs Outdated
ExecutionKind::SessionBytes(_wasm_bytes) => Key::Account(initiator),
};

// Here we don't carry the cost from callee. Each execution g
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Incomplete comment

@mpapierski mpapierski merged commit d846844 into casper-network:dev Mar 11, 2025
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[New VM] Port messages API to emit events from contracts

2 participants