From e0202603224c8253bcb4e34d42635d266d92850b Mon Sep 17 00:00:00 2001 From: Adam Lessey Date: Wed, 11 Sep 2024 16:09:25 -0400 Subject: [PATCH] ts fix --- src/swap/components/SwapProvider.tsx | 23 +++++++++++++---------- src/swap/utils/processSwapTransaction.ts | 12 ++++++------ 2 files changed, 19 insertions(+), 16 deletions(-) diff --git a/src/swap/components/SwapProvider.tsx b/src/swap/components/SwapProvider.tsx index 6d8059bc7e..e4e5e0e359 100644 --- a/src/swap/components/SwapProvider.tsx +++ b/src/swap/components/SwapProvider.tsx @@ -3,6 +3,7 @@ import { useCallback, useContext, useEffect, + useMemo, useState, } from 'react'; import { useAccount, useConfig, useSendTransaction } from 'wagmi'; @@ -48,13 +49,13 @@ export function SwapProvider({ // Core Hooks const config = useConfig(); - const initialLifecycleStatus = { + const initialLifecycleStatus = useMemo(() => ({ statusName: 'init', statusData: { isMissingRequiredField: true, maxSlippage: experimental.maxSlippage || 3 } - } as LifeCycleStatus; + } as LifeCycleStatus), [experimental.maxSlippage]); const [lifeCycleStatus, setLifeCycleStatus] = useState(initialLifecycleStatus); // Component lifecycle const [hasHandledSuccess, setHasHandledSuccess] = useState(false); const { from, to } = useFromTo(address); @@ -141,7 +142,9 @@ export function SwapProvider({ // token is missing isMissingRequiredField: true, }, - })); + } as LifeCycleStatus)); + // TODO: without an explicit cast, TS is not able to validate the type (other than the key) and allows anything to be passed here + // even though the setState signature is (prevState: S) => S return; } if (amount === '' || amount === '.' || Number.parseFloat(amount) === 0) { @@ -165,7 +168,7 @@ export function SwapProvider({ tokenFrom: from.token, // are these needed??? tokenTo: to.token, }, - })); + } as LifeCycleStatus)); try { const response = await getSwapQuote({ @@ -189,7 +192,7 @@ export function SwapProvider({ message: '', } }, - })); + } as LifeCycleStatus)); return; } const formattedAmount = formatTokenAmount( @@ -209,7 +212,7 @@ export function SwapProvider({ tokenFrom: from.token, tokenTo: to.token, }, - })); + } as LifeCycleStatus)); } catch (err) { setLifeCycleStatus((status: LifeCycleStatus) => ({ statusName: 'error', @@ -221,7 +224,7 @@ export function SwapProvider({ message: '', } }, - })); + } as LifeCycleStatus)); } finally { // reset loading state when quote request resolves destination.setLoading(false); @@ -242,7 +245,7 @@ export function SwapProvider({ // has all required fields isMissingRequiredField: false, }, - })); + } as LifeCycleStatus)); try { const response = await buildSwapTransaction({ @@ -264,7 +267,7 @@ export function SwapProvider({ message: response.message, } }, - })); + } as LifeCycleStatus)); return; } await processSwapTransaction({ @@ -290,7 +293,7 @@ export function SwapProvider({ message: errorMessage, }, }, - })); + } as LifeCycleStatus)); } }, [ address, diff --git a/src/swap/utils/processSwapTransaction.ts b/src/swap/utils/processSwapTransaction.ts index 0804ff8c0c..bd0213cd3c 100644 --- a/src/swap/utils/processSwapTransaction.ts +++ b/src/swap/utils/processSwapTransaction.ts @@ -25,7 +25,7 @@ export async function processSwapTransaction({ setLifeCycleStatus((status: LifeCycleStatus) => ({ ...status, statusName: 'transactionPending', - })); + } as LifeCycleStatus)); const approveTxHash = await sendTransactionAsync({ to: approveTransaction.to, value: approveTransaction.value, @@ -38,7 +38,7 @@ export async function processSwapTransaction({ transactionHash: approveTxHash, transactionType: useAggregator ? 'ERC20' : 'Permit2', }, - })); + } as LifeCycleStatus)); await waitForTransactionReceipt(config, { hash: approveTxHash, confirmations: 1, @@ -79,7 +79,7 @@ export async function processSwapTransaction({ transactionHash: permitTxnHash, transactionType: 'ERC20', }, - })); + } as LifeCycleStatus)); await waitForTransactionReceipt(config, { hash: permitTxnHash, confirmations: 1, @@ -91,7 +91,7 @@ export async function processSwapTransaction({ setLifeCycleStatus((status: LifeCycleStatus) => ({ ...status, statusName: 'transactionPending', - })); + } as LifeCycleStatus)); const txHash = await sendTransactionAsync({ to: transaction.to, value: transaction.value, @@ -105,7 +105,7 @@ export async function processSwapTransaction({ transactionHash: txHash, transactionType: useAggregator ? 'ERC20' : 'Permit2', }, - })); + } as LifeCycleStatus)); // wait for swap to land onchain const transactionReceipt = await waitForTransactionReceipt(config, { hash: txHash, @@ -118,5 +118,5 @@ export async function processSwapTransaction({ transactionReceipt: transactionReceipt, error: undefined, }, - })); + } as LifeCycleStatus)); }