From 97295b02a3b477c0225c73406ae87e323b09e7a2 Mon Sep 17 00:00:00 2001 From: Alfetopito Date: Mon, 16 Oct 2023 18:00:08 +0100 Subject: [PATCH] feat: query and add version to permitInfo --- .../src/modules/permit/types.ts | 1 + .../permit/utils/checkIsTokenPermittable.ts | 22 ++++++++++++++++--- .../permit/utils/generatePermitHook.ts | 2 ++ 3 files changed, 22 insertions(+), 3 deletions(-) diff --git a/apps/cowswap-frontend/src/modules/permit/types.ts b/apps/cowswap-frontend/src/modules/permit/types.ts index 92cdd414c6..312b5442a9 100644 --- a/apps/cowswap-frontend/src/modules/permit/types.ts +++ b/apps/cowswap-frontend/src/modules/permit/types.ts @@ -13,6 +13,7 @@ export type PermitType = 'dai-like' | 'eip-2612' export type SupportedPermitInfo = { type: PermitType + version: string | undefined // Some tokens have it different than `1`, and won't work without it } type UnsupportedPermitInfo = false export type PermitInfo = SupportedPermitInfo | UnsupportedPermitInfo diff --git a/apps/cowswap-frontend/src/modules/permit/utils/checkIsTokenPermittable.ts b/apps/cowswap-frontend/src/modules/permit/utils/checkIsTokenPermittable.ts index a0fca515ad..b0f420a5f6 100644 --- a/apps/cowswap-frontend/src/modules/permit/utils/checkIsTokenPermittable.ts +++ b/apps/cowswap-frontend/src/modules/permit/utils/checkIsTokenPermittable.ts @@ -74,6 +74,17 @@ async function actuallyCheckTokenIsPermittable(params: CheckIsTokenPermittablePa return { error: e.message || e.toString() } } + let version: string | undefined = undefined + + try { + // Required by USDC-mainnet as its version is `2`. + // There might be other tokens that need this as well. + version = await eip2612PermitUtils.getTokenVersion(tokenAddress) + } catch (e) { + // Not a problem, we can (try to) continue without it, and will default to `1` (part of the 1inch lib) + console.debug(`[checkTokenIsPermittable] Failed to get version for ${tokenAddress}`, e) + } + const baseParams: BaseParams = { chainId, eip2612PermitUtils, @@ -82,6 +93,7 @@ async function actuallyCheckTokenIsPermittable(params: CheckIsTokenPermittablePa tokenAddress, tokenName, walletAddress: owner, + version, } try { @@ -108,6 +120,7 @@ type BaseParams = { spender: string eip2612PermitUtils: Eip2612PermitUtils nonce: number + version: string | undefined } type EstimateParams = BaseParams & { @@ -116,7 +129,7 @@ type EstimateParams = BaseParams & { } async function estimateTokenPermit(params: EstimateParams): Promise { - const { provider, chainId, walletAddress, tokenAddress, type } = params + const { provider, chainId, walletAddress, tokenAddress, type, version } = params const getCallDataFn = type === 'eip-2612' ? getEip2612CallData : getDaiLikeCallData @@ -137,12 +150,13 @@ async function estimateTokenPermit(params: EstimateParams): Promise PERMIT_GAS_LIMIT_MIN[chainId] ? { type, + version, } : false } async function getEip2612CallData(params: BaseParams): Promise { - const { eip2612PermitUtils, walletAddress, spender, nonce, chainId, tokenName, tokenAddress } = params + const { eip2612PermitUtils, walletAddress, spender, nonce, chainId, tokenName, tokenAddress, version } = params return buildEip2162PermitCallData({ eip2162Utils: eip2612PermitUtils, callDataParams: [ @@ -155,12 +169,13 @@ async function getEip2612CallData(params: BaseParams): Promise { +chainId, tokenName, tokenAddress, + version, ], }) } async function getDaiLikeCallData(params: BaseParams): Promise { - const { eip2612PermitUtils, tokenAddress, walletAddress, spender, nonce, chainId, tokenName } = params + const { eip2612PermitUtils, tokenAddress, walletAddress, spender, nonce, chainId, tokenName, version } = params const permitTypeHash = await eip2612PermitUtils.getPermitTypeHash(tokenAddress) @@ -177,6 +192,7 @@ async function getDaiLikeCallData(params: BaseParams): Promise { chainId as number, tokenName, tokenAddress, + version, ], }) } diff --git a/apps/cowswap-frontend/src/modules/permit/utils/generatePermitHook.ts b/apps/cowswap-frontend/src/modules/permit/utils/generatePermitHook.ts index 7d781342aa..ea9db1f713 100644 --- a/apps/cowswap-frontend/src/modules/permit/utils/generatePermitHook.ts +++ b/apps/cowswap-frontend/src/modules/permit/utils/generatePermitHook.ts @@ -65,6 +65,7 @@ async function generatePermitHookRaw(params: PermitHookParams): Promise