@lifi/sdk 3.5.0-beta.0 → 3.5.1-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 (183) hide show
  1. package/package.json +8 -9
  2. package/src/_cjs/constants.js +2 -4
  3. package/src/_cjs/constants.js.map +1 -1
  4. package/src/_cjs/core/EVM/EVM.js +4 -0
  5. package/src/_cjs/core/EVM/EVM.js.map +1 -1
  6. package/src/_cjs/core/EVM/EVMStepExecutor.js +79 -145
  7. package/src/_cjs/core/EVM/EVMStepExecutor.js.map +1 -1
  8. package/src/_cjs/core/EVM/abi.js +43 -36
  9. package/src/_cjs/core/EVM/abi.js.map +1 -1
  10. package/src/_cjs/core/EVM/checkAllowance.js +30 -36
  11. package/src/_cjs/core/EVM/checkAllowance.js.map +1 -1
  12. package/src/_cjs/core/EVM/getAllowance.js.map +1 -1
  13. package/src/_cjs/core/EVM/getEVMBalance.js.map +1 -1
  14. package/src/_cjs/core/EVM/multisig.js +29 -0
  15. package/src/_cjs/core/EVM/multisig.js.map +1 -0
  16. package/src/_cjs/core/EVM/parseEVMErrors.js +0 -3
  17. package/src/_cjs/core/EVM/parseEVMErrors.js.map +1 -1
  18. package/src/_cjs/core/EVM/setAllowance.js +6 -4
  19. package/src/_cjs/core/EVM/setAllowance.js.map +1 -1
  20. package/src/_cjs/core/EVM/switchChain.js +3 -2
  21. package/src/_cjs/core/EVM/switchChain.js.map +1 -1
  22. package/src/_cjs/core/EVM/types.js.map +1 -1
  23. package/src/_cjs/core/EVM/utils.js +1 -2
  24. package/src/_cjs/core/EVM/utils.js.map +1 -1
  25. package/src/_cjs/core/Solana/SolanaStepExecutor.js +2 -48
  26. package/src/_cjs/core/Solana/SolanaStepExecutor.js.map +1 -1
  27. package/src/_cjs/core/UTXO/UTXOStepExecutor.js +2 -48
  28. package/src/_cjs/core/UTXO/UTXOStepExecutor.js.map +1 -1
  29. package/src/_cjs/core/checkBalance.js +3 -3
  30. package/src/_cjs/core/checkBalance.js.map +1 -1
  31. package/src/_cjs/core/waitForDestinationChainTransaction.js +53 -0
  32. package/src/_cjs/core/waitForDestinationChainTransaction.js.map +1 -0
  33. package/src/_cjs/core/{waitForReceivingTransaction.js → waitForTransactionStatus.js} +3 -3
  34. package/src/_cjs/core/waitForTransactionStatus.js.map +1 -0
  35. package/src/_cjs/index.js.map +1 -1
  36. package/src/_cjs/version.js +1 -1
  37. package/src/_esm/constants.js +1 -3
  38. package/src/_esm/constants.js.map +1 -1
  39. package/src/_esm/core/EVM/EVM.js +4 -0
  40. package/src/_esm/core/EVM/EVM.js.map +1 -1
  41. package/src/_esm/core/EVM/EVMStepExecutor.js +92 -179
  42. package/src/_esm/core/EVM/EVMStepExecutor.js.map +1 -1
  43. package/src/_esm/core/EVM/abi.js +42 -37
  44. package/src/_esm/core/EVM/abi.js.map +1 -1
  45. package/src/_esm/core/EVM/checkAllowance.js +32 -41
  46. package/src/_esm/core/EVM/checkAllowance.js.map +1 -1
  47. package/src/_esm/core/EVM/getAllowance.js.map +1 -1
  48. package/src/_esm/core/EVM/getEVMBalance.js.map +1 -1
  49. package/src/_esm/core/EVM/multisig.js +25 -0
  50. package/src/_esm/core/EVM/multisig.js.map +1 -0
  51. package/src/_esm/core/EVM/parseEVMErrors.js +0 -8
  52. package/src/_esm/core/EVM/parseEVMErrors.js.map +1 -1
  53. package/src/_esm/core/EVM/setAllowance.js +6 -4
  54. package/src/_esm/core/EVM/setAllowance.js.map +1 -1
  55. package/src/_esm/core/EVM/switchChain.js +3 -2
  56. package/src/_esm/core/EVM/switchChain.js.map +1 -1
  57. package/src/_esm/core/EVM/types.js.map +1 -1
  58. package/src/_esm/core/EVM/utils.js +1 -2
  59. package/src/_esm/core/EVM/utils.js.map +1 -1
  60. package/src/_esm/core/Solana/SolanaStepExecutor.js +3 -50
  61. package/src/_esm/core/Solana/SolanaStepExecutor.js.map +1 -1
  62. package/src/_esm/core/UTXO/UTXOStepExecutor.js +4 -51
  63. package/src/_esm/core/UTXO/UTXOStepExecutor.js.map +1 -1
  64. package/src/_esm/core/checkBalance.js +3 -3
  65. package/src/_esm/core/checkBalance.js.map +1 -1
  66. package/src/_esm/core/waitForDestinationChainTransaction.js +52 -0
  67. package/src/_esm/core/waitForDestinationChainTransaction.js.map +1 -0
  68. package/src/_esm/core/{waitForReceivingTransaction.js → waitForTransactionStatus.js} +2 -2
  69. package/src/_esm/core/waitForTransactionStatus.js.map +1 -0
  70. package/src/_esm/index.js.map +1 -1
  71. package/src/_esm/version.js +1 -1
  72. package/src/_types/constants.d.ts +1 -3
  73. package/src/_types/constants.d.ts.map +1 -1
  74. package/src/_types/core/EVM/EVM.d.ts.map +1 -1
  75. package/src/_types/core/EVM/EVMStepExecutor.d.ts +4 -7
  76. package/src/_types/core/EVM/EVMStepExecutor.d.ts.map +1 -1
  77. package/src/_types/core/EVM/abi.d.ts +5 -202
  78. package/src/_types/core/EVM/abi.d.ts.map +1 -1
  79. package/src/_types/core/EVM/checkAllowance.d.ts +2 -12
  80. package/src/_types/core/EVM/checkAllowance.d.ts.map +1 -1
  81. package/src/_types/core/EVM/getAllowance.d.ts +4 -5
  82. package/src/_types/core/EVM/getAllowance.d.ts.map +1 -1
  83. package/src/_types/core/EVM/getEVMBalance.d.ts +1 -2
  84. package/src/_types/core/EVM/getEVMBalance.d.ts.map +1 -1
  85. package/src/_types/core/EVM/multisig.d.ts +6 -0
  86. package/src/_types/core/EVM/multisig.d.ts.map +1 -0
  87. package/src/_types/core/EVM/setAllowance.d.ts +2 -2
  88. package/src/_types/core/EVM/setAllowance.d.ts.map +1 -1
  89. package/src/_types/core/EVM/switchChain.d.ts.map +1 -1
  90. package/src/_types/core/EVM/types.d.ts +18 -1
  91. package/src/_types/core/EVM/types.d.ts.map +1 -1
  92. package/src/_types/core/EVM/utils.d.ts +2 -2
  93. package/src/_types/core/EVM/utils.d.ts.map +1 -1
  94. package/src/_types/core/Solana/SolanaStepExecutor.d.ts.map +1 -1
  95. package/src/_types/core/UTXO/UTXOStepExecutor.d.ts.map +1 -1
  96. package/src/_types/core/checkBalance.d.ts.map +1 -1
  97. package/src/_types/core/waitForDestinationChainTransaction.d.ts +5 -0
  98. package/src/_types/core/waitForDestinationChainTransaction.d.ts.map +1 -0
  99. package/src/_types/core/waitForTransactionStatus.d.ts +4 -0
  100. package/src/_types/core/waitForTransactionStatus.d.ts.map +1 -0
  101. package/src/_types/index.d.ts +1 -1
  102. package/src/_types/index.d.ts.map +1 -1
  103. package/src/_types/version.d.ts +1 -1
  104. package/src/constants.ts +1 -6
  105. package/src/core/EVM/EVM.ts +4 -0
  106. package/src/core/EVM/EVMStepExecutor.ts +156 -245
  107. package/src/core/EVM/abi.ts +43 -40
  108. package/src/core/EVM/checkAllowance.ts +92 -96
  109. package/src/core/EVM/getAllowance.ts +8 -8
  110. package/src/core/EVM/getEVMBalance.ts +2 -2
  111. package/src/core/EVM/multisig.ts +54 -0
  112. package/src/core/EVM/parseEVMErrors.ts +0 -8
  113. package/src/core/EVM/setAllowance.ts +21 -15
  114. package/src/core/EVM/switchChain.ts +12 -3
  115. package/src/core/EVM/types.ts +27 -1
  116. package/src/core/EVM/utils.ts +3 -4
  117. package/src/core/Solana/SolanaStepExecutor.ts +9 -63
  118. package/src/core/UTXO/UTXOStepExecutor.ts +10 -68
  119. package/src/core/checkBalance.ts +3 -6
  120. package/src/core/waitForDestinationChainTransaction.ts +79 -0
  121. package/src/core/{waitForReceivingTransaction.ts → waitForTransactionStatus.ts} +1 -1
  122. package/src/index.ts +3 -0
  123. package/src/version.ts +1 -1
  124. package/src/_cjs/core/EVM/getNativePermit.js +0 -90
  125. package/src/_cjs/core/EVM/getNativePermit.js.map +0 -1
  126. package/src/_cjs/core/EVM/permit2/allowanceTransfer.js +0 -100
  127. package/src/_cjs/core/EVM/permit2/allowanceTransfer.js.map +0 -1
  128. package/src/_cjs/core/EVM/permit2/constants.js +0 -12
  129. package/src/_cjs/core/EVM/permit2/constants.js.map +0 -1
  130. package/src/_cjs/core/EVM/permit2/domain.js +0 -12
  131. package/src/_cjs/core/EVM/permit2/domain.js.map +0 -1
  132. package/src/_cjs/core/EVM/permit2/signatureTransfer.js +0 -121
  133. package/src/_cjs/core/EVM/permit2/signatureTransfer.js.map +0 -1
  134. package/src/_cjs/core/EVM/signPermitMessage.js +0 -168
  135. package/src/_cjs/core/EVM/signPermitMessage.js.map +0 -1
  136. package/src/_cjs/core/EVM/waitForBatchTransactionReceipt.js +0 -29
  137. package/src/_cjs/core/EVM/waitForBatchTransactionReceipt.js.map +0 -1
  138. package/src/_cjs/core/waitForReceivingTransaction.js.map +0 -1
  139. package/src/_cjs/utils/invariant.js +0 -17
  140. package/src/_cjs/utils/invariant.js.map +0 -1
  141. package/src/_esm/core/EVM/getNativePermit.js +0 -95
  142. package/src/_esm/core/EVM/getNativePermit.js.map +0 -1
  143. package/src/_esm/core/EVM/permit2/allowanceTransfer.js +0 -93
  144. package/src/_esm/core/EVM/permit2/allowanceTransfer.js.map +0 -1
  145. package/src/_esm/core/EVM/permit2/constants.js +0 -9
  146. package/src/_esm/core/EVM/permit2/constants.js.map +0 -1
  147. package/src/_esm/core/EVM/permit2/domain.js +0 -9
  148. package/src/_esm/core/EVM/permit2/domain.js.map +0 -1
  149. package/src/_esm/core/EVM/permit2/signatureTransfer.js +0 -117
  150. package/src/_esm/core/EVM/permit2/signatureTransfer.js.map +0 -1
  151. package/src/_esm/core/EVM/signPermitMessage.js +0 -162
  152. package/src/_esm/core/EVM/signPermitMessage.js.map +0 -1
  153. package/src/_esm/core/EVM/waitForBatchTransactionReceipt.js +0 -25
  154. package/src/_esm/core/EVM/waitForBatchTransactionReceipt.js.map +0 -1
  155. package/src/_esm/core/waitForReceivingTransaction.js.map +0 -1
  156. package/src/_esm/utils/invariant.js +0 -43
  157. package/src/_esm/utils/invariant.js.map +0 -1
  158. package/src/_types/core/EVM/getNativePermit.d.ts +0 -18
  159. package/src/_types/core/EVM/getNativePermit.d.ts.map +0 -1
  160. package/src/_types/core/EVM/permit2/allowanceTransfer.d.ts +0 -41
  161. package/src/_types/core/EVM/permit2/allowanceTransfer.d.ts.map +0 -1
  162. package/src/_types/core/EVM/permit2/constants.d.ts +0 -8
  163. package/src/_types/core/EVM/permit2/constants.d.ts.map +0 -1
  164. package/src/_types/core/EVM/permit2/domain.d.ts +0 -8
  165. package/src/_types/core/EVM/permit2/domain.d.ts.map +0 -1
  166. package/src/_types/core/EVM/permit2/signatureTransfer.d.ts +0 -42
  167. package/src/_types/core/EVM/permit2/signatureTransfer.d.ts.map +0 -1
  168. package/src/_types/core/EVM/signPermitMessage.d.ts +0 -22
  169. package/src/_types/core/EVM/signPermitMessage.d.ts.map +0 -1
  170. package/src/_types/core/EVM/waitForBatchTransactionReceipt.d.ts +0 -4
  171. package/src/_types/core/EVM/waitForBatchTransactionReceipt.d.ts.map +0 -1
  172. package/src/_types/core/waitForReceivingTransaction.d.ts +0 -4
  173. package/src/_types/core/waitForReceivingTransaction.d.ts.map +0 -1
  174. package/src/_types/utils/invariant.d.ts +0 -22
  175. package/src/_types/utils/invariant.d.ts.map +0 -1
  176. package/src/core/EVM/getNativePermit.ts +0 -113
  177. package/src/core/EVM/permit2/allowanceTransfer.ts +0 -168
  178. package/src/core/EVM/permit2/constants.ts +0 -11
  179. package/src/core/EVM/permit2/domain.ts +0 -20
  180. package/src/core/EVM/permit2/signatureTransfer.ts +0 -214
  181. package/src/core/EVM/signPermitMessage.ts +0 -248
  182. package/src/core/EVM/waitForBatchTransactionReceipt.ts +0 -49
  183. package/src/utils/invariant.ts +0 -51
@@ -1,168 +0,0 @@
1
- import {
2
- type Address,
3
- type TypedData,
4
- type TypedDataDomain,
5
- hashTypedData,
6
- } from 'viem'
7
- import { MaxUint48, MaxUint160 } from '../../../constants.js'
8
- import { invariant } from '../../../utils/invariant.js'
9
- import { MaxSigDeadline } from './constants.js'
10
- import { permit2Domain } from './domain.js'
11
-
12
- export const MaxAllowanceTransferAmount = MaxUint160
13
- export const MaxAllowanceExpiration = MaxUint48
14
- export const MaxOrderedNonce = MaxUint48
15
-
16
- export interface PermitDetails {
17
- token: Address
18
- amount: bigint
19
- expiration: number
20
- nonce: number
21
- }
22
-
23
- export interface PermitSingle {
24
- details: PermitDetails
25
- spender: Address
26
- sigDeadline: bigint
27
- }
28
-
29
- export interface PermitBatch {
30
- details: PermitDetails[]
31
- spender: Address
32
- sigDeadline: bigint
33
- }
34
-
35
- export type PermitSingleData = {
36
- domain: TypedDataDomain
37
- types: TypedData
38
- values: PermitSingle
39
- }
40
-
41
- export type PermitBatchData = {
42
- domain: TypedDataDomain
43
- types: TypedData
44
- values: PermitBatch
45
- }
46
-
47
- const PERMIT_DETAILS = [
48
- { name: 'token', type: 'address' },
49
- { name: 'amount', type: 'uint160' },
50
- { name: 'expiration', type: 'uint48' },
51
- { name: 'nonce', type: 'uint48' },
52
- ] as const
53
-
54
- const PERMIT_TYPES = {
55
- PermitDetails: PERMIT_DETAILS,
56
- PermitSingle: [
57
- { name: 'details', type: 'PermitDetails' },
58
- { name: 'spender', type: 'address' },
59
- { name: 'sigDeadline', type: 'uint256' },
60
- ],
61
- } as const
62
-
63
- const PERMIT_BATCH_TYPES = {
64
- PermitDetails: PERMIT_DETAILS,
65
- PermitBatch: [
66
- { name: 'details', type: 'PermitDetails[]' },
67
- { name: 'spender', type: 'address' },
68
- { name: 'sigDeadline', type: 'uint256' },
69
- ],
70
- } as const
71
-
72
- function isPermit(permit: PermitSingle | PermitBatch): permit is PermitSingle {
73
- return !Array.isArray(permit.details)
74
- }
75
-
76
- export function getPermitSingleData(
77
- permit: PermitSingle,
78
- permit2Address: Address,
79
- chainId: number
80
- ) {
81
- invariant(MaxSigDeadline >= permit.sigDeadline, 'SIG_DEADLINE_OUT_OF_RANGE')
82
-
83
- const domain = permit2Domain(permit2Address, chainId)
84
- validatePermitDetails(permit.details)
85
-
86
- return {
87
- domain,
88
- values: permit,
89
- }
90
- }
91
-
92
- export function getPermitBatchData(
93
- permit: PermitBatch,
94
- permit2Address: Address,
95
- chainId: number
96
- ) {
97
- invariant(MaxSigDeadline >= permit.sigDeadline, 'SIG_DEADLINE_OUT_OF_RANGE')
98
-
99
- const domain = permit2Domain(permit2Address, chainId)
100
- permit.details.forEach(validatePermitDetails)
101
-
102
- return {
103
- domain,
104
- values: permit,
105
- }
106
- }
107
-
108
- export function getPermitData(
109
- permit: PermitSingle | PermitBatch,
110
- permit2Address: Address,
111
- chainId: number
112
- ): PermitSingleData | PermitBatchData {
113
- invariant(MaxSigDeadline >= permit.sigDeadline, 'SIG_DEADLINE_OUT_OF_RANGE')
114
-
115
- const domain = permit2Domain(permit2Address, chainId)
116
- if (isPermit(permit)) {
117
- validatePermitDetails(permit.details)
118
- return {
119
- domain,
120
- types: PERMIT_TYPES,
121
- values: permit,
122
- }
123
- }
124
- permit.details.forEach(validatePermitDetails)
125
- return {
126
- domain,
127
- types: PERMIT_BATCH_TYPES,
128
- values: permit,
129
- }
130
- }
131
-
132
- export function hash(
133
- permit: PermitSingle | PermitBatch,
134
- permit2Address: Address,
135
- chainId: number
136
- ): string {
137
- if (isPermit(permit)) {
138
- const { domain, values } = getPermitSingleData(
139
- permit,
140
- permit2Address,
141
- chainId
142
- )
143
-
144
- return hashTypedData({
145
- domain,
146
- types: PERMIT_TYPES,
147
- primaryType: 'PermitSingle',
148
- message: values,
149
- })
150
- }
151
- const { domain, values } = getPermitBatchData(permit, permit2Address, chainId)
152
-
153
- return hashTypedData({
154
- domain,
155
- types: PERMIT_BATCH_TYPES,
156
- primaryType: 'PermitBatch',
157
- message: values,
158
- })
159
- }
160
-
161
- function validatePermitDetails(details: PermitDetails) {
162
- invariant(MaxOrderedNonce >= details.nonce, 'NONCE_OUT_OF_RANGE')
163
- invariant(MaxAllowanceTransferAmount >= details.amount, 'AMOUNT_OUT_OF_RANGE')
164
- invariant(
165
- MaxAllowanceExpiration >= details.expiration,
166
- 'EXPIRATION_OUT_OF_RANGE'
167
- )
168
- }
@@ -1,11 +0,0 @@
1
- import { MaxUint48, MaxUint160, MaxUint256 } from '../../../constants.js'
2
-
3
- export const MaxAllowanceTransferAmount = MaxUint160
4
- export const MaxAllowanceExpiration = MaxUint48
5
- export const MaxOrderedNonce = MaxUint48
6
-
7
- export const MaxSignatureTransferAmount = MaxUint256
8
- export const MaxUnorderedNonce = MaxUint256
9
- export const MaxSigDeadline = MaxUint256
10
-
11
- export const InstantExpiration = 0n
@@ -1,20 +0,0 @@
1
- import type { Address, TypedData, TypedDataDomain } from 'viem'
2
-
3
- const PERMIT2_DOMAIN_NAME = 'Permit2'
4
-
5
- export function permit2Domain(
6
- permit2Address: Address,
7
- chainId: number
8
- ): TypedDataDomain {
9
- return {
10
- name: PERMIT2_DOMAIN_NAME,
11
- chainId,
12
- verifyingContract: permit2Address,
13
- }
14
- }
15
-
16
- export type PermitData = {
17
- domain: TypedDataDomain
18
- types: TypedData
19
- values: any
20
- }
@@ -1,214 +0,0 @@
1
- import { hashTypedData } from 'viem'
2
- import type { Address, TypedData, TypedDataDomain } from 'viem'
3
- import { invariant } from '../../../utils/invariant.js'
4
- import {
5
- MaxSigDeadline,
6
- MaxSignatureTransferAmount,
7
- MaxUnorderedNonce,
8
- } from './constants.js'
9
- import { permit2Domain } from './domain.js'
10
-
11
- export interface Witness {
12
- witness: any
13
- witnessTypeName: string
14
- witnessType: { [key: string]: { name: string; type: string }[] }
15
- }
16
-
17
- export interface TokenPermissions {
18
- token: Address
19
- amount: bigint
20
- }
21
-
22
- export interface PermitTransferFrom {
23
- permitted: TokenPermissions
24
- spender: Address
25
- nonce: bigint
26
- deadline: bigint
27
- }
28
-
29
- export interface PermitBatchTransferFrom {
30
- permitted: TokenPermissions[]
31
- spender: Address
32
- nonce: bigint
33
- deadline: bigint
34
- }
35
-
36
- export type PermitTransferFromData = {
37
- domain: TypedDataDomain
38
- types: TypedData
39
- values: PermitTransferFrom
40
- }
41
-
42
- export type PermitBatchTransferFromData = {
43
- domain: TypedDataDomain
44
- types: TypedData
45
- values: PermitBatchTransferFrom
46
- }
47
-
48
- const TOKEN_PERMISSIONS = [
49
- { name: 'token', type: 'address' },
50
- { name: 'amount', type: 'uint256' },
51
- ] as const
52
-
53
- const PERMIT_TRANSFER_FROM_TYPES = {
54
- TokenPermissions: TOKEN_PERMISSIONS,
55
- PermitTransferFrom: [
56
- { name: 'permitted', type: 'TokenPermissions' },
57
- { name: 'spender', type: 'address' },
58
- { name: 'nonce', type: 'uint256' },
59
- { name: 'deadline', type: 'uint256' },
60
- ],
61
- } as const
62
-
63
- const PERMIT_BATCH_TRANSFER_FROM_TYPES = {
64
- TokenPermissions: TOKEN_PERMISSIONS,
65
- PermitBatchTransferFrom: [
66
- { name: 'permitted', type: 'TokenPermissions[]' },
67
- { name: 'spender', type: 'address' },
68
- { name: 'nonce', type: 'uint256' },
69
- { name: 'deadline', type: 'uint256' },
70
- ],
71
- } as const
72
-
73
- function isPermitTransferFrom(
74
- permit: PermitTransferFrom | PermitBatchTransferFrom
75
- ): permit is PermitTransferFrom {
76
- return !Array.isArray(permit.permitted)
77
- }
78
-
79
- export function getPermitTransferData(
80
- permit: PermitTransferFrom,
81
- permit2Address: Address,
82
- chainId: number,
83
- witness?: Witness
84
- ): PermitTransferFromData {
85
- invariant(MaxSigDeadline >= permit.deadline, 'SIG_DEADLINE_OUT_OF_RANGE')
86
- invariant(MaxUnorderedNonce >= permit.nonce, 'NONCE_OUT_OF_RANGE')
87
-
88
- const domain = permit2Domain(permit2Address, chainId)
89
-
90
- validateTokenPermissions(permit.permitted)
91
-
92
- const types = witness
93
- ? ({
94
- TokenPermissions: TOKEN_PERMISSIONS,
95
- ...witness.witnessType,
96
- PermitWitnessTransferFrom: [
97
- { name: 'permitted', type: 'TokenPermissions' },
98
- { name: 'spender', type: 'address' },
99
- { name: 'nonce', type: 'uint256' },
100
- { name: 'deadline', type: 'uint256' },
101
- { name: 'witness', type: witness.witnessTypeName },
102
- ],
103
- } as const)
104
- : PERMIT_TRANSFER_FROM_TYPES
105
-
106
- const values = witness
107
- ? Object.assign(permit, { witness: witness.witness })
108
- : permit
109
-
110
- return {
111
- domain,
112
- types,
113
- values,
114
- }
115
- }
116
-
117
- export function getPermitBatchTransferData(
118
- permit: PermitBatchTransferFrom,
119
- permit2Address: Address,
120
- chainId: number,
121
- witness?: Witness
122
- ): PermitBatchTransferFromData {
123
- invariant(MaxSigDeadline >= permit.deadline, 'SIG_DEADLINE_OUT_OF_RANGE')
124
- invariant(MaxUnorderedNonce >= permit.nonce, 'NONCE_OUT_OF_RANGE')
125
-
126
- const domain = permit2Domain(permit2Address, chainId)
127
-
128
- permit.permitted.forEach(validateTokenPermissions)
129
-
130
- const types = witness
131
- ? {
132
- ...witness.witnessType,
133
- TokenPermissions: TOKEN_PERMISSIONS,
134
- PermitBatchWitnessTransferFrom: [
135
- { name: 'permitted', type: 'TokenPermissions[]' },
136
- { name: 'spender', type: 'address' },
137
- { name: 'nonce', type: 'uint256' },
138
- { name: 'deadline', type: 'uint256' },
139
- { name: 'witness', type: witness.witnessTypeName },
140
- ],
141
- }
142
- : PERMIT_BATCH_TRANSFER_FROM_TYPES
143
-
144
- const values = witness
145
- ? Object.assign(permit, { witness: witness.witness })
146
- : permit
147
-
148
- return {
149
- domain,
150
- types,
151
- values,
152
- }
153
- }
154
-
155
- // return the data to be sent in a eth_signTypedData RPC call
156
- // for signing the given permit data
157
- export function getPermitData(
158
- permit: PermitTransferFrom | PermitBatchTransferFrom,
159
- permit2Address: Address,
160
- chainId: number,
161
- witness?: Witness
162
- ): PermitTransferFromData | PermitBatchTransferFromData {
163
- if (isPermitTransferFrom(permit)) {
164
- return getPermitTransferData(permit, permit2Address, chainId, witness)
165
- }
166
- return getPermitBatchTransferData(permit, permit2Address, chainId, witness)
167
- }
168
-
169
- export function hash(
170
- permit: PermitTransferFrom | PermitBatchTransferFrom,
171
- permit2Address: Address,
172
- chainId: number,
173
- witness?: Witness
174
- ) {
175
- if (isPermitTransferFrom(permit)) {
176
- const { domain, types, values } = getPermitTransferData(
177
- permit,
178
- permit2Address,
179
- chainId,
180
- witness
181
- )
182
-
183
- return hashTypedData({
184
- domain,
185
- types,
186
- primaryType: witness ? 'PermitWitnessTransferFrom' : 'PermitTransferFrom',
187
- message: {
188
- ...values,
189
- },
190
- })
191
- }
192
- const { domain, types, values } = getPermitBatchTransferData(
193
- permit,
194
- permit2Address,
195
- chainId,
196
- witness
197
- )
198
-
199
- return hashTypedData({
200
- domain,
201
- types,
202
- primaryType: witness
203
- ? 'PermitBatchWitnessTransferFrom'
204
- : 'PermitBatchTransferFrom',
205
- message: { ...values },
206
- })
207
- }
208
-
209
- function validateTokenPermissions(permissions: TokenPermissions) {
210
- invariant(
211
- MaxSignatureTransferAmount >= permissions.amount,
212
- 'AMOUNT_OUT_OF_RANGE'
213
- )
214
- }
@@ -1,248 +0,0 @@
1
- import type { ExtendedChain } from '@lifi/types'
2
- import type { Client, Hex } from 'viem'
3
- import { encodeFunctionData } from 'viem'
4
- import { parseSignature } from 'viem'
5
- import type { Address } from 'viem'
6
- import { keccak256 } from 'viem'
7
- import { readContract, signTypedData } from 'viem/actions'
8
- import type { TransactionParameters } from '../types.js'
9
- import { eip2612Types, permit2ProxyAbi } from './abi.js'
10
- import { type NativePermitData, getNativePermit } from './getNativePermit.js'
11
- import {
12
- type PermitTransferFrom,
13
- getPermitData,
14
- } from './permit2/signatureTransfer.js'
15
-
16
- export const signNativePermitMessage = async (
17
- client: Client,
18
- transactionRequest: TransactionParameters,
19
- chain: ExtendedChain,
20
- tokenAddress: Address,
21
- amount: bigint,
22
- nativePermit: NativePermitData
23
- ) => {
24
- const deadline = BigInt(Math.floor(Date.now() / 1000) + 30 * 60) // 30 minutes
25
-
26
- const domain = {
27
- name: nativePermit.name,
28
- version: nativePermit.version,
29
- chainId: chain.id,
30
- verifyingContract: tokenAddress,
31
- }
32
-
33
- const message = {
34
- owner: client.account!.address,
35
- spender: chain.permit2Proxy as Address,
36
- value: amount,
37
- nonce: nativePermit.nonce,
38
- deadline,
39
- }
40
-
41
- const signature = await signTypedData(client, {
42
- account: client.account!,
43
- domain,
44
- types: eip2612Types,
45
- primaryType: 'Permit',
46
- message,
47
- })
48
-
49
- const { v, r, s } = parseSignature(signature)
50
-
51
- const data = encodeFunctionData({
52
- abi: permit2ProxyAbi,
53
- functionName: 'callDiamondWithEIP2612Signature',
54
- args: [
55
- tokenAddress,
56
- amount,
57
- deadline,
58
- Number(v),
59
- r,
60
- s,
61
- transactionRequest.data as Hex,
62
- ],
63
- })
64
-
65
- return {
66
- signature,
67
- data,
68
- }
69
- }
70
-
71
- export const signPermit2Message = async (
72
- client: Client,
73
- transactionRequest: TransactionParameters,
74
- chain: ExtendedChain,
75
- tokenAddress: Address,
76
- amount: bigint
77
- ) => {
78
- const nonce = await readContract(client, {
79
- address: chain.permit2Proxy as Address,
80
- abi: permit2ProxyAbi,
81
- functionName: 'nextNonce',
82
- args: [client.account!.address],
83
- })
84
-
85
- const permitTransferFrom: PermitTransferFrom = {
86
- permitted: {
87
- token: tokenAddress,
88
- amount: amount,
89
- },
90
- spender: chain.permit2Proxy as Address,
91
- nonce: nonce,
92
- deadline: BigInt(Math.floor(Date.now() / 1000) + 30 * 60), // 30 minutes
93
- }
94
-
95
- const { domain, types, values } = getPermitData(
96
- permitTransferFrom,
97
- chain.permit2 as Address,
98
- chain.id
99
- )
100
-
101
- const signature = await signTypedData(client, {
102
- account: client.account!,
103
- primaryType: 'PermitTransferFrom',
104
- domain,
105
- types,
106
- message: { ...values },
107
- })
108
-
109
- const data = encodeFunctionData({
110
- abi: permit2ProxyAbi,
111
- functionName: 'callDiamondWithPermit2',
112
- args: [
113
- transactionRequest.data as Hex,
114
- [
115
- [tokenAddress, amount],
116
- permitTransferFrom.nonce,
117
- permitTransferFrom.deadline,
118
- ],
119
- signature as Hex,
120
- ],
121
- })
122
-
123
- return {
124
- signature,
125
- data,
126
- }
127
- }
128
-
129
- export const signPermit2WitnessMessage = async (
130
- client: Client,
131
- transactionRequest: TransactionParameters,
132
- chain: ExtendedChain,
133
- tokenAddress: Address,
134
- amount: bigint
135
- ) => {
136
- const nonce = await readContract(client, {
137
- address: chain.permit2Proxy as Address,
138
- abi: permit2ProxyAbi,
139
- functionName: 'nextNonce',
140
- args: [client.account!.address],
141
- })
142
-
143
- const permitTransferFrom: PermitTransferFrom = {
144
- permitted: {
145
- token: tokenAddress,
146
- amount: amount,
147
- },
148
- spender: chain.permit2Proxy as Address,
149
- nonce: nonce,
150
- deadline: BigInt(Math.floor(Date.now() / 1000) + 30 * 60), // 30 minutes
151
- }
152
-
153
- // Create witness data for the LI.FI call
154
- const witness = {
155
- witness: {
156
- tokenReceiver: chain.permit2Proxy as Address,
157
- diamondAddress: chain.diamondAddress as Address,
158
- diamondCalldataHash: keccak256(transactionRequest.data as Hex),
159
- },
160
- witnessTypeName: 'LiFiCall',
161
- witnessType: {
162
- LiFiCall: [
163
- { name: 'diamondAddress', type: 'address' },
164
- { name: 'diamondCalldataHash', type: 'bytes32' },
165
- ],
166
- },
167
- }
168
-
169
- const { domain, types, values } = getPermitData(
170
- permitTransferFrom,
171
- chain.permit2 as Address,
172
- chain.id,
173
- witness
174
- )
175
-
176
- const signature = await signTypedData(client, {
177
- account: client.account!,
178
- primaryType: 'PermitWitnessTransferFrom',
179
- domain,
180
- types,
181
- message: { ...values },
182
- })
183
-
184
- const data = encodeFunctionData({
185
- abi: permit2ProxyAbi,
186
- functionName: 'callDiamondWithPermit2Witness',
187
- args: [
188
- transactionRequest.data as Hex,
189
- client.account!.address,
190
- [
191
- [tokenAddress, amount],
192
- permitTransferFrom.nonce,
193
- permitTransferFrom.deadline,
194
- ],
195
- signature as Hex,
196
- ],
197
- })
198
-
199
- return {
200
- signature,
201
- data,
202
- }
203
- }
204
-
205
- export const signPermitMessage = async (
206
- client: Client,
207
- transactionRequest: TransactionParameters,
208
- chain: ExtendedChain,
209
- tokenAddress: Address,
210
- amount: bigint,
211
- nativePermit?: NativePermitData,
212
- useWitness = false
213
- ) => {
214
- let _nativePermit = nativePermit
215
-
216
- if (!_nativePermit) {
217
- _nativePermit = await getNativePermit(client, chain, tokenAddress)
218
- }
219
-
220
- if (_nativePermit.supported) {
221
- return signNativePermitMessage(
222
- client,
223
- transactionRequest,
224
- chain,
225
- tokenAddress,
226
- amount,
227
- _nativePermit
228
- )
229
- }
230
-
231
- if (useWitness) {
232
- return signPermit2WitnessMessage(
233
- client,
234
- transactionRequest,
235
- chain,
236
- tokenAddress,
237
- amount
238
- )
239
- }
240
-
241
- return signPermit2Message(
242
- client,
243
- transactionRequest,
244
- chain,
245
- tokenAddress,
246
- amount
247
- )
248
- }
@@ -1,49 +0,0 @@
1
- import type {
2
- Client,
3
- Hash,
4
- WalletCallReceipt as _WalletCallReceipt,
5
- } from 'viem'
6
- import { getCallsStatus } from 'viem/experimental'
7
- import { LiFiErrorCode } from '../../errors/constants.js'
8
- import { TransactionError } from '../../errors/errors.js'
9
- import { waitForResult } from '../../utils/waitForResult.js'
10
-
11
- export type WalletCallReceipt = _WalletCallReceipt<
12
- bigint,
13
- 'success' | 'reverted'
14
- >
15
-
16
- export const waitForBatchTransactionReceipt = async (
17
- client: Client,
18
- batchHash: Hash
19
- ): Promise<WalletCallReceipt> => {
20
- return waitForResult(async () => {
21
- const callsDetails = await getCallsStatus(client, {
22
- id: batchHash,
23
- })
24
-
25
- if (callsDetails.status === 'PENDING') {
26
- return undefined
27
- }
28
-
29
- if (callsDetails.status === 'CONFIRMED') {
30
- if (
31
- !callsDetails.receipts?.length ||
32
- !callsDetails.receipts.every((receipt) => receipt.transactionHash) ||
33
- callsDetails.receipts.some((receipt) => receipt.status === 'reverted')
34
- ) {
35
- throw new TransactionError(
36
- LiFiErrorCode.TransactionFailed,
37
- 'Transaction was reverted.'
38
- )
39
- }
40
- const transactionReceipt = callsDetails.receipts.at(-1)!
41
- return transactionReceipt
42
- }
43
-
44
- throw new TransactionError(
45
- LiFiErrorCode.TransactionFailed,
46
- 'Transaction not found.'
47
- )
48
- }, 3000)
49
- }