@hyperlane-xyz/sdk
Version:
The official SDK for the Hyperlane Network
65 lines • 3.06 kB
JavaScript
import { assert, ensure0x, messageId, pollAsync, } from '@hyperlane-xyz/utils';
import { BaseCosmosAdapter } from '../../app/MultiProtocolApp.js';
import { ProviderType, } from '../../providers/ProviderType.js';
const MESSAGE_DISPATCH_EVENT_TYPES = [
'hyperlane.core.v1.Dispatch',
'hyperlane.core.v1.EventDispatch',
];
const MESSAGE_ATTRIBUTE_KEY = 'message';
const MESSAGE_DESTINATION_ATTRIBUTE_KEY = 'destination';
function stripWrappingQuotes(value) {
if (value.startsWith('"') && value.endsWith('"')) {
return value.slice(1, -1);
}
return value;
}
export class CosmNativeCoreAdapter extends BaseCosmosAdapter {
chainName;
multiProvider;
addresses;
constructor(chainName, multiProvider, addresses) {
super(chainName, multiProvider, addresses);
this.chainName = chainName;
this.multiProvider = multiProvider;
this.addresses = addresses;
}
async extractMessageIds(sourceTx) {
assert(sourceTx.type === ProviderType.CosmJsNative, `Unsupported provider type for CosmNativeCoreAdapter ${sourceTx.type}`);
const dispatchEvents = sourceTx.receipt.events.filter((e) => MESSAGE_DISPATCH_EVENT_TYPES.includes(e.type));
return dispatchEvents.map((event) => {
const findAttribute = (key) => event.attributes.find((a) => a.key === key);
const messageAttribute = findAttribute(MESSAGE_ATTRIBUTE_KEY);
const destAttribute = findAttribute(MESSAGE_DESTINATION_ATTRIBUTE_KEY);
assert(messageAttribute, 'No message attribute found in dispatch event');
assert(destAttribute, 'No destination attribute found in dispatch event');
const messageValue = stripWrappingQuotes(messageAttribute.value);
const destinationValue = stripWrappingQuotes(destAttribute.value);
return {
messageId: ensure0x(messageId(messageValue)),
destination: this.multiProvider.getChainName(destinationValue),
};
});
}
async waitForMessageProcessed(messageId, destination, delayMs, maxAttempts) {
const provider = await this.multiProvider.getCosmJsNativeProvider(destination);
await pollAsync(async () => {
this.logger.debug(`Checking if message ${messageId} was processed`);
const delivered = await provider.isMessageDelivered({
mailboxAddress: this.addresses.mailbox,
messageId: messageId,
});
assert(delivered, `Message ${messageId} not yet processed`);
this.logger.info(`Message ${messageId} was processed`);
return delivered;
}, delayMs, maxAttempts);
return true;
}
async isDelivered(messageId, _blockTag) {
const provider = await this.multiProvider.getCosmJsNativeProvider(this.chainName);
return provider.isMessageDelivered({
mailboxAddress: this.addresses.mailbox,
messageId: messageId,
});
}
}
//# sourceMappingURL=CosmNativeCoreAdapter.js.map