Skip to content

Commit

Permalink
feat(swap): take partial approves into account for sc wallets
Browse files Browse the repository at this point in the history
  • Loading branch information
shoom3301 committed Dec 25, 2024
1 parent 2cbf15a commit 4573c4d
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,16 @@ export type BuildApproveTxParams = {
erc20Contract: Erc20
spender: string
amountToApprove: CurrencyAmount<Currency>
isPartialApprove?: boolean
}

/**
* Builds the approval tx, without sending it
*/
export async function buildApproveTx(params: BuildApproveTxParams) {
const { erc20Contract, spender, amountToApprove } = params
const { erc20Contract, spender, amountToApprove, isPartialApprove } = params

const estimatedAmount = await estimateApprove(erc20Contract, spender, amountToApprove)
const estimatedAmount = await estimateApprove(erc20Contract, spender, amountToApprove, isPartialApprove)

return erc20Contract.populateTransaction.approve(spender, estimatedAmount.approveAmount)
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { useUserTransactionTTL } from 'legacy/state/user/hooks'
import { usePartialApprove, useUserTransactionTTL } from 'legacy/state/user/hooks'

import { useTradeFlowContext } from 'modules/tradeFlow'

import { useSafeMemoObject } from 'common/hooks/useSafeMemo'

export function useSwapFlowContext() {
const [deadline] = useUserTransactionTTL()
return useTradeFlowContext(useSafeMemoObject({ deadline }))
const [isPartialApprove] = usePartialApprove()

return useTradeFlowContext(useSafeMemoObject({ deadline, isPartialApprove }))
}
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ export function useTradeFlowContext({ deadline, isPartialApprove }: TradeFlowPar
deadline,
orderKind,
uiOrderType,
isPartialApprove,
]
: null,
([
Expand Down Expand Up @@ -153,7 +154,9 @@ export function useTradeFlowContext({ deadline, isPartialApprove }: TradeFlowPar
deadline,
orderKind,
uiOrderType,
isPartialApprove,
]) => {
console.log('CCCCC', isPartialApprove)
return {
context: {
chainId,
Expand All @@ -163,6 +166,7 @@ export function useTradeFlowContext({ deadline, isPartialApprove }: TradeFlowPar
},
flags: {
allowsOffchainSigning,
isPartialApprove,
},
callbacks: {
closeModals,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,15 @@ export async function safeBundleApprovalFlow(
return false
}

const { context, callbacks, orderParams, swapFlowAnalyticsContext, tradeConfirmActions, typedHooks } = tradeContext
const {
context,
callbacks,
orderParams,
swapFlowAnalyticsContext,
tradeConfirmActions,
typedHooks,
flags: { isPartialApprove },
} = tradeContext

const { spender, settlementContract, safeAppsSdk, erc20Contract } = safeBundleContext

Expand All @@ -52,6 +60,7 @@ export async function safeBundleApprovalFlow(
erc20Contract,
spender,
amountToApprove: context.inputAmount,
isPartialApprove,
})

orderParams.appData = await removePermitHookFromAppData(orderParams.appData, typedHooks)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,14 @@ export async function safeBundleEthFlow(
return false
}

const { context, callbacks, swapFlowAnalyticsContext, tradeConfirmActions, typedHooks } = tradeContext
const {
context,
callbacks,
swapFlowAnalyticsContext,
tradeConfirmActions,
typedHooks,
flags: { isPartialApprove },
} = tradeContext

const { spender, settlementContract, safeAppsSdk, needsApproval, wrappedNativeContract } = safeBundleContext

Expand Down Expand Up @@ -73,6 +80,7 @@ export async function safeBundleEthFlow(
erc20Contract: wrappedNativeContract as unknown as Erc20,
spender,
amountToApprove: inputAmount,
isPartialApprove,
})

txs.push({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export interface TradeFlowContext {
}
flags: {
allowsOffchainSigning: boolean
isPartialApprove?: boolean
}
callbacks: {
closeModals: Command
Expand Down

0 comments on commit 4573c4d

Please sign in to comment.