import { decodeAbiParameters, encodeAbiParameters } from "viem";
const SWAP_COMPONENTS = [
    { name: "routers", type: "address[]" },
    { name: "tokensIn", type: "address[]" },
    { name: "tokensOut", type: "address[]" },
    { name: "maxAmountPerTx", type: "uint256" },
    { name: "maxSlippageBps", type: "uint256" },
    { name: "priceOracle", type: "address" },
    { name: "maxPriceAgeSec", type: "uint256" },
];
const BORROW_COMPONENTS = [
    { name: "protocols", type: "address[]" },
    { name: "assets", type: "address[]" },
    { name: "maxAmountPerTx", type: "uint256" },
    { name: "maxLtvBps", type: "uint256" },
    { name: "collateralOracle", type: "address" },
    { name: "borrowOracle", type: "address" },
    { name: "maxPriceAgeSec", type: "uint256" },
];
const TRANSFER_COMPONENTS = [
    { name: "recipients", type: "address[]" },
    { name: "tokens", type: "address[]" },
    { name: "maxAmountPerTx", type: "uint256" },
];
const ABI = [
    { name: "swap", type: "tuple", components: SWAP_COMPONENTS },
    { name: "borrow", type: "tuple", components: BORROW_COMPONENTS },
    { name: "transfer", type: "tuple", components: TRANSFER_COMPONENTS },
];
export const defiBundleTemplate = {
    name: "SharedDeFiBundlePermission",
    address: "0x0000000000000000000000000000000000000000",
    encoder: {
        encode(params) {
            return encodeAbiParameters(ABI, [
                {
                    routers: params.swap.routers,
                    tokensIn: params.swap.tokensIn,
                    tokensOut: params.swap.tokensOut,
                    maxAmountPerTx: params.swap.maxAmountPerTx,
                    maxSlippageBps: BigInt(params.swap.maxSlippageBps),
                    priceOracle: params.swap.priceOracle,
                    maxPriceAgeSec: BigInt(params.swap.maxPriceAgeSec),
                },
                {
                    protocols: params.borrow.protocols,
                    assets: params.borrow.assets,
                    maxAmountPerTx: params.borrow.maxAmountPerTx,
                    maxLtvBps: BigInt(params.borrow.maxLtvBps),
                    collateralOracle: params.borrow.collateralOracle,
                    borrowOracle: params.borrow.borrowOracle,
                    maxPriceAgeSec: BigInt(params.borrow.maxPriceAgeSec),
                },
                {
                    recipients: params.transfer.recipients,
                    tokens: params.transfer.tokens,
                    maxAmountPerTx: params.transfer.maxAmountPerTx,
                },
            ]);
        },
        decode(data) {
            const [swap, borrow, transfer] = decodeAbiParameters(ABI, data);
            return {
                swap: {
                    routers: [...swap.routers],
                    tokensIn: [...swap.tokensIn],
                    tokensOut: [...swap.tokensOut],
                    maxAmountPerTx: swap.maxAmountPerTx,
                    maxSlippageBps: Number(swap.maxSlippageBps),
                    priceOracle: swap.priceOracle,
                    maxPriceAgeSec: Number(swap.maxPriceAgeSec),
                },
                borrow: {
                    protocols: [...borrow.protocols],
                    assets: [...borrow.assets],
                    maxAmountPerTx: borrow.maxAmountPerTx,
                    maxLtvBps: Number(borrow.maxLtvBps),
                    collateralOracle: borrow.collateralOracle,
                    borrowOracle: borrow.borrowOracle,
                    maxPriceAgeSec: Number(borrow.maxPriceAgeSec),
                },
                transfer: {
                    recipients: [...transfer.recipients],
                    tokens: [...transfer.tokens],
                    maxAmountPerTx: transfer.maxAmountPerTx,
                },
            };
        },
    },
    explainer: {
        explain(params) {
            const warnings = [];
            if (params.swap.maxSlippageBps > 100) {
                warnings.push(`High swap slippage tolerance: ${params.swap.maxSlippageBps / 100}%`);
            }
            if (params.borrow.maxLtvBps > 8000) {
                warnings.push(`High borrow LTV cap: ${params.borrow.maxLtvBps / 100}%`);
            }
            if (params.borrow.protocols.length > 0 && params.swap.routers.length > 0) {
                warnings.push("Bundle permits both borrowing and swapping — review leverage exposure");
            }
            return {
                templateName: "SharedDeFiBundlePermission",
                humanReadable: [
                    `Swap — routers: ${params.swap.routers.join(", ") || "none"}; max/tx: ${params.swap.maxAmountPerTx.toString()}; slippage: ${params.swap.maxSlippageBps / 100}%`,
                    `Borrow — protocols: ${params.borrow.protocols.join(", ") || "none"}; max/tx: ${params.borrow.maxAmountPerTx.toString()}; LTV: ${params.borrow.maxLtvBps / 100}%`,
                    `Transfer — recipients: ${params.transfer.recipients.join(", ") || "none"}; max/tx: ${params.transfer.maxAmountPerTx.toString()}`,
                ],
                warnings,
            };
        },
    },
};
//# sourceMappingURL=defiBundle.js.map