@coinbase/cdp-sdk 1.15.0 → 1.16.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 (77) hide show
  1. package/CHANGELOG.md +14 -0
  2. package/README.md +82 -20
  3. package/_cjs/accounts/evm/toEvmServerAccount.js +13 -7
  4. package/_cjs/accounts/evm/toEvmServerAccount.js.map +1 -1
  5. package/_cjs/accounts/evm/toEvmSmartAccount.js +18 -0
  6. package/_cjs/accounts/evm/toEvmSmartAccount.js.map +1 -1
  7. package/_cjs/actions/evm/signAndWrapTypedDataForSmartAccount.js +186 -0
  8. package/_cjs/actions/evm/signAndWrapTypedDataForSmartAccount.js.map +1 -0
  9. package/_cjs/actions/evm/swap/createSwapQuote.js +26 -8
  10. package/_cjs/actions/evm/swap/createSwapQuote.js.map +1 -1
  11. package/_cjs/actions/evm/swap/getSwapPrice.js +3 -2
  12. package/_cjs/actions/evm/swap/getSwapPrice.js.map +1 -1
  13. package/_cjs/actions/evm/swap/sendSwapOperation.js +153 -0
  14. package/_cjs/actions/evm/swap/sendSwapOperation.js.map +1 -0
  15. package/_cjs/actions/evm/swap/sendSwapTransaction.js +16 -2
  16. package/_cjs/actions/evm/swap/sendSwapTransaction.js.map +1 -1
  17. package/_cjs/client/evm/evm.js +13 -6
  18. package/_cjs/client/evm/evm.js.map +1 -1
  19. package/_cjs/utils/uuidV4.js +26 -0
  20. package/_cjs/utils/uuidV4.js.map +1 -0
  21. package/_cjs/version.js +1 -1
  22. package/_esm/accounts/evm/toEvmServerAccount.js +14 -8
  23. package/_esm/accounts/evm/toEvmServerAccount.js.map +1 -1
  24. package/_esm/accounts/evm/toEvmSmartAccount.js +18 -0
  25. package/_esm/accounts/evm/toEvmSmartAccount.js.map +1 -1
  26. package/_esm/actions/evm/signAndWrapTypedDataForSmartAccount.js +181 -0
  27. package/_esm/actions/evm/signAndWrapTypedDataForSmartAccount.js.map +1 -0
  28. package/_esm/actions/evm/swap/createSwapQuote.js +26 -8
  29. package/_esm/actions/evm/swap/createSwapQuote.js.map +1 -1
  30. package/_esm/actions/evm/swap/getSwapPrice.js +3 -2
  31. package/_esm/actions/evm/swap/getSwapPrice.js.map +1 -1
  32. package/_esm/actions/evm/swap/sendSwapOperation.js +150 -0
  33. package/_esm/actions/evm/swap/sendSwapOperation.js.map +1 -0
  34. package/_esm/actions/evm/swap/sendSwapTransaction.js +16 -2
  35. package/_esm/actions/evm/swap/sendSwapTransaction.js.map +1 -1
  36. package/_esm/client/evm/evm.js +14 -7
  37. package/_esm/client/evm/evm.js.map +1 -1
  38. package/_esm/utils/uuidV4.js +23 -0
  39. package/_esm/utils/uuidV4.js.map +1 -0
  40. package/_esm/version.js +1 -1
  41. package/_types/accounts/evm/toEvmServerAccount.d.ts.map +1 -1
  42. package/_types/accounts/evm/toEvmSmartAccount.d.ts.map +1 -1
  43. package/_types/accounts/evm/types.d.ts +4 -4
  44. package/_types/accounts/evm/types.d.ts.map +1 -1
  45. package/_types/actions/evm/signAndWrapTypedDataForSmartAccount.d.ts +133 -0
  46. package/_types/actions/evm/signAndWrapTypedDataForSmartAccount.d.ts.map +1 -0
  47. package/_types/actions/evm/swap/createSwapQuote.d.ts.map +1 -1
  48. package/_types/actions/evm/swap/getSwapPrice.d.ts +2 -1
  49. package/_types/actions/evm/swap/getSwapPrice.d.ts.map +1 -1
  50. package/_types/actions/evm/swap/sendSwapOperation.d.ts +64 -0
  51. package/_types/actions/evm/swap/sendSwapOperation.d.ts.map +1 -0
  52. package/_types/actions/evm/swap/sendSwapTransaction.d.ts.map +1 -1
  53. package/_types/actions/evm/swap/types.d.ts +131 -5
  54. package/_types/actions/evm/swap/types.d.ts.map +1 -1
  55. package/_types/actions/evm/types.d.ts +112 -21
  56. package/_types/actions/evm/types.d.ts.map +1 -1
  57. package/_types/client/evm/evm.d.ts.map +1 -1
  58. package/_types/client/evm/evm.types.d.ts +32 -6
  59. package/_types/client/evm/evm.types.d.ts.map +1 -1
  60. package/_types/utils/uuidV4.d.ts +10 -0
  61. package/_types/utils/uuidV4.d.ts.map +1 -0
  62. package/_types/version.d.ts +1 -1
  63. package/accounts/evm/toEvmServerAccount.ts +33 -15
  64. package/accounts/evm/toEvmSmartAccount.ts +25 -0
  65. package/accounts/evm/types.ts +14 -6
  66. package/actions/evm/signAndWrapTypedDataForSmartAccount.ts +246 -0
  67. package/actions/evm/swap/createSwapQuote.ts +42 -17
  68. package/actions/evm/swap/getSwapPrice.ts +15 -11
  69. package/actions/evm/swap/sendSwapOperation.ts +182 -0
  70. package/actions/evm/swap/sendSwapTransaction.ts +18 -2
  71. package/actions/evm/swap/types.ts +154 -5
  72. package/actions/evm/types.ts +123 -21
  73. package/client/evm/evm.ts +19 -8
  74. package/client/evm/evm.types.ts +33 -6
  75. package/package.json +1 -1
  76. package/utils/uuidV4.ts +23 -0
  77. package/version.ts +1 -1
@@ -1,2 +1,2 @@
1
- export declare const version = "1.15.0";
1
+ export declare const version = "1.16.0";
2
2
  //# sourceMappingURL=version.d.ts.map
@@ -1,4 +1,11 @@
1
- import { type TransactionSerializable, getTypesForEIP712Domain, serializeTransaction } from "viem";
1
+ import {
2
+ type TransactionSerializable,
3
+ type TypedDataDefinition,
4
+ type TypedData,
5
+ type HashTypedDataParameters,
6
+ getTypesForEIP712Domain,
7
+ serializeTransaction,
8
+ } from "viem";
2
9
 
3
10
  import { FundOptions, fund } from "../../actions/evm/fund/fund.js";
4
11
  import { Quote } from "../../actions/evm/fund/Quote.js";
@@ -31,13 +38,13 @@ import type {
31
38
  TransactionResult,
32
39
  } from "../../actions/evm/sendTransaction.js";
33
40
  import type {
34
- SwapOptions,
35
- SwapResult,
36
- QuoteSwapOptions,
37
- QuoteSwapResult,
41
+ AccountSwapOptions,
42
+ AccountSwapResult,
43
+ AccountQuoteSwapOptions,
44
+ AccountQuoteSwapResult,
38
45
  } from "../../actions/evm/swap/types.js";
39
46
  import type { CdpOpenApiClientType, EvmAccount } from "../../openapi-client/index.js";
40
- import type { Address, EIP712Message, Hash, Hex } from "../../types/misc.js";
47
+ import type { Address, EIP712Domain, Hash, Hex } from "../../types/misc.js";
41
48
 
42
49
  /**
43
50
  * Options for converting a pre-existing EvmAccount to a EvmServerAccount.
@@ -83,13 +90,24 @@ export function toEvmServerAccount(
83
90
  return result.signedTransaction as Hex;
84
91
  },
85
92
 
86
- async signTypedData(message: EIP712Message) {
87
- if (!message.types.EIP712Domain) {
88
- message.types.EIP712Domain = getTypesForEIP712Domain({
89
- domain: message.domain,
90
- });
91
- }
92
- const result = await apiClient.signEvmTypedData(options.account.address, message);
93
+ async signTypedData<
94
+ const typedData extends TypedData | Record<string, unknown>,
95
+ primaryType extends keyof typedData | "EIP712Domain" = keyof typedData,
96
+ >(parameters: TypedDataDefinition<typedData, primaryType>) {
97
+ const { domain = {}, message, primaryType } = parameters as HashTypedDataParameters;
98
+ const types = {
99
+ EIP712Domain: getTypesForEIP712Domain({ domain }),
100
+ ...parameters.types,
101
+ };
102
+
103
+ const openApiMessage = {
104
+ domain: domain as EIP712Domain,
105
+ types,
106
+ primaryType,
107
+ message,
108
+ };
109
+
110
+ const result = await apiClient.signEvmTypedData(options.account.address, openApiMessage);
93
111
  return result.signature as Hex;
94
112
  },
95
113
  async transfer(transferArgs): Promise<TransactionResult> {
@@ -134,13 +152,13 @@ export function toEvmServerAccount(
134
152
  ): Promise<WaitForFundOperationResult> {
135
153
  return waitForFundOperationReceipt(apiClient, options);
136
154
  },
137
- async quoteSwap(options: QuoteSwapOptions): Promise<QuoteSwapResult> {
155
+ async quoteSwap(options: AccountQuoteSwapOptions): Promise<AccountQuoteSwapResult> {
138
156
  return createSwapQuote(apiClient, {
139
157
  ...options,
140
158
  taker: this.address,
141
159
  });
142
160
  },
143
- async swap(options: SwapOptions): Promise<SwapResult> {
161
+ async swap(options: AccountSwapOptions): Promise<AccountSwapResult> {
144
162
  return sendSwapTransaction(apiClient, {
145
163
  ...options,
146
164
  address: this.address,
@@ -23,6 +23,8 @@ import {
23
23
  type SendUserOperationReturnType,
24
24
  sendUserOperation,
25
25
  } from "../../actions/evm/sendUserOperation.js";
26
+ import { createSwapQuote } from "../../actions/evm/swap/createSwapQuote.js";
27
+ import { sendSwapOperation } from "../../actions/evm/swap/sendSwapOperation.js";
26
28
  import { smartAccountTransferStrategy } from "../../actions/evm/transfer/smartAccountTransferStrategy.js";
27
29
  import { transfer } from "../../actions/evm/transfer/transfer.js";
28
30
  import {
@@ -33,6 +35,12 @@ import {
33
35
  import { GetUserOperationOptions, UserOperation } from "../../client/evm/evm.types.js";
34
36
 
35
37
  import type { EvmAccount, EvmSmartAccount } from "./types.js";
38
+ import type {
39
+ SmartAccountQuoteSwapOptions,
40
+ SmartAccountQuoteSwapResult,
41
+ SmartAccountSwapOptions,
42
+ SmartAccountSwapResult,
43
+ } from "../../actions/evm/swap/types.js";
36
44
  import type {
37
45
  CdpOpenApiClientType,
38
46
  EvmSmartAccount as EvmSmartAccountModel,
@@ -128,6 +136,23 @@ export function toEvmSmartAccount(
128
136
  ): Promise<WaitForFundOperationResult> {
129
137
  return waitForFundOperationReceipt(apiClient, options);
130
138
  },
139
+ async quoteSwap(options: SmartAccountQuoteSwapOptions): Promise<SmartAccountQuoteSwapResult> {
140
+ return createSwapQuote(apiClient, {
141
+ ...options,
142
+ taker: this.address, // Always use smart account's address as taker
143
+ signerAddress: this.owners[0].address, // Always use owner's address as signer
144
+ smartAccount: account, // Pass smart account for execute method support
145
+ });
146
+ },
147
+ async swap(options: SmartAccountSwapOptions): Promise<SmartAccountSwapResult> {
148
+ return sendSwapOperation(apiClient, {
149
+ ...options,
150
+ smartAccount: account,
151
+ taker: this.address, // Always use smart account's address as taker
152
+ signerAddress: this.owners[0].address, // Always use owner's address as signer
153
+ });
154
+ },
155
+
131
156
  name: options.smartAccount.name,
132
157
  type: "evm-smart",
133
158
  };
@@ -1,9 +1,12 @@
1
- import { SignableMessage, TransactionSerializable } from "viem";
2
-
3
- import { Prettify } from "../../types/utils.js";
4
-
5
1
  import type { AccountActions, SmartAccountActions } from "../../actions/evm/types.js";
6
- import type { Address, EIP712Message, Hash, Hex } from "../../types/misc.js";
2
+ import type { Address, Hash, Hex } from "../../types/misc.js";
3
+ import type { Prettify } from "../../types/utils.js";
4
+ import type {
5
+ SignableMessage,
6
+ TransactionSerializable,
7
+ TypedData,
8
+ TypedDataDefinition,
9
+ } from "viem";
7
10
 
8
11
  /**
9
12
  * Base type for any Ethereum account with signing capabilities.
@@ -19,7 +22,12 @@ export type EvmAccount = {
19
22
  /** Signs a transaction and returns the signed transaction as a hex string. */
20
23
  signTransaction: (transaction: TransactionSerializable) => Promise<Hex>;
21
24
  /** Signs a typed data and returns the signature as a hex string. */
22
- signTypedData: (message: EIP712Message) => Promise<Hex>;
25
+ signTypedData: <
26
+ const typedData extends TypedData | Record<string, unknown>,
27
+ primaryType extends keyof typedData | "EIP712Domain" = keyof typedData,
28
+ >(
29
+ parameters: TypedDataDefinition<typedData, primaryType>,
30
+ ) => Promise<Hex>;
23
31
  /** A list of Policy ID's that apply to the account. */
24
32
  policies?: string[];
25
33
  };
@@ -0,0 +1,246 @@
1
+ import { encodeAbiParameters, encodePacked, hashTypedData, sliceHex } from "viem";
2
+
3
+ import type { EvmSmartAccount } from "../../accounts/evm/types.js";
4
+ import type {
5
+ CdpOpenApiClientType,
6
+ EIP712Message as OpenApiEIP712Message,
7
+ } from "../../openapi-client/index.js";
8
+ import type { EIP712Message, Hex } from "../../types/misc.js";
9
+
10
+ /**
11
+ * Options for signing and wrapping EIP-712 typed data with a smart account.
12
+ */
13
+ export interface SignAndWrapTypedDataForSmartAccountOptions {
14
+ /** The smart account to sign with. */
15
+ smartAccount: EvmSmartAccount;
16
+ /** The chain ID for the signature (used for replay protection). */
17
+ chainId: bigint;
18
+ /** The EIP-712 typed data message to sign. */
19
+ typedData: EIP712Message;
20
+ /** The index of the owner to sign with (defaults to 0). */
21
+ ownerIndex?: bigint;
22
+ /** Optional idempotency key for the signing request. */
23
+ idempotencyKey?: string;
24
+ }
25
+
26
+ /**
27
+ * Result of signing and wrapping typed data for a smart account.
28
+ */
29
+ export interface SignAndWrapTypedDataForSmartAccountResult {
30
+ /** The signature ready for smart contract use. */
31
+ signature: Hex;
32
+ }
33
+
34
+ /**
35
+ * Signs and wraps an EIP-712 message for a smart account using the required Coinbase Smart Wallet signature format.
36
+ *
37
+ * **Important: Coinbase Smart Wallet Contract Requirements**
38
+ *
39
+ * Due to the Coinbase Smart Wallet contract implementation (ERC-1271), CDP Smart Wallets have
40
+ * specific requirements for EIP-712 message signing:
41
+ *
42
+ * 1. **Replay-Safe Hashing**: All typed messages must be wrapped in a replay-safe hash that
43
+ * includes the chain ID and smart account address. This prevents the same signature from
44
+ * being valid across different chains or accounts.
45
+ *
46
+ * 2. **Signature Wrapping**: The resulting signature must be wrapped in a `SignatureWrapper`
47
+ * struct that identifies which owner signed and contains the signature data in the format
48
+ * expected by the smart contract's `isValidSignature()` method.
49
+ *
50
+ * This function handles both requirements automatically, making it safe and convenient for
51
+ * developers to sign EIP-712 messages with CDP Smart Wallets.
52
+ *
53
+ * @param {CdpOpenApiClientType} client - The CDP API client
54
+ * @param {SignAndWrapTypedDataForSmartAccountOptions} options - Parameters for signing and wrapping the EIP-712 message.
55
+ * @returns A promise that resolves to the signature that can be used with smart contracts.
56
+ *
57
+ * @example
58
+ * ```ts
59
+ * const result = await signAndWrapTypedDataForSmartAccount(client, {
60
+ * smartAccount: smartAccount,
61
+ * chainId: 1n,
62
+ * typedData: {
63
+ * domain: {
64
+ * name: "Permit2",
65
+ * chainId: 1,
66
+ * verifyingContract: "0x000000000022D473030F116dDEE9F6B43aC78BA3",
67
+ * },
68
+ * types: {
69
+ * EIP712Domain: [
70
+ * { name: "name", type: "string" },
71
+ * { name: "chainId", type: "uint256" },
72
+ * { name: "verifyingContract", type: "address" },
73
+ * ],
74
+ * PermitTransferFrom: [
75
+ * { name: "permitted", type: "TokenPermissions" },
76
+ * { name: "spender", type: "address" },
77
+ * { name: "nonce", type: "uint256" },
78
+ * { name: "deadline", type: "uint256" },
79
+ * ],
80
+ * TokenPermissions: [
81
+ * { name: "token", type: "address" },
82
+ * { name: "amount", type: "uint256" },
83
+ * ],
84
+ * },
85
+ * primaryType: "PermitTransferFrom",
86
+ * message: {
87
+ * permitted: {
88
+ * token: "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48",
89
+ * amount: "1000000",
90
+ * },
91
+ * spender: "0xFfFfFfFFfFFfFFfFFfFFFFFffFFFffffFfFFFfFf",
92
+ * nonce: "0",
93
+ * deadline: "1717123200",
94
+ * },
95
+ * },
96
+ * });
97
+ *
98
+ * // Use result.signature for smart contract calls
99
+ * console.log(`Signature: ${result.signature}`);
100
+ * ```
101
+ */
102
+ export async function signAndWrapTypedDataForSmartAccount(
103
+ client: CdpOpenApiClientType,
104
+ options: SignAndWrapTypedDataForSmartAccountOptions,
105
+ ): Promise<SignAndWrapTypedDataForSmartAccountResult> {
106
+ const { smartAccount, chainId, typedData, ownerIndex = 0n } = options;
107
+
108
+ // Create the replay-safe typed data
109
+ const replaySafeTypedData = createReplaySafeTypedData({
110
+ typedData,
111
+ chainId,
112
+ smartAccountAddress: smartAccount.address as Hex,
113
+ });
114
+
115
+ // Sign the replay-safe typed data with the smart account owner
116
+ const owner = smartAccount.owners[Number(ownerIndex)];
117
+ const signature = await client.signEvmTypedData(
118
+ owner.address,
119
+ replaySafeTypedData,
120
+ options.idempotencyKey,
121
+ );
122
+
123
+ // Wrap the signature in the format expected by the smart contract
124
+ const wrappedSignature = createSmartAccountSignatureWrapper({
125
+ signatureHex: signature.signature as Hex,
126
+ ownerIndex,
127
+ });
128
+
129
+ return {
130
+ signature: wrappedSignature as Hex,
131
+ };
132
+ }
133
+
134
+ /**
135
+ * Creates a replay-safe EIP-712 typed data structure by wrapping the original typed data with
136
+ * chain ID and smart account address.
137
+ *
138
+ * **Coinbase Smart Wallet Requirement**: Due to the Coinbase Smart Wallet contract's ERC-1271
139
+ * implementation, all EIP-712 messages must be wrapped in a replay-safe hash before signing.
140
+ * This prevents signature replay attacks across different chains or smart account addresses.
141
+ *
142
+ * The smart contract's `isValidSignature()` method expects signatures to be validated against
143
+ * this replay-safe hash, not the original message hash.
144
+ *
145
+ * @param params - The replay-safe hash parameters
146
+ * @param params.typedData - The original EIP-712 typed data to make replay-safe
147
+ * @param params.chainId - The chain ID for replay protection
148
+ * @param params.smartAccountAddress - The smart account address for additional context
149
+ * @returns The EIP-712 typed data structure for the replay-safe hash
150
+ */
151
+ export function createReplaySafeTypedData({
152
+ typedData,
153
+ chainId,
154
+ smartAccountAddress,
155
+ }: {
156
+ typedData: EIP712Message;
157
+ chainId: bigint;
158
+ smartAccountAddress: Hex;
159
+ }): OpenApiEIP712Message {
160
+ // First hash the original typed data
161
+ const originalHash = hashTypedData(typedData);
162
+
163
+ // Create and return the replay-safe typed data structure
164
+ return {
165
+ domain: {
166
+ name: "Coinbase Smart Wallet",
167
+ version: "1",
168
+ chainId: Number(chainId),
169
+ verifyingContract: smartAccountAddress,
170
+ },
171
+ types: {
172
+ EIP712Domain: [
173
+ { name: "name", type: "string" },
174
+ { name: "version", type: "string" },
175
+ { name: "chainId", type: "uint256" },
176
+ { name: "verifyingContract", type: "address" },
177
+ ],
178
+ CoinbaseSmartWalletMessage: [{ name: "hash", type: "bytes32" }],
179
+ },
180
+ primaryType: "CoinbaseSmartWalletMessage" as const,
181
+ message: {
182
+ hash: originalHash,
183
+ },
184
+ };
185
+ }
186
+
187
+ /**
188
+ * Builds a signature wrapper for Coinbase Smart Wallets by decomposing a hex signature
189
+ * into r, s, v components and encoding them in the format expected by the smart contract.
190
+ *
191
+ * All signatures on Coinbase Smart Wallets must be wrapped in this format to identify
192
+ * which owner signed and provide the signature data.
193
+ *
194
+ * @param params - The signature parameters
195
+ * @param params.signatureHex - The hex signature to wrap (65 bytes: r + s + v)
196
+ * @param params.ownerIndex - The index of the owner that signed (from MultiOwnable.ownerAtIndex)
197
+ * @returns The encoded signature wrapper in the format expected by the smart contract
198
+ */
199
+ export function createSmartAccountSignatureWrapper({
200
+ signatureHex,
201
+ ownerIndex,
202
+ }: {
203
+ signatureHex: Hex;
204
+ ownerIndex: bigint;
205
+ }): Hex {
206
+ // Decompose 65-byte hex signature into r (32 bytes), s (32 bytes), v (1 byte)
207
+ const r = sliceHex(signatureHex, 0, 32);
208
+ const s = sliceHex(signatureHex, 32, 64);
209
+ const v = Number(`0x${signatureHex.slice(130, 132)}`); // 130 = 2 + 64 + 64
210
+
211
+ const signatureData = encodePacked(["bytes32", "bytes32", "uint8"], [r, s, v]);
212
+
213
+ return encodeAbiParameters(
214
+ [SignatureWrapperStruct],
215
+ [
216
+ {
217
+ ownerIndex: Number(ownerIndex),
218
+ signatureData,
219
+ },
220
+ ],
221
+ ) as Hex;
222
+ }
223
+
224
+ /**
225
+ * The ABI structure for the SignatureWrapper struct expected by Coinbase Smart Wallets.
226
+ * This matches the struct defined in the smart contract:
227
+ *
228
+ * struct SignatureWrapper {
229
+ * uint256 ownerIndex;
230
+ * bytes signatureData;
231
+ * }
232
+ */
233
+ const SignatureWrapperStruct = {
234
+ components: [
235
+ {
236
+ name: "ownerIndex",
237
+ type: "uint8",
238
+ },
239
+ {
240
+ name: "signatureData",
241
+ type: "bytes",
242
+ },
243
+ ],
244
+ name: "SignatureWrapper",
245
+ type: "tuple",
246
+ } as const;
@@ -1,3 +1,4 @@
1
+ import { sendSwapOperation } from "./sendSwapOperation.js";
1
2
  import { sendSwapTransaction } from "./sendSwapTransaction.js";
2
3
  import {
3
4
  CreateSwapQuoteOptions,
@@ -10,9 +11,12 @@ import {
10
11
  CdpOpenApiClientType,
11
12
  CreateSwapQuoteResponse,
12
13
  type SendEvmTransactionBodyNetwork,
14
+ type EvmUserOperationNetwork,
13
15
  } from "../../../openapi-client/index.js";
14
16
  import { Address, Hex } from "../../../types/misc.js";
15
17
 
18
+ import type { EvmSmartAccount } from "../../../accounts/evm/types.js";
19
+
16
20
  /**
17
21
  * Creates a quote for a swap between two tokens on an EVM network.
18
22
  *
@@ -45,16 +49,19 @@ export async function createSwapQuote(
45
49
  const taker = options.taker as Address;
46
50
 
47
51
  // Call the createEvmSwapQuote function directly with the client's configured API
48
- const response = await client.createEvmSwapQuote({
49
- network: options.network,
50
- toToken: options.toToken,
51
- fromToken: options.fromToken,
52
- fromAmount: options.fromAmount.toString(),
53
- taker: taker,
54
- signerAddress: options.signerAddress,
55
- gasPrice: options.gasPrice?.toString(),
56
- slippageBps: options.slippageBps,
57
- });
52
+ const response = await client.createEvmSwapQuote(
53
+ {
54
+ network: options.network,
55
+ toToken: options.toToken,
56
+ fromToken: options.fromToken,
57
+ fromAmount: options.fromAmount.toString(),
58
+ taker: taker,
59
+ signerAddress: options.signerAddress,
60
+ gasPrice: options.gasPrice?.toString(),
61
+ slippageBps: options.slippageBps,
62
+ },
63
+ options.idempotencyKey,
64
+ );
58
65
 
59
66
  // Check if liquidity is unavailable
60
67
  if (!response.liquidityAvailable) {
@@ -134,13 +141,31 @@ export async function createSwapQuote(
134
141
  execute: async (
135
142
  executeOptions: ExecuteSwapQuoteOptions = {},
136
143
  ): Promise<ExecuteSwapQuoteResult> => {
137
- const { transactionHash } = await sendSwapTransaction(client, {
138
- address: options.signerAddress || taker, // Use signerAddress for smart accounts, taker for EOAs
139
- network: result.network as SendEvmTransactionBodyNetwork,
140
- swapQuote: result,
141
- idempotencyKey: executeOptions.idempotencyKey,
142
- });
143
- return { transactionHash };
144
+ if (options.smartAccount) {
145
+ // Smart account execution - use sendSwapOperation
146
+ const userOpResult = await sendSwapOperation(client, {
147
+ smartAccount: options.smartAccount as EvmSmartAccount,
148
+ network: result.network as EvmUserOperationNetwork,
149
+ swapQuote: result,
150
+ idempotencyKey: executeOptions.idempotencyKey,
151
+ });
152
+
153
+ return {
154
+ userOpHash: userOpResult.userOpHash,
155
+ smartAccountAddress: userOpResult.smartAccountAddress,
156
+ status: userOpResult.status,
157
+ };
158
+ } else {
159
+ // EOA execution - use sendSwapTransaction
160
+ const { transactionHash } = await sendSwapTransaction(client, {
161
+ address: taker,
162
+ network: result.network as SendEvmTransactionBodyNetwork,
163
+ swapQuote: result,
164
+ idempotencyKey: executeOptions.idempotencyKey,
165
+ });
166
+
167
+ return { transactionHash };
168
+ }
144
169
  },
145
170
  };
146
171
 
@@ -21,7 +21,8 @@ import { Address } from "../../../types/misc.js";
21
21
  * toToken: "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48", // USDC
22
22
  * fromToken: "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2", // WETH
23
23
  * fromAmount: BigInt("1000000000000000000"), // 1 WETH in wei
24
- * taker: "0x1234567890123456789012345678901234567890"
24
+ * taker: "0x1234567890123456789012345678901234567890",
25
+ * idempotencyKey: "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" // Optional: for request deduplication
25
26
  * });
26
27
  * ```
27
28
  */
@@ -30,16 +31,19 @@ export async function getSwapPrice(
30
31
  options: GetSwapPriceOptions,
31
32
  ): Promise<GetSwapPriceResult | SwapUnavailableResult> {
32
33
  // Call the getEvmSwapPrice function directly with the client's configured API
33
- const response = await client.getEvmSwapPrice({
34
- network: options.network,
35
- toToken: options.toToken,
36
- fromToken: options.fromToken,
37
- fromAmount: options.fromAmount.toString(),
38
- taker: options.taker,
39
- signerAddress: options.signerAddress,
40
- gasPrice: options.gasPrice?.toString(),
41
- slippageBps: options.slippageBps,
42
- });
34
+ const response = await client.getEvmSwapPrice(
35
+ {
36
+ network: options.network,
37
+ toToken: options.toToken,
38
+ fromToken: options.fromToken,
39
+ fromAmount: options.fromAmount.toString(),
40
+ taker: options.taker,
41
+ signerAddress: options.signerAddress,
42
+ gasPrice: options.gasPrice?.toString(),
43
+ slippageBps: options.slippageBps,
44
+ },
45
+ options.idempotencyKey,
46
+ );
43
47
 
44
48
  // Check if liquidity is unavailable
45
49
  if (!response.liquidityAvailable) {