Skip to content

Commit

Permalink
ts fix
Browse files Browse the repository at this point in the history
  • Loading branch information
alessey committed Sep 11, 2024
1 parent 315b920 commit e020260
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 16 deletions.
23 changes: 13 additions & 10 deletions src/swap/components/SwapProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
useCallback,
useContext,
useEffect,
useMemo,
useState,
} from 'react';
import { useAccount, useConfig, useSendTransaction } from 'wagmi';
Expand Down Expand Up @@ -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<LifeCycleStatus>(initialLifecycleStatus); // Component lifecycle
const [hasHandledSuccess, setHasHandledSuccess] = useState(false);
const { from, to } = useFromTo(address);
Expand Down Expand Up @@ -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) {
Expand All @@ -165,7 +168,7 @@ export function SwapProvider({
tokenFrom: from.token, // are these needed???
tokenTo: to.token,
},
}));
} as LifeCycleStatus));

try {
const response = await getSwapQuote({
Expand All @@ -189,7 +192,7 @@ export function SwapProvider({
message: '',
}
},
}));
} as LifeCycleStatus));
return;
}
const formattedAmount = formatTokenAmount(
Expand All @@ -209,7 +212,7 @@ export function SwapProvider({
tokenFrom: from.token,
tokenTo: to.token,
},
}));
} as LifeCycleStatus));
} catch (err) {
setLifeCycleStatus((status: LifeCycleStatus) => ({
statusName: 'error',
Expand All @@ -221,7 +224,7 @@ export function SwapProvider({
message: '',
}
},
}));
} as LifeCycleStatus));
} finally {
// reset loading state when quote request resolves
destination.setLoading(false);
Expand All @@ -242,7 +245,7 @@ export function SwapProvider({
// has all required fields
isMissingRequiredField: false,
},
}));
} as LifeCycleStatus));

try {
const response = await buildSwapTransaction({
Expand All @@ -264,7 +267,7 @@ export function SwapProvider({
message: response.message,
}
},
}));
} as LifeCycleStatus));
return;
}
await processSwapTransaction({
Expand All @@ -290,7 +293,7 @@ export function SwapProvider({
message: errorMessage,
},
},
}));
} as LifeCycleStatus));
}
}, [
address,
Expand Down
12 changes: 6 additions & 6 deletions src/swap/utils/processSwapTransaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -38,7 +38,7 @@ export async function processSwapTransaction({
transactionHash: approveTxHash,
transactionType: useAggregator ? 'ERC20' : 'Permit2',
},
}));
} as LifeCycleStatus));
await waitForTransactionReceipt(config, {
hash: approveTxHash,
confirmations: 1,
Expand Down Expand Up @@ -79,7 +79,7 @@ export async function processSwapTransaction({
transactionHash: permitTxnHash,
transactionType: 'ERC20',
},
}));
} as LifeCycleStatus));
await waitForTransactionReceipt(config, {
hash: permitTxnHash,
confirmations: 1,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -118,5 +118,5 @@ export async function processSwapTransaction({
transactionReceipt: transactionReceipt,
error: undefined,
},
}));
} as LifeCycleStatus));
}

0 comments on commit e020260

Please sign in to comment.