Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 14 additions & 12 deletions packages/swap-widget/src/hooks/useSwapApproval.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,18 @@ import { switchOrAddChain, VIEM_CHAINS_BY_ID } from '../constants/viemChains'
import { useSwapWallet } from '../contexts/SwapWalletContext'
import { SwapMachineCtx } from '../machines/SwapMachineContext'
import { getEvmNetworkId } from '../types'
import { getErrorMessage } from '../utils/errors'

export const useSwapApproval = () => {
const stateValue = SwapMachineCtx.useSelector(s => s.value)
const context = SwapMachineCtx.useSelector(s => s.context)
const actorRef = SwapMachineCtx.useActorRef()

const { walletClient, walletAddress } = useSwapWallet()

const approvingRef = useRef(false)

useEffect(() => {
const snap = actorRef.getSnapshot()
if (!snap.matches('approving') || approvingRef.current) return
if (stateValue !== 'approving' || approvingRef.current) return
approvingRef.current = true

const executeApproval = async () => {
Expand All @@ -29,13 +28,14 @@ export const useSwapApproval = () => {
return
}

const quote = context.quote
const { quote, sellAsset, sellAmountBaseUnit } = actorRef.getSnapshot().context

if (!quote?.approval?.spender) {
actorRef.send({ type: 'APPROVAL_ERROR', error: 'No approval data in quote' })
return
}

const sellAssetAddress = context.sellAsset.assetId.split('/')[1]?.split(':')[1]
const sellAssetAddress = sellAsset.assetId.split('/')[1]?.split(':')[1]
Comment thread
kaladinlight marked this conversation as resolved.
if (!sellAssetAddress || !/^0x[a-fA-F0-9]{40}$/.test(sellAssetAddress)) {
actorRef.send({
type: 'APPROVAL_ERROR',
Expand All @@ -44,15 +44,15 @@ export const useSwapApproval = () => {
return
}

const requiredChainId = getEvmNetworkId(context.sellAsset.chainId)
const requiredChainId = getEvmNetworkId(sellAsset.chainId)
const client = walletClient as WalletClient

const currentChainId = await client.getChainId()
if (currentChainId !== requiredChainId) {
await switchOrAddChain(client, requiredChainId)
}

const baseAsset = getBaseAsset(context.sellAsset.chainId)
const baseAsset = getBaseAsset(sellAsset.chainId)
const nativeCurrency = baseAsset
? { name: baseAsset.name, symbol: baseAsset.symbol, decimals: baseAsset.precision }
: { name: 'ETH', symbol: 'ETH', decimals: 18 }
Expand All @@ -65,15 +65,15 @@ export const useSwapApproval = () => {
rpcUrls: { default: { http: [] } },
}

if (!context.sellAmountBaseUnit || context.sellAmountBaseUnit === '0') {
if (!sellAmountBaseUnit || sellAmountBaseUnit === '0') {
actorRef.send({ type: 'APPROVAL_ERROR', error: 'No sell amount specified' })
return
}

const approvalData = encodeFunctionData({
abi: erc20Abi,
functionName: 'approve',
args: [quote.approval.spender as `0x${string}`, BigInt(context.sellAmountBaseUnit)],
args: [quote.approval.spender as `0x${string}`, BigInt(sellAmountBaseUnit)],
})

const approvalHash = await client.sendTransaction({
Expand All @@ -93,14 +93,16 @@ export const useSwapApproval = () => {

actorRef.send({ type: 'APPROVAL_SUCCESS', txHash: approvalHash })
} catch (error) {
const errorMessage = error instanceof Error ? error.message : 'Approval failed'
actorRef.send({ type: 'APPROVAL_ERROR', error: errorMessage })
actorRef.send({
type: 'APPROVAL_ERROR',
error: getErrorMessage(error, 'Approval failed'),
})
} finally {
approvingRef.current = false
}
}

executeApproval()
// eslint-disable-next-line react-hooks/exhaustive-deps -- stateValue is the sole trigger; other deps are stable refs read from snapshot
// eslint-disable-next-line react-hooks/exhaustive-deps -- only re-fire on state machine transitions; wallet handles close over the latest render
}, [stateValue])
}
Loading
Loading