From f38e0052fd56a5e2508b4723b6c5cd78e4ce8fb0 Mon Sep 17 00:00:00 2001 From: Lior Agnin Date: Sun, 11 May 2025 10:20:19 +0300 Subject: [PATCH] Refactor deposit logic to remove allowance checks and streamline transaction creation. Now directly deposits if balance is sufficient, simplifying the user flow. --- app/dashboard/deposit/page.tsx | 16 +------ hooks/useDeposit.ts | 78 ++++++++++++++++++++++++---------- 2 files changed, 57 insertions(+), 37 deletions(-) diff --git a/app/dashboard/deposit/page.tsx b/app/dashboard/deposit/page.tsx index aa7922e..ca3b1d4 100644 --- a/app/dashboard/deposit/page.tsx +++ b/app/dashboard/deposit/page.tsx @@ -21,15 +21,12 @@ import { useTotalAPY } from "@/hooks/useAnalytics"; export default function Home() { const [amount, setAmount] = useState(""); const { - allowance, balance, - approve, deposit, - approveStatus, depositStatus } = useDeposit(); const { data: price } = useTokenPriceUsd("usdc"); - const isLoading = approveStatus === Status.PENDING || depositStatus === Status.PENDING; + const isLoading = depositStatus === Status.PENDING; const { data: totalAPY } = useTotalAPY() const amountWei = parseUnits(amount, 6); @@ -38,11 +35,6 @@ export default function Home() { const getButtonText = () => { if (!amount) return "Enter an amount"; if (!balance || balance < amountWei) return "Insufficient balance"; - if (!allowance || allowance < amountWei) { - if (approveStatus === Status.PENDING) return "Approving"; - if (approveStatus === Status.ERROR) return "Error while approving"; - return "Approve"; - } if (depositStatus === Status.PENDING) return "Depositing"; if (depositStatus === Status.ERROR) return "Error while depositing"; if (depositStatus === Status.SUCCESS) return "Successfully deposited"; @@ -52,11 +44,7 @@ export default function Home() { const handleClick = async () => { if (!amount) return; if (!balance || balance < amountWei) return; - if (!allowance || allowance < amountWei) { - await approve(amount); - } else { - await deposit(amount); - } + await deposit(amount); }; return ( diff --git a/hooks/useDeposit.ts b/hooks/useDeposit.ts index 0b14447..d0652a1 100644 --- a/hooks/useDeposit.ts +++ b/hooks/useDeposit.ts @@ -18,6 +18,7 @@ import { ADDRESSES } from "@/lib/config"; import { Status } from "@/lib/types"; import useUser from "./useUser"; import { publicClient } from "@/lib/wagmi"; +import { BaseSafeOperation } from "@safe-global/relay-kit"; type DepositResult = { allowance: bigint | undefined; @@ -146,32 +147,63 @@ const useDeposit = (): DepositResult => { throw new Error("Insufficient USDC balance"); } + let safeOperation: BaseSafeOperation; + let transactions: any[] = []; + const safe4337Pack = await safeAA(user.passkey); if (allowance && allowance < amountWei) { - throw new Error("Insufficient allowance. Please approve first."); + const approveTransaction = { + to: ADDRESSES.ethereum.usdc, + data: encodeFunctionData({ + abi: ERC20_ABI, + functionName: "approve", + args: [ADDRESSES.ethereum.vault, amountWei], + }), + value: "0", + }; + + const depositTransaction = { + to: ADDRESSES.ethereum.teller, + data: encodeFunctionData({ + abi: ETHEREUM_TELLER_ABI, + functionName: "depositAndBridge", + args: [ + ADDRESSES.ethereum.usdc, + amountWei, + BigInt(0), + user.safeAddress, + encodeAbiParameters(parseAbiParameters("uint32"), [30138]), + ADDRESSES.ethereum.nativeFeeToken, + fee ? (fee * BigInt(12)) / BigInt(10) : BigInt(0), + ], + }), + value: fee?.toString() || "0", + }; + + transactions.push(approveTransaction, depositTransaction); + } else { + const depositTransaction = { + to: ADDRESSES.ethereum.teller, + data: encodeFunctionData({ + abi: ETHEREUM_TELLER_ABI, + functionName: "depositAndBridge", + args: [ + ADDRESSES.ethereum.usdc, + amountWei, + BigInt(0), + user.safeAddress, + encodeAbiParameters(parseAbiParameters("uint32"), [30138]), + ADDRESSES.ethereum.nativeFeeToken, + fee ? (fee * BigInt(12)) / BigInt(10) : BigInt(0), + ], + }), + value: fee?.toString() || "0", + }; + + transactions.push(depositTransaction); } - const safe4337Pack = await safeAA(user.passkey); - - const depositTransaction = { - to: ADDRESSES.ethereum.teller, - data: encodeFunctionData({ - abi: ETHEREUM_TELLER_ABI, - functionName: "depositAndBridge", - args: [ - ADDRESSES.ethereum.usdc, - amountWei, - BigInt(0), - user.safeAddress, - encodeAbiParameters(parseAbiParameters("uint32"), [30138]), - ADDRESSES.ethereum.nativeFeeToken, - fee ? (fee * BigInt(12)) / BigInt(10) : BigInt(0), - ], - }), - value: fee?.toString() || "0", - }; - - const safeOperation = await safe4337Pack.createTransaction({ - transactions: [depositTransaction], + safeOperation = await safe4337Pack.createTransaction({ + transactions, }); const signedSafeOperation = await safe4337Pack.signSafeOperation( safeOperation