@lifi/sdk 3.5.0-beta.0 → 3.5.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.
- package/package.json +7 -7
- package/src/_cjs/constants.js +2 -4
- package/src/_cjs/constants.js.map +1 -1
- package/src/_cjs/core/EVM/EVM.js +4 -0
- package/src/_cjs/core/EVM/EVM.js.map +1 -1
- package/src/_cjs/core/EVM/EVMStepExecutor.js +77 -98
- package/src/_cjs/core/EVM/EVMStepExecutor.js.map +1 -1
- package/src/_cjs/core/EVM/abi.js +43 -36
- package/src/_cjs/core/EVM/abi.js.map +1 -1
- package/src/_cjs/core/EVM/checkAllowance.js +30 -36
- package/src/_cjs/core/EVM/checkAllowance.js.map +1 -1
- package/src/_cjs/core/EVM/getAllowance.js.map +1 -1
- package/src/_cjs/core/EVM/getEVMBalance.js.map +1 -1
- package/src/_cjs/core/EVM/multisig.js +29 -0
- package/src/_cjs/core/EVM/multisig.js.map +1 -0
- package/src/_cjs/core/EVM/parseEVMErrors.js +0 -3
- package/src/_cjs/core/EVM/parseEVMErrors.js.map +1 -1
- package/src/_cjs/core/EVM/setAllowance.js +6 -4
- package/src/_cjs/core/EVM/setAllowance.js.map +1 -1
- package/src/_cjs/core/EVM/switchChain.js +3 -2
- package/src/_cjs/core/EVM/switchChain.js.map +1 -1
- package/src/_cjs/core/EVM/types.js.map +1 -1
- package/src/_cjs/core/EVM/utils.js +1 -2
- package/src/_cjs/core/EVM/utils.js.map +1 -1
- package/src/_cjs/core/checkBalance.js +3 -3
- package/src/_cjs/core/checkBalance.js.map +1 -1
- package/src/_cjs/index.js.map +1 -1
- package/src/_cjs/version.js +1 -1
- package/src/_cjs/version.js.map +1 -1
- package/src/_esm/constants.js +1 -3
- package/src/_esm/constants.js.map +1 -1
- package/src/_esm/core/EVM/EVM.js +4 -0
- package/src/_esm/core/EVM/EVM.js.map +1 -1
- package/src/_esm/core/EVM/EVMStepExecutor.js +87 -126
- package/src/_esm/core/EVM/EVMStepExecutor.js.map +1 -1
- package/src/_esm/core/EVM/abi.js +42 -37
- package/src/_esm/core/EVM/abi.js.map +1 -1
- package/src/_esm/core/EVM/checkAllowance.js +32 -41
- package/src/_esm/core/EVM/checkAllowance.js.map +1 -1
- package/src/_esm/core/EVM/getAllowance.js.map +1 -1
- package/src/_esm/core/EVM/getEVMBalance.js.map +1 -1
- package/src/_esm/core/EVM/multisig.js +25 -0
- package/src/_esm/core/EVM/multisig.js.map +1 -0
- package/src/_esm/core/EVM/parseEVMErrors.js +0 -8
- package/src/_esm/core/EVM/parseEVMErrors.js.map +1 -1
- package/src/_esm/core/EVM/setAllowance.js +6 -4
- package/src/_esm/core/EVM/setAllowance.js.map +1 -1
- package/src/_esm/core/EVM/switchChain.js +3 -2
- package/src/_esm/core/EVM/switchChain.js.map +1 -1
- package/src/_esm/core/EVM/types.js.map +1 -1
- package/src/_esm/core/EVM/utils.js +1 -2
- package/src/_esm/core/EVM/utils.js.map +1 -1
- package/src/_esm/core/checkBalance.js +3 -3
- package/src/_esm/core/checkBalance.js.map +1 -1
- package/src/_esm/index.js.map +1 -1
- package/src/_esm/version.js +1 -1
- package/src/_esm/version.js.map +1 -1
- package/src/_types/constants.d.ts +1 -3
- package/src/_types/constants.d.ts.map +1 -1
- package/src/_types/core/EVM/EVM.d.ts.map +1 -1
- package/src/_types/core/EVM/EVMStepExecutor.d.ts +4 -7
- package/src/_types/core/EVM/EVMStepExecutor.d.ts.map +1 -1
- package/src/_types/core/EVM/abi.d.ts +5 -202
- package/src/_types/core/EVM/abi.d.ts.map +1 -1
- package/src/_types/core/EVM/checkAllowance.d.ts +2 -12
- package/src/_types/core/EVM/checkAllowance.d.ts.map +1 -1
- package/src/_types/core/EVM/getAllowance.d.ts +4 -5
- package/src/_types/core/EVM/getAllowance.d.ts.map +1 -1
- package/src/_types/core/EVM/getEVMBalance.d.ts +1 -2
- package/src/_types/core/EVM/getEVMBalance.d.ts.map +1 -1
- package/src/_types/core/EVM/multisig.d.ts +6 -0
- package/src/_types/core/EVM/multisig.d.ts.map +1 -0
- package/src/_types/core/EVM/setAllowance.d.ts +2 -2
- package/src/_types/core/EVM/setAllowance.d.ts.map +1 -1
- package/src/_types/core/EVM/switchChain.d.ts.map +1 -1
- package/src/_types/core/EVM/types.d.ts +18 -1
- package/src/_types/core/EVM/types.d.ts.map +1 -1
- package/src/_types/core/EVM/utils.d.ts +2 -2
- package/src/_types/core/EVM/utils.d.ts.map +1 -1
- package/src/_types/core/checkBalance.d.ts.map +1 -1
- package/src/_types/index.d.ts +1 -1
- package/src/_types/index.d.ts.map +1 -1
- package/src/_types/version.d.ts +1 -1
- package/src/_types/version.d.ts.map +1 -1
- package/src/constants.ts +1 -6
- package/src/core/EVM/EVM.ts +4 -0
- package/src/core/EVM/EVMStepExecutor.ts +145 -174
- package/src/core/EVM/abi.ts +43 -40
- package/src/core/EVM/checkAllowance.ts +92 -96
- package/src/core/EVM/getAllowance.ts +8 -8
- package/src/core/EVM/getEVMBalance.ts +2 -2
- package/src/core/EVM/multisig.ts +54 -0
- package/src/core/EVM/parseEVMErrors.ts +0 -8
- package/src/core/EVM/setAllowance.ts +21 -15
- package/src/core/EVM/switchChain.ts +12 -3
- package/src/core/EVM/types.ts +27 -1
- package/src/core/EVM/utils.ts +3 -4
- package/src/core/checkBalance.ts +3 -6
- package/src/index.ts +3 -0
- package/src/version.ts +1 -1
- package/src/_cjs/core/EVM/getNativePermit.js +0 -90
- package/src/_cjs/core/EVM/getNativePermit.js.map +0 -1
- package/src/_cjs/core/EVM/permit2/allowanceTransfer.js +0 -100
- package/src/_cjs/core/EVM/permit2/allowanceTransfer.js.map +0 -1
- package/src/_cjs/core/EVM/permit2/constants.js +0 -12
- package/src/_cjs/core/EVM/permit2/constants.js.map +0 -1
- package/src/_cjs/core/EVM/permit2/domain.js +0 -12
- package/src/_cjs/core/EVM/permit2/domain.js.map +0 -1
- package/src/_cjs/core/EVM/permit2/signatureTransfer.js +0 -121
- package/src/_cjs/core/EVM/permit2/signatureTransfer.js.map +0 -1
- package/src/_cjs/core/EVM/signPermitMessage.js +0 -168
- package/src/_cjs/core/EVM/signPermitMessage.js.map +0 -1
- package/src/_cjs/core/EVM/waitForBatchTransactionReceipt.js +0 -29
- package/src/_cjs/core/EVM/waitForBatchTransactionReceipt.js.map +0 -1
- package/src/_cjs/utils/invariant.js +0 -17
- package/src/_cjs/utils/invariant.js.map +0 -1
- package/src/_esm/core/EVM/getNativePermit.js +0 -95
- package/src/_esm/core/EVM/getNativePermit.js.map +0 -1
- package/src/_esm/core/EVM/permit2/allowanceTransfer.js +0 -93
- package/src/_esm/core/EVM/permit2/allowanceTransfer.js.map +0 -1
- package/src/_esm/core/EVM/permit2/constants.js +0 -9
- package/src/_esm/core/EVM/permit2/constants.js.map +0 -1
- package/src/_esm/core/EVM/permit2/domain.js +0 -9
- package/src/_esm/core/EVM/permit2/domain.js.map +0 -1
- package/src/_esm/core/EVM/permit2/signatureTransfer.js +0 -117
- package/src/_esm/core/EVM/permit2/signatureTransfer.js.map +0 -1
- package/src/_esm/core/EVM/signPermitMessage.js +0 -162
- package/src/_esm/core/EVM/signPermitMessage.js.map +0 -1
- package/src/_esm/core/EVM/waitForBatchTransactionReceipt.js +0 -25
- package/src/_esm/core/EVM/waitForBatchTransactionReceipt.js.map +0 -1
- package/src/_esm/utils/invariant.js +0 -43
- package/src/_esm/utils/invariant.js.map +0 -1
- package/src/_types/core/EVM/getNativePermit.d.ts +0 -18
- package/src/_types/core/EVM/getNativePermit.d.ts.map +0 -1
- package/src/_types/core/EVM/permit2/allowanceTransfer.d.ts +0 -41
- package/src/_types/core/EVM/permit2/allowanceTransfer.d.ts.map +0 -1
- package/src/_types/core/EVM/permit2/constants.d.ts +0 -8
- package/src/_types/core/EVM/permit2/constants.d.ts.map +0 -1
- package/src/_types/core/EVM/permit2/domain.d.ts +0 -8
- package/src/_types/core/EVM/permit2/domain.d.ts.map +0 -1
- package/src/_types/core/EVM/permit2/signatureTransfer.d.ts +0 -42
- package/src/_types/core/EVM/permit2/signatureTransfer.d.ts.map +0 -1
- package/src/_types/core/EVM/signPermitMessage.d.ts +0 -22
- package/src/_types/core/EVM/signPermitMessage.d.ts.map +0 -1
- package/src/_types/core/EVM/waitForBatchTransactionReceipt.d.ts +0 -4
- package/src/_types/core/EVM/waitForBatchTransactionReceipt.d.ts.map +0 -1
- package/src/_types/utils/invariant.d.ts +0 -22
- package/src/_types/utils/invariant.d.ts.map +0 -1
- package/src/core/EVM/getNativePermit.ts +0 -113
- package/src/core/EVM/permit2/allowanceTransfer.ts +0 -168
- package/src/core/EVM/permit2/constants.ts +0 -11
- package/src/core/EVM/permit2/domain.ts +0 -20
- package/src/core/EVM/permit2/signatureTransfer.ts +0 -214
- package/src/core/EVM/signPermitMessage.ts +0 -248
- package/src/core/EVM/waitForBatchTransactionReceipt.ts +0 -49
- package/src/utils/invariant.ts +0 -51
|
@@ -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
|
-
}
|
package/src/utils/invariant.ts
DELETED
|
@@ -1,51 +0,0 @@
|
|
|
1
|
-
// https://github.com/alexreardon/tiny-invariant
|
|
2
|
-
|
|
3
|
-
const isProduction: boolean = process.env.NODE_ENV === 'production'
|
|
4
|
-
const prefix: string = 'Invariant failed'
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* `invariant` is used to [assert](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-7.html#assertion-functions) that the `condition` is [truthy](https://github.com/getify/You-Dont-Know-JS/blob/bdbe570600d4e1107d0b131787903ca1c9ec8140/up%20%26%20going/ch2.md#truthy--falsy).
|
|
8
|
-
*
|
|
9
|
-
* 💥 `invariant` will `throw` an `Error` if the `condition` is [falsey](https://github.com/getify/You-Dont-Know-JS/blob/bdbe570600d4e1107d0b131787903ca1c9ec8140/up%20%26%20going/ch2.md#truthy--falsy)
|
|
10
|
-
*
|
|
11
|
-
* 🤏 `message`s are not displayed in production environments to help keep bundles small
|
|
12
|
-
*
|
|
13
|
-
* @example
|
|
14
|
-
*
|
|
15
|
-
* ```ts
|
|
16
|
-
* const value: Person | null = { name: 'Alex' };
|
|
17
|
-
* invariant(value, 'Expected value to be a person');
|
|
18
|
-
* // type of `value`` has been narrowed to `Person`
|
|
19
|
-
* ```
|
|
20
|
-
*/
|
|
21
|
-
export function invariant(
|
|
22
|
-
condition: any,
|
|
23
|
-
// Not providing an inline default argument for message as the result is smaller
|
|
24
|
-
/**
|
|
25
|
-
* Can provide a string, or a function that returns a string for cases where
|
|
26
|
-
* the message takes a fair amount of effort to compute
|
|
27
|
-
*/
|
|
28
|
-
message?: string | (() => string)
|
|
29
|
-
): asserts condition {
|
|
30
|
-
if (condition) {
|
|
31
|
-
return
|
|
32
|
-
}
|
|
33
|
-
// Condition not passed
|
|
34
|
-
|
|
35
|
-
// In production we strip the message but still throw
|
|
36
|
-
if (isProduction) {
|
|
37
|
-
throw new Error(prefix)
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
// When not in production we allow the message to pass through
|
|
41
|
-
// *This block will be removed in production builds*
|
|
42
|
-
|
|
43
|
-
const provided: string | undefined =
|
|
44
|
-
typeof message === 'function' ? message() : message
|
|
45
|
-
|
|
46
|
-
// Options:
|
|
47
|
-
// 1. message provided: `${prefix}: ${provided}`
|
|
48
|
-
// 2. message not provided: prefix
|
|
49
|
-
const value: string = provided ? `${prefix}: ${provided}` : prefix
|
|
50
|
-
throw new Error(value)
|
|
51
|
-
}
|