@lifi/sdk 3.7.9 → 3.7.10-beta.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (35) hide show
  1. package/package.json +7 -7
  2. package/src/_cjs/core/EVM/EVMStepExecutor.js +3 -0
  3. package/src/_cjs/core/EVM/EVMStepExecutor.js.map +1 -1
  4. package/src/_cjs/core/EVM/abi.js +2 -10
  5. package/src/_cjs/core/EVM/abi.js.map +1 -1
  6. package/src/_cjs/core/EVM/permits/constants.js +32 -1
  7. package/src/_cjs/core/EVM/permits/constants.js.map +1 -1
  8. package/src/_cjs/core/EVM/permits/getNativePermit.js +57 -48
  9. package/src/_cjs/core/EVM/permits/getNativePermit.js.map +1 -1
  10. package/src/_cjs/version.js +1 -1
  11. package/src/_cjs/version.js.map +1 -1
  12. package/src/_esm/core/EVM/EVMStepExecutor.js +4 -1
  13. package/src/_esm/core/EVM/EVMStepExecutor.js.map +1 -1
  14. package/src/_esm/core/EVM/abi.js +1 -11
  15. package/src/_esm/core/EVM/abi.js.map +1 -1
  16. package/src/_esm/core/EVM/permits/constants.js +59 -0
  17. package/src/_esm/core/EVM/permits/constants.js.map +1 -1
  18. package/src/_esm/core/EVM/permits/getNativePermit.js +63 -70
  19. package/src/_esm/core/EVM/permits/getNativePermit.js.map +1 -1
  20. package/src/_esm/version.js +1 -1
  21. package/src/_esm/version.js.map +1 -1
  22. package/src/_types/core/EVM/EVMStepExecutor.d.ts.map +1 -1
  23. package/src/_types/core/EVM/abi.d.ts +8 -18
  24. package/src/_types/core/EVM/abi.d.ts.map +1 -1
  25. package/src/_types/core/EVM/permits/constants.d.ts +67 -0
  26. package/src/_types/core/EVM/permits/constants.d.ts.map +1 -1
  27. package/src/_types/core/EVM/permits/getNativePermit.d.ts +7 -0
  28. package/src/_types/core/EVM/permits/getNativePermit.d.ts.map +1 -1
  29. package/src/_types/version.d.ts +1 -1
  30. package/src/_types/version.d.ts.map +1 -1
  31. package/src/core/EVM/EVMStepExecutor.ts +4 -1
  32. package/src/core/EVM/abi.ts +1 -12
  33. package/src/core/EVM/permits/constants.ts +71 -0
  34. package/src/core/EVM/permits/getNativePermit.ts +107 -95
  35. package/src/version.ts +1 -1
@@ -9,33 +9,17 @@ import {
9
9
  import type { Address, Client, Hex } from 'viem'
10
10
  import type { TypedDataDomain } from 'viem'
11
11
  import { multicall, readContract } from 'viem/actions'
12
- import { eip2612Abi, eip2612Types } from '../abi.js'
12
+ import { eip2612Abi } from '../abi.js'
13
13
  import { getActionWithFallback } from '../getActionWithFallback.js'
14
14
  import { getMulticallAddress } from '../utils.js'
15
+ import {
16
+ DAI_LIKE_PERMIT_TYPEHASH,
17
+ EIP712_DOMAIN_TYPEHASH,
18
+ EIP712_DOMAIN_TYPEHASH_WITH_SALT,
19
+ eip2612Types,
20
+ } from './constants.js'
15
21
  import type { NativePermitData } from './types.js'
16
22
 
17
- /**
18
- * EIP-712 domain typehash with chainId
19
- * @link https://eips.ethereum.org/EIPS/eip-712#specification
20
- *
21
- * keccak256(toBytes(
22
- * 'EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)'
23
- * ))
24
- */
25
- const EIP712_DOMAIN_TYPEHASH =
26
- '0x8b73c3c69bb8fe3d512ecc4cf759cc79239f7b179b0ffacaa9a75d522b39400f' as Hex
27
-
28
- /**
29
- * EIP-712 domain typehash with salt (e.g. USDC.e on Polygon)
30
- * @link https://eips.ethereum.org/EIPS/eip-712#specification
31
- *
32
- * keccak256(toBytes(
33
- * 'EIP712Domain(string name,string version,address verifyingContract,bytes32 salt)'
34
- * ))
35
- */
36
- const EIP712_DOMAIN_TYPEHASH_WITH_SALT =
37
- '0x36c25de3e541d5d970f66e4210d728721220fff5c077cc6cd008b3a0c62adab7' as Hex
38
-
39
23
  export type GetNativePermitParams = {
40
24
  chainId: number
41
25
  tokenAddress: Address
@@ -142,18 +126,11 @@ function validateDomainSeparator({
142
126
  }
143
127
  }
144
128
 
145
- /**
146
- * Retrieves native permit data (EIP-2612) for a token on a specific chain
147
- * @link https://eips.ethereum.org/EIPS/eip-2612
148
- * @param client - The Viem client instance
149
- * @param chain - The extended chain object containing chain details
150
- * @param tokenAddress - The address of the token to check for permit support
151
- * @returns {Promise<NativePermitData>} Object containing permit data including name, version, nonce and support status
152
- */
153
- export const getNativePermit = async (
129
+ export const getContractData = async (
154
130
  client: Client,
155
- { chainId, tokenAddress, spenderAddress, amount }: GetNativePermitParams
156
- ): Promise<NativePermitData | undefined> => {
131
+ chainId: number,
132
+ tokenAddress: Address
133
+ ) => {
157
134
  try {
158
135
  const multicallAddress = await getMulticallAddress(chainId)
159
136
 
@@ -168,6 +145,11 @@ export const getNativePermit = async (
168
145
  abi: eip2612Abi,
169
146
  functionName: 'DOMAIN_SEPARATOR',
170
147
  },
148
+ {
149
+ address: tokenAddress,
150
+ abi: eip2612Abi,
151
+ functionName: 'PERMIT_TYPEHASH',
152
+ },
171
153
  {
172
154
  address: tokenAddress,
173
155
  abi: eip2612Abi,
@@ -183,11 +165,16 @@ export const getNativePermit = async (
183
165
 
184
166
  if (multicallAddress) {
185
167
  try {
186
- const [nameResult, domainSeparatorResult, noncesResult, versionResult] =
187
- await getActionWithFallback(client, multicall, 'multicall', {
188
- contracts: contractCalls,
189
- multicallAddress,
190
- })
168
+ const [
169
+ nameResult,
170
+ domainSeparatorResult,
171
+ permitTypehashResult,
172
+ noncesResult,
173
+ versionResult,
174
+ ] = await getActionWithFallback(client, multicall, 'multicall', {
175
+ contracts: contractCalls,
176
+ multicallAddress,
177
+ })
191
178
 
192
179
  if (
193
180
  nameResult.status !== 'success' ||
@@ -201,48 +188,35 @@ export const getNativePermit = async (
201
188
  throw new Error('Multicall failed')
202
189
  }
203
190
 
204
- const { isValid, domain } = validateDomainSeparator({
191
+ return {
205
192
  name: nameResult.result,
206
- version: versionResult.result ?? '1',
207
- chainId,
208
- verifyingContract: tokenAddress,
209
193
  domainSeparator: domainSeparatorResult.result,
210
- })
211
-
212
- if (!isValid) {
213
- return undefined
214
- }
215
-
216
- const message = {
217
- owner: client.account!.address,
218
- spender: spenderAddress,
219
- value: amount.toString(),
220
- nonce: noncesResult.result.toString(),
221
- deadline: BigInt(Math.floor(Date.now() / 1000) + 30 * 60).toString(), // 30 minutes
222
- }
223
-
224
- return {
225
- primaryType: 'Permit',
226
- domain,
227
- types: eip2612Types,
228
- message,
194
+ permitTypehash: permitTypehashResult.result,
195
+ nonce: noncesResult.result,
196
+ version: versionResult.result ?? '1',
229
197
  }
230
198
  } catch {
231
199
  // Fall through to individual calls
232
200
  }
233
201
  }
234
202
 
235
- const [nameResult, domainSeparatorResult, noncesResult, versionResult] =
236
- (await Promise.allSettled(
237
- contractCalls.map((call) =>
238
- getActionWithFallback(client, readContract, 'readContract', call)
239
- )
240
- )) as [
241
- PromiseSettledResult<string>,
242
- PromiseSettledResult<Hex>,
243
- PromiseSettledResult<bigint>,
244
- PromiseSettledResult<string>,
245
- ]
203
+ const [
204
+ nameResult,
205
+ domainSeparatorResult,
206
+ permitTypehashResult,
207
+ noncesResult,
208
+ versionResult,
209
+ ] = (await Promise.allSettled(
210
+ contractCalls.map((call) =>
211
+ getActionWithFallback(client, readContract, 'readContract', call)
212
+ )
213
+ )) as [
214
+ PromiseSettledResult<string>,
215
+ PromiseSettledResult<Hex>,
216
+ PromiseSettledResult<Hex>,
217
+ PromiseSettledResult<bigint>,
218
+ PromiseSettledResult<string>,
219
+ ]
246
220
 
247
221
  if (
248
222
  nameResult.status !== 'fulfilled' ||
@@ -255,33 +229,71 @@ export const getNativePermit = async (
255
229
  const name = nameResult.value
256
230
  const version =
257
231
  versionResult.status === 'fulfilled' ? versionResult.value : '1'
258
- const { isValid, domain } = validateDomainSeparator({
232
+
233
+ return {
259
234
  name,
260
- version,
261
- chainId,
262
- verifyingContract: tokenAddress,
263
235
  domainSeparator: domainSeparatorResult.value,
264
- })
265
-
266
- if (!isValid) {
267
- return undefined
236
+ permitTypehash:
237
+ permitTypehashResult.status === 'fulfilled'
238
+ ? permitTypehashResult.value
239
+ : undefined,
240
+ nonce: noncesResult.value,
241
+ version,
268
242
  }
243
+ } catch {
244
+ return undefined
245
+ }
246
+ }
269
247
 
270
- const message = {
271
- owner: client.account!.address,
272
- spender: spenderAddress,
273
- value: amount.toString(),
274
- nonce: noncesResult.value.toString(),
275
- deadline: BigInt(Math.floor(Date.now() / 1000) + 30 * 60).toString(), // 30 minutes
276
- }
248
+ /**
249
+ * Retrieves native permit data (EIP-2612) for a token on a specific chain
250
+ * @link https://eips.ethereum.org/EIPS/eip-2612
251
+ * @param client - The Viem client instance
252
+ * @param chain - The extended chain object containing chain details
253
+ * @param tokenAddress - The address of the token to check for permit support
254
+ * @returns {Promise<NativePermitData>} Object containing permit data including name, version, nonce and support status
255
+ */
256
+ export const getNativePermit = async (
257
+ client: Client,
258
+ { chainId, tokenAddress, spenderAddress, amount }: GetNativePermitParams
259
+ ): Promise<NativePermitData | undefined> => {
260
+ const contractData = await getContractData(client, chainId, tokenAddress)
261
+ if (!contractData) {
262
+ return undefined
263
+ }
264
+ const { name, domainSeparator, permitTypehash, nonce, version } = contractData
277
265
 
278
- return {
279
- primaryType: 'Permit',
280
- domain,
281
- types: eip2612Types,
282
- message,
283
- }
284
- } catch {
266
+ // We don't support DAI-like permits yet (e.g. DAI on Ethereum)
267
+ if (permitTypehash === DAI_LIKE_PERMIT_TYPEHASH) {
268
+ return undefined
269
+ }
270
+
271
+ const { isValid, domain } = validateDomainSeparator({
272
+ name,
273
+ version,
274
+ chainId,
275
+ verifyingContract: tokenAddress,
276
+ domainSeparator,
277
+ })
278
+
279
+ if (!isValid) {
285
280
  return undefined
286
281
  }
282
+
283
+ const deadline = BigInt(Math.floor(Date.now() / 1000) + 30 * 60).toString() // 30 minutes
284
+
285
+ const message = {
286
+ owner: client.account!.address,
287
+ spender: spenderAddress,
288
+ value: amount.toString(),
289
+ nonce: nonce.toString(),
290
+ deadline,
291
+ }
292
+
293
+ return {
294
+ primaryType: 'Permit',
295
+ domain,
296
+ types: eip2612Types,
297
+ message,
298
+ }
287
299
  }
package/src/version.ts CHANGED
@@ -1,2 +1,2 @@
1
1
  export const name = '@lifi/sdk'
2
- export const version = '3.7.9'
2
+ export const version = '3.7.10-beta.0'