@funkit/connect 12.8.0 → 12.10.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.
@@ -1,296 +0,0 @@
1
- "use client";
2
- import {
3
- toWithdrawalAmountBaseUnit
4
- } from "./chunk-4DBUYTEE.js";
5
- import {
6
- isExpectedNadoWithdrawalFailure,
7
- toNadoWithdrawalErrorMessage
8
- } from "./chunk-B5KCBBSV.js";
9
- import {
10
- getAccount,
11
- signTypedData,
12
- switchChain
13
- } from "./chunk-OODIKHYE.js";
14
- import {
15
- i18n_default
16
- } from "./chunk-7GPRX32F.js";
17
- import {
18
- generateClientMetadataForTokenTransfer
19
- } from "./chunk-I5GSXPFE.js";
20
- import {
21
- logger
22
- } from "./chunk-AHYOV7AV.js";
23
-
24
- // src/clients/internal/nado/withdrawalCallback.ts
25
- import {
26
- NADO_API_KEY,
27
- initializeCheckoutTokenTransferAddress
28
- } from "@funkit/api-base";
29
- import {
30
- deductFlatFee,
31
- normalizeRecipientAddress,
32
- safeBigInt
33
- } from "@funkit/connect-core";
34
- import {
35
- INK_CHAIN_ID,
36
- NADO_WITHDRAWAL_ACTION_TYPE,
37
- NadoGatewayError,
38
- NadoWithdrawalUnconfirmedError,
39
- buildWithdrawCollateralV2TypedData,
40
- executeNadoWithdrawCollateralV2,
41
- getNadoMaxWithdrawable,
42
- getNadoTxNonce,
43
- x18ToRawBaseUnit
44
- } from "@funkit/connect-core/clients/nado";
45
- import { isAddressEquivalent } from "@funkit/utils";
46
-
47
- // src/utils/signature.ts
48
- import { isSwitchChainError, isUserRejectedError } from "@funkit/connect-core";
49
-
50
- // src/clients/internal/nado/withdrawalCallback.ts
51
- var NadoWithdrawalInputError = class extends Error {
52
- };
53
- var NadoWithdrawalUserError = class extends Error {
54
- };
55
- function createNadoWithdrawalCallback({
56
- walletAddress,
57
- subaccount,
58
- wagmiConfig
59
- }) {
60
- return async (param) => {
61
- const {
62
- customerData,
63
- userInputAmount,
64
- targetAssetAddress,
65
- targetChainId,
66
- destinationAddress
67
- } = param;
68
- const recipientAddr = normalizeRecipientAddress(destinationAddress);
69
- let step = "validate";
70
- try {
71
- if (customerData?.customerName !== "nado" || !userInputAmount) {
72
- throw new NadoWithdrawalInputError(
73
- "Missing Nado customerData or userInputAmount"
74
- );
75
- }
76
- const {
77
- spotLeverage,
78
- sequencerFeeTokenAmount,
79
- selectedSourceToken: { productId, decimals }
80
- } = customerData;
81
- const netTokens = deductFlatFee(userInputAmount, sequencerFeeTokenAmount);
82
- const netRawAmount = toWithdrawalAmountBaseUnit(netTokens, decimals);
83
- if (netRawAmount <= 0n) {
84
- logger.warn("nado:withdrawal:amountTooLow", {
85
- productId,
86
- userInputAmount,
87
- sequencerFeeTokenAmount
88
- });
89
- throw new NadoWithdrawalUserError(
90
- i18n_default.t("withdrawal.nado.errorAmountTooLow")
91
- );
92
- }
93
- logger.info("nado:withdrawal:start", {
94
- productId,
95
- spotLeverage,
96
- decimals,
97
- userInputAmount,
98
- netTokens,
99
- sequencerFeeTokenAmount,
100
- targetChainId,
101
- targetAssetAddress,
102
- destinationAddress: recipientAddr
103
- });
104
- await assertWithinMaxWithdrawable({
105
- subaccount,
106
- productId,
107
- decimals,
108
- spotLeverage,
109
- netRawAmount
110
- });
111
- step = "switchChain";
112
- await ensureWalletOnInk(wagmiConfig);
113
- assertOwnerIsSigner({ walletAddress, wagmiConfig });
114
- step = "uda";
115
- const transferInit = await initializeCheckoutTokenTransferAddress({
116
- apiKey: NADO_API_KEY,
117
- // No Fun user context in a client bundle entry; the owner wallet is the
118
- // stable per-user id and matches what sourceOfFund records.
119
- userId: walletAddress,
120
- recipientAddr,
121
- toChainId: targetChainId.toString(),
122
- toTokenAddress: targetAssetAddress,
123
- sourceOfFund: `balance|nado|${walletAddress}`,
124
- // Attribution only — forces a separate UDA from Nado deposits so the
125
- // direct executions off it are tagged as withdrawals.
126
- actionType: NADO_WITHDRAWAL_ACTION_TYPE,
127
- logger,
128
- clientMetadata: {
129
- ...generateClientMetadataForTokenTransfer(),
130
- isWithdrawal: true
131
- }
132
- });
133
- const depositAddr = transferInit.depositAddr;
134
- logger.info("nado:withdrawal:udaGenerated", {
135
- depositAddr,
136
- productId,
137
- destinationAddress: recipientAddr
138
- });
139
- step = "sign";
140
- const nonce = await getNadoTxNonce(walletAddress, { logger });
141
- const signature = await signTypedData(wagmiConfig, {
142
- ...buildWithdrawCollateralV2TypedData({
143
- sender: subaccount,
144
- productId,
145
- amount: netRawAmount,
146
- nonce,
147
- sendTo: depositAddr
148
- }),
149
- account: walletAddress
150
- });
151
- logger.info("nado:withdrawal:signed", {
152
- nonce: nonce.toString(),
153
- depositAddr,
154
- productId
155
- });
156
- step = "execute";
157
- await executeNadoWithdrawCollateralV2(
158
- {
159
- sender: subaccount,
160
- productId,
161
- amount: netRawAmount,
162
- nonce,
163
- sendTo: depositAddr,
164
- signature,
165
- spotLeverage
166
- },
167
- { logger }
168
- );
169
- logger.info("nado:withdrawal:executeSubmitted", {
170
- nonce: nonce.toString(),
171
- depositAddr,
172
- productId,
173
- netRawAmount: netRawAmount.toString()
174
- });
175
- return depositAddr;
176
- } catch (error) {
177
- throw toThrownWithdrawalError(error, {
178
- step,
179
- walletAddress,
180
- subaccount,
181
- destinationAddress: recipientAddr,
182
- targetChainId
183
- });
184
- }
185
- };
186
- }
187
- async function assertWithinMaxWithdrawable({
188
- subaccount,
189
- productId,
190
- decimals,
191
- spotLeverage,
192
- netRawAmount
193
- }) {
194
- const maxWithdrawableX18 = await getNadoMaxWithdrawable(
195
- { productId, sender: subaccount, spotLeverage },
196
- { logger }
197
- );
198
- const maxRawAmount = x18ToRawBaseUnit(
199
- safeBigInt(maxWithdrawableX18, 0n),
200
- decimals
201
- );
202
- if (netRawAmount <= maxRawAmount) {
203
- return;
204
- }
205
- logger.warn("nado:withdrawal:exceedsMaxWithdrawable", {
206
- productId,
207
- spotLeverage,
208
- netRawAmount: netRawAmount.toString(),
209
- maxRawAmount: maxRawAmount.toString()
210
- });
211
- throw new NadoWithdrawalUserError(
212
- i18n_default.t("withdrawal.nado.errorBalanceChanged")
213
- );
214
- }
215
- async function ensureWalletOnInk(wagmiConfig) {
216
- const connectedChainId = getAccount(wagmiConfig).chainId;
217
- if (connectedChainId === INK_CHAIN_ID) {
218
- return;
219
- }
220
- logger.info("nado:withdrawal:switchingChain", {
221
- fromChainId: connectedChainId ?? null
222
- });
223
- try {
224
- await switchChain(wagmiConfig, { chainId: INK_CHAIN_ID });
225
- } catch (error) {
226
- if (isUserRejectedError(error)) {
227
- throw error;
228
- }
229
- logger.warn("nado:withdrawal:switchChainFailed", {
230
- fromChainId: connectedChainId ?? null,
231
- errorMessage: error instanceof Error ? error.message : String(error)
232
- });
233
- throw new NadoWithdrawalUserError(
234
- i18n_default.t("withdrawal.nado.errorSwitchChain")
235
- );
236
- }
237
- }
238
- function assertOwnerIsSigner({
239
- walletAddress,
240
- wagmiConfig
241
- }) {
242
- const connectedAddress = getAccount(wagmiConfig).address;
243
- if (connectedAddress && isAddressEquivalent({
244
- firstAddress: connectedAddress,
245
- secondAddress: walletAddress
246
- })) {
247
- return;
248
- }
249
- logger.warn("nado:withdrawal:signerMismatch", {
250
- walletAddress,
251
- connectedAddress: connectedAddress ?? null
252
- });
253
- throw new NadoWithdrawalUserError(
254
- i18n_default.t("withdrawal.nado.errorSignerMismatch")
255
- );
256
- }
257
- function toThrownWithdrawalError(error, context) {
258
- const logContext = {
259
- ...context,
260
- nadoErrorCode: error instanceof NadoGatewayError ? error.errorCode ?? null : null,
261
- nadoErrorKind: error instanceof NadoGatewayError ? error.kind : null,
262
- errorMessage: error instanceof Error ? error.message : String(error)
263
- };
264
- if (isUserRejectedError(error)) {
265
- logger.warn("nado:withdrawal:rejected", logContext);
266
- return error;
267
- }
268
- if (error instanceof NadoWithdrawalUserError) {
269
- return error;
270
- }
271
- if (error instanceof NadoWithdrawalInputError) {
272
- logger.error("nado:withdrawal:invalidInput", error, logContext);
273
- return new Error(i18n_default.t("withdrawal.nado.errorGeneric"));
274
- }
275
- if (error instanceof NadoWithdrawalUnconfirmedError) {
276
- return new Error(toNadoWithdrawalErrorMessage(error));
277
- }
278
- if (!(error instanceof NadoGatewayError)) {
279
- logger.error(
280
- "nado:withdrawal:failed",
281
- error instanceof Error ? error : new Error(String(error)),
282
- logContext
283
- );
284
- return error;
285
- }
286
- if (isExpectedNadoWithdrawalFailure(error)) {
287
- logger.warn("nado:withdrawal:gatewayRejected", logContext);
288
- } else {
289
- logger.error("nado:withdrawal:gatewayFailed", error, logContext);
290
- }
291
- return new Error(toNadoWithdrawalErrorMessage(error));
292
- }
293
-
294
- export {
295
- createNadoWithdrawalCallback
296
- };