"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.NobleClient = void 0;
const proto_signing_1 = require("@cosmjs/proto-signing");
const stargate_1 = require("@cosmjs/stargate");
const constants_1 = require("./constants");
const cctpProto_1 = require("./lib/cctpProto");
class NobleClient {
    constructor(restEndpoint, defaultClientMemo) {
        this.restEndpoint = restEndpoint;
        this.defaultClientMemo = defaultClientMemo;
    }
    get isConnected() {
        return Boolean(this.stargateClient);
    }
    async connect(wallet) {
        if ((wallet === null || wallet === void 0 ? void 0 : wallet.offlineSigner) === undefined) {
            throw new Error('Wallet signer not found');
        }
        this.wallet = wallet;
        this.stargateClient = await stargate_1.SigningStargateClient.connectWithSigner(this.restEndpoint, wallet.offlineSigner, {
            registry: new proto_signing_1.Registry([
                ['/circle.cctp.v1.MsgDepositForBurn', cctpProto_1.MsgDepositForBurn],
                ['/circle.cctp.v1.MsgDepositForBurnWithCaller', cctpProto_1.MsgDepositForBurnWithCaller],
                ...stargate_1.defaultRegistryTypes,
            ]),
        });
    }
    getAccountBalances() {
        var _a;
        if (!this.stargateClient || ((_a = this.wallet) === null || _a === void 0 ? void 0 : _a.address) === undefined) {
            throw new Error('stargateClient not initialized');
        }
        return this.stargateClient.getAllBalances(this.wallet.address);
    }
    getAccountBalance(denom) {
        var _a;
        if (!this.stargateClient || ((_a = this.wallet) === null || _a === void 0 ? void 0 : _a.address) === undefined) {
            throw new Error('stargateClient not initialized');
        }
        return this.stargateClient.getBalance(this.wallet.address, denom);
    }
    async IBCTransfer(message) {
        const tx = await this.send([message]);
        return tx;
    }
    async send(messages, gasPrice = stargate_1.GasPrice.fromString('0.1uusdc'), memo) {
        var _a;
        if (!this.stargateClient) {
            throw new Error('NobleClient stargateClient not initialized');
        }
        if (((_a = this.wallet) === null || _a === void 0 ? void 0 : _a.address) === undefined) {
            throw new Error('NobleClient wallet not initialized');
        }
        // Simulate to get the gas estimate
        const fee = await this.simulateTransaction(messages, gasPrice, memo !== null && memo !== void 0 ? memo : this.defaultClientMemo);
        // Sign and broadcast the transaction
        return this.stargateClient.signAndBroadcast(this.wallet.address, messages, fee, memo !== null && memo !== void 0 ? memo : this.defaultClientMemo);
    }
    async simulateTransaction(messages, gasPrice = stargate_1.GasPrice.fromString('0.1uusdc'), memo) {
        var _a, _b;
        if (!this.stargateClient) {
            throw new Error('NobleClient stargateClient not initialized');
        }
        if (((_a = this.wallet) === null || _a === void 0 ? void 0 : _a.address) === undefined) {
            throw new Error('NobleClient wallet not initialized');
        }
        // Get simulated response
        const gasEstimate = await this.stargateClient.simulate((_b = this.wallet) === null || _b === void 0 ? void 0 : _b.address, messages, memo);
        // Calculate and return the fee
        return (0, stargate_1.calculateFee)(Math.floor(gasEstimate * constants_1.GAS_MULTIPLIER), gasPrice);
    }
}
exports.NobleClient = NobleClient;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoibm9ibGUtY2xpZW50LmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vLi4vLi4vLi4vc3JjL2NsaWVudHMvbm9ibGUtY2xpZW50LnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7OztBQUFBLHlEQUFxRTtBQUNyRSwrQ0FRMEI7QUFFMUIsMkNBQTZDO0FBQzdDLCtDQUFpRjtBQUdqRixNQUFhLFdBQVc7SUFNdEIsWUFBWSxZQUFvQixFQUFFLGlCQUEwQjtRQUMxRCxJQUFJLENBQUMsWUFBWSxHQUFHLFlBQVksQ0FBQztRQUNqQyxJQUFJLENBQUMsaUJBQWlCLEdBQUcsaUJBQWlCLENBQUM7SUFDN0MsQ0FBQztJQUVELElBQUksV0FBVztRQUNiLE9BQU8sT0FBTyxDQUFDLElBQUksQ0FBQyxjQUFjLENBQUMsQ0FBQztJQUN0QyxDQUFDO0lBRUQsS0FBSyxDQUFDLE9BQU8sQ0FBQyxNQUFtQjtRQUMvQixJQUFJLENBQUEsTUFBTSxhQUFOLE1BQU0sdUJBQU4sTUFBTSxDQU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