Skip to content

Commit

Permalink
chore: redid the logic to update the signature requesting
Browse files Browse the repository at this point in the history
  • Loading branch information
alfetopito committed Sep 28, 2023
1 parent 97e51c2 commit e10d855
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export interface SwapConfirmManager {
closeSwapConfirm(): void
sendTransaction(tradeToConfirm: TradeGp): void
transactionSent(txHash: string): void
requestPermitSignature(): void
permitSigned(): void
}

Expand All @@ -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)
Expand All @@ -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' }
Expand All @@ -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
})
Expand All @@ -90,6 +98,7 @@ export function useSwapConfirmManager(): SwapConfirmManager {
swapErrorMessage: undefined,
showConfirm: !isExpertMode,
txHash,
permitSignatureState: undefined,
}
console.debug('[Swap confirm state] transactionSent: ', state)
return state
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<SwapConfirmState>({
Expand Down

0 comments on commit e10d855

Please sign in to comment.