Skip to content

Commit

Permalink
feat(swap): add sell amount to permit value
Browse files Browse the repository at this point in the history
  • Loading branch information
shoom3301 committed Dec 24, 2024
1 parent 9cf72b4 commit a017d48
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { GeneratePermitHook, GeneratePermitHookParams } from '../types'
/**
* Hook that returns callback to generate permit hook data
*/
export function useGeneratePermitHook(): GeneratePermitHook {
export function useGeneratePermitHook(isPartialApprove?: boolean): GeneratePermitHook {
const { chainId } = useWalletInfo()
const storePermit = useSetAtom(storePermitCacheAtom)
const getCachedPermit = useGetCachedPermit()
Expand All @@ -36,7 +36,7 @@ export function useGeneratePermitHook(): GeneratePermitHook {

return useCallback(
async (params: GeneratePermitHookParams): Promise<PermitHookData | undefined> => {
const { inputToken, account, permitInfo, customSpender } = params
const { inputToken, account, permitInfo, customSpender, amount } = params

if (!provider || !isSupportedPermitInfo(permitInfo)) {
return
Expand Down Expand Up @@ -66,12 +66,13 @@ export function useGeneratePermitHook(): GeneratePermitHook {
eip2162Utils,
account,
nonce,
amount: isPartialApprove ? amount : undefined,
})

hookData && storePermit({ ...permitParams, hookData, spender })

return hookData
},
[provider, chainId, getCachedPermit, storePermit],
[provider, chainId, getCachedPermit, storePermit, isPartialApprove],
)
}
2 changes: 1 addition & 1 deletion apps/cowswap-frontend/src/modules/permit/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export type AddPermitTokenParams = {
permitInfo: PermitInfo
}

export type GeneratePermitHookParams = Pick<PermitHookParams, 'inputToken' | 'permitInfo' | 'account'> & {
export type GeneratePermitHookParams = Pick<PermitHookParams, 'inputToken' | 'permitInfo' | 'account' | 'amount'> & {
customSpender?: string
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { HandlePermitParams } from '../types'
* Returns the updated appData
*/
export async function handlePermit(params: HandlePermitParams): Promise<AppDataInfo> {
const { permitInfo, inputToken, account, appData, typedHooks, generatePermitHook } = params
const { permitInfo, inputToken, account, appData, typedHooks, generatePermitHook, amount } = params

if (isSupportedPermitInfo(permitInfo) && !getIsNativeToken(inputToken)) {
// permitInfo will only be set if there's NOT enough allowance
Expand All @@ -31,6 +31,7 @@ export async function handlePermit(params: HandlePermitParams): Promise<AppDataI
inputToken: { address: inputToken.address, name: inputToken.name },
account,
permitInfo,
amount,
})

if (!permitData) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useCallback } from 'react'

import { Field } from 'legacy/state/types'
import { useUserTransactionTTL } from 'legacy/state/user/hooks'
import { usePartialApprove, useUserTransactionTTL } from 'legacy/state/user/hooks'

import { TradeWidgetActions, useTradePriceImpact } from 'modules/trade'
import { logTradeFlow } from 'modules/trade/utils/logger'
Expand All @@ -25,7 +25,11 @@ export function useHandleSwapOrEthFlow(actions: TradeWidgetActions) {
const { onUserInput, onChangeRecipient } = actions

const [deadline] = useUserTransactionTTL()
const { callback: handleSwap, contextIsReady } = useHandleSwap(useSafeMemoObject({ deadline }), actions)
const [isPartialApprove] = usePartialApprove()
const { callback: handleSwap, contextIsReady } = useHandleSwap(
useSafeMemoObject({ deadline, isPartialApprove }),
actions,
)

const callback = useCallback(async () => {
if (!swapFlowContext) return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { Currency, CurrencyAmount } from '@uniswap/sdk-core'
import { useToggleWalletModal } from 'legacy/state/application/hooks'
import { useGetQuoteAndStatus, useIsBestQuoteLoading } from 'legacy/state/price/hooks'
import { Field } from 'legacy/state/types'
import { usePartialApprove } from 'legacy/state/user/hooks'

import { useCurrencyAmountBalanceCombined } from 'modules/combinedBalances'
import { useInjectedWidgetParams } from 'modules/injectedWidget'
Expand All @@ -38,8 +39,6 @@ import { useSafeMemo } from 'common/hooks/useSafeMemo'
import { useHandleSwapOrEthFlow } from './useHandleSwapOrEthFlow'
import { useDerivedSwapInfo, useSwapActionHandlers } from './useSwapState'

import { usePartialApprove } from '../../../legacy/state/user/hooks'

export interface SwapButtonInput {
feeWarningAccepted: boolean
impactWarningAccepted: boolean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@ import { TradeFlowContext } from '../types/TradeFlowContext'

export interface TradeFlowParams {
deadline: number
isPartialApprove?: boolean
}

export function useTradeFlowContext({ deadline }: TradeFlowParams): TradeFlowContext | null {
export function useTradeFlowContext({ deadline, isPartialApprove }: TradeFlowParams): TradeFlowContext | null {
const { chainId, account } = useWalletInfo()
const provider = useWalletProvider()
const { allowsOffchainSigning } = useWalletDetails()
Expand All @@ -49,7 +50,7 @@ export function useTradeFlowContext({ deadline }: TradeFlowParams): TradeFlowCon
const networkFee = receiveAmountInfo?.costs.networkFee.amountInSellCurrency

const permitInfo = usePermitInfo(sellCurrency, tradeType)
const generatePermitHook = useGeneratePermitHook()
const generatePermitHook = useGeneratePermitHook(isPartialApprove)
const getCachedPermit = useGetCachedPermit()
const closeModals = useCloseModals()
const dispatch = useDispatch<AppDispatch>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ export async function swapFlow(
inputToken: inputCurrency,
permitInfo,
generatePermitHook,
amount: BigInt(inputAmount.quotient.toString()),
})

if (callDataContainsPermitSigner(orderParams.appData.fullAppData)) {
Expand Down
14 changes: 12 additions & 2 deletions libs/permit-utils/src/lib/generatePermitHook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,17 @@ export async function generatePermitHook(params: PermitHookParams): Promise<Perm
}

async function generatePermitHookRaw(params: PermitHookParams): Promise<PermitHookData> {
const { inputToken, spender, chainId, permitInfo, provider, account, eip2162Utils, nonce: preFetchedNonce } = params
const {
inputToken,
spender,
chainId,
permitInfo,
provider,
account,
eip2162Utils,
nonce: preFetchedNonce,
amount,
} = params

const tokenAddress = inputToken.address
// TODO: remove the need for `name` from input token. Should come from permitInfo instead
Expand All @@ -58,7 +68,7 @@ async function generatePermitHookRaw(params: PermitHookParams): Promise<PermitHo
const nonce = preFetchedNonce === undefined ? await eip2162Utils.getTokenNonce(tokenAddress, owner) : preFetchedNonce

const deadline = getPermitDeadline()
const value = DEFAULT_PERMIT_VALUE
const value = amount ? amount.toString() : DEFAULT_PERMIT_VALUE

const callData =
permitInfo.type === 'eip-2612'
Expand Down
1 change: 1 addition & 0 deletions libs/permit-utils/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export type PermitHookParams = {
permitInfo: PermitInfo
provider: JsonRpcProvider
eip2162Utils: Eip2612PermitUtils
amount?: bigint
account?: string | undefined
nonce?: number | undefined
}
Expand Down

0 comments on commit a017d48

Please sign in to comment.