Skip to content

Commit

Permalink
chore: revert change
Browse files Browse the repository at this point in the history
  • Loading branch information
bayological committed Nov 27, 2024
1 parent 5024627 commit e222f98
Showing 1 changed file with 28 additions and 54 deletions.
82 changes: 28 additions & 54 deletions src/features/swap/SwapConfirm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,12 @@ export function SwapConfirmCard({ formValues }: Props) {
approveAmount = thresholdAmountWei
}

const { sendApproveTx, isApproveTxSuccess, isApproveTxLoading, needsApproval } =
useApproveTransaction(chainId, fromTokenId, approveAmount, address)

const { sendApproveTx, isApproveTxSuccess, isApproveTxLoading } = useApproveTransaction(
chainId,
fromTokenId,
approveAmount,
address
)
const [isApproveConfirmed, setApproveConfirmed] = useState(false)

const { sendSwapTx, isSwapTxLoading, isSwapTxSuccess } = useSwapTransaction(
Expand All @@ -94,57 +97,30 @@ export function SwapConfirmCard({ formValues }: Props) {
amountWei,
thresholdAmountWei,
direction,
needsApproval,
address,
isApproveConfirmed
)

const onSubmit = async () => {
if (!rate || !amountWei || !address || !isConnected) return

setIsModalOpen(true)

if (needsApproval) {
if (!sendApproveTx || isApproveTxSuccess || isApproveTxLoading) {
logger.debug('Approve already started or finished, ignoring submit')
return
}
if (!sendApproveTx || isApproveTxSuccess || isApproveTxLoading) {
logger.debug('Approve already started or finished, ignoring submit')
return
}

try {
logger.info('Sending approve tx')
const approveResult = await sendApproveTx()
const approveReceipt = await approveResult.wait(1)
toastToYourSuccess(
'Approve complete, starting swap',
approveReceipt.transactionHash,
chainId
)
setApproveConfirmed(true)
logger.info(`Tx receipt received for approve: ${approveReceipt.transactionHash}`)
} catch (error) {
logger.error('Failed to approve token', error)
setIsModalOpen(false)
}
} else {
// If no approval needed, trigger swap directly
if (!sendSwapTx) {
logger.error('Swap transaction not ready')
setIsModalOpen(false)
return
}
setIsModalOpen(true)

try {
logger.info('Sending swap tx')
const swapResult = await sendSwapTx()
const swapReceipt = await swapResult.wait(1)
logger.info(`Tx receipt received for swap: ${swapReceipt.transactionHash}`)
toastToYourSuccess('Swap Complete!', swapReceipt.transactionHash, chainId)
dispatch(setFormValues(null))
setIsModalOpen(false)
} catch (error) {
logger.error('Failed to execute swap', error)
setIsModalOpen(false)
}
try {
logger.info('Sending approve tx')
const approveResult = await sendApproveTx()
const approveReceipt = await approveResult.wait(1)
toastToYourSuccess('Approve complete, starting swap', approveReceipt.transactionHash, chainId)
setApproveConfirmed(true)
logger.info(`Tx receipt received for approve: ${approveReceipt.transactionHash}`)
} catch (error) {
logger.error('Failed to approve token', error)
setIsModalOpen(false)
}
}

Expand Down Expand Up @@ -231,7 +207,7 @@ export function SwapConfirmCard({ formValues }: Props) {
close={() => setIsModalOpen(false)}
width="max-w-[432px]"
>
<MentoLogoLoader needsApproval={needsApproval} />
<MentoLogoLoader />
</Modal>
</FloatingBox>
)
Expand Down Expand Up @@ -295,7 +271,7 @@ const ChevronRight = (props: SVGProps<SVGSVGElement>) => (
</svg>
)

const MentoLogoLoader = ({ needsApproval }: { needsApproval: boolean }) => {
const MentoLogoLoader = () => {
const { connector } = useAccount()

return (
Expand All @@ -310,14 +286,12 @@ const MentoLogoLoader = ({ needsApproval }: { needsApproval: boolean }) => {
</div>

<div className="my-6">
<div className="text-sm text-center text-[#636768] dark:text-[#AAB3B6]">
{needsApproval
? 'Sending two transactions: Approve and Swap'
: 'Sending swap transaction'}
</div>
<div className="mt-3 text-sm text-center text-[#636768] dark:text-[#AAB3B6]">
{`Sign with ${connector?.name || 'wallet'} to proceed`}
<div className=" text-sm text-center text-[#636768] dark:text-[#AAB3B6]">
Sending two transactions: Approve and Swap
</div>
<div className="mt-3 text-sm text-center text-[#636768] dark:text-[#AAB3B6]">{`Sign with ${
connector?.name || 'wallet'
} to proceed`}</div>
</div>
</>
)
Expand Down

0 comments on commit e222f98

Please sign in to comment.