@coinbase/cdp-sdk 1.11.0 → 1.13.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 (103) hide show
  1. package/CHANGELOG.md +14 -0
  2. package/README.md +126 -0
  3. package/_cjs/accounts/evm/toEvmServerAccount.js +15 -0
  4. package/_cjs/accounts/evm/toEvmServerAccount.js.map +1 -1
  5. package/_cjs/actions/evm/swap/createSwapQuote.js +126 -0
  6. package/_cjs/actions/evm/swap/createSwapQuote.js.map +1 -0
  7. package/_cjs/actions/evm/swap/getSwapPrice.js +86 -0
  8. package/_cjs/actions/evm/swap/getSwapPrice.js.map +1 -0
  9. package/_cjs/actions/evm/swap/sendSwapTransaction.js +141 -0
  10. package/_cjs/actions/evm/swap/sendSwapTransaction.js.map +1 -0
  11. package/_cjs/actions/evm/swap/types.js +3 -0
  12. package/_cjs/actions/evm/swap/types.js.map +1 -0
  13. package/_cjs/client/evm/evm.js +46 -1
  14. package/_cjs/client/evm/evm.js.map +1 -1
  15. package/_cjs/openapi-client/generated/coinbaseDeveloperPlatformAPIs.schemas.js +28 -1
  16. package/_cjs/openapi-client/generated/coinbaseDeveloperPlatformAPIs.schemas.js.map +1 -1
  17. package/_cjs/openapi-client/generated/evm-swaps/evm-swaps.js +26 -0
  18. package/_cjs/openapi-client/generated/evm-swaps/evm-swaps.js.map +1 -0
  19. package/_cjs/openapi-client/generated/evm-swaps/evm-swaps.msw.js +179 -0
  20. package/_cjs/openapi-client/generated/evm-swaps/evm-swaps.msw.js.map +1 -0
  21. package/_cjs/openapi-client/generated/index.msw.js +5 -4
  22. package/_cjs/openapi-client/generated/index.msw.js.map +1 -1
  23. package/_cjs/openapi-client/generated/policy-engine/policy-engine.msw.js +59 -1
  24. package/_cjs/openapi-client/generated/policy-engine/policy-engine.msw.js.map +1 -1
  25. package/_cjs/openapi-client/index.js +4 -0
  26. package/_cjs/openapi-client/index.js.map +1 -1
  27. package/_cjs/policies/schema.js +59 -1
  28. package/_cjs/policies/schema.js.map +1 -1
  29. package/_cjs/version.js +1 -1
  30. package/_esm/accounts/evm/toEvmServerAccount.js +15 -0
  31. package/_esm/accounts/evm/toEvmServerAccount.js.map +1 -1
  32. package/_esm/actions/evm/swap/createSwapQuote.js +123 -0
  33. package/_esm/actions/evm/swap/createSwapQuote.js.map +1 -0
  34. package/_esm/actions/evm/swap/getSwapPrice.js +83 -0
  35. package/_esm/actions/evm/swap/getSwapPrice.js.map +1 -0
  36. package/_esm/actions/evm/swap/sendSwapTransaction.js +138 -0
  37. package/_esm/actions/evm/swap/sendSwapTransaction.js.map +1 -0
  38. package/_esm/actions/evm/swap/types.js +2 -0
  39. package/_esm/actions/evm/swap/types.js.map +1 -0
  40. package/_esm/client/evm/evm.js +46 -1
  41. package/_esm/client/evm/evm.js.map +1 -1
  42. package/_esm/openapi-client/generated/coinbaseDeveloperPlatformAPIs.schemas.js +27 -0
  43. package/_esm/openapi-client/generated/coinbaseDeveloperPlatformAPIs.schemas.js.map +1 -1
  44. package/_esm/openapi-client/generated/evm-swaps/evm-swaps.js +21 -0
  45. package/_esm/openapi-client/generated/evm-swaps/evm-swaps.js.map +1 -0
  46. package/_esm/openapi-client/generated/evm-swaps/evm-swaps.msw.js +167 -0
  47. package/_esm/openapi-client/generated/evm-swaps/evm-swaps.msw.js.map +1 -0
  48. package/_esm/openapi-client/generated/index.msw.js +5 -4
  49. package/_esm/openapi-client/generated/index.msw.js.map +1 -1
  50. package/_esm/openapi-client/generated/policy-engine/policy-engine.msw.js +54 -0
  51. package/_esm/openapi-client/generated/policy-engine/policy-engine.msw.js.map +1 -1
  52. package/_esm/openapi-client/index.js +4 -0
  53. package/_esm/openapi-client/index.js.map +1 -1
  54. package/_esm/policies/schema.js +58 -0
  55. package/_esm/policies/schema.js.map +1 -1
  56. package/_esm/version.js +1 -1
  57. package/_types/accounts/evm/toEvmServerAccount.d.ts.map +1 -1
  58. package/_types/actions/evm/swap/createSwapQuote.d.ts +23 -0
  59. package/_types/actions/evm/swap/createSwapQuote.d.ts.map +1 -0
  60. package/_types/actions/evm/swap/getSwapPrice.d.ts +23 -0
  61. package/_types/actions/evm/swap/getSwapPrice.d.ts.map +1 -0
  62. package/_types/actions/evm/swap/sendSwapTransaction.d.ts +64 -0
  63. package/_types/actions/evm/swap/sendSwapTransaction.d.ts.map +1 -0
  64. package/_types/actions/evm/swap/types.d.ts +119 -0
  65. package/_types/actions/evm/swap/types.d.ts.map +1 -0
  66. package/_types/actions/evm/types.d.ts +86 -0
  67. package/_types/actions/evm/types.d.ts.map +1 -1
  68. package/_types/client/evm/evm.d.ts +39 -1
  69. package/_types/client/evm/evm.d.ts.map +1 -1
  70. package/_types/client/evm/evm.types.d.ts +207 -3
  71. package/_types/client/evm/evm.types.d.ts.map +1 -1
  72. package/_types/openapi-client/generated/coinbaseDeveloperPlatformAPIs.schemas.d.ts +364 -4
  73. package/_types/openapi-client/generated/coinbaseDeveloperPlatformAPIs.schemas.d.ts.map +1 -1
  74. package/_types/openapi-client/generated/evm-swaps/evm-swaps.d.ts +24 -0
  75. package/_types/openapi-client/generated/evm-swaps/evm-swaps.d.ts.map +1 -0
  76. package/_types/openapi-client/generated/evm-swaps/evm-swaps.msw.d.ts +12 -0
  77. package/_types/openapi-client/generated/evm-swaps/evm-swaps.msw.d.ts.map +1 -0
  78. package/_types/openapi-client/generated/index.msw.d.ts +5 -4
  79. package/_types/openapi-client/generated/index.msw.d.ts.map +1 -1
  80. package/_types/openapi-client/generated/policy-engine/policy-engine.msw.d.ts +5 -1
  81. package/_types/openapi-client/generated/policy-engine/policy-engine.msw.d.ts.map +1 -1
  82. package/_types/openapi-client/index.d.ts +5 -0
  83. package/_types/openapi-client/index.d.ts.map +1 -1
  84. package/_types/policies/schema.d.ts +338 -0
  85. package/_types/policies/schema.d.ts.map +1 -1
  86. package/_types/version.d.ts +1 -1
  87. package/accounts/evm/toEvmServerAccount.ts +21 -0
  88. package/actions/evm/swap/createSwapQuote.ts +148 -0
  89. package/actions/evm/swap/getSwapPrice.ts +95 -0
  90. package/actions/evm/swap/sendSwapTransaction.ts +173 -0
  91. package/actions/evm/swap/types.ts +137 -0
  92. package/actions/evm/types.ts +88 -0
  93. package/client/evm/evm.ts +58 -1
  94. package/client/evm/evm.types.ts +225 -1
  95. package/openapi-client/generated/coinbaseDeveloperPlatformAPIs.schemas.ts +422 -4
  96. package/openapi-client/generated/evm-swaps/evm-swaps.msw.ts +217 -0
  97. package/openapi-client/generated/evm-swaps/evm-swaps.ts +51 -0
  98. package/openapi-client/generated/index.msw.ts +5 -4
  99. package/openapi-client/generated/policy-engine/policy-engine.msw.ts +87 -0
  100. package/openapi-client/index.ts +4 -0
  101. package/package.json +1 -1
  102. package/policies/schema.ts +70 -0
  103. package/version.ts +1 -1
package/client/evm/evm.ts CHANGED
@@ -27,6 +27,11 @@ import {
27
27
  SignTypedDataOptions,
28
28
  UpdateEvmAccountOptions,
29
29
  ImportServerAccountOptions,
30
+ GetSwapPriceOptions,
31
+ CreateSwapQuoteOptions,
32
+ GetSwapPriceResult,
33
+ CreateSwapQuoteResult,
34
+ SwapUnavailableResult,
30
35
  } from "./evm.types.js";
31
36
  import { toEvmServerAccount } from "../../accounts/evm/toEvmServerAccount.js";
32
37
  import { toEvmSmartAccount } from "../../accounts/evm/toEvmSmartAccount.js";
@@ -47,6 +52,8 @@ import {
47
52
  SendUserOperationOptions,
48
53
  SendUserOperationReturnType,
49
54
  } from "../../actions/evm/sendUserOperation.js";
55
+ import { createSwapQuote } from "../../actions/evm/swap/createSwapQuote.js";
56
+ import { getSwapPrice } from "../../actions/evm/swap/getSwapPrice.js";
50
57
  import {
51
58
  waitForUserOperation,
52
59
  WaitForUserOperationReturnType,
@@ -160,6 +167,8 @@ export class EvmClient implements EvmClientInterface {
160
167
  * ```
161
168
  */
162
169
  async importAccount(options: ImportServerAccountOptions): Promise<ServerAccount> {
170
+ const encryptionPublicKey = options.encryptionPublicKey || ImportEvmAccountPublicRSAKey;
171
+
163
172
  const privateKeyHex = options.privateKey.startsWith("0x")
164
173
  ? options.privateKey.slice(2)
165
174
  : options.privateKey;
@@ -173,7 +182,7 @@ export class EvmClient implements EvmClientInterface {
173
182
 
174
183
  const encryptedPrivateKey = publicEncrypt(
175
184
  {
176
- key: ImportEvmAccountPublicRSAKey,
185
+ key: encryptionPublicKey,
177
186
  padding: constants.RSA_PKCS1_OAEP_PADDING,
178
187
  oaepHash: "sha256",
179
188
  },
@@ -386,6 +395,54 @@ export class EvmClient implements EvmClientInterface {
386
395
  }
387
396
  }
388
397
 
398
+ /**
399
+ * Gets the price for a swap between two tokens on an EVM network.
400
+ *
401
+ * @param {GetSwapPriceOptions} options - The options for getting a swap price.
402
+ *
403
+ * @returns {Promise<GetSwapPriceResult | SwapUnavailableResult>} A promise that resolves to the swap price result or a response indicating that liquidity is unavailable.
404
+ *
405
+ * @example
406
+ * ```typescript
407
+ * const price = await cdp.evm.getSwapPrice({
408
+ * network: "ethereum-mainnet",
409
+ * toToken: "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48", // USDC
410
+ * fromToken: "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2", // WETH
411
+ * fromAmount: BigInt("1000000000000000000"), // 1 WETH
412
+ * taker: "0x1234567890123456789012345678901234567890"
413
+ * });
414
+ * ```
415
+ */
416
+ async getSwapPrice(
417
+ options: GetSwapPriceOptions,
418
+ ): Promise<GetSwapPriceResult | SwapUnavailableResult> {
419
+ return getSwapPrice(CdpOpenApiClient, options);
420
+ }
421
+
422
+ /**
423
+ * Creates a quote for a swap between two tokens on an EVM network.
424
+ *
425
+ * @param {CreateSwapQuoteOptions} options - The options for creating a swap quote.
426
+ *
427
+ * @returns {Promise<CreateSwapQuoteResult | SwapUnavailableResult>} A promise that resolves to the swap quote result or a response indicating that liquidity is unavailable.
428
+ *
429
+ * @example
430
+ * ```typescript
431
+ * const swapQuote = await cdp.evm.createSwapQuote({
432
+ * network: "ethereum",
433
+ * toToken: "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48", // USDC
434
+ * fromToken: "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2", // WETH
435
+ * fromAmount: BigInt("1000000000000000000"), // 1 WETH
436
+ * taker: "0x1234567890123456789012345678901234567890"
437
+ * });
438
+ * ```
439
+ */
440
+ async createSwapQuote(
441
+ options: CreateSwapQuoteOptions,
442
+ ): Promise<CreateSwapQuoteResult | SwapUnavailableResult> {
443
+ return createSwapQuote(CdpOpenApiClient, options);
444
+ }
445
+
389
446
  /**
390
447
  * Gets a user operation for a smart account by user operation hash.
391
448
  *
@@ -18,6 +18,7 @@ import type {
18
18
  } from "../../actions/evm/sendUserOperation.js";
19
19
  import type { SmartAccountActions } from "../../actions/evm/types.js";
20
20
  import type {
21
+ EvmSwapsNetwork,
21
22
  EvmUserOperationNetwork,
22
23
  EvmUserOperationStatus,
23
24
  OpenApiEvmMethods,
@@ -38,6 +39,8 @@ export type EvmClientInterface = Omit<
38
39
  | "getEvmAccount" // mapped to getAccount
39
40
  | "getEvmAccountByName" // mapped to getAccount
40
41
  | "getEvmSmartAccount" // mapped to getSmartAccount
42
+ | "getEvmSwapPrice" // mapped to getSwapPrice
43
+ | "createEvmSwapQuote" // mapped to createSwapQuote
41
44
  | "getUserOperation"
42
45
  | "updateEvmAccount" // mapped to updateAccount
43
46
  | "listEvmAccounts" // mapped to listAccounts
@@ -59,6 +62,12 @@ export type EvmClientInterface = Omit<
59
62
  importAccount: (options: ImportServerAccountOptions) => Promise<ServerAccount>;
60
63
  getAccount: (options: GetServerAccountOptions) => Promise<ServerAccount>;
61
64
  getSmartAccount: (options: GetSmartAccountOptions) => Promise<SmartAccount>;
65
+ getSwapPrice: (
66
+ options: GetSwapPriceOptions,
67
+ ) => Promise<GetSwapPriceResult | SwapUnavailableResult>;
68
+ createSwapQuote: (
69
+ options: CreateSwapQuoteOptions,
70
+ ) => Promise<CreateSwapQuoteResult | SwapUnavailableResult>;
62
71
  getOrCreateAccount: (options: GetOrCreateServerAccountOptions) => Promise<ServerAccount>;
63
72
  getUserOperation: (options: GetUserOperationOptions) => Promise<UserOperation>;
64
73
  updateAccount: (options: UpdateEvmAccountOptions) => Promise<ServerAccount>;
@@ -79,6 +88,153 @@ export type EvmClientInterface = Omit<
79
88
 
80
89
  export type { ServerAccount, SmartAccount };
81
90
 
91
+ /**
92
+ * Options for creating a swap quote between two tokens on an EVM network.
93
+ */
94
+ export interface CreateSwapQuoteOptions {
95
+ /** The network to create a swap quote on. */
96
+ network: EvmSwapsNetwork;
97
+ /** The token to receive (destination token). */
98
+ toToken: Address;
99
+ /** The token to send (source token). */
100
+ fromToken: Address;
101
+ /** The amount to send in atomic units of the token. */
102
+ fromAmount: bigint;
103
+ /** The address that will perform the swap. */
104
+ taker?: Address;
105
+ /** The signer address (only needed if taker is a smart contract). */
106
+ signerAddress?: Address;
107
+ /** The gas price in Wei. */
108
+ gasPrice?: bigint;
109
+ /** The slippage tolerance in basis points (0-10000). */
110
+ slippageBps?: number;
111
+ }
112
+
113
+ /**
114
+ * Options for getting a swap price.
115
+ */
116
+ export interface GetSwapPriceOptions {
117
+ /** The network to get a price from. */
118
+ network: EvmSwapsNetwork;
119
+ /** The token to receive (destination token). */
120
+ toToken: Address;
121
+ /** The token to send (source token). */
122
+ fromToken: Address;
123
+ /** The amount to send in atomic units of the token. */
124
+ fromAmount: bigint;
125
+ /** The address that will perform the swap. */
126
+ taker: Address;
127
+ /** The signer address (only needed if taker is a smart contract). */
128
+ signerAddress?: Address;
129
+ /** The gas price in Wei. */
130
+ gasPrice?: bigint;
131
+ /** The slippage tolerance in basis points (0-10000). */
132
+ slippageBps?: number;
133
+ }
134
+
135
+ /**
136
+ * Result of getting a swap price.
137
+ */
138
+ export interface GetSwapPriceResult {
139
+ /** Whether liquidity is available for the swap. */
140
+ liquidityAvailable: true;
141
+ /** The token to receive (destination token). */
142
+ toToken: Address;
143
+ /** The token to send (source token). */
144
+ fromToken: Address;
145
+ /** The amount to send in atomic units of the token. */
146
+ fromAmount: bigint;
147
+ /** The amount to receive in atomic units of the token. */
148
+ toAmount: bigint;
149
+ /** The minimum amount to receive after slippage in atomic units of the token. */
150
+ minToAmount: bigint;
151
+ /** The block number at which the liquidity conditions were examined. */
152
+ blockNumber: bigint;
153
+ /** The estimated fees for the swap. */
154
+ fees: SwapFees;
155
+ /** Potential issues discovered during validation. */
156
+ issues: SwapIssues;
157
+ /** The gas estimate for the swap. */
158
+ gas?: bigint;
159
+ /** The gas price in Wei. */
160
+ gasPrice?: bigint;
161
+ }
162
+
163
+ /**
164
+ * Result when liquidity is unavailable for a swap.
165
+ */
166
+ export interface SwapUnavailableResult {
167
+ /** Whether liquidity is available for the swap. */
168
+ liquidityAvailable: false;
169
+ }
170
+
171
+ /**
172
+ * Options for executing a swap quote.
173
+ */
174
+ export interface ExecuteSwapQuoteOptions {
175
+ /** Optional idempotency key for the request. */
176
+ idempotencyKey?: string;
177
+ }
178
+
179
+ /**
180
+ * Result of executing a swap quote.
181
+ */
182
+ export interface ExecuteSwapQuoteResult {
183
+ /** The transaction hash of the executed swap. */
184
+ transactionHash: Hex;
185
+ }
186
+
187
+ /**
188
+ * Result of creating a swap quote.
189
+ */
190
+ export interface CreateSwapQuoteResult {
191
+ /** Whether liquidity is available for the swap. */
192
+ liquidityAvailable: true;
193
+ /** The network for which this swap quote was created. */
194
+ network: EvmSwapsNetwork;
195
+ /** The token to receive (destination token). */
196
+ toToken: Address;
197
+ /** The token to send (source token). */
198
+ fromToken: Address;
199
+ /** The amount to send in atomic units of the token. */
200
+ fromAmount: bigint;
201
+ /** The amount to receive in atomic units of the token. */
202
+ toAmount: bigint;
203
+ /** The minimum amount to receive after slippage in atomic units of the token. */
204
+ minToAmount: bigint;
205
+ /** The block number at which the liquidity conditions were examined. */
206
+ blockNumber: bigint;
207
+ /** The estimated fees for the swap. */
208
+ fees: SwapFees;
209
+ /** Potential issues discovered during validation. */
210
+ issues: SwapIssues;
211
+ /** The transaction to execute the swap. */
212
+ transaction?: {
213
+ /** The contract address to send the transaction to. */
214
+ to: Address;
215
+ /** The transaction data. */
216
+ data: Hex;
217
+ /** The value to send with the transaction in Wei. */
218
+ value: bigint;
219
+ /** The gas limit for the transaction. */
220
+ gas: bigint;
221
+ /** The gas price for the transaction in Wei. */
222
+ gasPrice: bigint;
223
+ };
224
+ /** Permit2 data if required for the swap. */
225
+ permit2?: {
226
+ /** EIP-712 typed data for signing. */
227
+ eip712: EIP712Message;
228
+ };
229
+ /**
230
+ * Execute the swap using the quote.
231
+ *
232
+ * @param {ExecuteSwapQuoteOptions} options - Options for executing the swap.
233
+ * @returns {Promise<ExecuteSwapQuoteResult>} A promise that resolves to the swap execution result.
234
+ */
235
+ execute: (options?: ExecuteSwapQuoteOptions) => Promise<ExecuteSwapQuoteResult>;
236
+ }
237
+
82
238
  /**
83
239
  * Options for getting a user operation.
84
240
  */
@@ -155,6 +311,8 @@ export interface CreateServerAccountOptions {
155
311
  * Options for importing an EVM server account.
156
312
  */
157
313
  export interface ImportServerAccountOptions {
314
+ /** The public RSA key used to encrypt the private key when importing an EVM account. */
315
+ encryptionPublicKey?: string;
158
316
  /** The name of the account. */
159
317
  name?: string;
160
318
  /** The idempotency key. */
@@ -295,7 +453,7 @@ export interface SignMessageOptions {
295
453
  }
296
454
 
297
455
  /**
298
- * Options for signing an EVM message.
456
+ * Options for signing an EVM typed data message.
299
457
  */
300
458
  export interface SignTypedDataOptions {
301
459
  /** The address of the account. */
@@ -332,6 +490,60 @@ export interface SignatureResult {
332
490
  signature: Hex;
333
491
  }
334
492
 
493
+ /**
494
+ * A fee in a specific token.
495
+ */
496
+ export interface TokenFee {
497
+ /** The amount of the fee in atomic units of the token. */
498
+ amount: bigint;
499
+ /** The contract address of the token that the fee is paid in. */
500
+ token: Address;
501
+ }
502
+
503
+ /**
504
+ * The estimated fees for a swap.
505
+ */
506
+ export interface SwapFees {
507
+ /** The estimated gas fee for the swap. */
508
+ gasFee?: TokenFee;
509
+ /** The estimated protocol fee for the swap. */
510
+ protocolFee?: TokenFee;
511
+ }
512
+
513
+ /**
514
+ * Details of allowance issues for a swap.
515
+ */
516
+ export interface SwapAllowanceIssue {
517
+ /** The current allowance of the fromToken by the taker. */
518
+ currentAllowance: bigint;
519
+ /** The address to set the allowance on. */
520
+ spender: Address;
521
+ }
522
+
523
+ /**
524
+ * Details of balance issues for a swap.
525
+ */
526
+ export interface SwapBalanceIssue {
527
+ /** The contract address of the token. */
528
+ token: Address;
529
+ /** The current balance of the fromToken by the taker. */
530
+ currentBalance: bigint;
531
+ /** The amount of the token that the taker must hold. */
532
+ requiredBalance: bigint;
533
+ }
534
+
535
+ /**
536
+ * Potential issues discovered during swap validation.
537
+ */
538
+ export interface SwapIssues {
539
+ /** Details of the allowances that the taker must set. Null if no allowance is required. */
540
+ allowance?: SwapAllowanceIssue;
541
+ /** Details of the balance of the fromToken that the taker must hold. Null if sufficient balance. */
542
+ balance?: SwapBalanceIssue;
543
+ /** True when the transaction cannot be validated (e.g., insufficient balance). */
544
+ simulationIncomplete: boolean;
545
+ }
546
+
335
547
  /**
336
548
  * Options for waiting for a user operation.
337
549
  */
@@ -343,3 +555,15 @@ export interface WaitForUserOperationOptions {
343
555
  /** The wait options. */
344
556
  waitOptions?: WaitOptions;
345
557
  }
558
+
559
+ /**
560
+ * Legacy type aliases for backwards compatibility.
561
+ *
562
+ * @deprecated Use the new type names instead.
563
+ */
564
+ export type CreateSwapOptions = CreateSwapQuoteOptions;
565
+ export type CreateSwapResult = CreateSwapQuoteResult;
566
+ export type GetSwapQuoteOptions = GetSwapPriceOptions;
567
+ export type GetSwapQuoteResult = GetSwapPriceResult;
568
+ export type SwapQuoteUnavailableResult = SwapUnavailableResult;
569
+ export type SwapPriceUnavailableResult = SwapUnavailableResult;