@safe-global/api-kit 2.5.6 → 2.5.8-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.
Files changed (40) hide show
  1. package/dist/cjs/index.cjs +961 -0
  2. package/dist/esm/index.mjs +930 -0
  3. package/dist/src/SafeApiKit.d.ts +1 -0
  4. package/dist/src/SafeApiKit.d.ts.map +1 -0
  5. package/dist/src/index.d.ts +1 -0
  6. package/dist/src/index.d.ts.map +1 -0
  7. package/dist/src/types/safeTransactionServiceTypes.d.ts +20 -25
  8. package/dist/src/types/safeTransactionServiceTypes.d.ts.map +1 -0
  9. package/dist/src/utils/config.d.ts +1 -0
  10. package/dist/src/utils/config.d.ts.map +1 -0
  11. package/dist/src/utils/constants.d.ts +1 -0
  12. package/dist/src/utils/constants.d.ts.map +1 -0
  13. package/dist/src/utils/httpRequests.d.ts +1 -0
  14. package/dist/src/utils/httpRequests.d.ts.map +1 -0
  15. package/dist/src/utils/index.d.ts +1 -0
  16. package/dist/src/utils/index.d.ts.map +1 -0
  17. package/dist/src/utils/safeOperation.d.ts +1 -0
  18. package/dist/src/utils/safeOperation.d.ts.map +1 -0
  19. package/dist/src/utils/signDelegate.d.ts +1 -0
  20. package/dist/src/utils/signDelegate.d.ts.map +1 -0
  21. package/package.json +17 -8
  22. package/dist/src/SafeApiKit.js +0 -788
  23. package/dist/src/SafeApiKit.js.map +0 -1
  24. package/dist/src/index.js +0 -23
  25. package/dist/src/index.js.map +0 -1
  26. package/dist/src/types/safeTransactionServiceTypes.js +0 -3
  27. package/dist/src/types/safeTransactionServiceTypes.js.map +0 -1
  28. package/dist/src/utils/config.js +0 -25
  29. package/dist/src/utils/config.js.map +0 -1
  30. package/dist/src/utils/constants.js +0 -5
  31. package/dist/src/utils/constants.js.map +0 -1
  32. package/dist/src/utils/httpRequests.js +0 -59
  33. package/dist/src/utils/httpRequests.js.map +0 -1
  34. package/dist/src/utils/index.js +0 -7
  35. package/dist/src/utils/index.js.map +0 -1
  36. package/dist/src/utils/safeOperation.js +0 -19
  37. package/dist/src/utils/safeOperation.js.map +0 -1
  38. package/dist/src/utils/signDelegate.js +0 -25
  39. package/dist/src/utils/signDelegate.js.map +0 -1
  40. package/dist/tsconfig.build.tsbuildinfo +0 -1
@@ -0,0 +1,930 @@
1
+ // src/utils/httpRequests.ts
2
+ import fetch from "node-fetch";
3
+ async function sendRequest({ url, method, body }) {
4
+ const response = await fetch(url, {
5
+ method,
6
+ headers: {
7
+ Accept: "application/json",
8
+ "Content-Type": "application/json"
9
+ },
10
+ body: JSON.stringify(body)
11
+ });
12
+ let jsonResponse;
13
+ try {
14
+ jsonResponse = await response.json();
15
+ } catch (error) {
16
+ if (!response.ok) {
17
+ throw new Error(response.statusText);
18
+ }
19
+ }
20
+ if (response.ok) {
21
+ return jsonResponse;
22
+ }
23
+ if (jsonResponse.data) {
24
+ throw new Error(jsonResponse.data);
25
+ }
26
+ if (jsonResponse.detail) {
27
+ throw new Error(jsonResponse.detail);
28
+ }
29
+ if (jsonResponse.message) {
30
+ throw new Error(jsonResponse.message);
31
+ }
32
+ if (jsonResponse.nonFieldErrors) {
33
+ throw new Error(jsonResponse.nonFieldErrors);
34
+ }
35
+ if (jsonResponse.delegate) {
36
+ throw new Error(jsonResponse.delegate);
37
+ }
38
+ if (jsonResponse.safe) {
39
+ throw new Error(jsonResponse.safe);
40
+ }
41
+ if (jsonResponse.delegator) {
42
+ throw new Error(jsonResponse.delegator);
43
+ }
44
+ throw new Error(response.statusText);
45
+ }
46
+
47
+ // src/utils/signDelegate.ts
48
+ async function signDelegate(walletClient, delegateAddress, chainId) {
49
+ const domain = {
50
+ name: "Safe Transaction Service",
51
+ version: "1.0",
52
+ chainId: Number(chainId)
53
+ };
54
+ const types = {
55
+ Delegate: [
56
+ { name: "delegateAddress", type: "address" },
57
+ { name: "totp", type: "uint256" }
58
+ ]
59
+ };
60
+ const totp = Math.floor(Date.now() / 1e3 / 3600);
61
+ return walletClient.signTypedData({
62
+ domain,
63
+ types,
64
+ primaryType: "Delegate",
65
+ message: { delegateAddress, totp }
66
+ });
67
+ }
68
+
69
+ // src/SafeApiKit.ts
70
+ import { validateEip3770Address, validateEthereumAddress } from "@safe-global/protocol-kit";
71
+ import {
72
+ isSafeOperation
73
+ } from "@safe-global/types-kit";
74
+
75
+ // src/utils/config.ts
76
+ var TRANSACTION_SERVICE_URLS = {
77
+ "1": "https://safe-transaction-mainnet.safe.global/api",
78
+ "10": "https://safe-transaction-optimism.safe.global/api",
79
+ "56": "https://safe-transaction-bsc.safe.global/api",
80
+ "100": "https://safe-transaction-gnosis-chain.safe.global/api",
81
+ "137": "https://safe-transaction-polygon.safe.global/api",
82
+ "196": "https://safe-transaction-xlayer.safe.global/api",
83
+ "324": "https://safe-transaction-zksync.safe.global/api",
84
+ "1101": "https://safe-transaction-zkevm.safe.global/api",
85
+ "5000": "https://safe-transaction-mantle.safe.global/api",
86
+ "8453": "https://safe-transaction-base.safe.global/api",
87
+ "42161": "https://safe-transaction-arbitrum.safe.global/api",
88
+ "42220": "https://safe-transaction-celo.safe.global/api",
89
+ "43114": "https://safe-transaction-avalanche.safe.global/api",
90
+ "59144": "https://safe-transaction-linea.safe.global/api",
91
+ "81457": "https://safe-transaction-blast.safe.global/api",
92
+ "84532": "https://safe-transaction-base-sepolia.safe.global/api",
93
+ "534352": "https://safe-transaction-scroll.safe.global/api",
94
+ "11155111": "https://safe-transaction-sepolia.safe.global/api",
95
+ "1313161554": "https://safe-transaction-aurora.safe.global/api"
96
+ };
97
+
98
+ // src/utils/constants.ts
99
+ var EMPTY_DATA = "0x";
100
+
101
+ // src/utils/index.ts
102
+ var isEmptyData = (input) => !input || input === EMPTY_DATA;
103
+
104
+ // src/utils/safeOperation.ts
105
+ var getAddSafeOperationProps = async (safeOperation) => {
106
+ const userOperation = safeOperation.toUserOperation();
107
+ userOperation.signature = safeOperation.encodedSignatures();
108
+ return {
109
+ entryPoint: safeOperation.data.entryPoint,
110
+ moduleAddress: safeOperation.moduleAddress,
111
+ safeAddress: safeOperation.data.safe,
112
+ userOperation,
113
+ options: {
114
+ validAfter: safeOperation.data.validAfter,
115
+ validUntil: safeOperation.data.validUntil
116
+ }
117
+ };
118
+ };
119
+
120
+ // src/SafeApiKit.ts
121
+ var SafeApiKit = class {
122
+ #chainId;
123
+ #txServiceBaseUrl;
124
+ constructor({ chainId, txServiceUrl }) {
125
+ this.#chainId = chainId;
126
+ if (txServiceUrl) {
127
+ this.#txServiceBaseUrl = txServiceUrl;
128
+ } else {
129
+ const url = TRANSACTION_SERVICE_URLS[chainId.toString()];
130
+ if (!url) {
131
+ throw new TypeError(
132
+ `There is no transaction service available for chainId ${chainId}. Please set the txServiceUrl property to use a custom transaction service.`
133
+ );
134
+ }
135
+ this.#txServiceBaseUrl = url;
136
+ }
137
+ }
138
+ #isValidAddress(address) {
139
+ try {
140
+ validateEthereumAddress(address);
141
+ return true;
142
+ } catch {
143
+ return false;
144
+ }
145
+ }
146
+ #getEip3770Address(fullAddress) {
147
+ return validateEip3770Address(fullAddress, this.#chainId);
148
+ }
149
+ /**
150
+ * Returns the information and configuration of the service.
151
+ *
152
+ * @returns The information and configuration of the service
153
+ */
154
+ async getServiceInfo() {
155
+ return sendRequest({
156
+ url: `${this.#txServiceBaseUrl}/v1/about`,
157
+ method: "get" /* Get */
158
+ });
159
+ }
160
+ /**
161
+ * Returns the list of Safe singletons.
162
+ *
163
+ * @returns The list of Safe singletons
164
+ */
165
+ async getServiceSingletonsInfo() {
166
+ return sendRequest({
167
+ url: `${this.#txServiceBaseUrl}/v1/about/singletons`,
168
+ method: "get" /* Get */
169
+ });
170
+ }
171
+ /**
172
+ * Decodes the specified Safe transaction data.
173
+ *
174
+ * @param data - The Safe transaction data. '0x' prefixed hexadecimal string.
175
+ * @param to - The address of the receiving contract. If provided, the decoded data will be more accurate, as in case of an ABI collision the Safe Transaction Service would know which ABI to use
176
+ * @returns The transaction data decoded
177
+ * @throws "Invalid data"
178
+ * @throws "Not Found"
179
+ * @throws "Ensure this field has at least 1 hexadecimal chars (not counting 0x)."
180
+ */
181
+ async decodeData(data, to) {
182
+ if (data === "") {
183
+ throw new Error("Invalid data");
184
+ }
185
+ const dataDecoderRequest = { data };
186
+ if (to) {
187
+ dataDecoderRequest.to = to;
188
+ }
189
+ return sendRequest({
190
+ url: `${this.#txServiceBaseUrl}/v1/data-decoder/`,
191
+ method: "post" /* Post */,
192
+ body: dataDecoderRequest
193
+ });
194
+ }
195
+ /**
196
+ * Returns the list of Safes where the address provided is an owner.
197
+ *
198
+ * @param ownerAddress - The owner address
199
+ * @returns The list of Safes where the address provided is an owner
200
+ * @throws "Invalid owner address"
201
+ * @throws "Checksum address validation failed"
202
+ */
203
+ async getSafesByOwner(ownerAddress) {
204
+ if (ownerAddress === "") {
205
+ throw new Error("Invalid owner address");
206
+ }
207
+ const { address } = this.#getEip3770Address(ownerAddress);
208
+ return sendRequest({
209
+ url: `${this.#txServiceBaseUrl}/v1/owners/${address}/safes/`,
210
+ method: "get" /* Get */
211
+ });
212
+ }
213
+ /**
214
+ * Returns the list of Safes where the module address provided is enabled.
215
+ *
216
+ * @param moduleAddress - The Safe module address
217
+ * @returns The list of Safe addresses where the module provided is enabled
218
+ * @throws "Invalid module address"
219
+ * @throws "Module address checksum not valid"
220
+ */
221
+ async getSafesByModule(moduleAddress) {
222
+ if (moduleAddress === "") {
223
+ throw new Error("Invalid module address");
224
+ }
225
+ const { address } = this.#getEip3770Address(moduleAddress);
226
+ return sendRequest({
227
+ url: `${this.#txServiceBaseUrl}/v1/modules/${address}/safes/`,
228
+ method: "get" /* Get */
229
+ });
230
+ }
231
+ /**
232
+ * Returns all the information of a Safe transaction.
233
+ *
234
+ * @param safeTxHash - Hash of the Safe transaction
235
+ * @returns The information of a Safe transaction
236
+ * @throws "Invalid safeTxHash"
237
+ * @throws "Not found."
238
+ */
239
+ async getTransaction(safeTxHash) {
240
+ if (safeTxHash === "") {
241
+ throw new Error("Invalid safeTxHash");
242
+ }
243
+ return sendRequest({
244
+ url: `${this.#txServiceBaseUrl}/v1/multisig-transactions/${safeTxHash}/`,
245
+ method: "get" /* Get */
246
+ });
247
+ }
248
+ /**
249
+ * Returns the list of confirmations for a given a Safe transaction.
250
+ *
251
+ * @param safeTxHash - The hash of the Safe transaction
252
+ * @returns The list of confirmations
253
+ * @throws "Invalid safeTxHash"
254
+ */
255
+ async getTransactionConfirmations(safeTxHash) {
256
+ if (safeTxHash === "") {
257
+ throw new Error("Invalid safeTxHash");
258
+ }
259
+ return sendRequest({
260
+ url: `${this.#txServiceBaseUrl}/v1/multisig-transactions/${safeTxHash}/confirmations/`,
261
+ method: "get" /* Get */
262
+ });
263
+ }
264
+ /**
265
+ * Adds a confirmation for a Safe transaction.
266
+ *
267
+ * @param safeTxHash - Hash of the Safe transaction that will be confirmed
268
+ * @param signature - Signature of the transaction
269
+ * @returns
270
+ * @throws "Invalid safeTxHash"
271
+ * @throws "Invalid signature"
272
+ * @throws "Malformed data"
273
+ * @throws "Error processing data"
274
+ */
275
+ async confirmTransaction(safeTxHash, signature) {
276
+ if (safeTxHash === "") {
277
+ throw new Error("Invalid safeTxHash");
278
+ }
279
+ if (signature === "") {
280
+ throw new Error("Invalid signature");
281
+ }
282
+ return sendRequest({
283
+ url: `${this.#txServiceBaseUrl}/v1/multisig-transactions/${safeTxHash}/confirmations/`,
284
+ method: "post" /* Post */,
285
+ body: {
286
+ signature
287
+ }
288
+ });
289
+ }
290
+ /**
291
+ * Returns the information and configuration of the provided Safe address.
292
+ *
293
+ * @param safeAddress - The Safe address
294
+ * @returns The information and configuration of the provided Safe address
295
+ * @throws "Invalid Safe address"
296
+ * @throws "Checksum address validation failed"
297
+ */
298
+ async getSafeInfo(safeAddress) {
299
+ if (safeAddress === "") {
300
+ throw new Error("Invalid Safe address");
301
+ }
302
+ const { address } = this.#getEip3770Address(safeAddress);
303
+ return sendRequest({
304
+ url: `${this.#txServiceBaseUrl}/v1/safes/${address}/`,
305
+ method: "get" /* Get */
306
+ }).then((response) => {
307
+ if (!response?.singleton) {
308
+ const { masterCopy, ...rest } = response;
309
+ return { ...rest, singleton: masterCopy };
310
+ }
311
+ return response;
312
+ });
313
+ }
314
+ /**
315
+ * Returns the list of delegates.
316
+ *
317
+ * @param getSafeDelegateProps - Properties to filter the returned list of delegates
318
+ * @returns The list of delegates
319
+ * @throws "Checksum address validation failed"
320
+ */
321
+ async getSafeDelegates({
322
+ safeAddress,
323
+ delegateAddress,
324
+ delegatorAddress,
325
+ label,
326
+ limit,
327
+ offset
328
+ }) {
329
+ const url = new URL(`${this.#txServiceBaseUrl}/v2/delegates`);
330
+ if (safeAddress) {
331
+ const { address: safe } = this.#getEip3770Address(safeAddress);
332
+ url.searchParams.set("safe", safe);
333
+ }
334
+ if (delegateAddress) {
335
+ const { address: delegate } = this.#getEip3770Address(delegateAddress);
336
+ url.searchParams.set("delegate", delegate);
337
+ }
338
+ if (delegatorAddress) {
339
+ const { address: delegator } = this.#getEip3770Address(delegatorAddress);
340
+ url.searchParams.set("delegator", delegator);
341
+ }
342
+ if (label) {
343
+ url.searchParams.set("label", label);
344
+ }
345
+ if (limit != null) {
346
+ url.searchParams.set("limit", limit.toString());
347
+ }
348
+ if (offset != null) {
349
+ url.searchParams.set("offset", offset.toString());
350
+ }
351
+ return sendRequest({
352
+ url: url.toString(),
353
+ method: "get" /* Get */
354
+ });
355
+ }
356
+ /**
357
+ * Adds a new delegate for a given Safe address.
358
+ *
359
+ * @param addSafeDelegateProps - The configuration of the new delegate
360
+ * @returns
361
+ * @throws "Invalid Safe delegate address"
362
+ * @throws "Invalid Safe delegator address"
363
+ * @throws "Invalid label"
364
+ * @throws "Checksum address validation failed"
365
+ * @throws "Address <delegate_address> is not checksumed"
366
+ * @throws "Safe=<safe_address> does not exist or it's still not indexed"
367
+ * @throws "Signing owner is not an owner of the Safe"
368
+ */
369
+ async addSafeDelegate({
370
+ safeAddress,
371
+ delegateAddress,
372
+ delegatorAddress,
373
+ label,
374
+ signer
375
+ }) {
376
+ if (delegateAddress === "") {
377
+ throw new Error("Invalid Safe delegate address");
378
+ }
379
+ if (delegatorAddress === "") {
380
+ throw new Error("Invalid Safe delegator address");
381
+ }
382
+ if (label === "") {
383
+ throw new Error("Invalid label");
384
+ }
385
+ const { address: delegate } = this.#getEip3770Address(delegateAddress);
386
+ const { address: delegator } = this.#getEip3770Address(delegatorAddress);
387
+ const signature = await signDelegate(signer, delegate, this.#chainId);
388
+ const body = {
389
+ safe: safeAddress ? this.#getEip3770Address(safeAddress).address : null,
390
+ delegate,
391
+ delegator,
392
+ label,
393
+ signature
394
+ };
395
+ return sendRequest({
396
+ url: `${this.#txServiceBaseUrl}/v2/delegates/`,
397
+ method: "post" /* Post */,
398
+ body
399
+ });
400
+ }
401
+ /**
402
+ * Removes a delegate for a given Safe address.
403
+ *
404
+ * @param deleteSafeDelegateProps - The configuration for the delegate that will be removed
405
+ * @returns
406
+ * @throws "Invalid Safe delegate address"
407
+ * @throws "Invalid Safe delegator address"
408
+ * @throws "Checksum address validation failed"
409
+ * @throws "Signing owner is not an owner of the Safe"
410
+ * @throws "Not found"
411
+ */
412
+ async removeSafeDelegate({
413
+ delegateAddress,
414
+ delegatorAddress,
415
+ signer
416
+ }) {
417
+ if (delegateAddress === "") {
418
+ throw new Error("Invalid Safe delegate address");
419
+ }
420
+ if (delegatorAddress === "") {
421
+ throw new Error("Invalid Safe delegator address");
422
+ }
423
+ const { address: delegate } = this.#getEip3770Address(delegateAddress);
424
+ const { address: delegator } = this.#getEip3770Address(delegatorAddress);
425
+ const signature = await signDelegate(signer, delegate, this.#chainId);
426
+ return sendRequest({
427
+ url: `${this.#txServiceBaseUrl}/v2/delegates/${delegate}`,
428
+ method: "delete" /* Delete */,
429
+ body: {
430
+ delegator,
431
+ signature
432
+ }
433
+ });
434
+ }
435
+ /**
436
+ * Returns the creation information of a Safe.
437
+ *
438
+ * @param safeAddress - The Safe address
439
+ * @returns The creation information of a Safe
440
+ * @throws "Invalid Safe address"
441
+ * @throws "Safe creation not found"
442
+ * @throws "Checksum address validation failed"
443
+ * @throws "Problem connecting to Ethereum network"
444
+ */
445
+ async getSafeCreationInfo(safeAddress) {
446
+ if (safeAddress === "") {
447
+ throw new Error("Invalid Safe address");
448
+ }
449
+ const { address } = this.#getEip3770Address(safeAddress);
450
+ return sendRequest({
451
+ url: `${this.#txServiceBaseUrl}/v1/safes/${address}/creation/`,
452
+ method: "get" /* Get */
453
+ });
454
+ }
455
+ /**
456
+ * Estimates the safeTxGas for a given Safe multi-signature transaction.
457
+ *
458
+ * @param safeAddress - The Safe address
459
+ * @param safeTransaction - The Safe transaction to estimate
460
+ * @returns The safeTxGas for the given Safe transaction
461
+ * @throws "Invalid Safe address"
462
+ * @throws "Data not valid"
463
+ * @throws "Safe not found"
464
+ * @throws "Tx not valid"
465
+ */
466
+ async estimateSafeTransaction(safeAddress, safeTransaction) {
467
+ if (safeAddress === "") {
468
+ throw new Error("Invalid Safe address");
469
+ }
470
+ const { address } = this.#getEip3770Address(safeAddress);
471
+ return sendRequest({
472
+ url: `${this.#txServiceBaseUrl}/v1/safes/${address}/multisig-transactions/estimations/`,
473
+ method: "post" /* Post */,
474
+ body: safeTransaction
475
+ });
476
+ }
477
+ /**
478
+ * Creates a new multi-signature transaction with its confirmations and stores it in the Safe Transaction Service.
479
+ *
480
+ * @param proposeTransactionConfig - The configuration of the proposed transaction
481
+ * @returns The hash of the Safe transaction proposed
482
+ * @throws "Invalid Safe address"
483
+ * @throws "Invalid safeTxHash"
484
+ * @throws "Invalid data"
485
+ * @throws "Invalid ethereum address/User is not an owner/Invalid signature/Nonce already executed/Sender is not an owner"
486
+ */
487
+ async proposeTransaction({
488
+ safeAddress,
489
+ safeTransactionData,
490
+ safeTxHash,
491
+ senderAddress,
492
+ senderSignature,
493
+ origin
494
+ }) {
495
+ if (safeAddress === "") {
496
+ throw new Error("Invalid Safe address");
497
+ }
498
+ const { address: safe } = this.#getEip3770Address(safeAddress);
499
+ const { address: sender } = this.#getEip3770Address(senderAddress);
500
+ if (safeTxHash === "") {
501
+ throw new Error("Invalid safeTxHash");
502
+ }
503
+ return sendRequest({
504
+ url: `${this.#txServiceBaseUrl}/v1/safes/${safe}/multisig-transactions/`,
505
+ method: "post" /* Post */,
506
+ body: {
507
+ ...safeTransactionData,
508
+ contractTransactionHash: safeTxHash,
509
+ sender,
510
+ signature: senderSignature,
511
+ origin
512
+ }
513
+ });
514
+ }
515
+ /**
516
+ * Returns the history of incoming transactions of a Safe account.
517
+ *
518
+ * @param safeAddress - The Safe address
519
+ * @returns The history of incoming transactions
520
+ * @throws "Invalid Safe address"
521
+ * @throws "Checksum address validation failed"
522
+ */
523
+ async getIncomingTransactions(safeAddress) {
524
+ if (safeAddress === "") {
525
+ throw new Error("Invalid Safe address");
526
+ }
527
+ const { address } = this.#getEip3770Address(safeAddress);
528
+ return sendRequest({
529
+ url: `${this.#txServiceBaseUrl}/v1/safes/${address}/incoming-transfers?executed=true`,
530
+ method: "get" /* Get */
531
+ });
532
+ }
533
+ /**
534
+ * Returns the history of module transactions of a Safe account.
535
+ *
536
+ * @param safeAddress - The Safe address
537
+ * @returns The history of module transactions
538
+ * @throws "Invalid Safe address"
539
+ * @throws "Invalid data"
540
+ * @throws "Invalid ethereum address"
541
+ */
542
+ async getModuleTransactions(safeAddress) {
543
+ if (safeAddress === "") {
544
+ throw new Error("Invalid Safe address");
545
+ }
546
+ const { address } = this.#getEip3770Address(safeAddress);
547
+ return sendRequest({
548
+ url: `${this.#txServiceBaseUrl}/v1/safes/${address}/module-transactions/`,
549
+ method: "get" /* Get */
550
+ });
551
+ }
552
+ /**
553
+ * Returns the history of multi-signature transactions of a Safe account.
554
+ *
555
+ * @param safeAddress - The Safe address
556
+ * @returns The history of multi-signature transactions
557
+ * @throws "Invalid Safe address"
558
+ * @throws "Checksum address validation failed"
559
+ */
560
+ async getMultisigTransactions(safeAddress) {
561
+ if (safeAddress === "") {
562
+ throw new Error("Invalid Safe address");
563
+ }
564
+ const { address } = this.#getEip3770Address(safeAddress);
565
+ return sendRequest({
566
+ url: `${this.#txServiceBaseUrl}/v1/safes/${address}/multisig-transactions/`,
567
+ method: "get" /* Get */
568
+ });
569
+ }
570
+ async getPendingTransactions(safeAddress, propsOrCurrentNonce = {}) {
571
+ if (safeAddress === "") {
572
+ throw new Error("Invalid Safe address");
573
+ }
574
+ let currentNonce;
575
+ let hasConfirmations;
576
+ let ordering;
577
+ let limit;
578
+ let offset;
579
+ if (typeof propsOrCurrentNonce === "object") {
580
+ ;
581
+ ({ currentNonce, hasConfirmations, ordering, limit, offset } = propsOrCurrentNonce);
582
+ } else {
583
+ console.warn(
584
+ "Deprecated: Use `currentNonce` inside an object instead. See `PendingTransactionsOptions`."
585
+ );
586
+ currentNonce = propsOrCurrentNonce;
587
+ }
588
+ const { address } = this.#getEip3770Address(safeAddress);
589
+ const nonce = currentNonce ? currentNonce : (await this.getSafeInfo(address)).nonce;
590
+ const url = new URL(
591
+ `${this.#txServiceBaseUrl}/v1/safes/${address}/multisig-transactions/?executed=false&nonce__gte=${nonce}`
592
+ );
593
+ if (hasConfirmations) {
594
+ url.searchParams.set("has_confirmations", hasConfirmations.toString());
595
+ }
596
+ if (ordering) {
597
+ url.searchParams.set("ordering", ordering);
598
+ }
599
+ if (limit != null) {
600
+ url.searchParams.set("limit", limit.toString());
601
+ }
602
+ if (offset != null) {
603
+ url.searchParams.set("offset", offset.toString());
604
+ }
605
+ return sendRequest({
606
+ url: url.toString(),
607
+ method: "get" /* Get */
608
+ });
609
+ }
610
+ /**
611
+ * Returns a list of transactions for a Safe. The list has different structures depending on the transaction type
612
+ *
613
+ * @param safeAddress - The Safe address
614
+ * @returns The list of transactions waiting for the confirmation of the Safe owners
615
+ * @throws "Invalid Safe address"
616
+ * @throws "Checksum address validation failed"
617
+ */
618
+ async getAllTransactions(safeAddress, options) {
619
+ if (safeAddress === "") {
620
+ throw new Error("Invalid Safe address");
621
+ }
622
+ const { address } = this.#getEip3770Address(safeAddress);
623
+ const url = new URL(`${this.#txServiceBaseUrl}/v1/safes/${address}/all-transactions/`);
624
+ const trusted = options?.trusted?.toString() || "true";
625
+ url.searchParams.set("trusted", trusted);
626
+ const queued = options?.queued?.toString() || "true";
627
+ url.searchParams.set("queued", queued);
628
+ const executed = options?.executed?.toString() || "false";
629
+ url.searchParams.set("executed", executed);
630
+ return sendRequest({
631
+ url: url.toString(),
632
+ method: "get" /* Get */
633
+ });
634
+ }
635
+ /**
636
+ * Returns the right nonce to propose a new transaction after the last pending transaction.
637
+ *
638
+ * @param safeAddress - The Safe address
639
+ * @returns The right nonce to propose a new transaction after the last pending transaction
640
+ * @throws "Invalid Safe address"
641
+ * @throws "Invalid data"
642
+ * @throws "Invalid ethereum address"
643
+ */
644
+ async getNextNonce(safeAddress) {
645
+ if (safeAddress === "") {
646
+ throw new Error("Invalid Safe address");
647
+ }
648
+ const { address } = this.#getEip3770Address(safeAddress);
649
+ const pendingTransactions = await this.getPendingTransactions(address);
650
+ if (pendingTransactions.results.length > 0) {
651
+ const nonces = pendingTransactions.results.map((tx) => tx.nonce);
652
+ const lastNonce = Math.max(...nonces);
653
+ return lastNonce + 1;
654
+ }
655
+ const safeInfo = await this.getSafeInfo(address);
656
+ return safeInfo.nonce;
657
+ }
658
+ /**
659
+ * Returns the list of all the ERC20 tokens handled by the Safe.
660
+ *
661
+ * @returns The list of all the ERC20 tokens
662
+ */
663
+ async getTokenList() {
664
+ return sendRequest({
665
+ url: `${this.#txServiceBaseUrl}/v1/tokens/`,
666
+ method: "get" /* Get */
667
+ });
668
+ }
669
+ /**
670
+ * Returns the information of a given ERC20 token.
671
+ *
672
+ * @param tokenAddress - The token address
673
+ * @returns The information of the given ERC20 token
674
+ * @throws "Invalid token address"
675
+ * @throws "Checksum address validation failed"
676
+ */
677
+ async getToken(tokenAddress) {
678
+ if (tokenAddress === "") {
679
+ throw new Error("Invalid token address");
680
+ }
681
+ const { address } = this.#getEip3770Address(tokenAddress);
682
+ return sendRequest({
683
+ url: `${this.#txServiceBaseUrl}/v1/tokens/${address}/`,
684
+ method: "get" /* Get */
685
+ });
686
+ }
687
+ /**
688
+ * Get a message by its safe message hash
689
+ * @param messageHash The Safe message hash
690
+ * @returns The message
691
+ */
692
+ async getMessage(messageHash) {
693
+ if (!messageHash) {
694
+ throw new Error("Invalid messageHash");
695
+ }
696
+ return sendRequest({
697
+ url: `${this.#txServiceBaseUrl}/v1/messages/${messageHash}/`,
698
+ method: "get" /* Get */
699
+ });
700
+ }
701
+ /**
702
+ * Get the list of messages associated to a Safe account
703
+ * @param safeAddress The safe address
704
+ * @param options The options to filter the list of messages
705
+ * @returns The paginated list of messages
706
+ */
707
+ async getMessages(safeAddress, { ordering, limit, offset } = {}) {
708
+ if (!this.#isValidAddress(safeAddress)) {
709
+ throw new Error("Invalid safeAddress");
710
+ }
711
+ const url = new URL(`${this.#txServiceBaseUrl}/v1/safes/${safeAddress}/messages/`);
712
+ if (ordering) {
713
+ url.searchParams.set("ordering", ordering);
714
+ }
715
+ if (limit != null) {
716
+ url.searchParams.set("limit", limit.toString());
717
+ }
718
+ if (offset != null) {
719
+ url.searchParams.set("offset", offset.toString());
720
+ }
721
+ return sendRequest({
722
+ url: url.toString(),
723
+ method: "get" /* Get */
724
+ });
725
+ }
726
+ /**
727
+ * Creates a new message with an initial signature
728
+ * Add more signatures from other owners using addMessageSignature()
729
+ * @param safeAddress The safe address
730
+ * @param options The raw message to add, signature and safeAppId if any
731
+ */
732
+ async addMessage(safeAddress, addMessageProps) {
733
+ if (!this.#isValidAddress(safeAddress)) {
734
+ throw new Error("Invalid safeAddress");
735
+ }
736
+ return sendRequest({
737
+ url: `${this.#txServiceBaseUrl}/v1/safes/${safeAddress}/messages/`,
738
+ method: "post" /* Post */,
739
+ body: addMessageProps
740
+ });
741
+ }
742
+ /**
743
+ * Add a signature to an existing message
744
+ * @param messageHash The safe message hash
745
+ * @param signature The signature
746
+ */
747
+ async addMessageSignature(messageHash, signature) {
748
+ if (!messageHash || !signature) {
749
+ throw new Error("Invalid messageHash or signature");
750
+ }
751
+ return sendRequest({
752
+ url: `${this.#txServiceBaseUrl}/v1/messages/${messageHash}/signatures/`,
753
+ method: "post" /* Post */,
754
+ body: {
755
+ signature
756
+ }
757
+ });
758
+ }
759
+ /**
760
+ * Get the SafeOperations that were sent from a particular address.
761
+ * @param getSafeOperationsProps - The parameters to filter the list of SafeOperations
762
+ * @throws "Safe address must not be empty"
763
+ * @throws "Invalid Ethereum address {safeAddress}"
764
+ * @returns The SafeOperations sent from the given Safe's address
765
+ */
766
+ async getSafeOperationsByAddress({
767
+ safeAddress,
768
+ ordering,
769
+ limit,
770
+ offset
771
+ }) {
772
+ if (!safeAddress) {
773
+ throw new Error("Safe address must not be empty");
774
+ }
775
+ const { address } = this.#getEip3770Address(safeAddress);
776
+ const url = new URL(`${this.#txServiceBaseUrl}/v1/safes/${address}/safe-operations/`);
777
+ if (ordering) {
778
+ url.searchParams.set("ordering", ordering);
779
+ }
780
+ if (limit != null) {
781
+ url.searchParams.set("limit", limit.toString());
782
+ }
783
+ if (offset != null) {
784
+ url.searchParams.set("offset", offset.toString());
785
+ }
786
+ return sendRequest({
787
+ url: url.toString(),
788
+ method: "get" /* Get */
789
+ });
790
+ }
791
+ /**
792
+ * Get a SafeOperation by its hash.
793
+ * @param safeOperationHash The SafeOperation hash
794
+ * @throws "SafeOperation hash must not be empty"
795
+ * @throws "Not found."
796
+ * @returns The SafeOperation
797
+ */
798
+ async getSafeOperation(safeOperationHash) {
799
+ if (!safeOperationHash) {
800
+ throw new Error("SafeOperation hash must not be empty");
801
+ }
802
+ return sendRequest({
803
+ url: `${this.#txServiceBaseUrl}/v1/safe-operations/${safeOperationHash}/`,
804
+ method: "get" /* Get */
805
+ });
806
+ }
807
+ /**
808
+ * Create a new 4337 SafeOperation for a Safe.
809
+ * @param addSafeOperationProps - The configuration of the SafeOperation
810
+ * @throws "Safe address must not be empty"
811
+ * @throws "Invalid Safe address {safeAddress}"
812
+ * @throws "Module address must not be empty"
813
+ * @throws "Invalid module address {moduleAddress}"
814
+ * @throws "Signature must not be empty"
815
+ */
816
+ async addSafeOperation(safeOperation) {
817
+ let safeAddress, moduleAddress;
818
+ let addSafeOperationProps;
819
+ if (isSafeOperation(safeOperation)) {
820
+ addSafeOperationProps = await getAddSafeOperationProps(safeOperation);
821
+ } else {
822
+ addSafeOperationProps = safeOperation;
823
+ }
824
+ const {
825
+ entryPoint,
826
+ moduleAddress: moduleAddressProp,
827
+ options,
828
+ safeAddress: safeAddressProp,
829
+ userOperation
830
+ } = addSafeOperationProps;
831
+ if (!safeAddressProp) {
832
+ throw new Error("Safe address must not be empty");
833
+ }
834
+ try {
835
+ safeAddress = this.#getEip3770Address(safeAddressProp).address;
836
+ } catch (err) {
837
+ throw new Error(`Invalid Safe address ${safeAddressProp}`);
838
+ }
839
+ if (!moduleAddressProp) {
840
+ throw new Error("Module address must not be empty");
841
+ }
842
+ try {
843
+ moduleAddress = this.#getEip3770Address(moduleAddressProp).address;
844
+ } catch (err) {
845
+ throw new Error(`Invalid module address ${moduleAddressProp}`);
846
+ }
847
+ if (isEmptyData(userOperation.signature)) {
848
+ throw new Error("Signature must not be empty");
849
+ }
850
+ const getISOString = (date) => !date ? null : new Date(date * 1e3).toISOString();
851
+ return sendRequest({
852
+ url: `${this.#txServiceBaseUrl}/v1/safes/${safeAddress}/safe-operations/`,
853
+ method: "post" /* Post */,
854
+ body: {
855
+ nonce: Number(userOperation.nonce),
856
+ initCode: isEmptyData(userOperation.initCode) ? null : userOperation.initCode,
857
+ callData: userOperation.callData,
858
+ callGasLimit: userOperation.callGasLimit.toString(),
859
+ verificationGasLimit: userOperation.verificationGasLimit.toString(),
860
+ preVerificationGas: userOperation.preVerificationGas.toString(),
861
+ maxFeePerGas: userOperation.maxFeePerGas.toString(),
862
+ maxPriorityFeePerGas: userOperation.maxPriorityFeePerGas.toString(),
863
+ paymasterAndData: isEmptyData(userOperation.paymasterAndData) ? null : userOperation.paymasterAndData,
864
+ entryPoint,
865
+ validAfter: getISOString(options?.validAfter),
866
+ validUntil: getISOString(options?.validUntil),
867
+ signature: userOperation.signature,
868
+ moduleAddress
869
+ }
870
+ });
871
+ }
872
+ /**
873
+ * Returns the list of confirmations for a given a SafeOperation.
874
+ *
875
+ * @param safeOperationHash - The hash of the SafeOperation to get confirmations for
876
+ * @param getSafeOperationConfirmationsOptions - Additional options for fetching the list of confirmations
877
+ * @returns The list of confirmations
878
+ * @throws "Invalid SafeOperation hash"
879
+ * @throws "Invalid data"
880
+ */
881
+ async getSafeOperationConfirmations(safeOperationHash, { limit, offset } = {}) {
882
+ if (!safeOperationHash) {
883
+ throw new Error("Invalid SafeOperation hash");
884
+ }
885
+ const url = new URL(
886
+ `${this.#txServiceBaseUrl}/v1/safe-operations/${safeOperationHash}/confirmations/`
887
+ );
888
+ if (limit != null) {
889
+ url.searchParams.set("limit", limit.toString());
890
+ }
891
+ if (offset != null) {
892
+ url.searchParams.set("offset", offset.toString());
893
+ }
894
+ return sendRequest({
895
+ url: url.toString(),
896
+ method: "get" /* Get */
897
+ });
898
+ }
899
+ /**
900
+ * Adds a confirmation for a SafeOperation.
901
+ *
902
+ * @param safeOperationHash The SafeOperation hash
903
+ * @param signature - Signature of the SafeOperation
904
+ * @returns
905
+ * @throws "Invalid SafeOperation hash"
906
+ * @throws "Invalid signature"
907
+ * @throws "Malformed data"
908
+ * @throws "Error processing data"
909
+ */
910
+ async confirmSafeOperation(safeOperationHash, signature) {
911
+ if (!safeOperationHash) {
912
+ throw new Error("Invalid SafeOperation hash");
913
+ }
914
+ if (!signature) {
915
+ throw new Error("Invalid signature");
916
+ }
917
+ return sendRequest({
918
+ url: `${this.#txServiceBaseUrl}/v1/safe-operations/${safeOperationHash}/confirmations/`,
919
+ method: "post" /* Post */,
920
+ body: { signature }
921
+ });
922
+ }
923
+ };
924
+ var SafeApiKit_default = SafeApiKit;
925
+
926
+ // src/index.ts
927
+ var src_default = SafeApiKit_default;
928
+ export {
929
+ src_default as default
930
+ };