@lifi/sdk 4.0.0-beta.7 → 4.0.0-beta.8
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/dist/cjs/actions/getChains.js.map +1 -1
- package/dist/cjs/actions/getConnections.js.map +1 -1
- package/dist/cjs/actions/getContractCallsQuote.js.map +1 -1
- package/dist/cjs/actions/getNameServiceAddress.js.map +1 -1
- package/dist/cjs/actions/getQuote.js.map +1 -1
- package/dist/cjs/actions/getRelayedTransactionStatus.js +2 -2
- package/dist/cjs/actions/getRelayedTransactionStatus.js.map +1 -1
- package/dist/cjs/actions/getRelayerQuote.js +2 -2
- package/dist/cjs/actions/getRelayerQuote.js.map +1 -1
- package/dist/cjs/actions/getTokenBalances.js.map +1 -1
- package/dist/cjs/actions/getTokenBalancesByChain.js.map +1 -1
- package/dist/cjs/actions/getTokens.js.map +1 -1
- package/dist/cjs/actions/getWalletBalances.js.map +1 -1
- package/dist/cjs/actions/relayTransaction.js +2 -2
- package/dist/cjs/actions/relayTransaction.js.map +1 -1
- package/dist/cjs/client/createClient.js.map +1 -1
- package/dist/cjs/core/BaseStepExecutor.js.map +1 -1
- package/dist/cjs/core/StatusManager.js.map +1 -1
- package/dist/cjs/core/TaskPipeline.js.map +1 -1
- package/dist/cjs/core/execution.js +2 -2
- package/dist/cjs/core/execution.js.map +1 -1
- package/dist/cjs/core/tasks/CheckBalanceTask.d.ts +9 -0
- package/dist/cjs/core/tasks/CheckBalanceTask.js +13 -3
- package/dist/cjs/core/tasks/CheckBalanceTask.js.map +1 -1
- package/dist/cjs/core/tasks/PrepareTransactionTask.js +3 -3
- package/dist/cjs/core/tasks/PrepareTransactionTask.js.map +1 -1
- package/dist/cjs/core/tasks/WaitForTransactionStatusTask.js +2 -2
- package/dist/cjs/core/tasks/WaitForTransactionStatusTask.js.map +1 -1
- package/dist/cjs/core/tasks/helpers/checkBalance.d.ts +12 -2
- package/dist/cjs/core/tasks/helpers/checkBalance.js +19 -12
- package/dist/cjs/core/tasks/helpers/checkBalance.js.map +1 -1
- package/dist/cjs/core/tasks/helpers/getTransactionRequestData.js +3 -3
- package/dist/cjs/core/tasks/helpers/getTransactionRequestData.js.map +1 -1
- package/dist/cjs/core/tasks/helpers/stepComparison.js +2 -2
- package/dist/cjs/core/tasks/helpers/stepComparison.js.map +1 -1
- package/dist/cjs/core/utils.js.map +1 -1
- package/dist/cjs/errors/errors.js +9 -9
- package/dist/cjs/errors/errors.js.map +1 -1
- package/dist/cjs/errors/httpError.js +17 -17
- package/dist/cjs/errors/httpError.js.map +1 -1
- package/dist/cjs/index.d.ts +2 -2
- package/dist/cjs/types/core.d.ts +0 -4
- package/dist/cjs/utils/checkPackageUpdates.js +1 -1
- package/dist/cjs/utils/checkPackageUpdates.js.map +1 -1
- package/dist/cjs/utils/convertQuoteToRoute.js.map +1 -1
- package/dist/cjs/utils/decode.js.map +1 -1
- package/dist/cjs/utils/fetchTxErrorDetails.js.map +1 -1
- package/dist/cjs/utils/getTransactionMessage.js.map +1 -1
- package/dist/cjs/version.d.ts +1 -1
- package/dist/cjs/version.js +1 -1
- package/dist/cjs/version.js.map +1 -1
- package/dist/esm/actions/getChains.js.map +1 -1
- package/dist/esm/actions/getConnections.js.map +1 -1
- package/dist/esm/actions/getContractCallsQuote.js.map +1 -1
- package/dist/esm/actions/getNameServiceAddress.js.map +1 -1
- package/dist/esm/actions/getQuote.js.map +1 -1
- package/dist/esm/actions/getRelayedTransactionStatus.js +2 -2
- package/dist/esm/actions/getRelayedTransactionStatus.js.map +1 -1
- package/dist/esm/actions/getRelayerQuote.js +2 -2
- package/dist/esm/actions/getRelayerQuote.js.map +1 -1
- package/dist/esm/actions/getTokenBalances.js.map +1 -1
- package/dist/esm/actions/getTokenBalancesByChain.js.map +1 -1
- package/dist/esm/actions/getTokens.js.map +1 -1
- package/dist/esm/actions/getWalletBalances.js.map +1 -1
- package/dist/esm/actions/relayTransaction.js +2 -2
- package/dist/esm/actions/relayTransaction.js.map +1 -1
- package/dist/esm/client/createClient.js.map +1 -1
- package/dist/esm/core/BaseStepExecutor.js.map +1 -1
- package/dist/esm/core/StatusManager.js.map +1 -1
- package/dist/esm/core/TaskPipeline.js.map +1 -1
- package/dist/esm/core/execution.js +2 -2
- package/dist/esm/core/execution.js.map +1 -1
- package/dist/esm/core/tasks/CheckBalanceTask.d.ts +9 -0
- package/dist/esm/core/tasks/CheckBalanceTask.d.ts.map +1 -1
- package/dist/esm/core/tasks/CheckBalanceTask.js +13 -3
- package/dist/esm/core/tasks/CheckBalanceTask.js.map +1 -1
- package/dist/esm/core/tasks/PrepareTransactionTask.js +3 -3
- package/dist/esm/core/tasks/PrepareTransactionTask.js.map +1 -1
- package/dist/esm/core/tasks/WaitForTransactionStatusTask.js +2 -3
- package/dist/esm/core/tasks/WaitForTransactionStatusTask.js.map +1 -1
- package/dist/esm/core/tasks/helpers/checkBalance.d.ts +12 -2
- package/dist/esm/core/tasks/helpers/checkBalance.d.ts.map +1 -1
- package/dist/esm/core/tasks/helpers/checkBalance.js +19 -12
- package/dist/esm/core/tasks/helpers/checkBalance.js.map +1 -1
- package/dist/esm/core/tasks/helpers/getTransactionRequestData.js +3 -3
- package/dist/esm/core/tasks/helpers/getTransactionRequestData.js.map +1 -1
- package/dist/esm/core/tasks/helpers/stepComparison.js +2 -2
- package/dist/esm/core/tasks/helpers/stepComparison.js.map +1 -1
- package/dist/esm/core/utils.js.map +1 -1
- package/dist/esm/errors/errors.js +9 -9
- package/dist/esm/errors/errors.js.map +1 -1
- package/dist/esm/errors/httpError.js +17 -17
- package/dist/esm/errors/httpError.js.map +1 -1
- package/dist/esm/index.d.ts +2 -2
- package/dist/esm/types/core.d.ts +0 -4
- package/dist/esm/types/core.d.ts.map +1 -1
- package/dist/esm/utils/checkPackageUpdates.js +1 -1
- package/dist/esm/utils/checkPackageUpdates.js.map +1 -1
- package/dist/esm/utils/convertQuoteToRoute.js.map +1 -1
- package/dist/esm/utils/decode.js.map +1 -1
- package/dist/esm/utils/fetchTxErrorDetails.js.map +1 -1
- package/dist/esm/utils/getTransactionMessage.js.map +1 -1
- package/dist/esm/version.d.ts +1 -1
- package/dist/esm/version.js +1 -1
- package/dist/esm/version.js.map +1 -1
- package/package.json +2 -2
- package/src/core/tasks/CheckBalanceTask.ts +19 -2
- package/src/core/tasks/helpers/checkBalance.ts +53 -21
- package/src/index.ts +4 -1
- package/src/types/core.ts +0 -4
- package/src/version.ts +1 -1
|
@@ -2,9 +2,25 @@ import { LiFiErrorCode } from '../../errors/constants.js'
|
|
|
2
2
|
import { TransactionError } from '../../errors/errors.js'
|
|
3
3
|
import type { StepExecutorContext, TaskResult } from '../../types/execution.js'
|
|
4
4
|
import { BaseStepExecutionTask } from '../BaseStepExecutionTask.js'
|
|
5
|
-
import {
|
|
5
|
+
import {
|
|
6
|
+
type CheckBalanceOptions,
|
|
7
|
+
checkBalance,
|
|
8
|
+
} from './helpers/checkBalance.js'
|
|
6
9
|
|
|
7
10
|
export class CheckBalanceTask extends BaseStepExecutionTask {
|
|
11
|
+
/**
|
|
12
|
+
* Per-step options hook for chain-specific subclasses (e.g. skip the
|
|
13
|
+
* gas check for smart-contract wallets or relayed steps). Default `{}`
|
|
14
|
+
* keeps behavior unchanged for every provider that doesn't override.
|
|
15
|
+
* Resolved lazily inside `run()` — pipelines that resume past
|
|
16
|
+
* `CheckBalance` pay no cost.
|
|
17
|
+
*/
|
|
18
|
+
protected getCheckBalanceOptions(
|
|
19
|
+
_context: StepExecutorContext
|
|
20
|
+
): Promise<CheckBalanceOptions> {
|
|
21
|
+
return Promise.resolve({})
|
|
22
|
+
}
|
|
23
|
+
|
|
8
24
|
async run(context: StepExecutorContext): Promise<TaskResult> {
|
|
9
25
|
const { client, step, statusManager, isBridgeExecution } = context
|
|
10
26
|
|
|
@@ -23,7 +39,8 @@ export class CheckBalanceTask extends BaseStepExecutionTask {
|
|
|
23
39
|
)
|
|
24
40
|
}
|
|
25
41
|
|
|
26
|
-
await
|
|
42
|
+
const options = await this.getCheckBalanceOptions(context)
|
|
43
|
+
await checkBalance(client, walletAddress, step, options)
|
|
27
44
|
return { status: 'COMPLETED' }
|
|
28
45
|
}
|
|
29
46
|
}
|
|
@@ -11,10 +11,24 @@ const BACKOFF_BASE_MS = 150
|
|
|
11
11
|
const OVERALL_TIMEOUT_MS = 10_000
|
|
12
12
|
const SLIPPAGE_PRECISION = 1_000_000_000n
|
|
13
13
|
|
|
14
|
+
type Bucket = 'source' | 'gas' | 'fee'
|
|
15
|
+
|
|
14
16
|
type Requirement = {
|
|
15
17
|
token: Token
|
|
16
|
-
sourcePart: bigint //
|
|
17
|
-
|
|
18
|
+
sourcePart: bigint // step.action.fromAmount
|
|
19
|
+
gasPart: bigint // step.estimate.gasCosts in this token
|
|
20
|
+
feePart: bigint // non-included step.estimate.feeCosts in this token
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export type CheckBalanceOptions = {
|
|
24
|
+
/**
|
|
25
|
+
* Set to `false` when outer-tx gas is paid by something other than
|
|
26
|
+
* `walletAddress` (SCA executor, 4337 bundler / paymaster, relayer):
|
|
27
|
+
* `gasPart` is excluded from the sufficiency check and slippage rescue.
|
|
28
|
+
* Source amount and non-included fees (e.g. LZ `msg.value`) are still
|
|
29
|
+
* verified. Defaults to `true` (strict, today's behavior).
|
|
30
|
+
*/
|
|
31
|
+
walletPaysGas?: boolean
|
|
18
32
|
}
|
|
19
33
|
|
|
20
34
|
/**
|
|
@@ -32,11 +46,13 @@ type Requirement = {
|
|
|
32
46
|
export const checkBalance = async (
|
|
33
47
|
client: SDKClient,
|
|
34
48
|
walletAddress: string,
|
|
35
|
-
step: LiFiStep
|
|
49
|
+
step: LiFiStep,
|
|
50
|
+
options: CheckBalanceOptions = {}
|
|
36
51
|
): Promise<void> => {
|
|
52
|
+
const walletPaysGas = options.walletPaysGas ?? true
|
|
37
53
|
const fromChainId = step.action.fromChainId
|
|
38
54
|
const requirements = new Map<string, Requirement>()
|
|
39
|
-
const add = (token: Token, amount: bigint,
|
|
55
|
+
const add = (token: Token, amount: bigint, bucket: Bucket): void => {
|
|
40
56
|
if (token.chainId !== fromChainId || amount === 0n) {
|
|
41
57
|
return
|
|
42
58
|
}
|
|
@@ -44,25 +60,41 @@ export const checkBalance = async (
|
|
|
44
60
|
const req = requirements.get(key) ?? {
|
|
45
61
|
token,
|
|
46
62
|
sourcePart: 0n,
|
|
47
|
-
|
|
63
|
+
gasPart: 0n,
|
|
64
|
+
feePart: 0n,
|
|
48
65
|
}
|
|
49
|
-
if (source) {
|
|
66
|
+
if (bucket === 'source') {
|
|
50
67
|
req.sourcePart += amount
|
|
68
|
+
} else if (bucket === 'gas') {
|
|
69
|
+
req.gasPart += amount
|
|
51
70
|
} else {
|
|
52
|
-
req.
|
|
71
|
+
req.feePart += amount
|
|
53
72
|
}
|
|
54
73
|
requirements.set(key, req)
|
|
55
74
|
}
|
|
56
|
-
add(step.action.fromToken, BigInt(step.action.fromAmount),
|
|
75
|
+
add(step.action.fromToken, BigInt(step.action.fromAmount), 'source')
|
|
57
76
|
for (const gas of step.estimate?.gasCosts ?? []) {
|
|
58
|
-
add(gas.token, BigInt(gas.amount),
|
|
77
|
+
add(gas.token, BigInt(gas.amount), 'gas')
|
|
59
78
|
}
|
|
60
79
|
for (const fee of step.estimate?.feeCosts ?? []) {
|
|
61
80
|
// Included fees are already part of fromAmount — don't count twice.
|
|
62
81
|
if (!fee.included) {
|
|
63
|
-
add(fee.token, BigInt(fee.amount),
|
|
82
|
+
add(fee.token, BigInt(fee.amount), 'fee')
|
|
64
83
|
}
|
|
65
84
|
}
|
|
85
|
+
|
|
86
|
+
const reservedOverhead = (r: Requirement): bigint =>
|
|
87
|
+
r.feePart + (walletPaysGas ? r.gasPart : 0n)
|
|
88
|
+
const need = (r: Requirement): bigint => r.sourcePart + reservedOverhead(r)
|
|
89
|
+
|
|
90
|
+
// Drop pure-gas entries when `walletPaysGas` is false (e.g. native ETH
|
|
91
|
+
// on Safe Apps with only `gasPart`) — saves one balance read each.
|
|
92
|
+
for (const [key, req] of requirements) {
|
|
93
|
+
if (need(req) === 0n) {
|
|
94
|
+
requirements.delete(key)
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
66
98
|
if (requirements.size === 0) {
|
|
67
99
|
return
|
|
68
100
|
}
|
|
@@ -110,7 +142,7 @@ export const checkBalance = async (
|
|
|
110
142
|
const have = balanceByAddress.get(req.token.address.toLowerCase())
|
|
111
143
|
if (have === undefined) {
|
|
112
144
|
unknown.push(req.token)
|
|
113
|
-
} else if (have < req
|
|
145
|
+
} else if (have < need(req)) {
|
|
114
146
|
insufficient.push({ req, have })
|
|
115
147
|
}
|
|
116
148
|
}
|
|
@@ -119,9 +151,9 @@ export const checkBalance = async (
|
|
|
119
151
|
return
|
|
120
152
|
}
|
|
121
153
|
|
|
122
|
-
// Final-attempt slippage rescue: only when the sole shortfall is
|
|
123
|
-
// source-token portion. Trim source
|
|
124
|
-
// the overhead reserve is preserved.
|
|
154
|
+
// Final-attempt slippage rescue: only when the sole shortfall is
|
|
155
|
+
// the source-token portion. Trim source to (balance − reserved
|
|
156
|
+
// overhead) so the overhead reserve is preserved.
|
|
125
157
|
if (
|
|
126
158
|
isFinal &&
|
|
127
159
|
unknown.length === 0 &&
|
|
@@ -129,11 +161,11 @@ export const checkBalance = async (
|
|
|
129
161
|
insufficient[0].req.sourcePart > 0n
|
|
130
162
|
) {
|
|
131
163
|
const { req, have } = insufficient[0]
|
|
164
|
+
const reserved = reservedOverhead(req)
|
|
132
165
|
const minAcceptable =
|
|
133
|
-
(req.sourcePart * slippageScaled) / SLIPPAGE_PRECISION +
|
|
134
|
-
req.overheadPart
|
|
166
|
+
(req.sourcePart * slippageScaled) / SLIPPAGE_PRECISION + reserved
|
|
135
167
|
if (have >= minAcceptable) {
|
|
136
|
-
step.action.fromAmount = (have -
|
|
168
|
+
step.action.fromAmount = (have - reserved).toString()
|
|
137
169
|
return
|
|
138
170
|
}
|
|
139
171
|
}
|
|
@@ -150,12 +182,12 @@ export const checkBalance = async (
|
|
|
150
182
|
)
|
|
151
183
|
}
|
|
152
184
|
const lines = insufficient.map(({ req, have }) => {
|
|
153
|
-
const needed = formatUnits(
|
|
154
|
-
req.sourcePart + req.overheadPart,
|
|
155
|
-
req.token.decimals
|
|
156
|
-
)
|
|
185
|
+
const needed = formatUnits(need(req), req.token.decimals)
|
|
157
186
|
const current = formatUnits(have, req.token.decimals)
|
|
158
187
|
const symbol = req.token.symbol
|
|
188
|
+
// The "fees" branch covers pure-overhead tokens; with
|
|
189
|
+
// walletPaysGas=false, gas is excluded from `need(req)` so it
|
|
190
|
+
// only fires for genuine fee shortfalls.
|
|
159
191
|
return req.sourcePart > 0n
|
|
160
192
|
? `Your ${symbol} balance is too low, you try to transfer ${needed} ${symbol}, but your wallet only holds ${current} ${symbol}.`
|
|
161
193
|
: `Insufficient ${symbol} for fees: need ${needed} ${symbol}, have ${current} ${symbol}.`
|
package/src/index.ts
CHANGED
|
@@ -44,7 +44,10 @@ export {
|
|
|
44
44
|
} from './core/storage.js'
|
|
45
45
|
export { TaskPipeline } from './core/TaskPipeline.js'
|
|
46
46
|
export { CheckBalanceTask } from './core/tasks/CheckBalanceTask.js'
|
|
47
|
-
export {
|
|
47
|
+
export {
|
|
48
|
+
type CheckBalanceOptions,
|
|
49
|
+
checkBalance,
|
|
50
|
+
} from './core/tasks/helpers/checkBalance.js'
|
|
48
51
|
export { getTransactionRequestData } from './core/tasks/helpers/getTransactionRequestData.js'
|
|
49
52
|
export { stepComparison } from './core/tasks/helpers/stepComparison.js'
|
|
50
53
|
export { PrepareTransactionTask } from './core/tasks/PrepareTransactionTask.js'
|
package/src/types/core.ts
CHANGED
|
@@ -205,10 +205,6 @@ export interface ExecutionOptions {
|
|
|
205
205
|
getContractCalls?: GetContractCallsHook
|
|
206
206
|
adjustZeroOutputFromPreviousStep?: boolean
|
|
207
207
|
executeInBackground?: boolean
|
|
208
|
-
/**
|
|
209
|
-
* @deprecated
|
|
210
|
-
*/
|
|
211
|
-
infiniteApproval?: boolean
|
|
212
208
|
}
|
|
213
209
|
|
|
214
210
|
export type ExecutionStatus = 'ACTION_REQUIRED' | 'PENDING' | 'FAILED' | 'DONE'
|
package/src/version.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
export const name = '@lifi/sdk'
|
|
2
|
-
export const version = '4.0.0-beta.
|
|
2
|
+
export const version = '4.0.0-beta.8'
|