"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CosmwasmWormholeCore = void 0;
const connect_sdk_1 = require("@wormhole-foundation/connect-sdk");
const connect_sdk_cosmwasm_1 = require("@wormhole-foundation/connect-sdk-cosmwasm");
class CosmwasmWormholeCore {
    network;
    chain;
    rpc;
    contracts;
    coreAddress;
    constructor(network, chain, rpc, contracts) {
        this.network = network;
        this.chain = chain;
        this.rpc = rpc;
        this.contracts = contracts;
        const coreAddress = this.contracts.coreBridge;
        if (!coreAddress)
            throw new Error(`Wormhole Token Bridge contract for domain ${chain} not found`);
        this.coreAddress = coreAddress;
    }
    getMessageFee() {
        throw new Error("Method not implemented.");
    }
    static async fromRpc(rpc, config) {
        const [network, chain] = await connect_sdk_cosmwasm_1.CosmwasmPlatform.chainFromRpc(rpc);
        const conf = config[chain];
        if (conf.network !== network)
            throw new Error(`Network mismatch: ${conf.network} != ${network}`);
        return new CosmwasmWormholeCore(network, chain, rpc, conf.contracts);
    }
    async *publishMessage(sender, message, nonce, consistencyLevel) {
        throw new Error("Method not implemented.");
    }
    async *verifyMessage(sender, vaa) {
        throw new Error("Not implemented.");
    }
    async parseTransaction(txid) {
        const tx = await this.rpc.getTx(txid);
        if (!tx)
            throw new Error("No transaction found for txid: " + txid);
        return [CosmwasmWormholeCore.parseWormholeMessage(this.chain, this.coreAddress, tx)];
    }
    // TODO: make consts
    static parseWormholeMessage(chain, coreAddress, tx) {
        const events = tx.events.filter((ev) => ev.type === "wasm" &&
            ev.attributes[0].key === "_contract_address" &&
            ev.attributes[0].value === coreAddress);
        if (events.length === 0)
            throw new Error("No wormhole message found in tx");
        if (events.length > 1)
            console.error(`Expected single message, found ${events.length}`);
        const [wasm] = events;
        const sequence = wasm.attributes.find((e) => {
            return e.key === "message.sequence";
        }).value;
        const emitter = wasm.attributes.find((e) => {
            return e.key === "message.sender";
        }).value;
        return {
            chain: chain,
            emitter: new connect_sdk_1.UniversalAddress(emitter),
            sequence: BigInt(sequence),
        };
    }
}
exports.CosmwasmWormholeCore = CosmwasmWormholeCore;
//# sourceMappingURL=core.js.map