cosmic-interchain-cli
Version:
A command-line utility for Cosmic Wire's interchain messaging protocol
76 lines • 3.46 kB
JavaScript
import { stringify as yamlStringify } from 'yaml';
import { HyperlaneCore, HyperlaneRelayer } from '@hyperlane-xyz/sdk';
import { addressToBytes32, timeout } from '@hyperlane-xyz/utils';
import { MINIMUM_TEST_SEND_GAS } from '../consts.js';
import { runPreflightChecksForChains } from '../deploy/utils.js';
import { errorRed, log, logBlue, logGreen } from '../logger.js';
import { runSingleChainSelectionStep } from '../utils/chains.js';
import { indentYamlOrJson } from '../utils/files.js';
import { stubMerkleTreeConfig } from '../utils/relay.js';
export async function sendTestMessage({ context, origin, destination, messageBody, timeoutSec, skipWaitForDelivery, selfRelay, }) {
const { chainMetadata } = context;
if (!origin) {
origin = await runSingleChainSelectionStep(chainMetadata, 'Select the origin chain');
}
if (!destination) {
destination = await runSingleChainSelectionStep(chainMetadata, 'Select the destination chain');
}
await runPreflightChecksForChains({
context,
chains: [origin, destination],
chainsToGasCheck: [origin],
minGas: MINIMUM_TEST_SEND_GAS,
});
await timeout(executeDelivery({
context,
origin,
destination,
messageBody,
skipWaitForDelivery,
selfRelay,
}), timeoutSec * 1000, 'Timed out waiting for messages to be delivered');
}
async function executeDelivery({ context, origin, destination, messageBody, skipWaitForDelivery, selfRelay, }) {
const { registry, multiProvider } = context;
const chainAddresses = await registry.getAddresses();
const core = HyperlaneCore.fromAddressesMap(chainAddresses, multiProvider);
const hook = chainAddresses[origin]?.customHook;
if (hook) {
logBlue(`Using custom hook ${hook} for ${origin} -> ${destination}`);
}
try {
const recipient = chainAddresses[destination].testRecipient;
if (!recipient) {
throw new Error(`Unable to find TestRecipient for ${destination}`);
}
const formattedRecipient = addressToBytes32(recipient);
log('Dispatching message');
const { dispatchTx, message } = await core.sendMessage(origin, destination, formattedRecipient, messageBody, hook, undefined);
logBlue(`Sent message from ${origin} to ${recipient} on ${destination}.`);
logBlue(`Message ID: ${message.id}`);
log(`Message:\n${indentYamlOrJson(yamlStringify(message, null, 2), 4)}`);
if (selfRelay) {
const relayer = new HyperlaneRelayer({ core });
const hookAddress = await core.getSenderHookAddress(message);
const merkleAddress = chainAddresses[origin].merkleTreeHook;
stubMerkleTreeConfig(relayer, origin, hookAddress, merkleAddress);
log('Attempting self-relay of message');
await relayer.relayMessage(dispatchTx);
logGreen('Message was self-relayed!');
}
else {
if (skipWaitForDelivery) {
return;
}
log('Waiting for message delivery on destination chain...');
// Max wait 10 minutes
await core.waitForMessageProcessed(dispatchTx, 10000, 60);
logGreen('Message was delivered!');
}
}
catch (e) {
errorRed(`Encountered error sending message from ${origin} to ${destination}`);
throw e;
}
}
//# sourceMappingURL=message.js.map