import { toAccAddress } from '@cosmjs/stargate/build/queryclient/utils.js';
import { MsgWalletSpendAction } from './codegen/agoric/swingset/msgs.js';
import { makeStargateClientKit } from './signing-client.js';
import {} from './smart-wallet-kit.js';
// TODO parameterize as part of https://github.com/Agoric/agoric-sdk/issues/5912
const defaultFee = {
    amount: [{ denom: 'ubld', amount: '30000' }], // XXX enough?
    gas: '197000',
};
/**
 * Augment a read-only SmartWalletKit with signing ability
 */
export const makeSigningSmartWalletKit = async ({ connectWithSigner, walletUtils, }, MNEMONIC) => {
    const { address, client } = await makeStargateClientKit(MNEMONIC, {
        connectWithSigner,
        // XXX always the first
        rpcAddr: walletUtils.networkConfig.rpcAddrs[0],
    });
    const query = {
        readPublished: walletUtils.readPublished,
        vstorage: walletUtils.vstorage,
        getLastUpdate: () => walletUtils.getLastUpdate(address),
        getCurrentWalletRecord: () => walletUtils.getCurrentWalletRecord(address),
        pollOffer: (...args) => walletUtils.pollOffer(address, ...args),
    };
    const sendBridgeAction = (action) => {
        const msgSpend = MsgWalletSpendAction.fromPartial({
            owner: toAccAddress(address),
            spendAction: JSON.stringify(walletUtils.marshaller.toCapData(action)),
        });
        return client.signAndBroadcast(address, [{ typeUrl: MsgWalletSpendAction.typeUrl, value: msgSpend }], defaultFee);
    };
    const executeOffer = async (offer) => {
        const before = await client.getBlock();
        await sendBridgeAction(harden({
            method: 'executeOffer',
            offer,
        }));
        return walletUtils.pollOffer(address, offer.id, before.header.height);
    };
    const invokeEntry = async (message) => {
        const transaction = await sendBridgeAction(harden({
            method: 'invokeEntry',
            message,
        }));
        return { result: { transaction } };
    };
    return Object.freeze({
        networkConfig: walletUtils.networkConfig,
        marshaller: walletUtils.marshaller,
        query,
        address,
        executeOffer,
        invokeEntry,
        sendBridgeAction,
    });
};
//# sourceMappingURL=signing-smart-wallet-kit.js.map