@lifi/sdk 3.1.4 → 3.2.0-alpha.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 +29 -29
- package/src/_cjs/core/EVM/EVM.js +2 -2
- package/src/_cjs/core/EVM/EVM.js.map +1 -1
- package/src/_cjs/core/EVM/EVMStepExecutor.js +36 -31
- package/src/_cjs/core/EVM/EVMStepExecutor.js.map +1 -1
- package/src/_cjs/core/EVM/checkAllowance.js +8 -8
- package/src/_cjs/core/EVM/checkAllowance.js.map +1 -1
- package/src/_cjs/core/EVM/getAllowance.js +2 -1
- package/src/_cjs/core/EVM/getAllowance.js.map +1 -1
- package/src/_cjs/core/EVM/getENSAddress.js +1 -1
- package/src/_cjs/core/EVM/getENSAddress.js.map +1 -1
- package/src/_cjs/core/EVM/getEVMBalance.js +6 -5
- package/src/_cjs/core/EVM/getEVMBalance.js.map +1 -1
- package/src/_cjs/core/EVM/publicClient.js +1 -1
- package/src/_cjs/core/EVM/publicClient.js.map +1 -1
- package/src/_cjs/core/EVM/setAllowance.js +5 -5
- package/src/_cjs/core/EVM/setAllowance.js.map +1 -1
- package/src/_cjs/core/EVM/switchChain.js +10 -6
- package/src/_cjs/core/EVM/switchChain.js.map +1 -1
- package/src/_cjs/core/EVM/utils.js +2 -1
- package/src/_cjs/core/EVM/utils.js.map +1 -1
- package/src/_cjs/core/EVM/waitForTransactionReceipt.js +4 -4
- package/src/_cjs/core/EVM/waitForTransactionReceipt.js.map +1 -1
- package/src/_cjs/version.js +1 -1
- package/src/_cjs/version.js.map +1 -1
- package/src/_esm/core/EVM/EVM.js +2 -2
- package/src/_esm/core/EVM/EVM.js.map +1 -1
- package/src/_esm/core/EVM/EVMStepExecutor.js +36 -31
- package/src/_esm/core/EVM/EVMStepExecutor.js.map +1 -1
- package/src/_esm/core/EVM/checkAllowance.js +8 -8
- package/src/_esm/core/EVM/checkAllowance.js.map +1 -1
- package/src/_esm/core/EVM/getAllowance.js +2 -1
- package/src/_esm/core/EVM/getAllowance.js.map +1 -1
- package/src/_esm/core/EVM/getENSAddress.js +2 -2
- package/src/_esm/core/EVM/getENSAddress.js.map +1 -1
- package/src/_esm/core/EVM/getEVMBalance.js +6 -5
- package/src/_esm/core/EVM/getEVMBalance.js.map +1 -1
- package/src/_esm/core/EVM/publicClient.js +2 -2
- package/src/_esm/core/EVM/publicClient.js.map +1 -1
- package/src/_esm/core/EVM/setAllowance.js +6 -6
- package/src/_esm/core/EVM/setAllowance.js.map +1 -1
- package/src/_esm/core/EVM/switchChain.js +14 -10
- package/src/_esm/core/EVM/switchChain.js.map +1 -1
- package/src/_esm/core/EVM/utils.js +2 -1
- package/src/_esm/core/EVM/utils.js.map +1 -1
- package/src/_esm/core/EVM/waitForTransactionReceipt.js +4 -4
- package/src/_esm/core/EVM/waitForTransactionReceipt.js.map +1 -1
- package/src/_esm/version.js +1 -1
- package/src/_esm/version.js.map +1 -1
- package/src/_types/core/EVM/EVMStepExecutor.d.ts +4 -4
- package/src/_types/core/EVM/EVMStepExecutor.d.ts.map +1 -1
- package/src/_types/core/EVM/checkAllowance.d.ts +2 -2
- package/src/_types/core/EVM/checkAllowance.d.ts.map +1 -1
- package/src/_types/core/EVM/getAllowance.d.ts.map +1 -1
- package/src/_types/core/EVM/getEVMBalance.d.ts.map +1 -1
- package/src/_types/core/EVM/publicClient.d.ts +2 -2
- package/src/_types/core/EVM/publicClient.d.ts.map +1 -1
- 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 +6 -6
- package/src/_types/core/EVM/switchChain.d.ts.map +1 -1
- package/src/_types/core/EVM/types.d.ts +5 -5
- 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/EVM/waitForTransactionReceipt.d.ts +3 -3
- package/src/_types/core/EVM/waitForTransactionReceipt.d.ts.map +1 -1
- package/src/_types/core/types.d.ts +2 -2
- package/src/_types/core/types.d.ts.map +1 -1
- package/src/_types/version.d.ts +1 -1
- package/src/_types/version.d.ts.map +1 -1
- package/src/core/EVM/EVM.ts +2 -2
- package/src/core/EVM/EVMStepExecutor.ts +40 -48
- package/src/core/EVM/checkAllowance.ts +9 -9
- package/src/core/EVM/getAllowance.ts +2 -1
- package/src/core/EVM/getENSAddress.ts +2 -2
- package/src/core/EVM/getEVMBalance.ts +11 -5
- package/src/core/EVM/publicClient.ts +5 -7
- package/src/core/EVM/setAllowance.ts +8 -13
- package/src/core/EVM/switchChain.ts +16 -12
- package/src/core/EVM/types.ts +5 -5
- package/src/core/EVM/utils.ts +4 -3
- package/src/core/EVM/waitForTransactionReceipt.ts +7 -10
- package/src/core/types.ts +2 -4
- package/src/version.ts +1 -1
|
@@ -3,22 +3,16 @@ import type {
|
|
|
3
3
|
FullStatusData,
|
|
4
4
|
Process,
|
|
5
5
|
} from '@lifi/types'
|
|
6
|
-
import type {
|
|
7
|
-
|
|
8
|
-
PublicClient,
|
|
9
|
-
SendTransactionParameters,
|
|
10
|
-
WalletClient,
|
|
11
|
-
} from 'viem'
|
|
12
|
-
import { publicActions } from 'viem'
|
|
6
|
+
import type { Client, Hash, SendTransactionParameters } from 'viem'
|
|
7
|
+
import { getAddresses, sendTransaction } from 'viem/actions'
|
|
13
8
|
import { config } from '../../config.js'
|
|
9
|
+
import { LiFiErrorCode } from '../../errors/constants.js'
|
|
10
|
+
import { TransactionError, ValidationError } from '../../errors/errors.js'
|
|
14
11
|
import { getStepTransaction } from '../../services/api.js'
|
|
15
12
|
import {
|
|
16
13
|
getTransactionFailedMessage,
|
|
17
14
|
isZeroAddress,
|
|
18
15
|
} from '../../utils/index.js'
|
|
19
|
-
import { ValidationError, TransactionError } from '../../errors/errors.js'
|
|
20
|
-
import { LiFiErrorCode } from '../../errors/constants.js'
|
|
21
|
-
import { parseEVMErrors } from './parseEVMErrors.js'
|
|
22
16
|
import { BaseStepExecutor } from '../BaseStepExecutor.js'
|
|
23
17
|
import { checkBalance } from '../checkBalance.js'
|
|
24
18
|
import { getSubstatusMessage } from '../processMessages.js'
|
|
@@ -31,44 +25,50 @@ import type {
|
|
|
31
25
|
import { waitForReceivingTransaction } from '../waitForReceivingTransaction.js'
|
|
32
26
|
import { checkAllowance } from './checkAllowance.js'
|
|
33
27
|
import { updateMultisigRouteProcess } from './multisig.js'
|
|
28
|
+
import { parseEVMErrors } from './parseEVMErrors.js'
|
|
34
29
|
import { switchChain } from './switchChain.js'
|
|
35
30
|
import type { MultisigConfig, MultisigTransaction } from './types.js'
|
|
36
31
|
import { getMaxPriorityFeePerGas } from './utils.js'
|
|
37
32
|
import { waitForTransactionReceipt } from './waitForTransactionReceipt.js'
|
|
38
33
|
|
|
39
34
|
export interface EVMStepExecutorOptions extends StepExecutorOptions {
|
|
40
|
-
|
|
35
|
+
client: Client
|
|
41
36
|
multisig?: MultisigConfig
|
|
42
37
|
}
|
|
43
38
|
|
|
44
39
|
export class EVMStepExecutor extends BaseStepExecutor {
|
|
45
|
-
private
|
|
40
|
+
private client: Client
|
|
46
41
|
private multisig?: MultisigConfig
|
|
47
42
|
|
|
48
43
|
constructor(options: EVMStepExecutorOptions) {
|
|
49
44
|
super(options)
|
|
50
|
-
this.
|
|
45
|
+
this.client = options.client
|
|
51
46
|
this.multisig = options.multisig
|
|
52
47
|
}
|
|
53
48
|
|
|
54
49
|
// Ensure that we are using the right chain and wallet when executing transactions.
|
|
55
|
-
|
|
50
|
+
checkClient = async (
|
|
56
51
|
step: LiFiStepExtended,
|
|
57
52
|
process?: Process
|
|
58
|
-
): Promise<
|
|
59
|
-
const
|
|
60
|
-
this.
|
|
53
|
+
): Promise<Client | undefined> => {
|
|
54
|
+
const updatedClient = await switchChain(
|
|
55
|
+
this.client,
|
|
61
56
|
this.statusManager,
|
|
62
57
|
step,
|
|
63
58
|
this.allowUserInteraction,
|
|
64
59
|
this.executionOptions?.switchChainHook
|
|
65
60
|
)
|
|
66
|
-
if (
|
|
67
|
-
this.
|
|
61
|
+
if (updatedClient) {
|
|
62
|
+
this.client = updatedClient
|
|
68
63
|
}
|
|
69
64
|
|
|
70
65
|
// Prevent execution of the quote by wallet different from the one which requested the quote
|
|
71
|
-
|
|
66
|
+
let accountAddress = this.client.account?.address
|
|
67
|
+
if (!accountAddress) {
|
|
68
|
+
const accountAddresses = await getAddresses(this.client)
|
|
69
|
+
accountAddress = accountAddresses?.[0]
|
|
70
|
+
}
|
|
71
|
+
if (accountAddress !== step.action.fromAddress) {
|
|
72
72
|
let processToUpdate = process
|
|
73
73
|
if (!processToUpdate) {
|
|
74
74
|
// We need to create some process if we don't have one so we can show the error
|
|
@@ -95,7 +95,7 @@ export class EVMStepExecutor extends BaseStepExecutor {
|
|
|
95
95
|
process
|
|
96
96
|
)
|
|
97
97
|
}
|
|
98
|
-
return
|
|
98
|
+
return updatedClient
|
|
99
99
|
}
|
|
100
100
|
|
|
101
101
|
executeStep = async (step: LiFiStepExtended): Promise<LiFiStepExtended> => {
|
|
@@ -111,13 +111,13 @@ export class EVMStepExecutor extends BaseStepExecutor {
|
|
|
111
111
|
// All changes are already done from the source chain
|
|
112
112
|
// Return the step
|
|
113
113
|
if (recievingChainProcess?.substatus !== 'WAIT_DESTINATION_TRANSACTION') {
|
|
114
|
-
const
|
|
115
|
-
if (!
|
|
114
|
+
const updatedClient = await this.checkClient(step)
|
|
115
|
+
if (!updatedClient) {
|
|
116
116
|
return step
|
|
117
117
|
}
|
|
118
118
|
}
|
|
119
119
|
|
|
120
|
-
const
|
|
120
|
+
const isMultisigClient = !!this.multisig?.isMultisigClient
|
|
121
121
|
const multisigBatchTransactions: MultisigTransaction[] = []
|
|
122
122
|
|
|
123
123
|
const shouldBatchTransactions =
|
|
@@ -139,13 +139,13 @@ export class EVMStepExecutor extends BaseStepExecutor {
|
|
|
139
139
|
const checkForAllowance =
|
|
140
140
|
!existingProcess?.txHash &&
|
|
141
141
|
!isZeroAddress(step.action.fromToken.address) &&
|
|
142
|
-
(shouldBatchTransactions || !
|
|
142
|
+
(shouldBatchTransactions || !isMultisigClient)
|
|
143
143
|
|
|
144
144
|
if (checkForAllowance) {
|
|
145
145
|
const data = await checkAllowance(
|
|
146
|
+
this.client,
|
|
146
147
|
fromChain,
|
|
147
148
|
step,
|
|
148
|
-
this.walletClient,
|
|
149
149
|
this.statusManager,
|
|
150
150
|
this.executionOptions,
|
|
151
151
|
this.allowUserInteraction,
|
|
@@ -175,7 +175,7 @@ export class EVMStepExecutor extends BaseStepExecutor {
|
|
|
175
175
|
)
|
|
176
176
|
|
|
177
177
|
try {
|
|
178
|
-
if (
|
|
178
|
+
if (isMultisigClient && multisigProcess) {
|
|
179
179
|
const multisigTxHash = multisigProcess.multisigTxHash as Hash
|
|
180
180
|
if (!multisigTxHash) {
|
|
181
181
|
throw new ValidationError(
|
|
@@ -195,11 +195,8 @@ export class EVMStepExecutor extends BaseStepExecutor {
|
|
|
195
195
|
let txHash: Hash
|
|
196
196
|
if (process.txHash) {
|
|
197
197
|
// Make sure that the chain is still correct
|
|
198
|
-
const
|
|
199
|
-
|
|
200
|
-
process
|
|
201
|
-
)
|
|
202
|
-
if (!updatedWalletClient) {
|
|
198
|
+
const updatedClient = await this.checkClient(step, process)
|
|
199
|
+
if (!updatedClient) {
|
|
203
200
|
return step
|
|
204
201
|
}
|
|
205
202
|
|
|
@@ -213,7 +210,7 @@ export class EVMStepExecutor extends BaseStepExecutor {
|
|
|
213
210
|
)
|
|
214
211
|
|
|
215
212
|
// Check balance
|
|
216
|
-
await checkBalance(this.
|
|
213
|
+
await checkBalance(this.client.account!.address, step)
|
|
217
214
|
|
|
218
215
|
// Create new transaction
|
|
219
216
|
if (!step.transactionRequest) {
|
|
@@ -241,11 +238,8 @@ export class EVMStepExecutor extends BaseStepExecutor {
|
|
|
241
238
|
|
|
242
239
|
// STEP 3: Send the transaction
|
|
243
240
|
// Make sure that the chain is still correct
|
|
244
|
-
const
|
|
245
|
-
|
|
246
|
-
process
|
|
247
|
-
)
|
|
248
|
-
if (!updatedWalletClient) {
|
|
241
|
+
const updatedClient = await this.checkClient(step, process)
|
|
242
|
+
if (!updatedClient) {
|
|
249
243
|
return step
|
|
250
244
|
}
|
|
251
245
|
|
|
@@ -276,10 +270,8 @@ export class EVMStepExecutor extends BaseStepExecutor {
|
|
|
276
270
|
// ? BigInt(step.transactionRequest.maxFeePerGas as string)
|
|
277
271
|
// : undefined,
|
|
278
272
|
maxPriorityFeePerGas:
|
|
279
|
-
this.
|
|
280
|
-
? await getMaxPriorityFeePerGas(
|
|
281
|
-
this.walletClient.extend(publicActions) as PublicClient
|
|
282
|
-
)
|
|
273
|
+
this.client.account?.type === 'local'
|
|
274
|
+
? await getMaxPriorityFeePerGas(this.client)
|
|
283
275
|
: step.transactionRequest.maxPriorityFeePerGas
|
|
284
276
|
? BigInt(step.transactionRequest.maxPriorityFeePerGas)
|
|
285
277
|
: undefined,
|
|
@@ -317,9 +309,9 @@ export class EVMStepExecutor extends BaseStepExecutor {
|
|
|
317
309
|
)
|
|
318
310
|
}
|
|
319
311
|
} else {
|
|
320
|
-
txHash = await this.
|
|
312
|
+
txHash = await sendTransaction(this.client, {
|
|
321
313
|
to: transactionRequest.to,
|
|
322
|
-
account: this.
|
|
314
|
+
account: this.client.account!,
|
|
323
315
|
data: transactionRequest.data,
|
|
324
316
|
value: transactionRequest.value,
|
|
325
317
|
gas: transactionRequest.gas,
|
|
@@ -331,7 +323,7 @@ export class EVMStepExecutor extends BaseStepExecutor {
|
|
|
331
323
|
}
|
|
332
324
|
|
|
333
325
|
// STEP 4: Wait for the transaction
|
|
334
|
-
if (
|
|
326
|
+
if (isMultisigClient) {
|
|
335
327
|
process = this.statusManager.updateProcess(
|
|
336
328
|
step,
|
|
337
329
|
process.type,
|
|
@@ -354,7 +346,7 @@ export class EVMStepExecutor extends BaseStepExecutor {
|
|
|
354
346
|
}
|
|
355
347
|
|
|
356
348
|
const transactionReceipt = await waitForTransactionReceipt({
|
|
357
|
-
|
|
349
|
+
client: this.client,
|
|
358
350
|
chainId: fromChain.id,
|
|
359
351
|
txHash,
|
|
360
352
|
onReplaced: (response) => {
|
|
@@ -367,7 +359,7 @@ export class EVMStepExecutor extends BaseStepExecutor {
|
|
|
367
359
|
|
|
368
360
|
// if it's multisig wallet client and the process is in ACTION_REQUIRED
|
|
369
361
|
// then signatures are still needed
|
|
370
|
-
if (
|
|
362
|
+
if (isMultisigClient && process.status === 'ACTION_REQUIRED') {
|
|
371
363
|
await updateMultisigRouteProcess(
|
|
372
364
|
transactionReceipt?.transactionHash || txHash,
|
|
373
365
|
step,
|
|
@@ -381,7 +373,7 @@ export class EVMStepExecutor extends BaseStepExecutor {
|
|
|
381
373
|
// Update pending process if the transaction hash from the receipt is different.
|
|
382
374
|
// This might happen if the transaction was replaced.
|
|
383
375
|
if (
|
|
384
|
-
!
|
|
376
|
+
!isMultisigClient &&
|
|
385
377
|
transactionReceipt?.transactionHash &&
|
|
386
378
|
transactionReceipt.transactionHash !== txHash
|
|
387
379
|
) {
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { Chain, LiFiStep, Process, ProcessType } from '@lifi/types'
|
|
2
|
-
import type { Address,
|
|
2
|
+
import type { Address, Client, Hash } from 'viem'
|
|
3
3
|
import type { StatusManager } from '../StatusManager.js'
|
|
4
4
|
import type { ExecutionOptions } from '../types.js'
|
|
5
5
|
import { getAllowance } from './getAllowance.js'
|
|
@@ -8,9 +8,9 @@ import { setAllowance } from './setAllowance.js'
|
|
|
8
8
|
import { waitForTransactionReceipt } from './waitForTransactionReceipt.js'
|
|
9
9
|
|
|
10
10
|
export const checkAllowance = async (
|
|
11
|
+
client: Client,
|
|
11
12
|
chain: Chain,
|
|
12
13
|
step: LiFiStep,
|
|
13
|
-
walletClient: WalletClient,
|
|
14
14
|
statusManager: StatusManager,
|
|
15
15
|
settings?: ExecutionOptions,
|
|
16
16
|
allowUserInteraction = false,
|
|
@@ -26,7 +26,7 @@ export const checkAllowance = async (
|
|
|
26
26
|
try {
|
|
27
27
|
if (allowanceProcess.txHash && allowanceProcess.status !== 'DONE') {
|
|
28
28
|
await waitForApprovalTransaction(
|
|
29
|
-
|
|
29
|
+
client,
|
|
30
30
|
allowanceProcess.txHash! as Address,
|
|
31
31
|
allowanceProcess.type,
|
|
32
32
|
step,
|
|
@@ -43,7 +43,7 @@ export const checkAllowance = async (
|
|
|
43
43
|
const approved = await getAllowance(
|
|
44
44
|
chain.id,
|
|
45
45
|
step.action.fromToken.address,
|
|
46
|
-
|
|
46
|
+
client.account!.address,
|
|
47
47
|
step.estimate.approvalAddress
|
|
48
48
|
)
|
|
49
49
|
|
|
@@ -56,7 +56,7 @@ export const checkAllowance = async (
|
|
|
56
56
|
|
|
57
57
|
if (shouldBatchTransactions) {
|
|
58
58
|
const approveTxHash = await setAllowance(
|
|
59
|
-
|
|
59
|
+
client,
|
|
60
60
|
step.action.fromToken.address,
|
|
61
61
|
step.estimate.approvalAddress,
|
|
62
62
|
fromAmount,
|
|
@@ -74,13 +74,13 @@ export const checkAllowance = async (
|
|
|
74
74
|
}
|
|
75
75
|
|
|
76
76
|
const approveTxHash = await setAllowance(
|
|
77
|
-
|
|
77
|
+
client,
|
|
78
78
|
step.action.fromToken.address,
|
|
79
79
|
step.estimate.approvalAddress,
|
|
80
80
|
fromAmount
|
|
81
81
|
)
|
|
82
82
|
await waitForApprovalTransaction(
|
|
83
|
-
|
|
83
|
+
client,
|
|
84
84
|
approveTxHash,
|
|
85
85
|
allowanceProcess.type,
|
|
86
86
|
step,
|
|
@@ -114,7 +114,7 @@ export const checkAllowance = async (
|
|
|
114
114
|
}
|
|
115
115
|
|
|
116
116
|
const waitForApprovalTransaction = async (
|
|
117
|
-
|
|
117
|
+
client: Client,
|
|
118
118
|
txHash: Hash,
|
|
119
119
|
processType: ProcessType,
|
|
120
120
|
step: LiFiStep,
|
|
@@ -127,7 +127,7 @@ const waitForApprovalTransaction = async (
|
|
|
127
127
|
})
|
|
128
128
|
|
|
129
129
|
const transactionReceipt = await waitForTransactionReceipt({
|
|
130
|
-
|
|
130
|
+
client: client,
|
|
131
131
|
chainId: chain.id,
|
|
132
132
|
txHash: txHash,
|
|
133
133
|
onReplaced(response) {
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type { BaseToken, ChainId } from '@lifi/types'
|
|
2
2
|
import type { Address } from 'viem'
|
|
3
3
|
import { getContract } from 'viem'
|
|
4
|
+
import { multicall } from 'viem/actions'
|
|
4
5
|
import { isNativeTokenAddress } from '../../utils/utils.js'
|
|
5
6
|
import { allowanceAbi } from './abi.js'
|
|
6
7
|
import { getPublicClient } from './publicClient.js'
|
|
@@ -57,7 +58,7 @@ export const getAllowanceMulticall = async (
|
|
|
57
58
|
args: [ownerAddress, token.spenderAddress],
|
|
58
59
|
}))
|
|
59
60
|
|
|
60
|
-
const results = await
|
|
61
|
+
const results = await multicall(client, {
|
|
61
62
|
contracts,
|
|
62
63
|
multicallAddress: multicallAddress as Address,
|
|
63
64
|
})
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { ChainId } from '@lifi/types'
|
|
2
|
-
import { normalize } from 'viem/ens'
|
|
2
|
+
import { getEnsAddress, normalize } from 'viem/ens'
|
|
3
3
|
import { getPublicClient } from './publicClient.js'
|
|
4
4
|
|
|
5
5
|
export const getENSAddress = async (
|
|
@@ -7,7 +7,7 @@ export const getENSAddress = async (
|
|
|
7
7
|
): Promise<string | undefined> => {
|
|
8
8
|
try {
|
|
9
9
|
const client = await getPublicClient(ChainId.ETH)
|
|
10
|
-
const address = await
|
|
10
|
+
const address = await getEnsAddress(client, {
|
|
11
11
|
name: normalize(name),
|
|
12
12
|
})
|
|
13
13
|
return address as string | undefined
|
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
import type { ChainId, Token, TokenAmount } from '@lifi/types'
|
|
2
2
|
import type { Address } from 'viem'
|
|
3
|
+
import {
|
|
4
|
+
getBalance,
|
|
5
|
+
getBlockNumber,
|
|
6
|
+
multicall,
|
|
7
|
+
readContract,
|
|
8
|
+
} from 'viem/actions'
|
|
3
9
|
import { isZeroAddress } from '../../utils/utils.js'
|
|
4
10
|
import { balanceOfAbi, getEthBalanceAbi } from './abi.js'
|
|
5
11
|
import { getPublicClient } from './publicClient.js'
|
|
@@ -57,8 +63,8 @@ const getEVMBalanceMulticall = async (
|
|
|
57
63
|
args: [walletAddress],
|
|
58
64
|
}
|
|
59
65
|
})
|
|
60
|
-
const blockNumber = await
|
|
61
|
-
const results = await
|
|
66
|
+
const blockNumber = await getBlockNumber(client)
|
|
67
|
+
const results = await multicall(client, {
|
|
62
68
|
contracts,
|
|
63
69
|
multicallAddress: multicallAddress as Address,
|
|
64
70
|
blockNumber,
|
|
@@ -83,14 +89,14 @@ const getEVMBalanceDefault = async (
|
|
|
83
89
|
walletAddress: string
|
|
84
90
|
): Promise<TokenAmount[]> => {
|
|
85
91
|
const client = await getPublicClient(chainId)
|
|
86
|
-
const blockNumber = await
|
|
92
|
+
const blockNumber = await getBlockNumber(client)
|
|
87
93
|
const queue: Promise<bigint>[] = tokens.map((token) => {
|
|
88
94
|
if (isZeroAddress(token.address)) {
|
|
89
|
-
return
|
|
95
|
+
return getBalance(client, {
|
|
90
96
|
address: walletAddress as Address,
|
|
91
97
|
})
|
|
92
98
|
}
|
|
93
|
-
return
|
|
99
|
+
return readContract(client, {
|
|
94
100
|
address: token.address as Address,
|
|
95
101
|
abi: balanceOfAbi,
|
|
96
102
|
functionName: 'balanceOf',
|
|
@@ -1,21 +1,19 @@
|
|
|
1
1
|
import { ChainId } from '@lifi/types'
|
|
2
|
-
import type {
|
|
3
|
-
import {
|
|
2
|
+
import type { Client } from 'viem'
|
|
3
|
+
import { createClient, fallback, http, webSocket } from 'viem'
|
|
4
4
|
import { mainnet, type Chain } from 'viem/chains'
|
|
5
5
|
import { config } from '../../config.js'
|
|
6
6
|
import { getRpcUrls } from '../rpc.js'
|
|
7
7
|
|
|
8
8
|
// cached providers
|
|
9
|
-
const publicClients: Record<number,
|
|
9
|
+
const publicClients: Record<number, Client> = {}
|
|
10
10
|
|
|
11
11
|
/**
|
|
12
12
|
* Get an instance of a provider for a specific chain
|
|
13
13
|
* @param chainId - Id of the chain the provider is for
|
|
14
14
|
* @returns The public client for the given chain
|
|
15
15
|
*/
|
|
16
|
-
export const getPublicClient = async (
|
|
17
|
-
chainId: number
|
|
18
|
-
): Promise<PublicClient> => {
|
|
16
|
+
export const getPublicClient = async (chainId: number): Promise<Client> => {
|
|
19
17
|
if (!publicClients[chainId]) {
|
|
20
18
|
const urls = await getRpcUrls(chainId)
|
|
21
19
|
const fallbackTransports = urls.map((url) =>
|
|
@@ -44,7 +42,7 @@ export const getPublicClient = async (
|
|
|
44
42
|
...chain.contracts,
|
|
45
43
|
}
|
|
46
44
|
}
|
|
47
|
-
publicClients[chainId] =
|
|
45
|
+
publicClients[chainId] = createClient({
|
|
48
46
|
chain: chain,
|
|
49
47
|
transport: fallback(fallbackTransports),
|
|
50
48
|
batch: {
|
|
@@ -1,10 +1,6 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
SendTransactionParameters,
|
|
5
|
-
WalletClient,
|
|
6
|
-
} from 'viem'
|
|
7
|
-
import { encodeFunctionData, publicActions } from 'viem'
|
|
1
|
+
import type { Client, Hash, SendTransactionParameters } from 'viem'
|
|
2
|
+
import { encodeFunctionData } from 'viem'
|
|
3
|
+
import { sendTransaction } from 'viem/actions'
|
|
8
4
|
import { isNativeTokenAddress } from '../../utils/utils.js'
|
|
9
5
|
import type { ExecutionOptions, TransactionParameters } from '../types.js'
|
|
10
6
|
import { approveAbi } from './abi.js'
|
|
@@ -13,7 +9,7 @@ import type { ApproveTokenRequest, RevokeApprovalRequest } from './types.js'
|
|
|
13
9
|
import { getMaxPriorityFeePerGas } from './utils.js'
|
|
14
10
|
|
|
15
11
|
export const setAllowance = async (
|
|
16
|
-
|
|
12
|
+
client: Client,
|
|
17
13
|
tokenAddress: string,
|
|
18
14
|
contractAddress: string,
|
|
19
15
|
amount: bigint,
|
|
@@ -29,14 +25,13 @@ export const setAllowance = async (
|
|
|
29
25
|
if (returnPopulatedTransaction) {
|
|
30
26
|
return data
|
|
31
27
|
}
|
|
32
|
-
const client = walletClient.extend(publicActions)
|
|
33
28
|
|
|
34
29
|
let transactionRequest: TransactionParameters = {
|
|
35
30
|
to: tokenAddress,
|
|
36
31
|
data,
|
|
37
32
|
maxPriorityFeePerGas:
|
|
38
|
-
|
|
39
|
-
? await getMaxPriorityFeePerGas(client
|
|
33
|
+
client.account?.type === 'local'
|
|
34
|
+
? await getMaxPriorityFeePerGas(client)
|
|
40
35
|
: undefined,
|
|
41
36
|
}
|
|
42
37
|
|
|
@@ -53,9 +48,9 @@ export const setAllowance = async (
|
|
|
53
48
|
}
|
|
54
49
|
}
|
|
55
50
|
|
|
56
|
-
return
|
|
51
|
+
return sendTransaction(client, {
|
|
57
52
|
to: transactionRequest.to,
|
|
58
|
-
account:
|
|
53
|
+
account: client.account!,
|
|
59
54
|
data: transactionRequest.data,
|
|
60
55
|
gas: transactionRequest.gas,
|
|
61
56
|
gasPrice: transactionRequest.gasPrice,
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { Client } from 'viem'
|
|
2
|
+
import { getChainId } from 'viem/actions'
|
|
2
3
|
import { LiFiErrorCode } from '../../errors/constants.js'
|
|
3
4
|
import { ProviderError } from '../../errors/errors.js'
|
|
4
5
|
import type { StatusManager } from '../StatusManager.js'
|
|
@@ -10,28 +11,28 @@ import type { LiFiStepExtended, SwitchChainHook } from '../types.js'
|
|
|
10
11
|
* If no and if user interaction is allowed it triggers the switchChainHook. If no user interaction is allowed it aborts.
|
|
11
12
|
*
|
|
12
13
|
* Account Type: local -
|
|
13
|
-
* We need to create and return a new
|
|
14
|
+
* We need to create and return a new connector client from the switchChainHook in order to continue execution on a new chain.
|
|
14
15
|
*
|
|
15
16
|
* Account Type: json-rpc -
|
|
16
|
-
* We can switch chain and return existing
|
|
17
|
-
* @param
|
|
17
|
+
* We can switch chain and return existing connector client from the switchChainHook in order to continue execution on a new chain.
|
|
18
|
+
* @param client
|
|
18
19
|
* @param statusManager
|
|
19
20
|
* @param step
|
|
20
21
|
* @param switchChainHook
|
|
21
22
|
* @param allowUserInteraction
|
|
22
|
-
* @returns New
|
|
23
|
+
* @returns New connector client
|
|
23
24
|
*/
|
|
24
25
|
export const switchChain = async (
|
|
25
|
-
|
|
26
|
+
client: Client,
|
|
26
27
|
statusManager: StatusManager,
|
|
27
28
|
step: LiFiStepExtended,
|
|
28
29
|
allowUserInteraction: boolean,
|
|
29
30
|
switchChainHook?: SwitchChainHook
|
|
30
|
-
): Promise<
|
|
31
|
+
): Promise<Client | undefined> => {
|
|
31
32
|
// if we are already on the correct chain we can proceed directly
|
|
32
|
-
const currentChainId = await
|
|
33
|
+
const currentChainId = await getChainId(client)
|
|
33
34
|
if (currentChainId === step.action.fromChainId) {
|
|
34
|
-
return
|
|
35
|
+
return client
|
|
35
36
|
}
|
|
36
37
|
|
|
37
38
|
// -> set status message
|
|
@@ -49,8 +50,11 @@ export const switchChain = async (
|
|
|
49
50
|
}
|
|
50
51
|
|
|
51
52
|
try {
|
|
52
|
-
const
|
|
53
|
-
|
|
53
|
+
const updatedClient = await switchChainHook?.(step.action.fromChainId)
|
|
54
|
+
let updatedChainId
|
|
55
|
+
if (updatedClient) {
|
|
56
|
+
updatedChainId = await getChainId(updatedClient)
|
|
57
|
+
}
|
|
54
58
|
if (updatedChainId !== step.action.fromChainId) {
|
|
55
59
|
throw new ProviderError(
|
|
56
60
|
LiFiErrorCode.ChainSwitchError,
|
|
@@ -64,7 +68,7 @@ export const switchChain = async (
|
|
|
64
68
|
'DONE'
|
|
65
69
|
)
|
|
66
70
|
statusManager.updateExecution(step, 'PENDING')
|
|
67
|
-
return
|
|
71
|
+
return updatedClient
|
|
68
72
|
} catch (error: any) {
|
|
69
73
|
statusManager.updateProcess(step, switchProcess.type, 'FAILED', {
|
|
70
74
|
error: {
|
package/src/core/EVM/types.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import { ChainType, type BaseToken } from '@lifi/types'
|
|
2
|
-
import type {
|
|
2
|
+
import type { Client, Hash } from 'viem'
|
|
3
3
|
import type { SwitchChainHook } from '../types.js'
|
|
4
4
|
import { type SDKProvider } from '../types.js'
|
|
5
5
|
|
|
6
6
|
export interface EVMProviderOptions {
|
|
7
|
-
getWalletClient?: () => Promise<
|
|
7
|
+
getWalletClient?: () => Promise<Client>
|
|
8
8
|
switchChain?: SwitchChainHook
|
|
9
9
|
multisig?: MultisigConfig
|
|
10
10
|
}
|
|
@@ -35,7 +35,7 @@ export type TokenSpenderAllowance = {
|
|
|
35
35
|
}
|
|
36
36
|
|
|
37
37
|
export interface ApproveTokenRequest {
|
|
38
|
-
walletClient:
|
|
38
|
+
walletClient: Client
|
|
39
39
|
token: BaseToken
|
|
40
40
|
spenderAddress: string
|
|
41
41
|
amount: bigint
|
|
@@ -46,7 +46,7 @@ export interface ApproveTokenRequest {
|
|
|
46
46
|
}
|
|
47
47
|
|
|
48
48
|
export interface RevokeApprovalRequest {
|
|
49
|
-
walletClient:
|
|
49
|
+
walletClient: Client
|
|
50
50
|
token: BaseToken
|
|
51
51
|
spenderAddress: string
|
|
52
52
|
}
|
|
@@ -63,7 +63,7 @@ export interface MultisigTransaction {
|
|
|
63
63
|
}
|
|
64
64
|
|
|
65
65
|
export interface MultisigConfig {
|
|
66
|
-
|
|
66
|
+
isMultisigClient: boolean
|
|
67
67
|
getMultisigTransactionDetails: (
|
|
68
68
|
txHash: Hash,
|
|
69
69
|
fromChainId: number,
|
package/src/core/EVM/utils.ts
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
import { type ChainId } from '@lifi/types'
|
|
2
|
-
import type {
|
|
2
|
+
import type { Client, Transaction } from 'viem'
|
|
3
|
+
import { getBlock } from 'viem/actions'
|
|
3
4
|
import { config } from '../../config.js'
|
|
4
5
|
import { median } from '../../utils/median.js'
|
|
5
6
|
|
|
6
7
|
export const getMaxPriorityFeePerGas = async (
|
|
7
|
-
client:
|
|
8
|
+
client: Client
|
|
8
9
|
): Promise<bigint | undefined> => {
|
|
9
|
-
const block = await
|
|
10
|
+
const block = await getBlock(client, {
|
|
10
11
|
includeTransactions: true,
|
|
11
12
|
})
|
|
12
13
|
|
|
@@ -1,33 +1,32 @@
|
|
|
1
1
|
import type { ChainId } from '@lifi/types'
|
|
2
2
|
import type {
|
|
3
3
|
Chain,
|
|
4
|
+
Client,
|
|
4
5
|
Hash,
|
|
5
|
-
PublicClient,
|
|
6
6
|
ReplacementReason,
|
|
7
7
|
ReplacementReturnType,
|
|
8
8
|
TransactionReceipt,
|
|
9
|
-
WalletClient,
|
|
10
9
|
} from 'viem'
|
|
11
|
-
import {
|
|
10
|
+
import { waitForTransactionReceipt as waitForTransactionReceiptInternal } from 'viem/actions'
|
|
12
11
|
import { LiFiErrorCode, TransactionError } from '../../utils/index.js'
|
|
13
12
|
import { getPublicClient } from './publicClient.js'
|
|
14
13
|
import { retryCount, retryDelay } from './utils.js'
|
|
15
14
|
|
|
16
15
|
interface WaitForTransactionReceiptProps {
|
|
17
|
-
|
|
16
|
+
client: Client
|
|
18
17
|
chainId: ChainId
|
|
19
18
|
txHash: Hash
|
|
20
19
|
onReplaced?: (response: ReplacementReturnType<Chain | undefined>) => void
|
|
21
20
|
}
|
|
22
21
|
|
|
23
22
|
export async function waitForTransactionReceipt({
|
|
24
|
-
|
|
23
|
+
client,
|
|
25
24
|
chainId,
|
|
26
25
|
txHash,
|
|
27
26
|
onReplaced,
|
|
28
27
|
}: WaitForTransactionReceiptProps): Promise<TransactionReceipt | undefined> {
|
|
29
28
|
let { transactionReceipt, replacementReason } = await waitForReceipt(
|
|
30
|
-
|
|
29
|
+
client,
|
|
31
30
|
txHash,
|
|
32
31
|
onReplaced
|
|
33
32
|
)
|
|
@@ -56,7 +55,7 @@ export async function waitForTransactionReceipt({
|
|
|
56
55
|
}
|
|
57
56
|
|
|
58
57
|
async function waitForReceipt(
|
|
59
|
-
client:
|
|
58
|
+
client: Client,
|
|
60
59
|
txHash: Hash,
|
|
61
60
|
onReplaced?: (response: ReplacementReturnType<Chain | undefined>) => void
|
|
62
61
|
): Promise<{
|
|
@@ -67,9 +66,7 @@ async function waitForReceipt(
|
|
|
67
66
|
let transactionReceipt: TransactionReceipt | undefined
|
|
68
67
|
|
|
69
68
|
try {
|
|
70
|
-
transactionReceipt = await (
|
|
71
|
-
client as PublicClient
|
|
72
|
-
).waitForTransactionReceipt({
|
|
69
|
+
transactionReceipt = await waitForTransactionReceiptInternal(client, {
|
|
73
70
|
hash: txHash,
|
|
74
71
|
onReplaced: (response) => {
|
|
75
72
|
replacementReason = response.reason
|
package/src/core/types.ts
CHANGED
|
@@ -7,7 +7,7 @@ import type {
|
|
|
7
7
|
Token,
|
|
8
8
|
TokenAmount,
|
|
9
9
|
} from '@lifi/types'
|
|
10
|
-
import type {
|
|
10
|
+
import type { Client } from 'viem'
|
|
11
11
|
|
|
12
12
|
export interface SDKProvider {
|
|
13
13
|
readonly type: ChainType
|
|
@@ -82,9 +82,7 @@ export type TransactionRequestUpdateHook = (
|
|
|
82
82
|
updatedTxRequest: TransactionRequestParameters
|
|
83
83
|
) => Promise<TransactionParameters>
|
|
84
84
|
|
|
85
|
-
export type SwitchChainHook = (
|
|
86
|
-
chainId: number
|
|
87
|
-
) => Promise<WalletClient | undefined>
|
|
85
|
+
export type SwitchChainHook = (chainId: number) => Promise<Client | undefined>
|
|
88
86
|
|
|
89
87
|
export interface AcceptSlippageUpdateHookParams {
|
|
90
88
|
toToken: Token
|