Send Message
Use api.message.send
method to send messages to the program:
try {
const message = {
destination: destination, // programId
payload: somePayload,
gasLimit: 10000000,
value: 1000,
};
// In that case payload will be encoded using meta.handle_input type
let extrinsic = gearApi.message.send(message, meta);
// So if you want to use another type you can specify it
extrinsic = gearApi.message.send(message, meta, meta.async_handle_input);
} catch (error) {
console.error(`${error.name}: ${error.message}`);
}
try {
await extrinsic.signAndSend(keyring, (event) => {
console.log(event.toHuman());
});
} catch (error) {
console.error(`${error.name}: ${error.message}`);
}
note
In real conditions to ensure successful message processing, the calculation of the required gas for processing the message should be performed by using api.program.calculateGas
method.
Send reply message
When you need to reply to a message received from a program, use api.message.reply
:
try {
const reply = {
replyToId: messageId,
payload: somePayload,
gasLimit: 10000000,
value: 1000,
};
// In this case payload will be encoded using `meta.async_handle_input` type if it exsits,
// otherwise `meta.async_init_input` will be used
const extrinsic = gearApi.message.sendReply(reply, meta);
} catch (error) {
console.error(`${error.name}: ${error.message}`);
}
try {
await extrinsic(keyring, (events) => {
console.log(event.toHuman());
});
} catch (error) {
console.error(`${error.name}: ${error.message}`);
}