Skip to main content

Interaction Between Programs and Asynchronous Programming

Asynchronous interaction between Vara programs is similar to the usual asynchronous request in using await and implemented by sending a message.

Program entry points​

If a program's logic implies asynchronous messaging, its main executable functions must differ from those used in synchronous communications.

async init()​

In case of an asynchronous call in the program initialization, the async init() must be used instead of init(). Also, it should be preceded by the gstd::async_init macro:

async fn init() {
gstd::debug!("Hello world!");

async main()​

The same for asynchronous messages, the async main() must be used instead of handle() and handle_reply(). Also, it should be preceded by the gstd::async_main macro:

async fn main() {
gstd::debug!("Hello world!");

async init сan be used together with async main. But functions init, handle_reply cannot be specified if this macro is used.

Cross-program message

To send a message to a Vara program, use the msg::send_for_reply() function. In this function:

  • program - the address of the program to send the message for;
  • payload - the message to the program;
  • value - the funds attached to the message (zero if no value is attached);
  • reply_deposit - used to provide gas for future reply handling (skipped if zero).

To get an encoded reply from another actor use the msg::send_for_reply_as() function.

pub fn send_for_reply_as<E: Encode, D: Decode>(
program: ActorId,
payload: E,
value: u128,
reply_deposit: u64
) -> Result<CodecMessageFuture<D>>

Usage example:

let reply: SomeEvent = msg::send_for_reply_as(
SomeAction {
command: 42,
.expect("Unable to send message")
.expect("Error in receiving reply");