@coinbase/cdp-sdk 1.19.0 → 1.21.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 (154) hide show
  1. package/CHANGELOG.md +28 -0
  2. package/README.md +8 -0
  3. package/_cjs/accounts/evm/chainToNetworkMapper.js +33 -0
  4. package/_cjs/accounts/evm/chainToNetworkMapper.js.map +1 -0
  5. package/_cjs/accounts/evm/getBaseNodeRpcUrl.js +38 -0
  6. package/_cjs/accounts/evm/getBaseNodeRpcUrl.js.map +1 -0
  7. package/_cjs/accounts/evm/networkCapabilities.js +91 -0
  8. package/_cjs/accounts/evm/networkCapabilities.js.map +1 -0
  9. package/_cjs/accounts/evm/resolveViemClients.js +2 -35
  10. package/_cjs/accounts/evm/resolveViemClients.js.map +1 -1
  11. package/_cjs/accounts/evm/toEvmServerAccount.js +1 -1
  12. package/_cjs/accounts/evm/toEvmServerAccount.js.map +1 -1
  13. package/_cjs/accounts/evm/toEvmSmartAccount.js +2 -2
  14. package/_cjs/accounts/evm/toEvmSmartAccount.js.map +1 -1
  15. package/_cjs/accounts/evm/toNetworkScopedEvmServerAccount.js +87 -18
  16. package/_cjs/accounts/evm/toNetworkScopedEvmServerAccount.js.map +1 -1
  17. package/_cjs/accounts/evm/toNetworkScopedEvmSmartAccount.js +126 -2
  18. package/_cjs/accounts/evm/toNetworkScopedEvmSmartAccount.js.map +1 -1
  19. package/_cjs/actions/evm/sendUserOperation.js +8 -1
  20. package/_cjs/actions/evm/sendUserOperation.js.map +1 -1
  21. package/_cjs/actions/evm/transfer/smartAccountTransferStrategy.js +3 -2
  22. package/_cjs/actions/evm/transfer/smartAccountTransferStrategy.js.map +1 -1
  23. package/_cjs/actions/evm/transfer/transfer.js +4 -0
  24. package/_cjs/actions/evm/transfer/transfer.js.map +1 -1
  25. package/_cjs/actions/evm/transfer/transferWithViem.js +53 -0
  26. package/_cjs/actions/evm/transfer/transferWithViem.js.map +1 -0
  27. package/_cjs/actions/evm/transfer/types.js +25 -0
  28. package/_cjs/actions/evm/transfer/types.js.map +1 -1
  29. package/_cjs/client/evm/evm.js +2 -2
  30. package/_cjs/client/evm/evm.js.map +1 -1
  31. package/_cjs/client/solana/solana.js +74 -0
  32. package/_cjs/client/solana/solana.js.map +1 -1
  33. package/_cjs/constants.js +18 -1
  34. package/_cjs/constants.js.map +1 -1
  35. package/_cjs/openapi-client/generated/coinbaseDeveloperPlatformAPIs.schemas.js +2 -0
  36. package/_cjs/openapi-client/generated/coinbaseDeveloperPlatformAPIs.schemas.js.map +1 -1
  37. package/_cjs/openapi-client/generated/index.msw.js +4 -4
  38. package/_cjs/openapi-client/generated/index.msw.js.map +1 -1
  39. package/_cjs/openapi-client/generated/solana-accounts/solana-accounts.js +10 -1
  40. package/_cjs/openapi-client/generated/solana-accounts/solana-accounts.js.map +1 -1
  41. package/_cjs/version.js +1 -1
  42. package/_esm/accounts/evm/chainToNetworkMapper.js +30 -0
  43. package/_esm/accounts/evm/chainToNetworkMapper.js.map +1 -0
  44. package/_esm/accounts/evm/getBaseNodeRpcUrl.js +35 -0
  45. package/_esm/accounts/evm/getBaseNodeRpcUrl.js.map +1 -0
  46. package/_esm/accounts/evm/networkCapabilities.js +86 -0
  47. package/_esm/accounts/evm/networkCapabilities.js.map +1 -0
  48. package/_esm/accounts/evm/resolveViemClients.js +1 -34
  49. package/_esm/accounts/evm/resolveViemClients.js.map +1 -1
  50. package/_esm/accounts/evm/toEvmServerAccount.js +1 -1
  51. package/_esm/accounts/evm/toEvmServerAccount.js.map +1 -1
  52. package/_esm/accounts/evm/toEvmSmartAccount.js +2 -2
  53. package/_esm/accounts/evm/toEvmSmartAccount.js.map +1 -1
  54. package/_esm/accounts/evm/toNetworkScopedEvmServerAccount.js +88 -19
  55. package/_esm/accounts/evm/toNetworkScopedEvmServerAccount.js.map +1 -1
  56. package/_esm/accounts/evm/toNetworkScopedEvmSmartAccount.js +126 -2
  57. package/_esm/accounts/evm/toNetworkScopedEvmSmartAccount.js.map +1 -1
  58. package/_esm/actions/evm/sendUserOperation.js +8 -1
  59. package/_esm/actions/evm/sendUserOperation.js.map +1 -1
  60. package/_esm/actions/evm/transfer/smartAccountTransferStrategy.js +3 -2
  61. package/_esm/actions/evm/transfer/smartAccountTransferStrategy.js.map +1 -1
  62. package/_esm/actions/evm/transfer/transfer.js +4 -0
  63. package/_esm/actions/evm/transfer/transfer.js.map +1 -1
  64. package/_esm/actions/evm/transfer/transferWithViem.js +50 -0
  65. package/_esm/actions/evm/transfer/transferWithViem.js.map +1 -0
  66. package/_esm/actions/evm/transfer/types.js +23 -1
  67. package/_esm/actions/evm/transfer/types.js.map +1 -1
  68. package/_esm/client/evm/evm.js +2 -2
  69. package/_esm/client/evm/evm.js.map +1 -1
  70. package/_esm/client/solana/solana.js +71 -0
  71. package/_esm/client/solana/solana.js.map +1 -1
  72. package/_esm/constants.js +17 -0
  73. package/_esm/constants.js.map +1 -1
  74. package/_esm/openapi-client/generated/coinbaseDeveloperPlatformAPIs.schemas.js +2 -0
  75. package/_esm/openapi-client/generated/coinbaseDeveloperPlatformAPIs.schemas.js.map +1 -1
  76. package/_esm/openapi-client/generated/index.msw.js +4 -4
  77. package/_esm/openapi-client/generated/index.msw.js.map +1 -1
  78. package/_esm/openapi-client/generated/solana-accounts/solana-accounts.js +8 -0
  79. package/_esm/openapi-client/generated/solana-accounts/solana-accounts.js.map +1 -1
  80. package/_esm/version.js +1 -1
  81. package/_types/accounts/evm/chainToNetworkMapper.d.ts +11 -0
  82. package/_types/accounts/evm/chainToNetworkMapper.d.ts.map +1 -0
  83. package/_types/accounts/evm/getBaseNodeRpcUrl.d.ts +8 -0
  84. package/_types/accounts/evm/getBaseNodeRpcUrl.d.ts.map +1 -0
  85. package/_types/accounts/evm/networkCapabilities.d.ts +103 -0
  86. package/_types/accounts/evm/networkCapabilities.d.ts.map +1 -0
  87. package/_types/accounts/evm/resolveViemClients.d.ts.map +1 -1
  88. package/_types/accounts/evm/toEvmServerAccount.d.ts.map +1 -1
  89. package/_types/accounts/evm/toEvmSmartAccount.d.ts.map +1 -1
  90. package/_types/accounts/evm/toNetworkScopedEvmServerAccount.d.ts +3 -3
  91. package/_types/accounts/evm/toNetworkScopedEvmServerAccount.d.ts.map +1 -1
  92. package/_types/accounts/evm/toNetworkScopedEvmSmartAccount.d.ts +9 -6
  93. package/_types/accounts/evm/toNetworkScopedEvmSmartAccount.d.ts.map +1 -1
  94. package/_types/accounts/evm/types.d.ts +109 -12
  95. package/_types/accounts/evm/types.d.ts.map +1 -1
  96. package/_types/actions/evm/sendUserOperation.d.ts.map +1 -1
  97. package/_types/actions/evm/swap/types.d.ts +9 -4
  98. package/_types/actions/evm/swap/types.d.ts.map +1 -1
  99. package/_types/actions/evm/transfer/smartAccountTransferStrategy.d.ts.map +1 -1
  100. package/_types/actions/evm/transfer/transfer.d.ts.map +1 -1
  101. package/_types/actions/evm/transfer/transferWithViem.d.ts +16 -0
  102. package/_types/actions/evm/transfer/transferWithViem.d.ts.map +1 -0
  103. package/_types/actions/evm/transfer/types.d.ts +17 -1
  104. package/_types/actions/evm/transfer/types.d.ts.map +1 -1
  105. package/_types/client/evm/evm.d.ts.map +1 -1
  106. package/_types/client/evm/evm.types.d.ts +1 -1
  107. package/_types/client/evm/evm.types.d.ts.map +1 -1
  108. package/_types/client/solana/solana.d.ts +40 -1
  109. package/_types/client/solana/solana.d.ts.map +1 -1
  110. package/_types/client/solana/solana.types.d.ts +15 -1
  111. package/_types/client/solana/solana.types.d.ts.map +1 -1
  112. package/_types/constants.d.ts +4 -0
  113. package/_types/constants.d.ts.map +1 -1
  114. package/_types/openapi-client/generated/coinbaseDeveloperPlatformAPIs.schemas.d.ts +2 -0
  115. package/_types/openapi-client/generated/coinbaseDeveloperPlatformAPIs.schemas.d.ts.map +1 -1
  116. package/_types/openapi-client/generated/index.msw.d.ts +4 -4
  117. package/_types/openapi-client/generated/index.msw.d.ts.map +1 -1
  118. package/_types/openapi-client/generated/solana-accounts/solana-accounts.d.ts +5 -0
  119. package/_types/openapi-client/generated/solana-accounts/solana-accounts.d.ts.map +1 -1
  120. package/_types/openapi-client/index.d.ts +10 -0
  121. package/_types/openapi-client/index.d.ts.map +1 -1
  122. package/_types/version.d.ts +1 -1
  123. package/accounts/evm/chainToNetworkMapper.ts +37 -0
  124. package/accounts/evm/getBaseNodeRpcUrl.ts +40 -0
  125. package/accounts/evm/networkCapabilities.ts +117 -0
  126. package/accounts/evm/resolveViemClients.ts +1 -38
  127. package/accounts/evm/toEvmServerAccount.ts +3 -3
  128. package/accounts/evm/toEvmSmartAccount.ts +3 -3
  129. package/accounts/evm/toNetworkScopedEvmServerAccount.ts +116 -29
  130. package/accounts/evm/toNetworkScopedEvmSmartAccount.ts +178 -11
  131. package/accounts/evm/types.ts +253 -23
  132. package/actions/evm/sendUserOperation.ts +9 -1
  133. package/actions/evm/swap/types.ts +11 -3
  134. package/actions/evm/transfer/smartAccountTransferStrategy.ts +5 -2
  135. package/actions/evm/transfer/transfer.ts +8 -0
  136. package/actions/evm/transfer/transferWithViem.ts +66 -0
  137. package/actions/evm/transfer/types.ts +31 -1
  138. package/client/evm/evm.ts +2 -2
  139. package/client/evm/evm.types.ts +1 -1
  140. package/client/solana/solana.ts +89 -0
  141. package/client/solana/solana.types.ts +16 -0
  142. package/constants.ts +18 -0
  143. package/openapi-client/generated/coinbaseDeveloperPlatformAPIs.schemas.ts +2 -0
  144. package/openapi-client/generated/index.msw.ts +4 -4
  145. package/openapi-client/generated/solana-accounts/solana-accounts.ts +20 -0
  146. package/package.json +1 -1
  147. package/version.ts +1 -1
  148. package/_cjs/client/evm/constants.js +0 -21
  149. package/_cjs/client/evm/constants.js.map +0 -1
  150. package/_esm/client/evm/constants.js +0 -18
  151. package/_esm/client/evm/constants.js.map +0 -1
  152. package/_types/client/evm/constants.d.ts +0 -5
  153. package/_types/client/evm/constants.d.ts.map +0 -1
  154. package/client/evm/constants.ts +0 -17
@@ -11,8 +11,7 @@ import {
11
11
  import { toAccount } from "viem/accounts";
12
12
  import * as chains from "viem/chains";
13
13
 
14
- import { generateJwt } from "../../auth/utils/jwt.js";
15
- import { config } from "../../openapi-client/cdpApiClient.js";
14
+ import { getBaseNodeRpcUrl } from "./getBaseNodeRpcUrl.js";
16
15
 
17
16
  import type { EvmAccount } from "./types.js";
18
17
 
@@ -135,42 +134,6 @@ export type ResolvedViemClients = {
135
134
  walletClient: WalletClient<Transport, Chain, Account>;
136
135
  };
137
136
 
138
- /**
139
- * Get the base node RPC URL for a given network
140
- *
141
- * @param network - The network identifier
142
- * @returns The base node RPC URL or undefined if the network is not supported
143
- */
144
- async function getBaseNodeRpcUrl(network: "base" | "base-sepolia"): Promise<string | undefined> {
145
- if (!config) {
146
- return;
147
- }
148
-
149
- try {
150
- const basePath = config.basePath?.replace("/platform", "");
151
-
152
- const jwt = await generateJwt({
153
- apiKeyId: config.apiKeyId,
154
- apiKeySecret: config.apiKeySecret,
155
- requestMethod: "GET",
156
- requestHost: basePath.replace("https://", ""),
157
- requestPath: "/apikeys/v1/tokens/active",
158
- });
159
-
160
- const response = await fetch(`${basePath}/apikeys/v1/tokens/active`, {
161
- headers: {
162
- Authorization: `Bearer ${jwt}`,
163
- "Content-Type": "application/json",
164
- },
165
- });
166
-
167
- const json = await response.json();
168
- return `${basePath}/rpc/v1/${network}/${json.id}`;
169
- } catch {
170
- return;
171
- }
172
- }
173
-
174
137
  /**
175
138
  * Resolves viem clients based on a network identifier or Node URL.
176
139
  *
@@ -33,7 +33,7 @@ import { sendSwapTransaction } from "../../actions/evm/swap/sendSwapTransaction.
33
33
  import { accountTransferStrategy } from "../../actions/evm/transfer/accountTransferStrategy.js";
34
34
  import { transfer } from "../../actions/evm/transfer/transfer.js";
35
35
 
36
- import type { EvmServerAccount } from "./types.js";
36
+ import type { EvmServerAccount, NetworkOrRpcUrl } from "./types.js";
37
37
  import type {
38
38
  SendTransactionOptions,
39
39
  TransactionResult,
@@ -169,8 +169,8 @@ export function toEvmServerAccount(
169
169
  name: options.account.name,
170
170
  type: "evm-server",
171
171
  policies: options.account.policies,
172
- useNetwork: async (networkOrRpcUrl: string) => {
173
- return toNetworkScopedEvmServerAccount(apiClient, {
172
+ useNetwork: async <Network extends NetworkOrRpcUrl>(networkOrRpcUrl: Network) => {
173
+ return toNetworkScopedEvmServerAccount({
174
174
  account,
175
175
  network: networkOrRpcUrl,
176
176
  });
@@ -35,7 +35,7 @@ import {
35
35
  } from "../../actions/evm/waitForUserOperation.js";
36
36
  import { GetUserOperationOptions, UserOperation } from "../../client/evm/evm.types.js";
37
37
 
38
- import type { EvmAccount, EvmSmartAccount } from "./types.js";
38
+ import type { EvmAccount, EvmSmartAccount, KnownEvmNetworks } from "./types.js";
39
39
  import type {
40
40
  SmartAccountQuoteSwapOptions,
41
41
  SmartAccountQuoteSwapResult,
@@ -156,11 +156,11 @@ export function toEvmSmartAccount(
156
156
 
157
157
  name: options.smartAccount.name,
158
158
  type: "evm-smart",
159
- __experimental_useNetwork: async (networkOrRpcUrl: string) => {
159
+ useNetwork: async <Network extends KnownEvmNetworks>(network: Network) => {
160
160
  return toNetworkScopedEvmSmartAccount(apiClient, {
161
161
  smartAccount: account,
162
162
  owner: options.owner,
163
- network: networkOrRpcUrl,
163
+ network,
164
164
  });
165
165
  },
166
166
  };
@@ -1,12 +1,27 @@
1
1
  import { WaitForTransactionReceiptParameters } from "viem";
2
- import { base, baseSepolia, sepolia } from "viem/chains";
2
+ import { base, baseSepolia, mainnet, sepolia } from "viem/chains";
3
3
 
4
+ import { mapChainToNetwork } from "./chainToNetworkMapper.js";
5
+ import { isMethodSupportedOnNetwork } from "./networkCapabilities.js";
4
6
  import { resolveViemClients } from "./resolveViemClients.js";
7
+ import { transferWithViem } from "../../actions/evm/transfer/transferWithViem.js";
5
8
 
6
9
  import type { EvmServerAccount, NetworkScopedEvmServerAccount } from "./types.js";
10
+ import type { FundOptions } from "../../actions/evm/fund/fund.js";
11
+ import type { QuoteFundOptions } from "../../actions/evm/fund/quoteFund.js";
12
+ import type { WaitForFundOperationOptions } from "../../actions/evm/fund/waitForFundOperationReceipt.js";
13
+ import type { ListTokenBalancesOptions } from "../../actions/evm/listTokenBalances.js";
7
14
  import type { RequestFaucetOptions } from "../../actions/evm/requestFaucet.js";
8
- import type { TransactionResult } from "../../actions/evm/sendTransaction.js";
9
- import type { CdpOpenApiClientType } from "../../openapi-client/index.js";
15
+ import type {
16
+ SendTransactionOptions,
17
+ TransactionResult,
18
+ } from "../../actions/evm/sendTransaction.js";
19
+ import type { AccountQuoteSwapOptions, AccountSwapOptions } from "../../actions/evm/swap/types.js";
20
+ import type { TransferOptions } from "../../actions/evm/transfer/types.js";
21
+ import type {
22
+ ListEvmTokenBalancesNetwork,
23
+ SendEvmTransactionBodyNetwork,
24
+ } from "../../openapi-client/index.js";
10
25
  import type { Address, TransactionRequestEIP1559 } from "../../types/misc.js";
11
26
 
12
27
  /**
@@ -23,24 +38,26 @@ export type ToNetworkScopedEvmServerAccountOptions = {
23
38
  * Creates a Network-scoped Server-managed EvmAccount instance from an existing EvmAccount.
24
39
  * Use this to interact with previously deployed EvmAccounts on a specific network.
25
40
  *
26
- * @param {CdpOpenApiClientType} apiClient - The API client.
27
41
  * @param {ToNetworkScopedEvmServerAccountOptions} options - Configuration options.
28
42
  * @param {EvmServerAccount} options.account - The EvmServerAccount that was previously created.
29
43
  * @param {string} options.network - The network to scope the account to.
30
44
  * @returns {NetworkScopedEvmServerAccount} A configured NetworkScopedEvmServerAccount instance ready for signing.
31
45
  */
32
- export async function toNetworkScopedEvmServerAccount(
33
- apiClient: CdpOpenApiClientType,
34
- options: ToNetworkScopedEvmServerAccountOptions,
35
- ): Promise<NetworkScopedEvmServerAccount> {
46
+ export async function toNetworkScopedEvmServerAccount<Network extends string>(
47
+ options: ToNetworkScopedEvmServerAccountOptions & { network: Network },
48
+ ): Promise<NetworkScopedEvmServerAccount<Network>> {
36
49
  const { publicClient, walletClient, chain } = await resolveViemClients({
37
50
  networkOrNodeUrl: options.network,
38
51
  account: options.account,
39
52
  });
40
53
 
41
- const shouldUseApi = chain.id === base.id || chain.id === baseSepolia.id;
54
+ const shouldUseApiForSends =
55
+ chain.id === base.id ||
56
+ chain.id === baseSepolia.id ||
57
+ chain.id === mainnet.id ||
58
+ chain.id === sepolia.id;
42
59
 
43
- const account: NetworkScopedEvmServerAccount = {
60
+ const account = {
44
61
  address: options.account.address as Address,
45
62
  network: options.network,
46
63
  signMessage: options.account.signMessage,
@@ -50,22 +67,11 @@ export async function toNetworkScopedEvmServerAccount(
50
67
  name: options.account.name,
51
68
  type: "evm-server",
52
69
  policies: options.account.policies,
53
- requestFaucet: async (faucetOptions: Omit<RequestFaucetOptions, "address" | "network">) => {
54
- if (chain.id !== baseSepolia.id && chain.id !== sepolia.id) {
55
- throw new Error(
56
- "Requesting a faucet is supported only on base-sepolia or ethereum-sepolia",
57
- );
58
- }
59
- return options.account.requestFaucet({
60
- ...faucetOptions,
61
- network: chain.id === baseSepolia.id ? "base-sepolia" : "ethereum-sepolia",
62
- });
63
- },
64
- sendTransaction: async txOpts => {
65
- if (shouldUseApi) {
70
+ sendTransaction: async (txOpts: Omit<SendTransactionOptions, "address" | "network">) => {
71
+ if (shouldUseApiForSends) {
66
72
  return options.account.sendTransaction({
67
73
  ...txOpts,
68
- network: chain.id === base.id ? "base" : "base-sepolia",
74
+ network: mapChainToNetwork(chain) as SendEvmTransactionBodyNetwork,
69
75
  });
70
76
  } else {
71
77
  const hash = await walletClient.sendTransaction(
@@ -74,17 +80,98 @@ export async function toNetworkScopedEvmServerAccount(
74
80
  return { transactionHash: hash };
75
81
  }
76
82
  },
83
+ transfer: async (transferArgs: Omit<TransferOptions, "address" | "network">) => {
84
+ if (shouldUseApiForSends) {
85
+ return options.account.transfer({
86
+ ...transferArgs,
87
+ network: mapChainToNetwork(chain) as SendEvmTransactionBodyNetwork,
88
+ });
89
+ } else {
90
+ return transferWithViem(walletClient, account, transferArgs);
91
+ }
92
+ },
77
93
  waitForTransactionReceipt: async (
78
- options: WaitForTransactionReceiptParameters | TransactionResult,
94
+ waitOptions: WaitForTransactionReceiptParameters | TransactionResult,
79
95
  ) => {
80
- if ("transactionHash" in options) {
96
+ if ("transactionHash" in waitOptions) {
81
97
  return publicClient.waitForTransactionReceipt({
82
- hash: options.transactionHash,
98
+ hash: waitOptions.transactionHash,
83
99
  });
84
100
  }
85
- return publicClient.waitForTransactionReceipt(options);
101
+ return publicClient.waitForTransactionReceipt(waitOptions);
86
102
  },
87
- };
103
+ } as NetworkScopedEvmServerAccount<Network>;
104
+
105
+ if (isMethodSupportedOnNetwork("listTokenBalances", options.network)) {
106
+ Object.assign(account, {
107
+ listTokenBalances: async (
108
+ listTokenBalancesOptions: Omit<ListTokenBalancesOptions, "address" | "network">,
109
+ ) => {
110
+ return options.account.listTokenBalances({
111
+ ...listTokenBalancesOptions,
112
+ network: options.network as ListEvmTokenBalancesNetwork,
113
+ });
114
+ },
115
+ });
116
+ }
117
+
118
+ if (isMethodSupportedOnNetwork("requestFaucet", options.network)) {
119
+ Object.assign(account, {
120
+ requestFaucet: async (faucetOptions: Omit<RequestFaucetOptions, "address" | "network">) => {
121
+ return options.account.requestFaucet({
122
+ ...faucetOptions,
123
+ network: chain.id === baseSepolia.id ? "base-sepolia" : "ethereum-sepolia",
124
+ });
125
+ },
126
+ });
127
+ }
128
+
129
+ if (isMethodSupportedOnNetwork("quoteFund", options.network)) {
130
+ Object.assign(account, {
131
+ quoteFund: async (quoteFundOptions: Omit<QuoteFundOptions, "address">) => {
132
+ return options.account.quoteFund({
133
+ ...quoteFundOptions,
134
+ });
135
+ },
136
+ });
137
+ }
138
+
139
+ if (isMethodSupportedOnNetwork("fund", options.network)) {
140
+ Object.assign(account, {
141
+ fund: async (fundOptions: Omit<FundOptions, "address">) => {
142
+ return options.account.fund({
143
+ ...fundOptions,
144
+ });
145
+ },
146
+ waitForFundOperationReceipt: async (waitOptions: WaitForFundOperationOptions) => {
147
+ return options.account.waitForFundOperationReceipt(waitOptions);
148
+ },
149
+ });
150
+ }
151
+
152
+ if (isMethodSupportedOnNetwork("transfer", options.network)) {
153
+ Object.assign(account, {
154
+ transfer: async (transferOptions: TransferOptions) => {
155
+ return options.account.transfer(transferOptions);
156
+ },
157
+ });
158
+ }
159
+
160
+ if (isMethodSupportedOnNetwork("quoteSwap", options.network)) {
161
+ Object.assign(account, {
162
+ quoteSwap: async (quoteSwapOptions: AccountQuoteSwapOptions) => {
163
+ return options.account.quoteSwap(quoteSwapOptions);
164
+ },
165
+ });
166
+ }
167
+
168
+ if (isMethodSupportedOnNetwork("swap", options.network)) {
169
+ Object.assign(account, {
170
+ swap: async (swapOptions: AccountSwapOptions) => {
171
+ return options.account.swap(swapOptions);
172
+ },
173
+ });
174
+ }
88
175
 
89
176
  return account;
90
177
  }
@@ -1,14 +1,52 @@
1
- import type { EvmAccount, EvmSmartAccount, NetworkScopedEvmSmartAccount } from "./types.js";
2
- import type { CdpOpenApiClientType } from "../../openapi-client/index.js";
1
+ import { getBaseNodeRpcUrl } from "./getBaseNodeRpcUrl.js";
2
+ import { isMethodSupportedOnNetwork } from "./networkCapabilities.js";
3
+ import { fund } from "../../actions/evm/fund/fund.js";
4
+ import { quoteFund } from "../../actions/evm/fund/quoteFund.js";
5
+ import { waitForFundOperationReceipt } from "../../actions/evm/fund/waitForFundOperationReceipt.js";
6
+ import { getUserOperation } from "../../actions/evm/getUserOperation.js";
7
+ import { listTokenBalances } from "../../actions/evm/listTokenBalances.js";
8
+ import { requestFaucet } from "../../actions/evm/requestFaucet.js";
9
+ import { sendUserOperation } from "../../actions/evm/sendUserOperation.js";
10
+ import { createSwapQuote } from "../../actions/evm/swap/createSwapQuote.js";
11
+ import { sendSwapOperation } from "../../actions/evm/swap/sendSwapOperation.js";
12
+ import { smartAccountTransferStrategy } from "../../actions/evm/transfer/smartAccountTransferStrategy.js";
13
+ import { transfer } from "../../actions/evm/transfer/transfer.js";
14
+ import { waitForUserOperation } from "../../actions/evm/waitForUserOperation.js";
15
+
16
+ import type {
17
+ EvmAccount,
18
+ EvmSmartAccount,
19
+ KnownEvmNetworks,
20
+ NetworkScopedEvmSmartAccount,
21
+ } from "./types.js";
22
+ import type { FundOptions } from "../../actions/evm/fund/fund.js";
23
+ import type { QuoteFundOptions } from "../../actions/evm/fund/quoteFund.js";
24
+ import type { WaitForFundOperationOptions } from "../../actions/evm/fund/waitForFundOperationReceipt.js";
25
+ import type { ListTokenBalancesOptions } from "../../actions/evm/listTokenBalances.js";
26
+ import type { RequestFaucetOptions } from "../../actions/evm/requestFaucet.js";
27
+ import type { SendUserOperationOptions } from "../../actions/evm/sendUserOperation.js";
28
+ import type {
29
+ SmartAccountQuoteSwapOptions,
30
+ SmartAccountSwapNetwork,
31
+ SmartAccountSwapOptions,
32
+ } from "../../actions/evm/swap/types.js";
33
+ import type { SmartAccountTransferOptions } from "../../actions/evm/transfer/types.js";
34
+ import type { WaitForUserOperationOptions } from "../../actions/evm/waitForUserOperation.js";
35
+ import type { GetUserOperationOptions } from "../../client/evm/evm.types.js";
36
+ import type {
37
+ CdpOpenApiClientType,
38
+ EvmUserOperationNetwork,
39
+ ListEvmTokenBalancesNetwork,
40
+ } from "../../openapi-client/index.js";
3
41
 
4
42
  /**
5
- * Options for converting a pre-existing EvmSmartAccount and owner to a EvmSmartAccount
43
+ * Options for converting a pre-existing EvmSmartAccount and owner to a NetworkScopedEvmSmartAccount
6
44
  */
7
- export type ToEvmSmartAccountOptions = {
45
+ export type ToNetworkScopedEvmSmartAccountOptions = {
8
46
  /** The pre-existing EvmSmartAccount. */
9
47
  smartAccount: EvmSmartAccount;
10
48
  /** The network to scope the smart account object to. */
11
- network: string;
49
+ network: KnownEvmNetworks;
12
50
  /** The owner of the smart account. */
13
51
  owner: EvmAccount;
14
52
  };
@@ -20,22 +58,151 @@ export type ToEvmSmartAccountOptions = {
20
58
  * The owner must be the original owner of the evm smart account.
21
59
  *
22
60
  * @param {CdpOpenApiClientType} apiClient - The API client.
23
- * @param {ToEvmSmartAccountOptions} options - Configuration options.
61
+ * @param {ToNetworkScopedEvmSmartAccountOptions} options - Configuration options.
24
62
  * @param {EvmSmartAccount} options.smartAccount - The deployed evm smart account.
25
63
  * @param {EvmAccount} options.owner - The owner which signs for the smart account.
64
+ * @param {KnownEvmNetworks} options.network - The network to scope the smart account to.
26
65
  * @returns {NetworkScopedEvmSmartAccount} A configured NetworkScopedEvmSmartAccount instance ready for user operation submission.
27
66
  */
28
- export function toNetworkScopedEvmSmartAccount(
67
+ export async function toNetworkScopedEvmSmartAccount<Network extends KnownEvmNetworks>(
29
68
  apiClient: CdpOpenApiClientType,
30
- options: ToEvmSmartAccountOptions,
31
- ): NetworkScopedEvmSmartAccount {
32
- const account: NetworkScopedEvmSmartAccount = {
69
+ options: ToNetworkScopedEvmSmartAccountOptions & { network: Network },
70
+ ): Promise<NetworkScopedEvmSmartAccount<Network>> {
71
+ const paymasterUrl = await (async () => {
72
+ if (options.network === "base") {
73
+ return getBaseNodeRpcUrl(options.network);
74
+ }
75
+ return undefined;
76
+ })();
77
+
78
+ const account = {
33
79
  address: options.smartAccount.address,
34
80
  network: options.network,
35
81
  owners: [options.owner],
36
82
  name: options.smartAccount.name,
37
83
  type: "evm-smart",
38
- };
84
+ sendUserOperation: async (
85
+ userOpOptions: Omit<SendUserOperationOptions<unknown[]>, "smartAccount" | "network">,
86
+ ) => {
87
+ return sendUserOperation(apiClient, {
88
+ ...userOpOptions,
89
+ smartAccount: options.smartAccount,
90
+ network: options.network as EvmUserOperationNetwork,
91
+ paymasterUrl: userOpOptions.paymasterUrl ?? paymasterUrl,
92
+ });
93
+ },
94
+ waitForUserOperation: async (
95
+ waitOptions: Omit<WaitForUserOperationOptions, "smartAccountAddress">,
96
+ ) => {
97
+ return waitForUserOperation(apiClient, {
98
+ ...waitOptions,
99
+ smartAccountAddress: options.smartAccount.address,
100
+ });
101
+ },
102
+ getUserOperation: async (getOptions: Omit<GetUserOperationOptions, "smartAccount">) => {
103
+ return getUserOperation(apiClient, {
104
+ ...getOptions,
105
+ smartAccount: options.smartAccount,
106
+ });
107
+ },
108
+ } as NetworkScopedEvmSmartAccount<Network>;
109
+
110
+ if (isMethodSupportedOnNetwork("transfer", options.network)) {
111
+ Object.assign(account, {
112
+ transfer: async (transferOptions: Omit<SmartAccountTransferOptions, "network">) => {
113
+ return transfer(
114
+ apiClient,
115
+ options.smartAccount,
116
+ {
117
+ ...transferOptions,
118
+ network: options.network as EvmUserOperationNetwork,
119
+ paymasterUrl: transferOptions.paymasterUrl ?? paymasterUrl,
120
+ },
121
+ smartAccountTransferStrategy,
122
+ );
123
+ },
124
+ });
125
+ }
126
+
127
+ if (isMethodSupportedOnNetwork("listTokenBalances", options.network)) {
128
+ Object.assign(account, {
129
+ listTokenBalances: async (
130
+ listOptions: Omit<ListTokenBalancesOptions, "address" | "network">,
131
+ ) => {
132
+ return listTokenBalances(apiClient, {
133
+ ...listOptions,
134
+ address: options.smartAccount.address,
135
+ network: options.network as ListEvmTokenBalancesNetwork,
136
+ });
137
+ },
138
+ });
139
+ }
140
+
141
+ if (isMethodSupportedOnNetwork("requestFaucet", options.network)) {
142
+ Object.assign(account, {
143
+ requestFaucet: async (faucetOptions: Omit<RequestFaucetOptions, "address" | "network">) => {
144
+ return requestFaucet(apiClient, {
145
+ ...faucetOptions,
146
+ address: options.smartAccount.address,
147
+ network: options.network as "base-sepolia" | "ethereum-sepolia",
148
+ });
149
+ },
150
+ });
151
+ }
152
+
153
+ if (isMethodSupportedOnNetwork("quoteFund", options.network)) {
154
+ Object.assign(account, {
155
+ quoteFund: async (quoteOptions: Omit<QuoteFundOptions, "address">) => {
156
+ return quoteFund(apiClient, {
157
+ ...quoteOptions,
158
+ address: options.smartAccount.address,
159
+ });
160
+ },
161
+ });
162
+ }
163
+
164
+ if (isMethodSupportedOnNetwork("fund", options.network)) {
165
+ Object.assign(account, {
166
+ fund: async (fundOptions: Omit<FundOptions, "address">) => {
167
+ return fund(apiClient, {
168
+ ...fundOptions,
169
+ address: options.smartAccount.address,
170
+ });
171
+ },
172
+ waitForFundOperationReceipt: async (waitOptions: WaitForFundOperationOptions) => {
173
+ return waitForFundOperationReceipt(apiClient, waitOptions);
174
+ },
175
+ });
176
+ }
177
+
178
+ if (isMethodSupportedOnNetwork("quoteSwap", options.network)) {
179
+ Object.assign(account, {
180
+ quoteSwap: async (quoteSwapOptions: SmartAccountQuoteSwapOptions) => {
181
+ return createSwapQuote(apiClient, {
182
+ ...quoteSwapOptions,
183
+ taker: options.smartAccount.address,
184
+ signerAddress: options.owner.address,
185
+ smartAccount: options.smartAccount,
186
+ network: options.network as SmartAccountSwapNetwork,
187
+ });
188
+ },
189
+ });
190
+ }
191
+
192
+ if (isMethodSupportedOnNetwork("swap", options.network)) {
193
+ Object.assign(account, {
194
+ swap: async (swapOptions: SmartAccountSwapOptions) => {
195
+ return sendSwapOperation(apiClient, {
196
+ ...swapOptions,
197
+ smartAccount: options.smartAccount,
198
+ taker: options.smartAccount.address,
199
+ signerAddress: options.owner.address,
200
+ network: options.network as SmartAccountSwapNetwork,
201
+ paymasterUrl: swapOptions.paymasterUrl ?? paymasterUrl,
202
+ });
203
+ },
204
+ });
205
+ }
39
206
 
40
207
  return account;
41
208
  }