@coinbase/cdp-sdk 1.6.0 → 1.8.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 (257) hide show
  1. package/CHANGELOG.md +23 -0
  2. package/README.md +284 -1
  3. package/_cjs/accounts/evm/toEvmServerAccount.js +4 -2
  4. package/_cjs/accounts/evm/toEvmServerAccount.js.map +1 -1
  5. package/_cjs/accounts/evm/toEvmSmartAccount.js +7 -0
  6. package/_cjs/accounts/evm/toEvmSmartAccount.js.map +1 -1
  7. package/_cjs/accounts/evm/types.js.map +1 -0
  8. package/_cjs/accounts/solana/toSolanaAccount.js +42 -0
  9. package/_cjs/accounts/solana/toSolanaAccount.js.map +1 -0
  10. package/_cjs/accounts/solana/types.js +3 -0
  11. package/_cjs/accounts/solana/types.js.map +1 -0
  12. package/_cjs/actions/evm/getUserOperation.js +36 -0
  13. package/_cjs/actions/evm/getUserOperation.js.map +1 -0
  14. package/_cjs/actions/evm/transfer/accountTransferStrategy.js +11 -7
  15. package/_cjs/actions/evm/transfer/accountTransferStrategy.js.map +1 -1
  16. package/_cjs/actions/evm/transfer/smartAccountTransferStrategy.js +9 -6
  17. package/_cjs/actions/evm/transfer/smartAccountTransferStrategy.js.map +1 -1
  18. package/_cjs/actions/evm/transfer/transfer.js +7 -3
  19. package/_cjs/actions/evm/transfer/transfer.js.map +1 -1
  20. package/_cjs/actions/solana/requestFaucet.js +29 -0
  21. package/_cjs/actions/solana/requestFaucet.js.map +1 -0
  22. package/_cjs/actions/solana/signMessage.js +29 -0
  23. package/_cjs/actions/solana/signMessage.js.map +1 -0
  24. package/_cjs/actions/solana/signTransaction.js +46 -0
  25. package/_cjs/actions/solana/signTransaction.js.map +1 -0
  26. package/_cjs/actions/solana/types.js +3 -0
  27. package/_cjs/actions/solana/types.js.map +1 -0
  28. package/_cjs/analytics.js +68 -8
  29. package/_cjs/analytics.js.map +1 -1
  30. package/_cjs/client/cdp.js +9 -3
  31. package/_cjs/client/cdp.js.map +1 -1
  32. package/_cjs/client/evm/evm.js +135 -27
  33. package/_cjs/client/evm/evm.js.map +1 -1
  34. package/_cjs/client/policies/index.js +6 -0
  35. package/_cjs/client/policies/index.js.map +1 -0
  36. package/_cjs/client/policies/policies.js +232 -0
  37. package/_cjs/client/policies/policies.js.map +1 -0
  38. package/_cjs/client/policies/policies.types.js +3 -0
  39. package/_cjs/client/policies/policies.types.js.map +1 -0
  40. package/_cjs/client/solana/solana.js +84 -22
  41. package/_cjs/client/solana/solana.js.map +1 -1
  42. package/_cjs/index.js +4 -1
  43. package/_cjs/index.js.map +1 -1
  44. package/_cjs/openapi-client/generated/coinbaseDeveloperPlatformAPIs.schemas.js +59 -1
  45. package/_cjs/openapi-client/generated/coinbaseDeveloperPlatformAPIs.schemas.js.map +1 -1
  46. package/_cjs/openapi-client/generated/evm-accounts/evm-accounts.js +27 -1
  47. package/_cjs/openapi-client/generated/evm-accounts/evm-accounts.js.map +1 -1
  48. package/_cjs/openapi-client/generated/evm-accounts/evm-accounts.msw.js +56 -1
  49. package/_cjs/openapi-client/generated/evm-accounts/evm-accounts.msw.js.map +1 -1
  50. package/_cjs/openapi-client/generated/evm-smart-accounts/evm-smart-accounts.js +3 -1
  51. package/_cjs/openapi-client/generated/evm-smart-accounts/evm-smart-accounts.js.map +1 -1
  52. package/_cjs/openapi-client/generated/index.msw.js +3 -2
  53. package/_cjs/openapi-client/generated/index.msw.js.map +1 -1
  54. package/_cjs/openapi-client/generated/policy-engine/policy-engine.js +56 -0
  55. package/_cjs/openapi-client/generated/policy-engine/policy-engine.js.map +1 -0
  56. package/_cjs/openapi-client/generated/policy-engine/policy-engine.msw.js +284 -0
  57. package/_cjs/openapi-client/generated/policy-engine/policy-engine.msw.js.map +1 -0
  58. package/_cjs/openapi-client/generated/solana-accounts/solana-accounts.js +14 -1
  59. package/_cjs/openapi-client/generated/solana-accounts/solana-accounts.js.map +1 -1
  60. package/_cjs/openapi-client/generated/solana-accounts/solana-accounts.msw.js +42 -1
  61. package/_cjs/openapi-client/generated/solana-accounts/solana-accounts.msw.js.map +1 -1
  62. package/_cjs/openapi-client/index.js +7 -1
  63. package/_cjs/openapi-client/index.js.map +1 -1
  64. package/_cjs/policies/schema.js +186 -0
  65. package/_cjs/policies/schema.js.map +1 -0
  66. package/_cjs/policies/types.js +3 -0
  67. package/_cjs/{accounts → policies}/types.js.map +1 -1
  68. package/_cjs/version.js +1 -1
  69. package/_esm/accounts/evm/toEvmServerAccount.js +4 -2
  70. package/_esm/accounts/evm/toEvmServerAccount.js.map +1 -1
  71. package/_esm/accounts/evm/toEvmSmartAccount.js +7 -0
  72. package/_esm/accounts/evm/toEvmSmartAccount.js.map +1 -1
  73. package/_esm/accounts/evm/types.js.map +1 -0
  74. package/_esm/accounts/solana/toSolanaAccount.js +39 -0
  75. package/_esm/accounts/solana/toSolanaAccount.js.map +1 -0
  76. package/_esm/accounts/solana/types.js +2 -0
  77. package/_esm/accounts/solana/types.js.map +1 -0
  78. package/_esm/actions/evm/getUserOperation.js +33 -0
  79. package/_esm/actions/evm/getUserOperation.js.map +1 -0
  80. package/_esm/actions/evm/transfer/accountTransferStrategy.js +11 -7
  81. package/_esm/actions/evm/transfer/accountTransferStrategy.js.map +1 -1
  82. package/_esm/actions/evm/transfer/smartAccountTransferStrategy.js +9 -6
  83. package/_esm/actions/evm/transfer/smartAccountTransferStrategy.js.map +1 -1
  84. package/_esm/actions/evm/transfer/transfer.js +7 -3
  85. package/_esm/actions/evm/transfer/transfer.js.map +1 -1
  86. package/_esm/actions/solana/requestFaucet.js +26 -0
  87. package/_esm/actions/solana/requestFaucet.js.map +1 -0
  88. package/_esm/actions/solana/signMessage.js +26 -0
  89. package/_esm/actions/solana/signMessage.js.map +1 -0
  90. package/_esm/actions/solana/signTransaction.js +43 -0
  91. package/_esm/actions/solana/signTransaction.js.map +1 -0
  92. package/_esm/actions/solana/types.js +2 -0
  93. package/_esm/actions/solana/types.js.map +1 -0
  94. package/_esm/analytics.js +68 -7
  95. package/_esm/analytics.js.map +1 -1
  96. package/_esm/client/cdp.js +10 -4
  97. package/_esm/client/cdp.js.map +1 -1
  98. package/_esm/client/evm/evm.js +135 -27
  99. package/_esm/client/evm/evm.js.map +1 -1
  100. package/_esm/client/policies/index.js +2 -0
  101. package/_esm/client/policies/index.js.map +1 -0
  102. package/_esm/client/policies/policies.js +228 -0
  103. package/_esm/client/policies/policies.js.map +1 -0
  104. package/_esm/client/policies/policies.types.js +2 -0
  105. package/_esm/client/policies/policies.types.js.map +1 -0
  106. package/_esm/client/solana/solana.js +84 -22
  107. package/_esm/client/solana/solana.js.map +1 -1
  108. package/_esm/index.js +1 -0
  109. package/_esm/index.js.map +1 -1
  110. package/_esm/openapi-client/generated/coinbaseDeveloperPlatformAPIs.schemas.js +58 -0
  111. package/_esm/openapi-client/generated/coinbaseDeveloperPlatformAPIs.schemas.js.map +1 -1
  112. package/_esm/openapi-client/generated/evm-accounts/evm-accounts.js +24 -0
  113. package/_esm/openapi-client/generated/evm-accounts/evm-accounts.js.map +1 -1
  114. package/_esm/openapi-client/generated/evm-accounts/evm-accounts.msw.js +51 -0
  115. package/_esm/openapi-client/generated/evm-accounts/evm-accounts.msw.js.map +1 -1
  116. package/_esm/openapi-client/generated/evm-smart-accounts/evm-smart-accounts.js +3 -1
  117. package/_esm/openapi-client/generated/evm-smart-accounts/evm-smart-accounts.js.map +1 -1
  118. package/_esm/openapi-client/generated/index.msw.js +3 -2
  119. package/_esm/openapi-client/generated/index.msw.js.map +1 -1
  120. package/_esm/openapi-client/generated/policy-engine/policy-engine.js +48 -0
  121. package/_esm/openapi-client/generated/policy-engine/policy-engine.js.map +1 -0
  122. package/_esm/openapi-client/generated/policy-engine/policy-engine.msw.js +259 -0
  123. package/_esm/openapi-client/generated/policy-engine/policy-engine.msw.js.map +1 -0
  124. package/_esm/openapi-client/generated/solana-accounts/solana-accounts.js +12 -0
  125. package/_esm/openapi-client/generated/solana-accounts/solana-accounts.js.map +1 -1
  126. package/_esm/openapi-client/generated/solana-accounts/solana-accounts.msw.js +39 -0
  127. package/_esm/openapi-client/generated/solana-accounts/solana-accounts.msw.js.map +1 -1
  128. package/_esm/openapi-client/index.js +6 -0
  129. package/_esm/openapi-client/index.js.map +1 -1
  130. package/_esm/policies/schema.js +183 -0
  131. package/_esm/policies/schema.js.map +1 -0
  132. package/_esm/policies/types.js +2 -0
  133. package/_esm/{accounts → policies}/types.js.map +1 -1
  134. package/_esm/version.js +1 -1
  135. package/_types/accounts/evm/toEvmServerAccount.d.ts +1 -1
  136. package/_types/accounts/evm/toEvmServerAccount.d.ts.map +1 -1
  137. package/_types/accounts/evm/toEvmSmartAccount.d.ts +1 -1
  138. package/_types/accounts/evm/toEvmSmartAccount.d.ts.map +1 -1
  139. package/_types/accounts/{types.d.ts → evm/types.d.ts} +10 -7
  140. package/_types/accounts/evm/types.d.ts.map +1 -0
  141. package/_types/accounts/solana/toSolanaAccount.d.ts +20 -0
  142. package/_types/accounts/solana/toSolanaAccount.d.ts.map +1 -0
  143. package/_types/accounts/solana/types.d.ts +12 -0
  144. package/_types/accounts/solana/types.d.ts.map +1 -0
  145. package/_types/actions/evm/getUserOperation.d.ts +22 -0
  146. package/_types/actions/evm/getUserOperation.d.ts.map +1 -0
  147. package/_types/actions/evm/sendUserOperation.d.ts +1 -1
  148. package/_types/actions/evm/sendUserOperation.d.ts.map +1 -1
  149. package/_types/actions/evm/transfer/accountTransferStrategy.d.ts +1 -1
  150. package/_types/actions/evm/transfer/accountTransferStrategy.d.ts.map +1 -1
  151. package/_types/actions/evm/transfer/smartAccountTransferStrategy.d.ts +1 -1
  152. package/_types/actions/evm/transfer/smartAccountTransferStrategy.d.ts.map +1 -1
  153. package/_types/actions/evm/transfer/transfer.d.ts +3 -3
  154. package/_types/actions/evm/transfer/transfer.d.ts.map +1 -1
  155. package/_types/actions/evm/transfer/types.d.ts +39 -6
  156. package/_types/actions/evm/transfer/types.d.ts.map +1 -1
  157. package/_types/actions/evm/types.d.ts +28 -5
  158. package/_types/actions/evm/types.d.ts.map +1 -1
  159. package/_types/actions/solana/requestFaucet.d.ts +23 -0
  160. package/_types/actions/solana/requestFaucet.d.ts.map +1 -0
  161. package/_types/actions/solana/signMessage.d.ts +23 -0
  162. package/_types/actions/solana/signMessage.d.ts.map +1 -0
  163. package/_types/actions/solana/signTransaction.d.ts +38 -0
  164. package/_types/actions/solana/signTransaction.d.ts.map +1 -0
  165. package/_types/actions/solana/types.d.ts +80 -0
  166. package/_types/actions/solana/types.d.ts.map +1 -0
  167. package/_types/analytics.d.ts +14 -5
  168. package/_types/analytics.d.ts.map +1 -1
  169. package/_types/client/cdp.d.ts +3 -0
  170. package/_types/client/cdp.d.ts.map +1 -1
  171. package/_types/client/evm/evm.d.ts +89 -1
  172. package/_types/client/evm/evm.d.ts.map +1 -1
  173. package/_types/client/evm/evm.types.d.ts +36 -6
  174. package/_types/client/evm/evm.types.d.ts.map +1 -1
  175. package/_types/client/policies/index.d.ts +2 -0
  176. package/_types/client/policies/index.d.ts.map +1 -0
  177. package/_types/client/policies/policies.d.ts +216 -0
  178. package/_types/client/policies/policies.d.ts.map +1 -0
  179. package/_types/client/policies/policies.types.d.ts +96 -0
  180. package/_types/client/policies/policies.types.d.ts.map +1 -0
  181. package/_types/client/solana/solana.d.ts +47 -4
  182. package/_types/client/solana/solana.d.ts.map +1 -1
  183. package/_types/client/solana/solana.types.d.ts +21 -12
  184. package/_types/client/solana/solana.types.d.ts.map +1 -1
  185. package/_types/index.d.ts +3 -1
  186. package/_types/index.d.ts.map +1 -1
  187. package/_types/openapi-client/generated/coinbaseDeveloperPlatformAPIs.schemas.d.ts +299 -0
  188. package/_types/openapi-client/generated/coinbaseDeveloperPlatformAPIs.schemas.d.ts.map +1 -1
  189. package/_types/openapi-client/generated/evm-accounts/evm-accounts.d.ts +13 -1
  190. package/_types/openapi-client/generated/evm-accounts/evm-accounts.d.ts.map +1 -1
  191. package/_types/openapi-client/generated/evm-accounts/evm-accounts.msw.d.ts +5 -1
  192. package/_types/openapi-client/generated/evm-accounts/evm-accounts.msw.d.ts.map +1 -1
  193. package/_types/openapi-client/generated/evm-smart-accounts/evm-smart-accounts.d.ts +3 -1
  194. package/_types/openapi-client/generated/evm-smart-accounts/evm-smart-accounts.d.ts.map +1 -1
  195. package/_types/openapi-client/generated/index.msw.d.ts +3 -2
  196. package/_types/openapi-client/generated/index.msw.d.ts.map +1 -1
  197. package/_types/openapi-client/generated/policy-engine/policy-engine.d.ts +43 -0
  198. package/_types/openapi-client/generated/policy-engine/policy-engine.d.ts.map +1 -0
  199. package/_types/openapi-client/generated/policy-engine/policy-engine.msw.d.ts +25 -0
  200. package/_types/openapi-client/generated/policy-engine/policy-engine.msw.d.ts.map +1 -0
  201. package/_types/openapi-client/generated/solana-accounts/solana-accounts.d.ts +7 -1
  202. package/_types/openapi-client/generated/solana-accounts/solana-accounts.d.ts.map +1 -1
  203. package/_types/openapi-client/generated/solana-accounts/solana-accounts.msw.d.ts +2 -0
  204. package/_types/openapi-client/generated/solana-accounts/solana-accounts.msw.d.ts.map +1 -1
  205. package/_types/openapi-client/index.d.ts +19 -0
  206. package/_types/openapi-client/index.d.ts.map +1 -1
  207. package/_types/policies/schema.d.ts +922 -0
  208. package/_types/policies/schema.d.ts.map +1 -0
  209. package/_types/policies/types.d.ts +19 -0
  210. package/_types/policies/types.d.ts.map +1 -0
  211. package/_types/types/misc.d.ts +39 -0
  212. package/_types/types/misc.d.ts.map +1 -1
  213. package/_types/version.d.ts +1 -1
  214. package/accounts/evm/toEvmServerAccount.ts +7 -4
  215. package/accounts/evm/toEvmSmartAccount.ts +11 -1
  216. package/accounts/{types.ts → evm/types.ts} +10 -12
  217. package/accounts/solana/toSolanaAccount.ts +60 -0
  218. package/accounts/solana/types.ts +13 -0
  219. package/actions/evm/getUserOperation.ts +40 -0
  220. package/actions/evm/sendUserOperation.ts +1 -1
  221. package/actions/evm/transfer/accountTransferStrategy.ts +12 -8
  222. package/actions/evm/transfer/smartAccountTransferStrategy.ts +10 -7
  223. package/actions/evm/transfer/transfer.ts +17 -6
  224. package/actions/evm/transfer/types.ts +54 -11
  225. package/actions/evm/types.ts +37 -5
  226. package/actions/solana/requestFaucet.ts +35 -0
  227. package/actions/solana/signMessage.ts +36 -0
  228. package/actions/solana/signTransaction.ts +53 -0
  229. package/actions/solana/types.ts +87 -0
  230. package/analytics.ts +84 -11
  231. package/client/cdp.ts +11 -4
  232. package/client/evm/evm.ts +162 -33
  233. package/client/evm/evm.types.ts +42 -4
  234. package/client/policies/index.ts +1 -0
  235. package/client/policies/policies.ts +246 -0
  236. package/client/policies/policies.types.ts +109 -0
  237. package/client/solana/solana.ts +107 -38
  238. package/client/solana/solana.types.ts +25 -15
  239. package/index.ts +8 -1
  240. package/openapi-client/generated/coinbaseDeveloperPlatformAPIs.schemas.ts +360 -0
  241. package/openapi-client/generated/evm-accounts/evm-accounts.msw.ts +102 -0
  242. package/openapi-client/generated/evm-accounts/evm-accounts.ts +43 -0
  243. package/openapi-client/generated/evm-smart-accounts/evm-smart-accounts.ts +3 -1
  244. package/openapi-client/generated/index.msw.ts +3 -2
  245. package/openapi-client/generated/policy-engine/policy-engine.msw.ts +404 -0
  246. package/openapi-client/generated/policy-engine/policy-engine.ts +95 -0
  247. package/openapi-client/generated/solana-accounts/solana-accounts.msw.ts +75 -0
  248. package/openapi-client/generated/solana-accounts/solana-accounts.ts +23 -0
  249. package/openapi-client/index.ts +7 -0
  250. package/package.json +3 -2
  251. package/policies/schema.ts +266 -0
  252. package/policies/types.ts +19 -0
  253. package/types/misc.ts +41 -0
  254. package/version.ts +1 -1
  255. package/_types/accounts/types.d.ts.map +0 -1
  256. /package/_cjs/accounts/{types.js → evm/types.js} +0 -0
  257. /package/_esm/accounts/{types.js → evm/types.js} +0 -0
@@ -1,14 +1,21 @@
1
- import { SendUserOperationReturnType, SendUserOperationOptions } from "./sendUserOperation.js";
1
+ import { SendUserOperationOptions, SendUserOperationReturnType } from "./sendUserOperation.js";
2
+ import { GetUserOperationOptions, UserOperation } from "../../client/evm/evm.types.js";
3
+ import { EvmAccount, EvmSmartAccount } from "../../openapi-client/index.js";
2
4
 
3
- import type { ListTokenBalancesResult, ListTokenBalancesOptions } from "./listTokenBalances.js";
5
+ import type { ListTokenBalancesOptions, ListTokenBalancesResult } from "./listTokenBalances.js";
4
6
  import type { RequestFaucetOptions, RequestFaucetResult } from "./requestFaucet.js";
5
7
  import type { SendTransactionOptions, TransactionResult } from "./sendTransaction.js";
6
- import type { TransferOptions, TransferResult } from "./transfer/types.js";
8
+ import type {
9
+ AccountTransferOptions,
10
+ SmartAccountTransferOptions,
11
+ TransferResult,
12
+ } from "./transfer/types.js";
7
13
  import type {
8
14
  WaitForUserOperationOptions,
9
15
  WaitForUserOperationReturnType,
10
16
  } from "./waitForUserOperation.js";
11
- export type Actions = {
17
+
18
+ type Actions<T extends EvmAccount | EvmSmartAccount> = {
12
19
  /**
13
20
  * Transfer an amount of a token from an account to another account.
14
21
  *
@@ -92,7 +99,9 @@ export type Actions = {
92
99
  * });
93
100
  * ```
94
101
  */
95
- transfer: (options: TransferOptions) => Promise<TransferResult>;
102
+ transfer: (
103
+ options: T extends EvmSmartAccount ? SmartAccountTransferOptions : AccountTransferOptions,
104
+ ) => Promise<TransferResult>;
96
105
 
97
106
  /**
98
107
  * List the token balances of an account.
@@ -132,7 +141,9 @@ export type Actions = {
132
141
  * ```
133
142
  */
134
143
  requestFaucet: (options: Omit<RequestFaucetOptions, "address">) => Promise<RequestFaucetResult>;
144
+ };
135
145
 
146
+ export type AccountActions = Actions<EvmAccount> & {
136
147
  /**
137
148
  * Signs an EVM transaction and sends it to the specified network using the Coinbase API.
138
149
  * This method handles nonce management and gas estimation automatically.
@@ -178,7 +189,9 @@ export type Actions = {
178
189
  * ```
179
190
  */
180
191
  sendTransaction: (options: Omit<SendTransactionOptions, "address">) => Promise<TransactionResult>;
192
+ };
181
193
 
194
+ export type SmartAccountActions = Actions<EvmSmartAccount> & {
182
195
  /**
183
196
  * Sends a user operation.
184
197
  *
@@ -241,4 +254,23 @@ export type Actions = {
241
254
  waitForUserOperation: (
242
255
  options: Omit<WaitForUserOperationOptions, "smartAccountAddress">,
243
256
  ) => Promise<WaitForUserOperationReturnType>;
257
+
258
+ /**
259
+ * Gets a user operation by its hash.
260
+ *
261
+ * @param {GetUserOperationOptions} options - Parameters for getting the user operation.
262
+ * @param {string} options.userOpHash - The user operation hash.
263
+ *
264
+ * @returns A promise that resolves to the user operation.
265
+ *
266
+ * @example
267
+ * ```ts
268
+ * const userOp = await smartAccount.getUserOperation({
269
+ * userOpHash: "0x1234567890123456789012345678901234567890",
270
+ * });
271
+ * ```
272
+ */
273
+ getUserOperation: (
274
+ options: Omit<GetUserOperationOptions, "smartAccount">,
275
+ ) => Promise<UserOperation>;
244
276
  };
@@ -0,0 +1,35 @@
1
+ import { RequestFaucetOptions, SignatureResult } from "../../client/solana/solana.types.js";
2
+ import { CdpOpenApiClientType } from "../../openapi-client/index.js";
3
+
4
+ /**
5
+ * Requests funds from a Solana faucet.
6
+ *
7
+ * @param apiClient - The API client.
8
+ * @param {RequestFaucetOptions} options - Parameters for requesting funds from the Solana faucet.
9
+ * @param {string} options.address - The address to request funds for.
10
+ * @param {string} options.token - The token to request funds for.
11
+ * @param {string} [options.idempotencyKey] - An idempotency key.
12
+ *
13
+ * @returns A promise that resolves to the transaction signature.
14
+ *
15
+ * @example
16
+ * ```ts
17
+ * const signature = await requestFaucet(cdp.solana, {
18
+ * address: "1234567890123456789012345678901234567890",
19
+ * token: "sol",
20
+ * });
21
+ * ```
22
+ */
23
+ export async function requestFaucet(
24
+ apiClient: CdpOpenApiClientType,
25
+ options: RequestFaucetOptions,
26
+ ): Promise<SignatureResult> {
27
+ const signature = await apiClient.requestSolanaFaucet(
28
+ { address: options.address, token: options.token },
29
+ options.idempotencyKey,
30
+ );
31
+
32
+ return {
33
+ signature: signature.transactionSignature,
34
+ };
35
+ }
@@ -0,0 +1,36 @@
1
+ import { SignMessageOptions, SignatureResult } from "../../client/solana/solana.types.js";
2
+ import { CdpOpenApiClientType } from "../../openapi-client/index.js";
3
+
4
+ /**
5
+ * Sign a message.
6
+ *
7
+ * @param apiClient - The API client.
8
+ * @param {SignMessageOptions} options - Parameters for signing the message.
9
+ * @param {string} options.address - The address to sign the message for.
10
+ * @param {string} options.message - The message to sign.
11
+ * @param {string} [options.idempotencyKey] - An idempotency key.
12
+ *
13
+ * @returns A promise that resolves to the transaction signature.
14
+ *
15
+ * @example
16
+ * ```ts
17
+ * const signature = await signMessage(cdp.solana, {
18
+ * address: "1234567890123456789012345678901234567890",
19
+ * message: "Hello, world!",
20
+ * });
21
+ * ```
22
+ */
23
+ export async function signMessage(
24
+ apiClient: CdpOpenApiClientType,
25
+ options: SignMessageOptions,
26
+ ): Promise<SignatureResult> {
27
+ const signature = await apiClient.signSolanaMessage(
28
+ options.address,
29
+ { message: options.message },
30
+ options.idempotencyKey,
31
+ );
32
+
33
+ return {
34
+ signature: signature.signature,
35
+ };
36
+ }
@@ -0,0 +1,53 @@
1
+ import { SignTransactionOptions, SignatureResult } from "../../client/solana/solana.types.js";
2
+ import { CdpOpenApiClientType } from "../../openapi-client/index.js";
3
+
4
+ /**
5
+ * Signs a transaction.
6
+ *
7
+ * @param apiClient - The API client.
8
+ * @param {SignTransactionOptions} options - Parameters for signing the transaction.
9
+ * @param {string} options.address - The address to sign the transaction for.
10
+ * @param {string} options.transaction - The transaction to sign.
11
+ * @param {string} [options.idempotencyKey] - An idempotency key.
12
+ *
13
+ * @returns A promise that resolves to the signature.
14
+ *
15
+ * @example
16
+ * ```ts
17
+ * // Create a Solana account
18
+ * const account = await cdp.solana.createAccount();
19
+ *
20
+ * // Add your transaction instructions here
21
+ * const transaction = new Transaction()
22
+ *
23
+ * // Make sure to set requireAllSignatures to false, since signing will be done through the API
24
+ * const serializedTransaction = transaction.serialize({
25
+ * requireAllSignatures: false,
26
+ * });
27
+ *
28
+ * // Base64 encode the serialized transaction
29
+ * const transaction = Buffer.from(serializedTransaction).toString("base64");
30
+ *
31
+ * // When you want to sign a transaction, you can do so by address and base64 encoded transaction
32
+ * const signature = await signTransaction(cdp.solana, {
33
+ * address: account.address,
34
+ * transaction,
35
+ * });
36
+ * ```
37
+ */
38
+ export async function signTransaction(
39
+ apiClient: CdpOpenApiClientType,
40
+ options: SignTransactionOptions,
41
+ ): Promise<SignatureResult> {
42
+ const signature = await apiClient.signSolanaTransaction(
43
+ options.address,
44
+ {
45
+ transaction: options.transaction,
46
+ },
47
+ options.idempotencyKey,
48
+ );
49
+
50
+ return {
51
+ signature: signature.signedTransaction,
52
+ };
53
+ }
@@ -0,0 +1,87 @@
1
+ import {
2
+ RequestFaucetOptions,
3
+ SignatureResult,
4
+ SignMessageOptions,
5
+ SignTransactionOptions,
6
+ } from "../../client/solana/solana.types.js";
7
+
8
+ export type AccountActions = {
9
+ /**
10
+ * Requests funds from a Solana faucet.
11
+ *
12
+ * @param {RequestFaucetOptions} options - Parameters for requesting funds from the Solana faucet.
13
+ * @param {string} options.token - The token to request funds for.
14
+ * @param {string} [options.idempotencyKey] - An idempotency key.
15
+ *
16
+ * @returns A promise that resolves to the transaction hash.
17
+ *
18
+ * @example
19
+ * ```ts
20
+ * // Create a Solana account
21
+ * const account = await cdp.solana.createAccount();
22
+ *
23
+ * // Request funds from the Solana faucet
24
+ * const result = await account.requestFaucet({
25
+ * token: "sol",
26
+ * });
27
+ * ```
28
+ */
29
+ requestFaucet: (options: Omit<RequestFaucetOptions, "address">) => Promise<SignatureResult>;
30
+
31
+ /**
32
+ * Signs a message.
33
+ *
34
+ * @param {SignMessageOptions} options - Parameters for signing the message.
35
+ * @param {string} options.address - The address to sign the message for.
36
+ * @param {string} options.message - The message to sign.
37
+ * @param {string} [options.idempotencyKey] - An idempotency key.
38
+ *
39
+ * @returns A promise that resolves to the signature.
40
+ *
41
+ * @example
42
+ * ```ts
43
+ * // Create a Solana account
44
+ * const account = await cdp.solana.createAccount();
45
+ *
46
+ * // Sign a message
47
+ * const { signature } = await account.signMessage({
48
+ * message: "Hello, world!",
49
+ * });
50
+ * ```
51
+ */
52
+ signMessage: (options: Omit<SignMessageOptions, "address">) => Promise<SignatureResult>;
53
+
54
+ /**
55
+ * Signs a transaction.
56
+ *
57
+ * @param {SignTransactionOptions} options - Parameters for signing the transaction.
58
+ * @param {string} options.address - The address to sign the transaction for.
59
+ * @param {string} options.transaction - The transaction to sign.
60
+ * @param {string} [options.idempotencyKey] - An idempotency key.
61
+ *
62
+ * @returns A promise that resolves to the signature.
63
+ *
64
+ * @example
65
+ * ```ts
66
+ * // Create a Solana account
67
+ * const account = await cdp.solana.createAccount();
68
+ *
69
+ * // Add your transaction instructions here
70
+ * const transaction = new Transaction()
71
+ *
72
+ * // Make sure to set requireAllSignatures to false, since signing will be done through the API
73
+ * const serializedTransaction = transaction.serialize({
74
+ * requireAllSignatures: false,
75
+ * });
76
+ *
77
+ * // Base64 encode the serialized transaction
78
+ * const transaction = Buffer.from(serializedTransaction).toString("base64");
79
+ *
80
+ * // When you want to sign a transaction, you can do so by address and base64 encoded transaction
81
+ * const { signature } = await account.signTransaction({
82
+ * transaction,
83
+ * });
84
+ * ```
85
+ */
86
+ signTransaction: (options: Omit<SignTransactionOptions, "address">) => Promise<SignatureResult>;
87
+ };
package/analytics.ts CHANGED
@@ -1,5 +1,7 @@
1
1
  import md5 from "md5";
2
2
 
3
+ import { APIError } from "./openapi-client/errors.js";
4
+
3
5
  /**
4
6
  * The data in an error event
5
7
  */
@@ -22,24 +24,33 @@ type ErrorEventData = {
22
24
  name: "error";
23
25
  };
24
26
 
25
- type EventData = ErrorEventData & {
26
- apiKeyId: string;
27
- };
27
+ type EventData = ErrorEventData;
28
28
 
29
29
  // This is a public client id for the analytics service
30
30
  const publicClientId = "54f2ee2fb3d2b901a829940d70fbfc13";
31
31
 
32
+ export const Analytics = {
33
+ identifier: "", // set in cdp.ts
34
+ wrapClassWithErrorTracking,
35
+ wrapObjectMethodsWithErrorTracking,
36
+ sendEvent,
37
+ };
38
+
32
39
  /**
33
40
  * Sends an analytics event to the default endpoint
34
41
  *
35
42
  * @param event - The event data containing event-specific fields
36
43
  * @returns Promise that resolves when the event is sent
37
44
  */
38
- export async function sendEvent(event: EventData): Promise<void> {
45
+ async function sendEvent(event: EventData): Promise<void> {
46
+ if (process.env.DISABLE_CDP_ERROR_REPORTING === "true") {
47
+ return;
48
+ }
49
+
39
50
  const timestamp = Date.now();
40
51
 
41
52
  const enhancedEvent = {
42
- user_id: event.apiKeyId,
53
+ user_id: Analytics.identifier,
43
54
  event_type: event.name,
44
55
  platform: "server",
45
56
  timestamp,
@@ -80,10 +91,13 @@ export async function sendEvent(event: EventData): Promise<void> {
80
91
  * Wraps all methods of a class with error tracking.
81
92
  *
82
93
  * @param ClassToWrap - The class whose prototype methods should be wrapped.
83
- * @param apiKeyId - The API key ID to use for the error tracking.
84
94
  */
85
95
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
86
- export function wrapClassWithErrorTracking(ClassToWrap: any, apiKeyId: string): void {
96
+ function wrapClassWithErrorTracking(ClassToWrap: any): void {
97
+ if (process.env.DISABLE_CDP_ERROR_REPORTING === "true") {
98
+ return;
99
+ }
100
+
87
101
  const methods = Object.getOwnPropertyNames(ClassToWrap.prototype).filter(
88
102
  name => name !== "constructor" && typeof ClassToWrap.prototype[name] === "function",
89
103
  );
@@ -94,14 +108,13 @@ export function wrapClassWithErrorTracking(ClassToWrap: any, apiKeyId: string):
94
108
  try {
95
109
  return await originalMethod.apply(this, args);
96
110
  } catch (error) {
97
- if (!(error instanceof Error)) {
98
- return;
111
+ if (!shouldTrackError(error)) {
112
+ throw error;
99
113
  }
100
114
 
101
- const { message, stack } = error;
115
+ const { message, stack } = error as Error;
102
116
 
103
117
  sendEvent({
104
- apiKeyId,
105
118
  method,
106
119
  message,
107
120
  stack,
@@ -115,3 +128,63 @@ export function wrapClassWithErrorTracking(ClassToWrap: any, apiKeyId: string):
115
128
  };
116
129
  }
117
130
  }
131
+
132
+ /**
133
+ * Wraps all methods of an object with error tracking.
134
+ *
135
+ * @param object - The object whose methods should be wrapped.
136
+ */
137
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
138
+ function wrapObjectMethodsWithErrorTracking(object: any): void {
139
+ if (process.env.DISABLE_CDP_ERROR_REPORTING === "true") {
140
+ return;
141
+ }
142
+
143
+ const methods = Object.getOwnPropertyNames(object).filter(
144
+ name => name !== "constructor" && typeof object[name] === "function",
145
+ );
146
+
147
+ for (const method of methods) {
148
+ const originalMethod = object[method];
149
+ object[method] = async function (...args: unknown[]) {
150
+ try {
151
+ return await originalMethod.apply(this, args);
152
+ } catch (error) {
153
+ if (!shouldTrackError(error)) {
154
+ throw error;
155
+ }
156
+
157
+ const { message, stack } = error as Error;
158
+
159
+ sendEvent({
160
+ method,
161
+ message,
162
+ stack,
163
+ name: "error",
164
+ }).catch(() => {
165
+ // ignore error
166
+ });
167
+
168
+ throw error;
169
+ }
170
+ };
171
+ }
172
+ }
173
+
174
+ /**
175
+ * Filters out non-errors and API errors
176
+ *
177
+ * @param error - The error to check.
178
+ * @returns True if the error should be tracked, false otherwise.
179
+ */
180
+ function shouldTrackError(error: unknown): boolean {
181
+ if (!(error instanceof Error)) {
182
+ return false;
183
+ }
184
+
185
+ if (error instanceof APIError) {
186
+ return false;
187
+ }
188
+
189
+ return true;
190
+ }
package/client/cdp.ts CHANGED
@@ -1,7 +1,8 @@
1
- import { wrapClassWithErrorTracking } from "../analytics.js";
1
+ import { Analytics } from "../analytics.js";
2
2
  import { CdpOpenApiClient } from "../openapi-client/index.js";
3
3
  import { version } from "../version.js";
4
4
  import { EvmClient } from "./evm/evm.js";
5
+ import { PoliciesClient } from "./policies/policies.js";
5
6
  import { SolanaClient } from "./solana/solana.js";
6
7
 
7
8
  interface CdpClientOptions {
@@ -27,6 +28,9 @@ export class CdpClient {
27
28
  /** Namespace containing all Solana methods. */
28
29
  public solana: SolanaClient;
29
30
 
31
+ /** Namespace containing all Policies methods. */
32
+ public policies: PoliciesClient;
33
+
30
34
  /**
31
35
  * The CdpClient is the main class for interacting with the CDP API.
32
36
  *
@@ -109,12 +113,15 @@ For more information, see: https://github.com/coinbase/cdp-sdk/blob/main/typescr
109
113
  });
110
114
 
111
115
  if (process.env.DISABLE_CDP_ERROR_REPORTING !== "true") {
112
- wrapClassWithErrorTracking(CdpClient, apiKeyId);
113
- wrapClassWithErrorTracking(EvmClient, apiKeyId);
114
- wrapClassWithErrorTracking(SolanaClient, apiKeyId);
116
+ Analytics.identifier = apiKeyId;
117
+ Analytics.wrapClassWithErrorTracking(CdpClient);
118
+ Analytics.wrapClassWithErrorTracking(EvmClient);
119
+ Analytics.wrapClassWithErrorTracking(SolanaClient);
120
+ Analytics.wrapClassWithErrorTracking(PoliciesClient);
115
121
  }
116
122
 
117
123
  this.evm = new EvmClient();
118
124
  this.solana = new SolanaClient();
125
+ this.policies = new PoliciesClient();
119
126
  }
120
127
  }