Skip to content

Commit

Permalink
feat: query and add version to permitInfo
Browse files Browse the repository at this point in the history
  • Loading branch information
alfetopito committed Oct 16, 2023
1 parent f1959bf commit 97295b0
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
1 change: 1 addition & 0 deletions apps/cowswap-frontend/src/modules/permit/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -82,6 +93,7 @@ async function actuallyCheckTokenIsPermittable(params: CheckIsTokenPermittablePa
tokenAddress,
tokenName,
walletAddress: owner,
version,
}

try {
Expand All @@ -108,6 +120,7 @@ type BaseParams = {
spender: string
eip2612PermitUtils: Eip2612PermitUtils
nonce: number
version: string | undefined
}

type EstimateParams = BaseParams & {
Expand All @@ -116,7 +129,7 @@ type EstimateParams = BaseParams & {
}

async function estimateTokenPermit(params: EstimateParams): Promise<EstimatePermitResult> {
const { provider, chainId, walletAddress, tokenAddress, type } = params
const { provider, chainId, walletAddress, tokenAddress, type, version } = params

const getCallDataFn = type === 'eip-2612' ? getEip2612CallData : getDaiLikeCallData

Expand All @@ -137,12 +150,13 @@ async function estimateTokenPermit(params: EstimateParams): Promise<EstimatePerm
return gasLimit > PERMIT_GAS_LIMIT_MIN[chainId]
? {
type,
version,
}
: false
}

async function getEip2612CallData(params: BaseParams): Promise<string> {
const { eip2612PermitUtils, walletAddress, spender, nonce, chainId, tokenName, tokenAddress } = params
const { eip2612PermitUtils, walletAddress, spender, nonce, chainId, tokenName, tokenAddress, version } = params
return buildEip2162PermitCallData({
eip2162Utils: eip2612PermitUtils,
callDataParams: [
Expand All @@ -155,12 +169,13 @@ async function getEip2612CallData(params: BaseParams): Promise<string> {
+chainId,
tokenName,
tokenAddress,
version,
],
})
}

async function getDaiLikeCallData(params: BaseParams): Promise<string | false> {
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)

Expand All @@ -177,6 +192,7 @@ async function getDaiLikeCallData(params: BaseParams): Promise<string | false> {
chainId as number,
tokenName,
tokenAddress,
version,
],
})
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ async function generatePermitHookRaw(params: PermitHookParams): Promise<PermitHo
chainId as number,
tokenName,
tokenAddress,
permitInfo.version,
],
})
: await buildDaiLikePermitCallData({
Expand All @@ -81,6 +82,7 @@ async function generatePermitHookRaw(params: PermitHookParams): Promise<PermitHo
chainId as number,
tokenName,
tokenAddress,
permitInfo.version,
],
})

Expand Down

0 comments on commit 97295b0

Please sign in to comment.