"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Gateway = void 0;
const connect_sdk_1 = require("@wormhole-foundation/connect-sdk");
const address_1 = require("./address");
const constants_1 = require("./constants");
const platform_1 = require("./platform");
class Gateway extends connect_sdk_1.ChainContext {
    static chain = "Wormchain";
    static gatewayAddress = (network) => connect_sdk_1.CONFIG[network].chains[Gateway.chain].contracts.gateway;
    static tokenBridgeAddress = (network) => connect_sdk_1.CONFIG[network].chains[Gateway.chain].contracts.tokenBridge;
    static coreAddress = (network) => connect_sdk_1.CONFIG[network].chains[Gateway.chain].contracts.coreBridge;
    // Get the wrapped version of an asset created on wormchain
    // for a given chain
    async getWrappedAsset(token) {
        const tb = await this.getTokenBridge();
        const wrappedAsset = new address_1.CosmwasmAddress(await tb.getWrappedAsset(token));
        // Encode the original address to base58 and add it
        // to the factory address for cw20 style factory token address
        const encodedAddress = connect_sdk_1.encoding.b58.encode(wrappedAsset.toUniversalAddress().toUint8Array());
        const factoryAddress = `factory/${Gateway.gatewayAddress(this.network)}/${encodedAddress}`;
        return new address_1.CosmwasmAddress(factoryAddress);
    }
    // Gets the the source channel for outgoing transfers from wormchain
    static getGatewaySourceChannel(network, chain) {
        const channels = platform_1.CosmwasmPlatform.getIbcChannels(network, chain);
        if (!channels)
            throw new Error("No channels configured for chain " + chain);
        if (!(Gateway.chain in channels))
            throw new Error("No channel configured for chain " + chain);
        return channels[Gateway.chain];
    }
    // derive the ics20 token denom from the
    // wrapped denom and destination channel
    static deriveIbcDenom(network, chain, denom) {
        // Otherwise compute the ibc address from the channel and denom
        const channel = this.getGatewaySourceChannel(network, chain);
        const hashData = connect_sdk_1.encoding.bytes.encode(`${constants_1.IBC_TRANSFER_PORT}/${channel}/${denom}`);
        const hash = connect_sdk_1.encoding.hex.encode((0, connect_sdk_1.sha256)(hashData));
        return new address_1.CosmwasmAddress(`ibc/${hash.toUpperCase()}`);
    }
    static makeTransferMsg(chain, recipient, fee = 0n, payload, nonce) {
        // Address of recipient is b64 encoded Cosmos bech32 address
        const address = connect_sdk_1.encoding.b64.encode(recipient.toUint8Array());
        const common = {
            chain: (0, connect_sdk_1.toChainId)(chain),
            recipient: address,
            fee: fee.toString(),
            nonce: nonce ?? Math.round(Math.random() * 100000),
        };
        const msg = payload
            ? {
                gateway_transfer_with_payload: { ...common, payload: payload },
            }
            : { gateway_transfer: { ...common } };
        return msg;
    }
}
exports.Gateway = Gateway;
//# sourceMappingURL=gateway.js.map