From e10d8551e779addce777bc7ea18e2b8302b4a3e2 Mon Sep 17 00:00:00 2001 From: Alfetopito Date: Thu, 28 Sep 2023 13:14:12 +0100 Subject: [PATCH] chore: redid the logic to update the signature requesting --- .../swap/hooks/useSwapButtonContext.ts | 2 +- .../swap/hooks/useSwapConfirmManager.ts | 19 ++++++++++++++----- .../modules/swap/services/swapFlow/index.ts | 2 ++ .../src/modules/swap/state/swapConfirmAtom.ts | 2 +- 4 files changed, 18 insertions(+), 7 deletions(-) diff --git a/apps/cowswap-frontend/src/modules/swap/hooks/useSwapButtonContext.ts b/apps/cowswap-frontend/src/modules/swap/hooks/useSwapButtonContext.ts index a26842fd94..7a19c7f4dd 100644 --- a/apps/cowswap-frontend/src/modules/swap/hooks/useSwapButtonContext.ts +++ b/apps/cowswap-frontend/src/modules/swap/hooks/useSwapButtonContext.ts @@ -131,7 +131,7 @@ export function useSwapButtonContext(input: SwapButtonInput): SwapButtonsContext // TODO: move permit info and status to an atom // We need to know before opening the confirmation modal if there's a cached permit // and if it's valid to know what to show... - trade && openSwapConfirmModal(trade, !!swapFlowContext?.permitInfo) + trade && openSwapConfirmModal(trade) }, toggleWalletModal, swapInputError, diff --git a/apps/cowswap-frontend/src/modules/swap/hooks/useSwapConfirmManager.ts b/apps/cowswap-frontend/src/modules/swap/hooks/useSwapConfirmManager.ts index acdfbd7b3f..4b2eccc879 100644 --- a/apps/cowswap-frontend/src/modules/swap/hooks/useSwapConfirmManager.ts +++ b/apps/cowswap-frontend/src/modules/swap/hooks/useSwapConfirmManager.ts @@ -13,6 +13,7 @@ export interface SwapConfirmManager { closeSwapConfirm(): void sendTransaction(tradeToConfirm: TradeGp): void transactionSent(txHash: string): void + requestPermitSignature(): void permitSigned(): void } @@ -24,19 +25,19 @@ export function useSwapConfirmManager(): SwapConfirmManager { () => ({ setSwapError(swapErrorMessage: string) { setSwapConfirmState((prev) => { - const state = { ...prev, swapErrorMessage, attemptingTxn: false } + const state = { ...prev, swapErrorMessage, attemptingTxn: false, permitSignatureState: undefined } console.debug('[Swap confirm state] setSwapError: ', state) return state }) }, - openSwapConfirmModal(tradeToConfirm: TradeGp, needsPermitSignature?: boolean) { + openSwapConfirmModal(tradeToConfirm: TradeGp) { const state: SwapConfirmState = { tradeToConfirm, attemptingTxn: false, swapErrorMessage: undefined, showConfirm: true, txHash: undefined, - permitSignatureState: needsPermitSignature ? 'needsSigning' : undefined, + permitSignatureState: undefined, } console.debug('[Swap confirm state] openSwapConfirmModal: ', state) setSwapConfirmState(state) @@ -48,9 +49,16 @@ export function useSwapConfirmManager(): SwapConfirmManager { return state }) }, + requestPermitSignature() { + setSwapConfirmState((prev) => { + const state: SwapConfirmState = { ...prev, permitSignatureState: 'requested' } + console.debug('[Swap confirm state] requestPermitSignature: ', state) + return state + }) + }, permitSigned() { setSwapConfirmState((prev) => { - if (prev.permitSignatureState === 'needsSigning') { + if (prev.permitSignatureState === 'requested') { // Move to signed state only if previous state was good // Set to undefined otherwise and make the action a no-op const state: SwapConfirmState = { ...prev, permitSignatureState: 'signed' } @@ -63,7 +71,7 @@ export function useSwapConfirmManager(): SwapConfirmManager { }, closeSwapConfirm() { setSwapConfirmState((prev) => { - const state = { ...prev, showConfirm: false } + const state = { ...prev, showConfirm: false, permitSignatureState: undefined } console.debug('[Swap confirm state] closeSwapConfirm: ', state) return state }) @@ -90,6 +98,7 @@ export function useSwapConfirmManager(): SwapConfirmManager { swapErrorMessage: undefined, showConfirm: !isExpertMode, txHash, + permitSignatureState: undefined, } console.debug('[Swap confirm state] transactionSent: ', state) return state diff --git a/apps/cowswap-frontend/src/modules/swap/services/swapFlow/index.ts b/apps/cowswap-frontend/src/modules/swap/services/swapFlow/index.ts index a7e7c04429..326f3889ed 100644 --- a/apps/cowswap-frontend/src/modules/swap/services/swapFlow/index.ts +++ b/apps/cowswap-frontend/src/modules/swap/services/swapFlow/index.ts @@ -27,6 +27,8 @@ export async function swapFlow( try { logTradeFlow('SWAP FLOW', 'STEP 2: handle permit') + if (input.permitInfo) input.swapConfirmManager.requestPermitSignature() + input.orderParams.appData = await handlePermit({ appData: input.orderParams.appData, diff --git a/apps/cowswap-frontend/src/modules/swap/state/swapConfirmAtom.ts b/apps/cowswap-frontend/src/modules/swap/state/swapConfirmAtom.ts index 63b0638d0e..0bd8d19589 100644 --- a/apps/cowswap-frontend/src/modules/swap/state/swapConfirmAtom.ts +++ b/apps/cowswap-frontend/src/modules/swap/state/swapConfirmAtom.ts @@ -8,7 +8,7 @@ export interface SwapConfirmState { attemptingTxn: boolean swapErrorMessage: string | undefined txHash: string | undefined - permitSignatureState: undefined | 'needsSigning' | 'signed' + permitSignatureState: undefined | 'requested' | 'signed' } export const swapConfirmAtom = atom({