0x0x230x

Untitled

Feb 16th, 2026
25
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.40 KB | None | 0 0
  1. /* eslint-disable @typescript-eslint/no-explicit-any */
  2. 'use client';
  3.  
  4. import { useCallback, useState, useEffect, useRef } from 'react';
  5. import { useEarnContext } from './YoarnProvider';
  6. import { cn } from '../styles/theme';
  7. import { ConnectWallet } from '@coinbase/onchainkit/wallet';
  8. import {
  9. LifecycleStatus,
  10. Transaction,
  11. TransactionButton,
  12. TransactionResponseType,
  13. } from '@coinbase/onchainkit/transaction';
  14.  
  15. export function DepositButton({ className }: any) {
  16. const {
  17. recipientAddress: address,
  18. chainId,
  19. vaultToken,
  20. depositCalls,
  21. depositAmount,
  22. depositAmountError,
  23. updateLifecycleStatus,
  24. refetchWalletBalance,
  25. isSponsored,
  26. lifecycleStatus,
  27. } = useEarnContext();
  28. const [depositedAmount, setDepositedAmount] = useState('');
  29. const [transactionKey, setTransactionKey] = useState(0);
  30. const wasErrorRef = useRef(false);
  31.  
  32. // Reset Transaction component when amount changes after an error
  33. useEffect(() => {
  34. if (lifecycleStatus?.statusName === 'error') {
  35. wasErrorRef.current = true;
  36. } else if (wasErrorRef.current && depositAmount) {
  37. // Amount changed after error, reset the transaction
  38. wasErrorRef.current = false;
  39. setTransactionKey((prev) => prev + 1);
  40. }
  41. }, [depositAmount, lifecycleStatus?.statusName]);
  42.  
  43. const handleOnStatus = useCallback(
  44. (status: LifecycleStatus) => {
  45. if (status.statusName === 'transactionPending') {
  46. updateLifecycleStatus({ statusName: 'transactionPending' });
  47. }
  48.  
  49. if (
  50. status.statusName === 'transactionLegacyExecuted' ||
  51. status.statusName === 'success' ||
  52. status.statusName === 'error'
  53. ) {
  54. updateLifecycleStatus(status);
  55. }
  56. },
  57. [updateLifecycleStatus],
  58. );
  59.  
  60. const handleOnSuccess = useCallback(
  61. (res: TransactionResponseType) => {
  62. if (
  63. res.transactionReceipts[0] &&
  64. res.transactionReceipts[0].status === 'success'
  65. ) {
  66. // Don't overwrite to '' when the second txn comes in
  67. if (depositAmount) {
  68. setDepositedAmount(depositAmount);
  69. }
  70. refetchWalletBalance();
  71. }
  72. },
  73. [depositAmount, refetchWalletBalance],
  74. );
  75.  
  76. if (!address) {
  77. return (
  78. <ConnectWallet
  79. className={cn('w-full', className)}
  80. disconnectedLabel='Connect to deposit'
  81. />
  82. );
  83. }
  84.  
  85. return (
  86. <Transaction
  87. key={transactionKey}
  88. className={className}
  89. chainId={chainId}
  90. calls={depositCalls}
  91. onStatus={handleOnStatus}
  92. onSuccess={handleOnSuccess}
  93. isSponsored={isSponsored}
  94. resetAfter={3_000}
  95. >
  96. <TransactionButton
  97. disabled={!!depositAmountError || !depositAmount}
  98. render={({ status, onSubmit, isDisabled }) => (
  99. <button
  100. onClick={status === 'success' ? undefined : onSubmit}
  101. disabled={isDisabled || status === 'success'}
  102. className='w-full rounded-xl bg-[#D1F651] py-4 font-bold text-black disabled:opacity-50'
  103. >
  104. {status === 'pending' && 'Processing...'}
  105. {status === 'success' &&
  106. `Deposited ${depositedAmount} ${vaultToken?.symbol}`}
  107. {status === 'error' && 'Try Again'}
  108. {status === 'default' && (depositAmountError ?? 'Deposit')}
  109. </button>
  110. )}
  111. />
  112. </Transaction>
  113. );
  114. }
  115.  
Advertisement
Add Comment
Please, Sign In to add comment