"use client";

// src/clients/rolly.tsx
import {
  encodeFunctionData,
  erc20Abi,
  getAddress,
  maxUint256
} from "viem";
var ROLLY_VAULT_ADDRESS = getAddress(
  "0x67Ae473bbf4CC04a591A479cF139EC0Ec58342b1"
);
var ARBITRUM_USDT0 = getAddress(
  "0xfd086bc7cd5c481dcc9c85ebe478a1c0b69fcbb9"
);
var VAULT_DEPOSITOR_ARBITRUM = getAddress(
  "0xC8A6871D4eC4DaE64f605db0f8a0B3D9ef928D64"
);
var AMOUNT_PLACEHOLDER = 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffdeadbeefn;
var ROLLY_VAULT_ABI = [
  {
    name: "depositToken",
    type: "function",
    stateMutability: "nonpayable",
    inputs: [
      { name: "to", type: "address" },
      { name: "amount", type: "uint256" }
    ],
    outputs: []
  }
];
var VAULT_DEPOSITOR_ABI = [
  {
    name: "deposit",
    type: "function",
    stateMutability: "nonpayable",
    inputs: [
      { name: "token", type: "address" },
      { name: "vault", type: "address" },
      { name: "callData", type: "bytes" },
      { name: "minAmountOut", type: "uint256" }
    ],
    outputs: [{ name: "returnData", type: "bytes" }]
  }
];
function createRollyDepositActions(config) {
  const {
    recipientAddress,
    tokenAddress = ARBITRUM_USDT0,
    vaultAddress = ROLLY_VAULT_ADDRESS
  } = config;
  return async () => {
    if (!recipientAddress) {
      throw new Error(
        "Please connect your wallet before starting a Rolly deposit"
      );
    }
    const depositCalldata = encodeFunctionData({
      abi: ROLLY_VAULT_ABI,
      functionName: "depositToken",
      args: [recipientAddress, AMOUNT_PLACEHOLDER]
    });
    return [
      // Step 1: Reset token approval
      {
        contractAbi: erc20Abi,
        contractAddress: tokenAddress,
        functionName: "approve",
        functionArgs: [VAULT_DEPOSITOR_ARBITRUM, 0n]
      },
      // Step 2: Approve token to VaultDepositor (infinite)
      {
        contractAbi: erc20Abi,
        contractAddress: tokenAddress,
        functionName: "approve",
        functionArgs: [VAULT_DEPOSITOR_ARBITRUM, maxUint256]
      },
      // Step 3: VaultDepositor pulls token, swaps placeholder, forwards to
      // Rolly.depositToken → credits recipient on the rollup
      {
        contractAbi: VAULT_DEPOSITOR_ABI,
        contractAddress: VAULT_DEPOSITOR_ARBITRUM,
        functionName: "deposit",
        functionArgs: [tokenAddress, vaultAddress, depositCalldata, 0n]
      }
    ];
  };
}
export {
  AMOUNT_PLACEHOLDER,
  ARBITRUM_USDT0,
  ROLLY_VAULT_ADDRESS,
  VAULT_DEPOSITOR_ARBITRUM,
  createRollyDepositActions
};
