@swapkit/helpers 2.5.0 → 3.0.0-beta.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 (106) hide show
  1. package/dist/api/index.cjs +3 -0
  2. package/dist/api/index.cjs.map +16 -0
  3. package/dist/api/index.js +3 -0
  4. package/dist/api/index.js.map +16 -0
  5. package/dist/chunk-fazw0jvt.js +3 -0
  6. package/dist/chunk-fazw0jvt.js.map +9 -0
  7. package/dist/chunk-nm00d52v.js +4 -0
  8. package/dist/chunk-nm00d52v.js.map +9 -0
  9. package/dist/contracts/index.cjs +3 -0
  10. package/dist/contracts/index.cjs.map +38 -0
  11. package/dist/contracts/index.js +3 -0
  12. package/dist/contracts/index.js.map +38 -0
  13. package/dist/index.cjs +6 -0
  14. package/dist/index.cjs.map +29 -0
  15. package/dist/index.js +3 -3
  16. package/dist/index.js.map +19 -20
  17. package/dist/tokens/index.cjs +3 -0
  18. package/dist/tokens/index.cjs.map +23 -0
  19. package/dist/tokens/index.js +3 -0
  20. package/dist/tokens/index.js.map +23 -0
  21. package/package.json +31 -15
  22. package/src/api/index.ts +19 -0
  23. package/src/api/microgard/endpoints.ts +83 -0
  24. package/src/api/microgard/types.ts +60 -0
  25. package/src/api/midgard/endpoints.ts +166 -0
  26. package/src/api/midgard/types.ts +155 -0
  27. package/src/api/swapkitApi/endpoints.ts +266 -0
  28. package/src/api/swapkitApi/types.ts +772 -0
  29. package/src/api/thornode/endpoints.ts +78 -0
  30. package/src/api/thornode/types.ts +241 -0
  31. package/src/api/thorswapStatic/endpoints.ts +30 -0
  32. package/src/api/thorswapStatic/types.ts +18 -0
  33. package/src/contracts/abis/avaxGeneric.ts +92 -0
  34. package/src/contracts/abis/avaxWoofi.ts +145 -0
  35. package/src/contracts/abis/bscGeneric.ts +106 -0
  36. package/src/contracts/abis/chainflipGateway.ts +330 -0
  37. package/src/contracts/abis/erc20.ts +99 -0
  38. package/src/contracts/abis/ethGeneric.ts +92 -0
  39. package/src/contracts/abis/mayaEvmVaults.ts +331 -0
  40. package/src/contracts/abis/pancakeV2.ts +145 -0
  41. package/src/contracts/abis/pangolin.ts +120 -0
  42. package/src/contracts/abis/sushiswap.ts +120 -0
  43. package/src/contracts/abis/tcEthVault.ts +650 -0
  44. package/src/contracts/abis/traderJoe.ts +120 -0
  45. package/src/contracts/abis/uniswapV2.ts +120 -0
  46. package/src/contracts/abis/uniswapV2Leg.ts +128 -0
  47. package/src/contracts/abis/uniswapV3_100.ts +128 -0
  48. package/src/contracts/abis/uniswapV3_10000.ts +128 -0
  49. package/src/contracts/abis/uniswapV3_3000.ts +128 -0
  50. package/src/contracts/abis/uniswapV3_500.ts +128 -0
  51. package/src/contracts/index.ts +95 -0
  52. package/src/contracts/routers/index.ts +58 -0
  53. package/src/contracts/routers/kyber.ts +402 -0
  54. package/src/contracts/routers/oneinch.ts +2188 -0
  55. package/src/contracts/routers/pancakeswap.ts +340 -0
  56. package/src/contracts/routers/pangolin.ts +340 -0
  57. package/src/contracts/routers/sushiswap.ts +340 -0
  58. package/src/contracts/routers/traderJoe.ts +340 -0
  59. package/src/contracts/routers/uniswapv2.ts +340 -0
  60. package/src/contracts/routers/uniswapv3.ts +254 -0
  61. package/src/contracts/routers/woofi.ts +171 -0
  62. package/src/index.ts +11 -12
  63. package/src/modules/__tests__/assetValue.test.ts +0 -2
  64. package/src/modules/__tests__/swapKitConfig.test.ts +37 -0
  65. package/src/modules/assetValue.ts +26 -92
  66. package/src/modules/bigIntArithmetics.ts +6 -2
  67. package/src/modules/requestClient.ts +26 -35
  68. package/src/modules/swapKitConfig.ts +131 -0
  69. package/src/modules/swapKitError.ts +6 -6
  70. package/src/tokens/index.ts +15 -0
  71. package/src/tokens/lists/camelot_v3.ts +15920 -0
  72. package/src/tokens/lists/caviar_v1.ts +1694 -0
  73. package/src/tokens/lists/chainflip.ts +104 -0
  74. package/src/tokens/lists/index.ts +13 -0
  75. package/src/tokens/lists/jupiter.ts +29606 -0
  76. package/src/tokens/lists/mayachain.ts +513 -0
  77. package/src/tokens/lists/oneinch.ts +14238 -0
  78. package/src/tokens/lists/openocean_v2.ts +11514 -0
  79. package/src/tokens/lists/pancakeswap.ts +4296 -0
  80. package/src/tokens/lists/pangolin_v1.ts +175 -0
  81. package/src/tokens/lists/sushiswap_v2.ts +965 -0
  82. package/src/tokens/lists/thorchain.ts +669 -0
  83. package/src/tokens/lists/traderjoe_v2.ts +1384 -0
  84. package/src/tokens/lists/uniswap_v2.ts +5596 -0
  85. package/src/tokens/lists/uniswap_v3.ts +5946 -0
  86. package/src/types/chains.ts +26 -46
  87. package/src/types/commonTypes.ts +3 -90
  88. package/src/types/index.ts +3 -4
  89. package/src/types/sdk.ts +0 -30
  90. package/src/types/tokens.ts +2 -2
  91. package/src/types/wallet.ts +37 -21
  92. package/src/{helpers → utils}/asset.ts +49 -19
  93. package/src/{helpers → utils}/derivationPath.ts +7 -2
  94. package/src/{helpers → utils}/memo.ts +0 -65
  95. package/src/{helpers → utils}/others.ts +12 -5
  96. package/src/utils/plugin.ts +13 -0
  97. package/src/{helpers/web3wallets.ts → utils/wallets.ts} +175 -139
  98. package/src/modules/walletUtils.ts +0 -30
  99. package/src/types/network.ts +0 -49
  100. /package/src/{helpers → utils}/__tests__/asset.test.ts +0 -0
  101. /package/src/{helpers/__tests__/derivationPath.ts → utils/__tests__/derivationPath.test.ts} +0 -0
  102. /package/src/{helpers → utils}/__tests__/memo.test.ts +0 -0
  103. /package/src/{helpers → utils}/__tests__/others.test.ts +0 -0
  104. /package/src/{helpers → utils}/__tests__/validators.test.ts +0 -0
  105. /package/src/{helpers → utils}/liquidity.ts +0 -0
  106. /package/src/{helpers → utils}/validators.ts +0 -0
@@ -0,0 +1,772 @@
1
+ import {
2
+ type AssetValue,
3
+ Chain,
4
+ ChainId,
5
+ ErrorCode,
6
+ FeeTypeEnum,
7
+ ProviderName,
8
+ WarningCodeEnum,
9
+ } from "@swapkit/helpers";
10
+ import { z } from "zod";
11
+
12
+ export enum PriorityLabel {
13
+ CHEAPEST = "CHEAPEST",
14
+ FASTEST = "FASTEST",
15
+ RECOMMENDED = "RECOMMENDED",
16
+ }
17
+
18
+ export enum RouteQuoteTxType {
19
+ PSBT = "PSBT",
20
+ EVM = "EVM",
21
+ COSMOS = "COSMOS",
22
+ RADIX = "RADIX",
23
+ }
24
+
25
+ export enum TxnType {
26
+ native_send = "native_send", // native send, msgSend, etc.
27
+ token_transfer = "token_transfer", // token transfer
28
+ native_contract_call = "native_contract_call", // native contract call
29
+ token_contract_call = "token_contract_call", // token contract call
30
+ approve = "approve", // token approve
31
+ deposit = "deposit", // msgDeposit and related cosmos operations, not deposit to vault or deposit contract name
32
+ thorname_action = "thorname_action", // should we use this or msgDeposit?
33
+ lp_action = "lp_action", // deposit to an evm pool, tc pool, etc.
34
+ swap = "swap", // any kind of operations that involves swapping assets
35
+ streaming_swap = "streaming_swap", // streaming swap
36
+ stake = "stake", // defi operations like $vthor and other types of staking
37
+ claim = "claim", // claim rewards, claim tokens, etc.
38
+ lending = "lending", // lending operations
39
+ unknown = "unknown",
40
+ }
41
+
42
+ // transaction status devoid of any business logic
43
+ export enum TxnStatus {
44
+ unknown = "unknown",
45
+ not_started = "not_started",
46
+ pending = "pending",
47
+ swappping = "swapping",
48
+ completed = "completed",
49
+ }
50
+
51
+ export enum TrackingStatus {
52
+ not_started = "not_started",
53
+ starting = "starting", // first status once we receive, old or new transaction
54
+ broadcasted = "broadcasted",
55
+ mempool = "mempool", // or indexing
56
+ inbound = "inbound",
57
+ outbound = "outbound",
58
+ swapping = "swapping", // more generic than streaming
59
+ completed = "completed",
60
+ refunded = "refunded",
61
+ partially_refunded = "partially_refunded",
62
+ dropped = "dropped",
63
+ reverted = "reverted",
64
+ replaced = "replaced",
65
+ retries_exceeded = "retries_exceeded",
66
+ parsing_error = "parsing_error",
67
+ }
68
+
69
+ type TokenProviderVersion = {
70
+ major: number;
71
+ minor: number;
72
+ patch: number;
73
+ };
74
+
75
+ export type TokenListProvidersResponse = Array<{
76
+ provider: ProviderName;
77
+ name: string;
78
+ timestamp: string;
79
+ version: TokenProviderVersion;
80
+ keywords: string[];
81
+ count: number;
82
+ url: string;
83
+ }>;
84
+
85
+ export type TokensResponseV2 = {
86
+ chainId: ChainId;
87
+ count: number;
88
+ keywords: string[];
89
+ name: string;
90
+ provider: ProviderName;
91
+ timestamp: string;
92
+ tokens: TokenV2[];
93
+ version: TokenProviderVersion;
94
+ };
95
+
96
+ export type TokenV2 = {
97
+ address?: string;
98
+ chain: string;
99
+ shortCode?: string;
100
+ chainId: string;
101
+ decimals: number;
102
+ extensions?: {};
103
+ identifier: string;
104
+ logoURI: string;
105
+ name?: string;
106
+ symbol: string;
107
+ ticker: string;
108
+ };
109
+
110
+ export interface TransactionProps {
111
+ chainId: ChainId;
112
+ hash: string;
113
+ block: number;
114
+ type?: TxnType;
115
+ status?: TxnStatus;
116
+ trackingStatus?: TrackingStatus;
117
+ fromAsset: AssetValue | null;
118
+ fromAddress: string;
119
+ toAsset: AssetValue | null;
120
+ toAddress: string;
121
+ finalisedAt?: number;
122
+ meta?: Partial<TxnMeta>;
123
+ payload?: Partial<TxnPayload>;
124
+ }
125
+
126
+ export type TrackerParams = {
127
+ chainId: ChainId;
128
+ hash: string;
129
+ block?: number;
130
+ };
131
+
132
+ export type TrackerResponse = TransactionProps & {
133
+ legs: TransactionLegDTO[];
134
+ transient?: TxnTransient;
135
+ };
136
+
137
+ export const ApiV2ErrorSchema = z.object({
138
+ error: z.string(),
139
+ message: z.string(),
140
+ });
141
+
142
+ export const AssetValueSchema = z.object({
143
+ chain: z.nativeEnum(Chain),
144
+ symbol: z.string(),
145
+ ticker: z.string(),
146
+ decimal: z.optional(z.number()),
147
+ address: z.optional(z.string()),
148
+ isGasAsset: z.boolean(),
149
+ isSynthetic: z.boolean(),
150
+ tax: z.optional(
151
+ z.object({
152
+ buy: z.number(),
153
+ sell: z.number(),
154
+ }),
155
+ ),
156
+ });
157
+
158
+ export const TokenDetailsMetadataSchema = z.object({
159
+ name: z.string(),
160
+ id: z.string(),
161
+ market_cap: z.number(),
162
+ total_volume: z.number(),
163
+ price_change_24h_usd: z.number(),
164
+ price_change_percentage_24h_usd: z.number(),
165
+ timestamp: z.string(),
166
+ sparkline_in_7d: z.array(z.number()),
167
+ });
168
+
169
+ export const PriceResponseSchema = z.array(
170
+ z
171
+ .object({
172
+ identifier: z.string(),
173
+ provider: z.string(),
174
+ cg: TokenDetailsMetadataSchema.optional(),
175
+ price_usd: z.number(),
176
+ timestamp: z.number(),
177
+ })
178
+ .partial(),
179
+ );
180
+
181
+ export type PriceResponse = z.infer<typeof PriceResponseSchema>;
182
+
183
+ export const QuoteRequestSchema = z
184
+ .object({
185
+ sellAsset: z.string({
186
+ description: "Asset to sell",
187
+ }),
188
+ buyAsset: z.string({
189
+ description: "Asset to buy",
190
+ }),
191
+ sellAmount: z
192
+ .string({
193
+ description: "Amount of asset to sell",
194
+ })
195
+ .refine((amount) => +amount > 0, {
196
+ message: "sellAmount must be greater than 0",
197
+ path: ["sellAmount"],
198
+ }),
199
+ providers: z.optional(
200
+ z.array(
201
+ z
202
+ .string({
203
+ description: "List of providers to use",
204
+ })
205
+ .refine(
206
+ (provider) => {
207
+ return ProviderName[provider as ProviderName] !== undefined;
208
+ },
209
+ {
210
+ message: "Invalid provider",
211
+ path: ["providers"],
212
+ },
213
+ ),
214
+ ),
215
+ ),
216
+ sourceAddress: z.optional(
217
+ z.string({
218
+ description: "Address to send asset from",
219
+ }),
220
+ ),
221
+ destinationAddress: z.optional(
222
+ z.string({
223
+ description: "Address to send asset to",
224
+ }),
225
+ ),
226
+ slippage: z.optional(
227
+ z.number({
228
+ description: "Slippage tolerance as a percentage. Default is 3%.",
229
+ }),
230
+ ),
231
+ affiliate: z.optional(
232
+ z.string({
233
+ description: "Affiliate thorname",
234
+ }),
235
+ ),
236
+ affiliateFee: z.optional(
237
+ z
238
+ .number({
239
+ description: "Affiliate fee in basis points",
240
+ })
241
+ .refine(
242
+ (fee) => {
243
+ return fee === Math.floor(fee) && fee >= 0;
244
+ },
245
+ {
246
+ message: "affiliateFee must be a positive integer",
247
+ path: ["affiliateFee"],
248
+ },
249
+ ),
250
+ ),
251
+ allowSmartContractSender: z.optional(
252
+ z.boolean({
253
+ description: "Allow smart contract as sender",
254
+ }),
255
+ ),
256
+ allowSmartContractReceiver: z.optional(
257
+ z.boolean({
258
+ description: "Allow smart contract as recipient",
259
+ }),
260
+ ),
261
+ disableSecurityChecks: z.optional(
262
+ z.boolean({
263
+ description: "Disable security checks",
264
+ }),
265
+ ),
266
+ includeTx: z.optional(
267
+ z.boolean({
268
+ description: "Set to true to include an transaction object (EVM only)",
269
+ }),
270
+ ),
271
+ cfBoost: z.optional(
272
+ z.boolean({
273
+ description: "Set to true to enable CF boost to speed up Chainflip swaps. BTC only.",
274
+ }),
275
+ ),
276
+ referrer: z.optional(
277
+ z.string({
278
+ description: "Referrer address (referral program)",
279
+ }),
280
+ ),
281
+ })
282
+ .refine((data) => data.sellAsset !== data.buyAsset, {
283
+ message: "Must be different",
284
+ path: ["sellAsset", "buyAsset"],
285
+ });
286
+
287
+ export type QuoteRequest = z.infer<typeof QuoteRequestSchema>;
288
+
289
+ export const PriceRequestSchema = z.object({
290
+ tokens: z.array(
291
+ z.object({
292
+ identifier: z.string(),
293
+ }),
294
+ ),
295
+ metadata: z.boolean(),
296
+ });
297
+
298
+ export type PriceRequest = z.infer<typeof PriceRequestSchema>;
299
+
300
+ export const BrokerDepositChannelParamsSchema = z.object({
301
+ sellAsset: z.object({
302
+ chain: z.string(), // identifier of the asset
303
+ asset: z.string(), // identifier of the asset
304
+ }),
305
+ buyAsset: z.object({
306
+ chain: z.string(), // identifier of the asset
307
+ asset: z.string(), // identifier of the asset
308
+ }),
309
+ destinationAddress: z.string(),
310
+ channelMetadata: z
311
+ .object({
312
+ cfParameters: z.string().optional(),
313
+ gasBudget: z.string().optional(),
314
+ message: z.string().optional(),
315
+ })
316
+ .optional(),
317
+ affiliateFees: z
318
+ .array(
319
+ z.object({
320
+ brokerAddress: z.string(),
321
+ feeBps: z.number(),
322
+ }),
323
+ )
324
+ .optional(),
325
+ refundParameters: z
326
+ .object({
327
+ minPrice: z.string().optional(),
328
+ refundAddress: z.string().optional(),
329
+ retryDuration: z.number().optional(),
330
+ })
331
+ .optional(),
332
+ dcaParameters: z
333
+ .object({
334
+ chunkInterval: z.number().optional(),
335
+ numberOfChunks: z.number().optional(),
336
+ })
337
+ .optional(),
338
+ brokerCommissionBps: z.number().optional(),
339
+ maxBoostFeeBps: z.number().optional(),
340
+ });
341
+
342
+ export type BrokerDepositChannelParams = z.infer<typeof BrokerDepositChannelParamsSchema>;
343
+
344
+ export const DepositChannelResponseSchema = z.object({
345
+ channelId: z.string(),
346
+ depositAddress: z.string(),
347
+ });
348
+
349
+ export type DepositChannelResponse = z.infer<typeof DepositChannelResponseSchema>;
350
+
351
+ const TxnPayloadSchema = z.object({
352
+ evmCalldata: z.optional(z.string()), // raw 0xcalldata
353
+ logs: z.optional(z.unknown()),
354
+ memo: z.optional(z.string()),
355
+ spender: z.optional(z.string()), // used in evm approve transactions
356
+ });
357
+
358
+ export type TxnPayload = z.infer<typeof TxnPayloadSchema>;
359
+
360
+ // props that are most important while the transaction is live
361
+ const TxnTransientSchema = z.object({
362
+ estimatedfinalisedAt: z.number(),
363
+ estimatedTimeToComplete: z.number(),
364
+ updatedAt: z.number(),
365
+ currentLegIndex: z.optional(z.number()),
366
+ providerDetails: z.optional(z.unknown()), // see ProviderTransientDetails
367
+ });
368
+
369
+ export type TxnTransient = z.infer<typeof TxnTransientSchema>;
370
+
371
+ const TransactionFeesSchema = z.object({
372
+ network: z.optional(AssetValueSchema), // gas on ethereum, network fee on thorchain, etc.
373
+ affiliate: z.optional(AssetValueSchema), // e.g. affiliate in memo, other affiliate mechanisms
374
+ liquidity: z.optional(AssetValueSchema), // fee paid to pool
375
+ protocol: z.optional(AssetValueSchema), // extra protocol fees (TS dex aggregation contracts, stargate fees, etc.)
376
+ tax: z.optional(AssetValueSchema), // taxed tokens
377
+ });
378
+
379
+ export type TransactionFees = z.infer<typeof TransactionFeesSchema>;
380
+
381
+ // props that are not part of the transaction itself, but are still relevant for integrators
382
+ const TxnMetaSchema = z.object({
383
+ broadcastedAt: z.optional(z.number()),
384
+ wallet: z.optional(z.string()),
385
+ quoteId: z.optional(z.string()),
386
+ explorerUrl: z.optional(z.string()),
387
+ affiliate: z.optional(z.string()),
388
+ fees: z.optional(TransactionFeesSchema),
389
+ provider: z.optional(z.nativeEnum(ProviderName)),
390
+ images: z.optional(
391
+ z.object({
392
+ from: z.optional(z.string()),
393
+ to: z.optional(z.string()),
394
+ provider: z.optional(z.string()),
395
+ chain: z.optional(z.string()),
396
+ }),
397
+ ),
398
+ });
399
+
400
+ export type TxnMeta = z.infer<typeof TxnMetaSchema>;
401
+
402
+ const TransactionLegDTOSchema = z.object({
403
+ chainId: z.nativeEnum(ChainId),
404
+ hash: z.string(),
405
+ block: z.number(),
406
+ type: z.nativeEnum(TxnType),
407
+ status: z.nativeEnum(TxnStatus),
408
+ trackingStatus: z.optional(z.nativeEnum(TrackingStatus)),
409
+
410
+ fromAsset: z.string(),
411
+ fromAmount: z.string(),
412
+ fromAddress: z.string(),
413
+ toAsset: z.string(),
414
+ toAmount: z.string(),
415
+ toAddress: z.string(),
416
+ finalAsset: z.optional(AssetValueSchema),
417
+ finalAddress: z.optional(z.string()),
418
+
419
+ finalisedAt: z.number(),
420
+
421
+ transient: z.optional(TxnTransientSchema),
422
+ meta: z.optional(TxnMetaSchema),
423
+ payload: z.optional(TxnPayloadSchema),
424
+ });
425
+
426
+ export type TransactionLegDTO = z.infer<typeof TransactionLegDTOSchema>;
427
+
428
+ export const TransactionSchema = TransactionLegDTOSchema.extend({
429
+ legs: z.array(TransactionLegDTOSchema),
430
+ });
431
+
432
+ export type TransactionDTO = z.infer<typeof TransactionLegDTOSchema> & {
433
+ legs: TransactionLegDTO[];
434
+ };
435
+
436
+ export const TransactionDTOSchema: z.ZodType<TransactionDTO> = TransactionLegDTOSchema.extend({
437
+ legs: z.array(TransactionLegDTOSchema),
438
+ });
439
+
440
+ export const FeesSchema = z.array(
441
+ z.object({
442
+ type: z.nativeEnum(FeeTypeEnum),
443
+ amount: z.string(),
444
+ asset: z.string(),
445
+ chain: z.string(),
446
+ protocol: z.nativeEnum(ProviderName),
447
+ }),
448
+ );
449
+
450
+ export type Fees = z.infer<typeof FeesSchema>;
451
+
452
+ export const EstimatedTimeSchema = z.object({
453
+ inbound: z.optional(
454
+ z.number({
455
+ description: "Time to receive inbound asset in seconds",
456
+ }),
457
+ ),
458
+ swap: z.optional(
459
+ z.number({
460
+ description: "Time to swap assets in seconds",
461
+ }),
462
+ ),
463
+ outbound: z.optional(
464
+ z.number({
465
+ description: "Time to receive outbound asset in seconds",
466
+ }),
467
+ ),
468
+ total: z.number({
469
+ description: "Total time in seconds",
470
+ }),
471
+ });
472
+
473
+ export type EstimatedTime = z.infer<typeof EstimatedTimeSchema>;
474
+
475
+ export const EVMTransactionSchema = z.object({
476
+ to: z.string({
477
+ description: "Address of the recipient",
478
+ }),
479
+ from: z.string({
480
+ description: "Address of the sender",
481
+ }),
482
+ value: z.string({
483
+ description: "Value to send",
484
+ }),
485
+ data: z.string({
486
+ description: "Data to send",
487
+ }),
488
+ });
489
+
490
+ export type EVMTransaction = z.infer<typeof EVMTransactionSchema>;
491
+
492
+ export const EVMTransactionDetailsParamsSchema = z.array(
493
+ z.union([
494
+ z.string(),
495
+ z.number(),
496
+ z.array(z.string()),
497
+ z
498
+ .object({
499
+ from: z.string(),
500
+ value: z.string(),
501
+ })
502
+ .describe("Parameters to pass to the contract method"),
503
+ ]),
504
+ );
505
+
506
+ export type EVMTransactionDetailsParams = z.infer<typeof EVMTransactionDetailsParamsSchema>;
507
+
508
+ export const EVMTransactionDetailsSchema = z.object({
509
+ contractAddress: z.string({
510
+ description: "Address of the contract to interact with",
511
+ }),
512
+ contractMethod: z.string({
513
+ description: "Name of the method to call",
514
+ }),
515
+ contractParams: EVMTransactionDetailsParamsSchema,
516
+ // contractParamsStreaming: z.array(
517
+ // z.string({
518
+ // description:
519
+ // "If making a streaming swap through THORChain, parameters to pass to the contract method",
520
+ // }),
521
+ // ),
522
+ contractParamNames: z.array(
523
+ z.string({
524
+ description: "Names of the parameters to pass to the contract method",
525
+ }),
526
+ ),
527
+ approvalToken: z.optional(
528
+ z.string({
529
+ description: "Address of the token to approve spending of",
530
+ }),
531
+ ),
532
+ approvalSpender: z.optional(
533
+ z.string({
534
+ description: "Address of the spender to approve",
535
+ }),
536
+ ),
537
+ });
538
+
539
+ export type EVMTransactionDetails = z.infer<typeof EVMTransactionDetailsSchema>;
540
+
541
+ const EncodeObjectSchema = z.object({
542
+ typeUrl: z.string(),
543
+ value: z.unknown(),
544
+ });
545
+
546
+ const FeeSchema = z.object({
547
+ amount: z.array(
548
+ z.object({
549
+ denom: z.string(),
550
+ amount: z.string(),
551
+ }),
552
+ ),
553
+ gas: z.string(),
554
+ });
555
+
556
+ // Define the full schema
557
+ export const CosmosTransactionSchema = z.object({
558
+ memo: z.string(),
559
+ accountNumber: z.number(),
560
+ sequence: z.number(),
561
+ chainId: z.nativeEnum(ChainId),
562
+ msgs: z.array(EncodeObjectSchema),
563
+ fee: FeeSchema,
564
+ });
565
+
566
+ export type CosmosTransaction = z.infer<typeof CosmosTransactionSchema>;
567
+
568
+ export const RouteLegSchema = z.object({
569
+ sellAsset: z.string({
570
+ description: "Asset to sell",
571
+ }),
572
+ buyAsset: z.string({
573
+ description: "Asset to buy",
574
+ }),
575
+ provider: z.nativeEnum(ProviderName),
576
+ sourceAddress: z.string({
577
+ description: "Source address",
578
+ }),
579
+ destinationAddress: z.string({
580
+ description: "Destination address",
581
+ }),
582
+ estimatedTime: EstimatedTimeSchema.optional(),
583
+ affiliate: z
584
+ .string({
585
+ description: "Affiliate address",
586
+ })
587
+ .optional(),
588
+ affiliateFee: z
589
+ .number({
590
+ description: "Affiliate fee",
591
+ })
592
+ .optional(),
593
+ slipPercentage: z.number({
594
+ description: "Slippage as a percentage",
595
+ }),
596
+ });
597
+
598
+ export type RouteLeg = z.infer<typeof RouteLegSchema>;
599
+
600
+ export const RouteLegWithoutAddressesSchema = RouteLegSchema.omit({
601
+ sourceAddress: true,
602
+ destinationAddress: true,
603
+ slipPercentage: true,
604
+ });
605
+
606
+ export type RouteLegWithoutAddresses = z.infer<typeof RouteLegWithoutAddressesSchema>;
607
+
608
+ export const RouteQuoteMetadataAssetSchema = z.object({
609
+ asset: z.string({
610
+ description: "Asset name",
611
+ }),
612
+ price: z.number({
613
+ description: "Price in USD",
614
+ }),
615
+ image: z.string({
616
+ description: "Asset image",
617
+ }),
618
+ });
619
+
620
+ export type RouteQuoteMetadataAsset = z.infer<typeof RouteQuoteMetadataAssetSchema>;
621
+
622
+ export const ChainflipMetadataSchema = BrokerDepositChannelParamsSchema;
623
+
624
+ export type ChainflipMetadata = z.infer<typeof ChainflipMetadataSchema>;
625
+
626
+ export const RouteQuoteMetadataSchema = z.object({
627
+ priceImpact: z.optional(
628
+ z.number({
629
+ description: "Price impact",
630
+ }),
631
+ ),
632
+ assets: z.optional(z.array(RouteQuoteMetadataAssetSchema)),
633
+ approvalAddress: z.optional(
634
+ z.string({
635
+ description: "Approval address for swap",
636
+ }),
637
+ ),
638
+ streamingInterval: z.number().optional(),
639
+ maxStreamingQuantity: z.number().optional(),
640
+ tags: z.array(z.nativeEnum(PriorityLabel)),
641
+
642
+ affiliate: z.optional(z.string()),
643
+ affiliateFee: z.optional(z.string()),
644
+
645
+ txType: z.optional(z.nativeEnum(RouteQuoteTxType)),
646
+ chainflip: z.optional(ChainflipMetadataSchema),
647
+ referrer: z.optional(z.string()),
648
+ });
649
+
650
+ export const RouteQuoteWarningSchema = z.array(
651
+ z.object({
652
+ code: z.nativeEnum(WarningCodeEnum),
653
+ display: z.string(),
654
+ tooltip: z.string().optional(),
655
+ }),
656
+ );
657
+
658
+ export type RouteQuoteWarning = z.infer<typeof RouteQuoteWarningSchema>;
659
+
660
+ const QuoteResponseRouteLegItem = z.object({
661
+ provider: z.nativeEnum(ProviderName),
662
+ sellAsset: z.string({
663
+ description: "Asset to sell",
664
+ }),
665
+ sellAmount: z.string({
666
+ description: "Sell amount",
667
+ }),
668
+ buyAsset: z.string({
669
+ description: "Asset to buy",
670
+ }),
671
+ buyAmount: z.string({
672
+ description: "Buy amount",
673
+ }),
674
+ buyAmountMaxSlippage: z.string({
675
+ description: "Buy amount max slippage",
676
+ }),
677
+ fees: z.optional(FeesSchema), // TODO remove optionality
678
+ });
679
+
680
+ const QuoteResponseRouteItem = z.object({
681
+ providers: z.array(z.nativeEnum(ProviderName)),
682
+ sellAsset: z.string({
683
+ description: "Asset to sell",
684
+ }),
685
+ sellAmount: z.string({
686
+ description: "Sell amount",
687
+ }),
688
+ buyAsset: z.string({
689
+ description: "Asset to buy",
690
+ }),
691
+ expectedBuyAmount: z.string({
692
+ description: "Expected Buy amount",
693
+ }),
694
+ expectedBuyAmountMaxSlippage: z.string({
695
+ description: "Expected Buy amount max slippage",
696
+ }),
697
+ sourceAddress: z.string({
698
+ description: "Source address",
699
+ }),
700
+ destinationAddress: z.string({
701
+ description: "Destination address",
702
+ }),
703
+ targetAddress: z.optional(
704
+ z.string({
705
+ description: "Target address",
706
+ }),
707
+ ),
708
+ inboundAddress: z.optional(
709
+ z.string({
710
+ description: "Inbound address",
711
+ }),
712
+ ),
713
+ expiration: z.optional(
714
+ z.string({
715
+ description: "Expiration",
716
+ }),
717
+ ),
718
+ memo: z.optional(
719
+ z.string({
720
+ description: "Memo",
721
+ }),
722
+ ),
723
+ fees: FeesSchema,
724
+ txType: z.optional(z.nativeEnum(RouteQuoteTxType)),
725
+ tx: z.optional(z.union([EVMTransactionSchema, CosmosTransactionSchema, z.string()])),
726
+ estimatedTime: z.optional(EstimatedTimeSchema), // TODO remove optionality
727
+ totalSlippageBps: z.number({
728
+ description: "Total slippage in bps",
729
+ }),
730
+ legs: z.array(QuoteResponseRouteLegItem),
731
+ warnings: RouteQuoteWarningSchema,
732
+ meta: RouteQuoteMetadataSchema,
733
+ });
734
+
735
+ export const QuoteResponseSchema = z.object({
736
+ quoteId: z.string({
737
+ description: "Quote ID",
738
+ }),
739
+ routes: z.array(QuoteResponseRouteItem),
740
+ // in case of bad request or actual backend error, not bad quotes from providers
741
+ error: z.optional(
742
+ z.string({
743
+ description: "Error message",
744
+ }),
745
+ ),
746
+ // errors from providers
747
+ providerErrors: z.optional(
748
+ z.array(
749
+ z.object({
750
+ provider: z.nativeEnum(ProviderName).optional(),
751
+ errorCode: z.optional(z.nativeEnum(ErrorCode)),
752
+ message: z.optional(z.string()),
753
+ }),
754
+ ),
755
+ ),
756
+ });
757
+
758
+ export type QuoteResponse = z.infer<typeof QuoteResponseSchema>;
759
+ export type QuoteResponseRoute = z.infer<typeof QuoteResponseRouteItem>;
760
+ export type QuoteResponseRouteLeg = z.infer<typeof QuoteResponseRouteLegItem>;
761
+
762
+ export const GasResponseSchema = z.array(
763
+ z.object({
764
+ id: z.number(),
765
+ chainId: z.string(),
766
+ value: z.string(),
767
+ unit: z.string(),
768
+ createdAt: z.string(),
769
+ }),
770
+ );
771
+
772
+ export type GasResponse = z.infer<typeof GasResponseSchema>;