-
Notifications
You must be signed in to change notification settings - Fork 23
/
index.ts
705 lines (647 loc) · 19.8 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
import { deleteEndpoint, getEndpoint, postEndpoint, putEndpoint } from './endpoint'
import type { operations } from './types/api'
import type {
SafeTransactionEstimation,
TransactionDetails,
TransactionListPage,
SafeIncomingTransfersResponse,
SafeModuleTransactionsResponse,
SafeMultisigTransactionsResponse,
NoncesResponse,
} from './types/transactions'
import type {
EthereumAddress,
AllOwnedSafes,
FiatCurrencies,
OwnedSafes,
SafeBalanceResponse,
SafeCollectibleResponse,
SafeCollectiblesPage,
} from './types/common'
import type { SafeInfo, SafeOverview } from './types/safe-info'
import type { ChainListResponse, ChainInfo } from './types/chains'
import type { SafeAppsResponse } from './types/safe-apps'
import type { MasterCopyReponse } from './types/master-copies'
import type { AnyConfirmationView, DecodedDataResponse } from './types/decoded-data'
import type { SafeMessage, SafeMessageListPage } from './types/safe-messages'
import { DEFAULT_BASE_URL } from './config'
import type { DelegateResponse, DelegatesRequest } from './types/delegates'
import type { GetEmailResponse } from './types/emails'
import type { RelayCountResponse, RelayTransactionResponse } from './types/relay'
import type { Contract } from './types/contracts'
import type { AuthNonce } from './types/auth'
export * from './types/safe-info'
export * from './types/safe-apps'
export * from './types/transactions'
export * from './types/chains'
export * from './types/common'
export * from './types/master-copies'
export * from './types/decoded-data'
export * from './types/safe-messages'
export * from './types/notifications'
export * from './types/relay'
// Can be set externally to a different CGW host
let baseUrl: string = DEFAULT_BASE_URL
/**
* Set the base CGW URL
*/
export const setBaseUrl = (url: string): void => {
baseUrl = url
}
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
/**
* Relay a transaction from a Safe
*/
export function relayTransaction(
chainId: string,
body: operations['relay_transaction']['parameters']['body'],
): Promise<RelayTransactionResponse> {
return postEndpoint(baseUrl, '/v1/chains/{chainId}/relay', { path: { chainId }, body })
}
/**
* Get the relay limit and number of remaining relays remaining
*/
export function getRelayCount(chainId: string, address: string): Promise<RelayCountResponse> {
return getEndpoint(baseUrl, '/v1/chains/{chainId}/relay/{address}', { path: { chainId, address } })
}
/**
* Get basic information about a Safe. E.g. owners, modules, version etc
*/
export function getSafeInfo(chainId: string, address: string): Promise<SafeInfo> {
return getEndpoint(baseUrl, '/v1/chains/{chainId}/safes/{address}', { path: { chainId, address } })
}
/**
* Get filterable list of incoming transactions
*/
export function getIncomingTransfers(
chainId: string,
address: string,
query?: operations['incoming_transfers']['parameters']['query'],
pageUrl?: string,
): Promise<SafeIncomingTransfersResponse> {
return getEndpoint(
baseUrl,
'/v1/chains/{chainId}/safes/{address}/incoming-transfers/',
{
path: { chainId, address },
query,
},
pageUrl,
)
}
/**
* Get filterable list of module transactions
*/
export function getModuleTransactions(
chainId: string,
address: string,
query?: operations['module_transactions']['parameters']['query'],
pageUrl?: string,
): Promise<SafeModuleTransactionsResponse> {
return getEndpoint(
baseUrl,
'/v1/chains/{chainId}/safes/{address}/module-transactions/',
{
path: { chainId, address },
query,
},
pageUrl,
)
}
/**
* Get filterable list of multisig transactions
*/
export function getMultisigTransactions(
chainId: string,
address: string,
query?: operations['multisig_transactions']['parameters']['query'],
pageUrl?: string,
): Promise<SafeMultisigTransactionsResponse> {
return getEndpoint(
baseUrl,
'/v1/chains/{chainId}/safes/{address}/multisig-transactions/',
{
path: { chainId, address },
query,
},
pageUrl,
)
}
/**
* Get the total balance and all assets stored in a Safe
*/
export function getBalances(
chainId: string,
address: string,
currency = 'usd',
query: operations['safes_balances_list']['parameters']['query'] = {},
): Promise<SafeBalanceResponse> {
return getEndpoint(baseUrl, '/v1/chains/{chainId}/safes/{address}/balances/{currency}', {
path: { chainId, address, currency },
query,
})
}
/**
* Get a list of supported fiat currencies (e.g. USD, EUR etc)
*/
export function getFiatCurrencies(): Promise<FiatCurrencies> {
return getEndpoint(baseUrl, '/v1/balances/supported-fiat-codes')
}
/**
* Get the addresses of all Safes belonging to an owner
*/
export function getOwnedSafes(chainId: string, address: string): Promise<OwnedSafes> {
return getEndpoint(baseUrl, '/v1/chains/{chainId}/owners/{address}/safes', { path: { chainId, address } })
}
/**
* Get the addresses of all Safes belonging to an owner on all chains
*/
export function getAllOwnedSafes(address: string): Promise<AllOwnedSafes> {
return getEndpoint(baseUrl, '/v1/owners/{address}/safes', { path: { address } })
}
/**
* Get NFTs stored in a Safe
*/
export function getCollectibles(
chainId: string,
address: string,
query: operations['safes_collectibles_list']['parameters']['query'] = {},
): Promise<SafeCollectibleResponse[]> {
return getEndpoint(baseUrl, '/v1/chains/{chainId}/safes/{address}/collectibles', {
path: { chainId, address },
query,
})
}
/**
* Get NFTs stored in a Safe
*/
export function getCollectiblesPage(
chainId: string,
address: string,
query: operations['safes_collectibles_list_paginated']['parameters']['query'] = {},
pageUrl?: string,
): Promise<SafeCollectiblesPage> {
return getEndpoint(
baseUrl,
'/v2/chains/{chainId}/safes/{address}/collectibles',
{ path: { chainId, address }, query },
pageUrl,
)
}
/**
* Get a list of past Safe transactions
*/
export function getTransactionHistory(
chainId: string,
address: string,
query: operations['history_transactions']['parameters']['query'] = {},
pageUrl?: string,
): Promise<TransactionListPage> {
return getEndpoint(
baseUrl,
'/v1/chains/{chainId}/safes/{safe_address}/transactions/history',
{ path: { chainId, safe_address: address }, query },
pageUrl,
)
}
/**
* Get the list of pending transactions
*/
export function getTransactionQueue(
chainId: string,
address: string,
query: operations['queued_transactions']['parameters']['query'] = {},
pageUrl?: string,
): Promise<TransactionListPage> {
return getEndpoint(
baseUrl,
'/v1/chains/{chainId}/safes/{safe_address}/transactions/queued',
{ path: { chainId, safe_address: address }, query },
pageUrl,
)
}
/**
* Get the details of an individual transaction by its id
*/
export function getTransactionDetails(chainId: string, transactionId: string): Promise<TransactionDetails> {
return getEndpoint(baseUrl, '/v1/chains/{chainId}/transactions/{transactionId}', {
path: { chainId, transactionId },
})
}
/**
* Delete a transaction by its safeTxHash
*/
export function deleteTransaction(
chainId: string,
safeTxHash: string,
signature: operations['delete_transaction']['parameters']['body']['signature'],
): Promise<void> {
return deleteEndpoint(baseUrl, '/v1/chains/{chainId}/transactions/{safeTxHash}', {
path: { chainId, safeTxHash },
body: { signature },
})
}
/**
* Request a gas estimate & recommmended tx nonce for a created transaction
*/
export function postSafeGasEstimation(
chainId: string,
address: string,
body: operations['post_safe_gas_estimation']['parameters']['body'],
): Promise<SafeTransactionEstimation> {
return postEndpoint(baseUrl, '/v2/chains/{chainId}/safes/{safe_address}/multisig-transactions/estimations', {
path: { chainId, safe_address: address },
body,
})
}
export function getNonces(chainId: string, address: string): Promise<NoncesResponse> {
return getEndpoint(baseUrl, '/v1/chains/{chainId}/safes/{safe_address}/nonces', {
path: { chainId, safe_address: address },
})
}
/**
* Propose a new transaction for other owners to sign/execute
*/
export function proposeTransaction(
chainId: string,
address: string,
body: operations['propose_transaction']['parameters']['body'],
): Promise<TransactionDetails> {
return postEndpoint(baseUrl, '/v1/chains/{chainId}/transactions/{safe_address}/propose', {
path: { chainId, safe_address: address },
body,
})
}
/**
* Returns decoded data
*/
export function getConfirmationView(
chainId: string,
safeAddress: string,
data: operations['data_decoder']['parameters']['body']['data'],
to?: operations['data_decoder']['parameters']['body']['to'],
value?: operations['data_decoder']['parameters']['body']['value'],
): Promise<AnyConfirmationView> {
return postEndpoint(baseUrl, '/v1/chains/{chainId}/safes/{safe_address}/views/transaction-confirmation', {
path: { chainId, safe_address: safeAddress },
body: { data, to, value },
})
}
/**
* Returns all defined chain configs
*/
export function getChainsConfig(query?: operations['chains_list']['parameters']['query']): Promise<ChainListResponse> {
return getEndpoint(baseUrl, '/v1/chains', {
query,
})
}
/**
* Returns a chain config
*/
export function getChainConfig(chainId: string): Promise<ChainInfo> {
return getEndpoint(baseUrl, '/v1/chains/{chainId}', {
path: { chainId: chainId },
})
}
/**
* Returns Safe Apps List
*/
export function getSafeApps(
chainId: string,
query: operations['safe_apps_read']['parameters']['query'] = {},
): Promise<SafeAppsResponse> {
return getEndpoint(baseUrl, '/v1/chains/{chainId}/safe-apps', {
path: { chainId: chainId },
query,
})
}
/**
* Returns list of Master Copies
*/
export function getMasterCopies(chainId: string): Promise<MasterCopyReponse> {
return getEndpoint(baseUrl, '/v1/chains/{chainId}/about/master-copies', {
path: { chainId: chainId },
})
}
/**
* Returns decoded data
*/
export function getDecodedData(
chainId: string,
encodedData: operations['data_decoder']['parameters']['body']['data'],
to?: operations['data_decoder']['parameters']['body']['to'],
): Promise<DecodedDataResponse> {
return postEndpoint(baseUrl, '/v1/chains/{chainId}/data-decoder', {
path: { chainId: chainId },
body: { data: encodedData, to },
})
}
/**
* Returns list of `SafeMessage`s
*/
export function getSafeMessages(chainId: string, address: string, pageUrl?: string): Promise<SafeMessageListPage> {
return getEndpoint(
baseUrl,
'/v1/chains/{chainId}/safes/{safe_address}/messages',
{ path: { chainId, safe_address: address }, query: {} },
pageUrl,
)
}
/**
* Returns a `SafeMessage`
*/
export function getSafeMessage(chainId: string, messageHash: string): Promise<Omit<SafeMessage, 'type'>> {
return getEndpoint(baseUrl, '/v1/chains/{chainId}/messages/{message_hash}', {
path: { chainId, message_hash: messageHash },
})
}
/**
* Propose a new `SafeMessage` for other owners to sign
*/
export function proposeSafeMessage(
chainId: string,
address: string,
body: operations['propose_safe_message']['parameters']['body'],
): Promise<void> {
return postEndpoint(baseUrl, '/v1/chains/{chainId}/safes/{safe_address}/messages', {
path: { chainId, safe_address: address },
body,
})
}
/**
* Add a confirmation to a `SafeMessage`
*/
export function confirmSafeMessage(
chainId: string,
messageHash: string,
body: operations['confirm_safe_message']['parameters']['body'],
): Promise<void> {
return postEndpoint(baseUrl, '/v1/chains/{chainId}/messages/{message_hash}/signatures', {
path: { chainId, message_hash: messageHash },
body,
})
}
/**
* Returns a list of delegates
*/
export function getDelegates(chainId: string, query: DelegatesRequest = {}): Promise<DelegateResponse> {
return getEndpoint(baseUrl, '/v2/chains/{chainId}/delegates', {
path: { chainId },
query,
})
}
/**
* Registers a device/Safe for notifications
*/
export function registerDevice(body: operations['register_device']['parameters']['body']): Promise<void> {
return postEndpoint(baseUrl, '/v1/register/notifications', {
body,
})
}
/**
* Unregisters a Safe from notifications
*/
export function unregisterSafe(chainId: string, address: string, uuid: string): Promise<void> {
return deleteEndpoint(baseUrl, '/v1/chains/{chainId}/notifications/devices/{uuid}/safes/{safe_address}', {
path: { chainId, safe_address: address, uuid },
})
}
/**
* Unregisters a device from notifications
*/
export function unregisterDevice(chainId: string, uuid: string): Promise<void> {
return deleteEndpoint(baseUrl, '/v1/chains/{chainId}/notifications/devices/{uuid}', {
path: { chainId, uuid },
})
}
/**
* Registers a email address for a safe signer.
*
* The signer wallet has to sign a message of format: `email-register-{chainId}-{safeAddress}-{emailAddress}-{signer}-{timestamp}`
* The signature is valid for 5 minutes.
*
* @param chainId
* @param safeAddress
* @param body Signer address and email address
* @param headers Signature and Signature timestamp
* @returns 200 if signature matches the data
*/
export function registerEmail(
chainId: string,
safeAddress: string,
body: operations['register_email']['parameters']['body'],
headers: operations['register_email']['parameters']['headers'],
): Promise<void> {
return postEndpoint(baseUrl, '/v1/chains/{chainId}/safes/{safe_address}/emails', {
path: { chainId, safe_address: safeAddress },
body,
headers,
})
}
/**
* Changes an already registered email address for a safe signer. The new email address still needs to be verified.
*
* The signer wallet has to sign a message of format: `email-edit-{chainId}-{safeAddress}-{emailAddress}-{signer}-{timestamp}`
* The signature is valid for 5 minutes.
*
* @param chainId
* @param safeAddress
* @param signerAddress
* @param body New email address
* @param headers Signature and Signature timestamp
* @returns 202 if signature matches the data
*/
export function changeEmail(
chainId: string,
safeAddress: string,
signerAddress: string,
body: operations['change_email']['parameters']['body'],
headers: operations['change_email']['parameters']['headers'],
): Promise<void> {
return putEndpoint(baseUrl, '/v1/chains/{chainId}/safes/{safe_address}/emails/{signer}', {
path: { chainId, safe_address: safeAddress, signer: signerAddress },
body,
headers,
})
}
/**
* Resends an email verification code.
*/
export function resendEmailVerificationCode(
chainId: string,
safeAddress: string,
signerAddress: string,
): Promise<void> {
return postEndpoint(baseUrl, '/v1/chains/{chainId}/safes/{safe_address}/emails/{signer}/verify-resend', {
path: { chainId, safe_address: safeAddress, signer: signerAddress },
body: '',
})
}
/**
* Verifies a pending email address registration.
*
* @param chainId
* @param safeAddress
* @param signerAddress address who signed the email registration
* @param body Verification code
*/
export function verifyEmail(
chainId: string,
safeAddress: string,
signerAddress: string,
body: operations['verify_email']['parameters']['body'],
): Promise<void> {
return putEndpoint(baseUrl, '/v1/chains/{chainId}/safes/{safe_address}/emails/{signer}/verify', {
path: { chainId, safe_address: safeAddress, signer: signerAddress },
body,
})
}
/**
* Gets the registered email address of the signer
*
* The signer wallet will have to sign a message of format: `email-retrieval-{chainId}-{safe}-{signer}-{timestamp}`
* The signature is valid for 5 minutes.
*
* @param chainId
* @param safeAddress
* @param signerAddress address of the owner of the Safe
*
* @returns email address and verified flag
*/
export function getRegisteredEmail(
chainId: string,
safeAddress: string,
signerAddress: string,
headers: operations['get_email']['parameters']['headers'],
): Promise<GetEmailResponse> {
return getEndpoint(baseUrl, '/v1/chains/{chainId}/safes/{safe_address}/emails/{signer}', {
path: { chainId, safe_address: safeAddress, signer: signerAddress },
headers,
})
}
/**
* Delete a registered email address for the signer
*
* The signer wallet will have to sign a message of format: `email-delete-{chainId}-{safe}-{signer}-{timestamp}`
* The signature is valid for 5 minutes.
*
* @param chainId
* @param safeAddress
* @param signerAddress
* @param headers
*/
export function deleteRegisteredEmail(
chainId: string,
safeAddress: string,
signerAddress: string,
headers: operations['delete_email']['parameters']['headers'],
): Promise<void> {
return deleteEndpoint(baseUrl, '/v1/chains/{chainId}/safes/{safe_address}/emails/{signer}', {
path: { chainId, safe_address: safeAddress, signer: signerAddress },
headers,
})
}
/**
* Register a recovery module for receiving alerts
* @param chainId
* @param safeAddress
* @param body - { moduleAddress: string }
*/
export function registerRecoveryModule(
chainId: string,
safeAddress: string,
body: operations['register_recovery_module']['parameters']['body'],
): Promise<void> {
return postEndpoint(baseUrl, '/v1/chains/{chainId}/safes/{safe_address}/recovery', {
path: { chainId, safe_address: safeAddress },
body,
})
}
/**
* Delete email subscription for a single category
* @param query
*/
export function unsubscribeSingle(query: operations['unsubscribe_single']['parameters']['query']) {
return deleteEndpoint(baseUrl, '/v1/subscriptions', { query })
}
/**
* Delete email subscription for all categories
* @param query
*/
export function unsubscribeAll(query: operations['unsubscribe_all']['parameters']['query']) {
return deleteEndpoint(baseUrl, '/v1/subscriptions/all', { query })
}
/**
* Get Safe overviews per address
*/
export function getSafeOverviews(
safes: `${number}:${EthereumAddress}`[],
query: Omit<operations['SafesController_getSafeOverview']['parameters']['query'], 'safes'>,
): Promise<SafeOverview[]> {
return getEndpoint(baseUrl, '/v1/safes', {
query: {
...query,
safes: safes.join(','),
},
})
}
export function getContract(chainId: string, contractAddress: string): Promise<Contract> {
return getEndpoint(baseUrl, '/v1/chains/{chainId}/contracts/{contractAddress}', {
path: {
chainId: chainId,
contractAddress: contractAddress,
},
})
}
export function getAuthNonce(): Promise<AuthNonce> {
return getEndpoint(baseUrl, '/v1/auth/nonce', { credentials: 'include' })
}
export function verifyAuth(body: operations['verify_auth']['parameters']['body']) {
return postEndpoint(baseUrl, '/v1/auth/verify', {
body,
credentials: 'include',
})
}
export function createAccount(body: operations['create_account']['parameters']['body']) {
return postEndpoint(baseUrl, '/v1/accounts', {
body,
credentials: 'include',
})
}
export function getAccount(address: string) {
return getEndpoint(baseUrl, '/v1/accounts/{address}', {
path: { address },
credentials: 'include',
})
}
export function deleteAccount(address: string) {
return deleteEndpoint(baseUrl, '/v1/accounts/{address}', {
path: { address },
credentials: 'include',
})
}
export function getAccountDataTypes() {
return getEndpoint(baseUrl, '/v1/accounts/data-types')
}
export function getAccountDataSettings(address: string) {
return getEndpoint(baseUrl, '/v1/accounts/{address}/data-settings', {
path: { address },
credentials: 'include',
})
}
export function putAccountDataSettings(
address: string,
body: operations['put_account_data_settings']['parameters']['body'],
) {
return putEndpoint(baseUrl, '/v1/accounts/{address}/data-settings', {
path: { address },
body,
credentials: 'include',
})
}
export function getIndexingStatus(chainId: string) {
return getEndpoint(baseUrl, '/v1/chains/{chainId}/about/indexing', {
path: { chainId },
})
}
/* eslint-enable @typescript-eslint/explicit-module-boundary-types */