Skip to main content

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,
// prepaid: true,
// account: accountId,
// if you send message with issued voucher
};
// In that case payload will be encoded using meta.types.handle.input type
let extrinsic = api.message.send(message, meta);
// So if you want to use another type you can specify it
extrinsic = api.message.send(message, meta, meta.types.other.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.

more info

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,
// prepaid: true,
// account: accountId,
// if you send message with issued voucher
};
// In this case payload will be encoded using `meta.types.reply.input` type.
const extrinsic = api.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}`);
}