@coinbase/cdp-sdk 1.11.0 → 1.12.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.
- package/CHANGELOG.md +6 -0
- package/README.md +117 -0
- package/_cjs/accounts/evm/toEvmServerAccount.js +14 -0
- package/_cjs/accounts/evm/toEvmServerAccount.js.map +1 -1
- package/_cjs/actions/evm/swap/createSwapQuote.js +126 -0
- package/_cjs/actions/evm/swap/createSwapQuote.js.map +1 -0
- package/_cjs/actions/evm/swap/getSwapPrice.js +86 -0
- package/_cjs/actions/evm/swap/getSwapPrice.js.map +1 -0
- package/_cjs/actions/evm/swap/sendSwapTransaction.js +141 -0
- package/_cjs/actions/evm/swap/sendSwapTransaction.js.map +1 -0
- package/_cjs/actions/evm/swap/types.js +3 -0
- package/_cjs/actions/evm/swap/types.js.map +1 -0
- package/_cjs/client/evm/evm.js +46 -1
- package/_cjs/client/evm/evm.js.map +1 -1
- package/_cjs/openapi-client/generated/coinbaseDeveloperPlatformAPIs.schemas.js +6 -1
- package/_cjs/openapi-client/generated/coinbaseDeveloperPlatformAPIs.schemas.js.map +1 -1
- package/_cjs/openapi-client/generated/evm-swaps/evm-swaps.js +26 -0
- package/_cjs/openapi-client/generated/evm-swaps/evm-swaps.js.map +1 -0
- package/_cjs/openapi-client/generated/evm-swaps/evm-swaps.msw.js +179 -0
- package/_cjs/openapi-client/generated/evm-swaps/evm-swaps.msw.js.map +1 -0
- package/_cjs/openapi-client/generated/index.msw.js +5 -4
- package/_cjs/openapi-client/generated/index.msw.js.map +1 -1
- package/_cjs/openapi-client/index.js +4 -0
- package/_cjs/openapi-client/index.js.map +1 -1
- package/_cjs/version.js +1 -1
- package/_esm/accounts/evm/toEvmServerAccount.js +14 -0
- package/_esm/accounts/evm/toEvmServerAccount.js.map +1 -1
- package/_esm/actions/evm/swap/createSwapQuote.js +123 -0
- package/_esm/actions/evm/swap/createSwapQuote.js.map +1 -0
- package/_esm/actions/evm/swap/getSwapPrice.js +83 -0
- package/_esm/actions/evm/swap/getSwapPrice.js.map +1 -0
- package/_esm/actions/evm/swap/sendSwapTransaction.js +138 -0
- package/_esm/actions/evm/swap/sendSwapTransaction.js.map +1 -0
- package/_esm/actions/evm/swap/types.js +2 -0
- package/_esm/actions/evm/swap/types.js.map +1 -0
- package/_esm/client/evm/evm.js +46 -1
- package/_esm/client/evm/evm.js.map +1 -1
- package/_esm/openapi-client/generated/coinbaseDeveloperPlatformAPIs.schemas.js +5 -0
- package/_esm/openapi-client/generated/coinbaseDeveloperPlatformAPIs.schemas.js.map +1 -1
- package/_esm/openapi-client/generated/evm-swaps/evm-swaps.js +21 -0
- package/_esm/openapi-client/generated/evm-swaps/evm-swaps.js.map +1 -0
- package/_esm/openapi-client/generated/evm-swaps/evm-swaps.msw.js +167 -0
- package/_esm/openapi-client/generated/evm-swaps/evm-swaps.msw.js.map +1 -0
- package/_esm/openapi-client/generated/index.msw.js +5 -4
- package/_esm/openapi-client/generated/index.msw.js.map +1 -1
- package/_esm/openapi-client/index.js +4 -0
- package/_esm/openapi-client/index.js.map +1 -1
- package/_esm/version.js +1 -1
- package/_types/accounts/evm/toEvmServerAccount.d.ts.map +1 -1
- package/_types/actions/evm/swap/createSwapQuote.d.ts +23 -0
- package/_types/actions/evm/swap/createSwapQuote.d.ts.map +1 -0
- package/_types/actions/evm/swap/getSwapPrice.d.ts +23 -0
- package/_types/actions/evm/swap/getSwapPrice.d.ts.map +1 -0
- package/_types/actions/evm/swap/sendSwapTransaction.d.ts +64 -0
- package/_types/actions/evm/swap/sendSwapTransaction.d.ts.map +1 -0
- package/_types/actions/evm/swap/types.d.ts +80 -0
- package/_types/actions/evm/swap/types.d.ts.map +1 -0
- package/_types/actions/evm/types.d.ts +86 -0
- package/_types/actions/evm/types.d.ts.map +1 -1
- package/_types/client/evm/evm.d.ts +39 -1
- package/_types/client/evm/evm.d.ts.map +1 -1
- package/_types/client/evm/evm.types.d.ts +207 -3
- package/_types/client/evm/evm.types.d.ts.map +1 -1
- package/_types/openapi-client/generated/coinbaseDeveloperPlatformAPIs.schemas.d.ts +300 -3
- package/_types/openapi-client/generated/coinbaseDeveloperPlatformAPIs.schemas.d.ts.map +1 -1
- package/_types/openapi-client/generated/evm-swaps/evm-swaps.d.ts +24 -0
- package/_types/openapi-client/generated/evm-swaps/evm-swaps.d.ts.map +1 -0
- package/_types/openapi-client/generated/evm-swaps/evm-swaps.msw.d.ts +12 -0
- package/_types/openapi-client/generated/evm-swaps/evm-swaps.msw.d.ts.map +1 -0
- package/_types/openapi-client/generated/index.msw.d.ts +5 -4
- package/_types/openapi-client/generated/index.msw.d.ts.map +1 -1
- package/_types/openapi-client/index.d.ts +5 -0
- package/_types/openapi-client/index.d.ts.map +1 -1
- package/_types/version.d.ts +1 -1
- package/accounts/evm/toEvmServerAccount.ts +21 -0
- package/actions/evm/swap/createSwapQuote.ts +148 -0
- package/actions/evm/swap/getSwapPrice.ts +95 -0
- package/actions/evm/swap/sendSwapTransaction.ts +173 -0
- package/actions/evm/swap/types.ts +94 -0
- package/actions/evm/types.ts +88 -0
- package/client/evm/evm.ts +58 -1
- package/client/evm/evm.types.ts +225 -1
- package/openapi-client/generated/coinbaseDeveloperPlatformAPIs.schemas.ts +329 -3
- package/openapi-client/generated/evm-swaps/evm-swaps.msw.ts +217 -0
- package/openapi-client/generated/evm-swaps/evm-swaps.ts +51 -0
- package/openapi-client/generated/index.msw.ts +5 -4
- package/openapi-client/index.ts +4 -0
- package/package.json +1 -1
- package/version.ts +1 -1
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
import { concat, numberToHex, size } from "viem";
|
|
2
|
+
|
|
3
|
+
import { createSwapQuote } from "./createSwapQuote.js";
|
|
4
|
+
import { sendTransaction } from "../sendTransaction.js";
|
|
5
|
+
|
|
6
|
+
import type { SendSwapTransactionOptions, SendSwapTransactionResult } from "./types.js";
|
|
7
|
+
import type {
|
|
8
|
+
CreateSwapQuoteResult,
|
|
9
|
+
CreateSwapQuoteOptions,
|
|
10
|
+
SwapUnavailableResult,
|
|
11
|
+
} from "../../../client/evm/evm.types.js";
|
|
12
|
+
import type {
|
|
13
|
+
CdpOpenApiClientType,
|
|
14
|
+
SendEvmTransactionBodyNetwork,
|
|
15
|
+
} from "../../../openapi-client/index.js";
|
|
16
|
+
import type { Hex } from "../../../types/misc.js";
|
|
17
|
+
import type { TransactionRequestEIP1559 } from "viem";
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Sends a swap transaction to the blockchain.
|
|
21
|
+
* Handles any permit2 signatures required for the swap.
|
|
22
|
+
*
|
|
23
|
+
* If you encounter token allowance issues, you'll need to perform a token approval transaction first to allow
|
|
24
|
+
* the Permit2 contract to spend the appropriate amount of your fromToken.
|
|
25
|
+
* See `examples/typescript/evm/account.sendSwapTransaction.ts` for example code on handling token approvals.
|
|
26
|
+
*
|
|
27
|
+
* @param {CdpOpenApiClientType} client - The client to use for sending the swap.
|
|
28
|
+
* @param {SendSwapTransactionOptions} options - The options for the swap submission.
|
|
29
|
+
*
|
|
30
|
+
* @returns {Promise<SendSwapTransactionResult>} A promise that resolves to the transaction hash.
|
|
31
|
+
*
|
|
32
|
+
* @throws {Error} If liquidity is not available for the swap.
|
|
33
|
+
* @throws {Error} If there are insufficient token allowances. In this case, you need to approve the
|
|
34
|
+
* Permit2 contract to spend your tokens before attempting the swap. The error message
|
|
35
|
+
* will include the current allowance and the spender address that needs approval.
|
|
36
|
+
* @throws {Error} If no transaction data is found in the swap result.
|
|
37
|
+
*
|
|
38
|
+
* @example **Sending a swap with pre-created swap quote object**
|
|
39
|
+
* ```ts
|
|
40
|
+
* // First create a swap quote
|
|
41
|
+
* const swapQuote = await cdp.evm.createSwapQuote({
|
|
42
|
+
* network: "base",
|
|
43
|
+
* toToken: "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48", // USDC
|
|
44
|
+
* fromToken: "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2", // WETH
|
|
45
|
+
* fromAmount: BigInt("1000000000000000000"), // 1 WETH in wei
|
|
46
|
+
* taker: account.address
|
|
47
|
+
* });
|
|
48
|
+
*
|
|
49
|
+
* // Check if liquidity is available
|
|
50
|
+
* if (!swapQuote.liquidityAvailable) {
|
|
51
|
+
* console.error("Insufficient liquidity for swap");
|
|
52
|
+
* return;
|
|
53
|
+
* }
|
|
54
|
+
*
|
|
55
|
+
* // Send the swap
|
|
56
|
+
* const result = await sendSwapTransaction(client, {
|
|
57
|
+
* address: account.address,
|
|
58
|
+
* swapQuote: swapQuote
|
|
59
|
+
* });
|
|
60
|
+
*
|
|
61
|
+
* console.log(`Swap sent with transaction hash: ${result.transactionHash}`);
|
|
62
|
+
* ```
|
|
63
|
+
*
|
|
64
|
+
* @example **Sending a swap with inline options (all-in-one)**
|
|
65
|
+
* ```ts
|
|
66
|
+
* // Send swap in one call
|
|
67
|
+
* const result = await sendSwapTransaction(client, {
|
|
68
|
+
* address: account.address,
|
|
69
|
+
* network: "base",
|
|
70
|
+
* toToken: "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48", // USDC
|
|
71
|
+
* fromToken: "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2", // WETH
|
|
72
|
+
* fromAmount: BigInt("1000000000000000000"), // 1 WETH in wei
|
|
73
|
+
* taker: account.address
|
|
74
|
+
* });
|
|
75
|
+
*
|
|
76
|
+
* console.log(`Swap sent with transaction hash: ${result.transactionHash}`);
|
|
77
|
+
* ```
|
|
78
|
+
*/
|
|
79
|
+
export async function sendSwapTransaction(
|
|
80
|
+
client: CdpOpenApiClientType,
|
|
81
|
+
options: SendSwapTransactionOptions,
|
|
82
|
+
): Promise<SendSwapTransactionResult> {
|
|
83
|
+
const { address, idempotencyKey } = options;
|
|
84
|
+
|
|
85
|
+
let swapResult: CreateSwapQuoteResult | SwapUnavailableResult;
|
|
86
|
+
|
|
87
|
+
// Determine if we need to create the swap quote or use the provided one
|
|
88
|
+
if ("swapQuote" in options) {
|
|
89
|
+
// Use the provided swap quote
|
|
90
|
+
swapResult = options.swapQuote;
|
|
91
|
+
} else {
|
|
92
|
+
// Create the swap quote using the provided options (InlineSendSwapTransactionOptions)
|
|
93
|
+
swapResult = await createSwapQuote(client, {
|
|
94
|
+
network: options.network as CreateSwapQuoteOptions["network"],
|
|
95
|
+
toToken: options.toToken,
|
|
96
|
+
fromToken: options.fromToken,
|
|
97
|
+
fromAmount: options.fromAmount,
|
|
98
|
+
taker: options.taker,
|
|
99
|
+
signerAddress: options.signerAddress,
|
|
100
|
+
gasPrice: options.gasPrice,
|
|
101
|
+
slippageBps: options.slippageBps,
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// Check if liquidity is available
|
|
106
|
+
if (!swapResult.liquidityAvailable) {
|
|
107
|
+
throw new Error("Insufficient liquidity for swap");
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// At this point, we know that swapResult is CreateSwapQuoteResult
|
|
111
|
+
const swap = swapResult as CreateSwapQuoteResult;
|
|
112
|
+
|
|
113
|
+
// Check for allowance issues
|
|
114
|
+
if (swap.issues?.allowance) {
|
|
115
|
+
const { currentAllowance, spender } = swap.issues.allowance;
|
|
116
|
+
throw new Error(
|
|
117
|
+
`Insufficient token allowance for swap. Current allowance: ${currentAllowance}. ` +
|
|
118
|
+
`Please approve the Permit2 contract (${spender}) to spend your tokens.`,
|
|
119
|
+
);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
// If the transaction doesn't exist, throw an error
|
|
123
|
+
if (!swap.transaction) {
|
|
124
|
+
throw new Error("No transaction data found in the swap");
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
// Get the transaction data and modify it if needed for Permit2
|
|
128
|
+
let txData = swap.transaction.data as Hex;
|
|
129
|
+
|
|
130
|
+
if (swap.permit2?.eip712) {
|
|
131
|
+
// Sign the Permit2 EIP-712 message
|
|
132
|
+
const signature = await client.signEvmTypedData(
|
|
133
|
+
address,
|
|
134
|
+
{
|
|
135
|
+
domain: swap.permit2.eip712.domain,
|
|
136
|
+
types: swap.permit2.eip712.types,
|
|
137
|
+
primaryType: swap.permit2.eip712.primaryType,
|
|
138
|
+
message: swap.permit2.eip712.message,
|
|
139
|
+
},
|
|
140
|
+
idempotencyKey,
|
|
141
|
+
);
|
|
142
|
+
|
|
143
|
+
// Calculate the signature length as a 32-byte hex value
|
|
144
|
+
const signatureLengthInHex = numberToHex(size(signature.signature as Hex), {
|
|
145
|
+
signed: false,
|
|
146
|
+
size: 32,
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
// Append the signature length and signature to the transaction data
|
|
150
|
+
txData = concat([txData, signatureLengthInHex, signature.signature as Hex]);
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
// Create a transaction object
|
|
154
|
+
const transaction: TransactionRequestEIP1559 = {
|
|
155
|
+
to: swap.transaction.to as `0x${string}`,
|
|
156
|
+
data: txData,
|
|
157
|
+
// Only include these properties if they exist
|
|
158
|
+
...(swap.transaction.value ? { value: BigInt(swap.transaction.value) } : {}),
|
|
159
|
+
...(swap.transaction.gas ? { gas: BigInt(swap.transaction.gas) } : {}),
|
|
160
|
+
};
|
|
161
|
+
|
|
162
|
+
// Use sendTransaction instead of directly calling client.sendEvmTransaction
|
|
163
|
+
const result = await sendTransaction(client, {
|
|
164
|
+
address,
|
|
165
|
+
network: swap.network as SendEvmTransactionBodyNetwork,
|
|
166
|
+
transaction,
|
|
167
|
+
idempotencyKey,
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
return {
|
|
171
|
+
transactionHash: result.transactionHash,
|
|
172
|
+
};
|
|
173
|
+
}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
CreateSwapQuoteOptions,
|
|
3
|
+
CreateSwapQuoteResult,
|
|
4
|
+
SwapUnavailableResult,
|
|
5
|
+
} from "../../../client/evm/evm.types.js";
|
|
6
|
+
import type { SendEvmTransactionBodyNetwork } from "../../../openapi-client/index.js";
|
|
7
|
+
import type { Address, Hex } from "../../../types/misc.js";
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Base options for sending a swap transaction.
|
|
11
|
+
*/
|
|
12
|
+
interface BaseSendSwapTransactionOptions {
|
|
13
|
+
/**
|
|
14
|
+
* The address of the account that will execute the swap.
|
|
15
|
+
*/
|
|
16
|
+
address: Address;
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Optional idempotency key for the request.
|
|
20
|
+
*/
|
|
21
|
+
idempotencyKey?: string;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Options when providing an already created swap quote.
|
|
26
|
+
*/
|
|
27
|
+
interface QuoteBasedSendSwapTransactionOptions extends BaseSendSwapTransactionOptions {
|
|
28
|
+
/**
|
|
29
|
+
* The swap quote returned by the createSwapQuote method.
|
|
30
|
+
*/
|
|
31
|
+
swapQuote: CreateSwapQuoteResult;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Options when creating a swap quote inline.
|
|
36
|
+
*/
|
|
37
|
+
interface InlineSendSwapTransactionOptions extends BaseSendSwapTransactionOptions {
|
|
38
|
+
/**
|
|
39
|
+
* The network to execute the swap on (e.g., "ethereum", "base").
|
|
40
|
+
*/
|
|
41
|
+
network: SendEvmTransactionBodyNetwork;
|
|
42
|
+
/** The token to receive (destination token). */
|
|
43
|
+
toToken: Address;
|
|
44
|
+
/** The token to send (source token). */
|
|
45
|
+
fromToken: Address;
|
|
46
|
+
/** The amount to send in atomic units of the token. */
|
|
47
|
+
fromAmount: bigint;
|
|
48
|
+
/** The address that will perform the swap. */
|
|
49
|
+
taker: Address;
|
|
50
|
+
/** The signer address (only needed if taker is a smart contract). */
|
|
51
|
+
signerAddress?: Address;
|
|
52
|
+
/** The gas price in Wei. */
|
|
53
|
+
gasPrice?: bigint;
|
|
54
|
+
/** The slippage tolerance in basis points (0-10000). */
|
|
55
|
+
slippageBps?: number;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Options for sending a swap transaction.
|
|
60
|
+
* Either provide a pre-created swap quote result or inline swap parameters.
|
|
61
|
+
*/
|
|
62
|
+
export type SendSwapTransactionOptions =
|
|
63
|
+
| QuoteBasedSendSwapTransactionOptions
|
|
64
|
+
| InlineSendSwapTransactionOptions;
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Result of sending a swap transaction.
|
|
68
|
+
*/
|
|
69
|
+
export interface SendSwapTransactionResult {
|
|
70
|
+
/**
|
|
71
|
+
* The transaction hash of the submitted swap.
|
|
72
|
+
*/
|
|
73
|
+
transactionHash: Hex;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Options for creating a swap quote (account-level).
|
|
78
|
+
*/
|
|
79
|
+
export type QuoteSwapOptions = Omit<CreateSwapQuoteOptions, "taker">;
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Result of creating a swap quote (account-level).
|
|
83
|
+
*/
|
|
84
|
+
export type QuoteSwapResult = CreateSwapQuoteResult | SwapUnavailableResult;
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Options for executing a token swap (account-level).
|
|
88
|
+
*/
|
|
89
|
+
export type SwapOptions = Omit<SendSwapTransactionOptions, "address">;
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Result of executing a token swap (account-level).
|
|
93
|
+
*/
|
|
94
|
+
export type SwapResult = SendSwapTransactionResult;
|
package/actions/evm/types.ts
CHANGED
|
@@ -13,6 +13,7 @@ import { Hex } from "../../types/misc.js";
|
|
|
13
13
|
import type { ListTokenBalancesOptions, ListTokenBalancesResult } from "./listTokenBalances.js";
|
|
14
14
|
import type { RequestFaucetOptions, RequestFaucetResult } from "./requestFaucet.js";
|
|
15
15
|
import type { SendTransactionOptions, TransactionResult } from "./sendTransaction.js";
|
|
16
|
+
import type { SwapOptions, SwapResult, QuoteSwapOptions, QuoteSwapResult } from "./swap/types.js";
|
|
16
17
|
import type { TransferOptions } from "./transfer/types.js";
|
|
17
18
|
import type {
|
|
18
19
|
WaitForUserOperationOptions,
|
|
@@ -249,6 +250,93 @@ export type AccountActions = Actions & {
|
|
|
249
250
|
* ```
|
|
250
251
|
*/
|
|
251
252
|
sendTransaction: (options: Omit<SendTransactionOptions, "address">) => Promise<TransactionResult>;
|
|
253
|
+
|
|
254
|
+
/**
|
|
255
|
+
* Creates a swap quote without executing the transaction.
|
|
256
|
+
* This is useful when you need to get swap details before executing the swap.
|
|
257
|
+
* The taker is automatically set to the account's address.
|
|
258
|
+
*
|
|
259
|
+
* @param {QuoteSwapOptions} options - Configuration options for creating the swap quote.
|
|
260
|
+
* @param {string} options.network - The network to create the quote on
|
|
261
|
+
* @param {string} options.fromToken - The token address to sell
|
|
262
|
+
* @param {string} options.toToken - The token address to buy
|
|
263
|
+
* @param {bigint} options.fromAmount - The amount of fromToken to sell
|
|
264
|
+
* @param {string} [options.signerAddress] - The signer address (only needed if taker is a smart contract)
|
|
265
|
+
* @param {bigint} [options.gasPrice] - The gas price in Wei
|
|
266
|
+
* @param {number} [options.slippageBps] - The slippage tolerance in basis points (0-10000)
|
|
267
|
+
*
|
|
268
|
+
* @returns A promise that resolves to the swap quote or a response indicating that liquidity is unavailable.
|
|
269
|
+
*
|
|
270
|
+
* @example
|
|
271
|
+
* ```ts
|
|
272
|
+
* const swapQuote = await account.quoteSwap({
|
|
273
|
+
* network: "base",
|
|
274
|
+
* fromToken: "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2", // WETH
|
|
275
|
+
* toToken: "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48", // USDC
|
|
276
|
+
* fromAmount: BigInt("1000000000000000000"), // 1 WETH in wei
|
|
277
|
+
* });
|
|
278
|
+
*
|
|
279
|
+
* if (swapQuote.liquidityAvailable) {
|
|
280
|
+
* console.log(`Can swap for ${swapQuote.toAmount} USDC`);
|
|
281
|
+
* }
|
|
282
|
+
* ```
|
|
283
|
+
*/
|
|
284
|
+
quoteSwap: (options: QuoteSwapOptions) => Promise<QuoteSwapResult>;
|
|
285
|
+
|
|
286
|
+
/**
|
|
287
|
+
* Executes a token swap on the specified network.
|
|
288
|
+
* This method handles all the steps required for a swap, including Permit2 signatures if needed.
|
|
289
|
+
*
|
|
290
|
+
* @param {SwapOptions} options - Configuration options for the swap.
|
|
291
|
+
* @param {string} [options.network] - The network to execute the swap on
|
|
292
|
+
* @param {CreateSwapQuoteResult} [options.swapQuote] - The swap quote returned by the createSwapQuote method.
|
|
293
|
+
* @param {CreateSwapQuoteOptions} [options.swapOptions] - Options to create a swap quote. The function will call createSwapQuote internally.
|
|
294
|
+
* @param {string} [options.idempotencyKey] - Optional idempotency key for the request.
|
|
295
|
+
*
|
|
296
|
+
* @returns A promise that resolves to the transaction hash.
|
|
297
|
+
*
|
|
298
|
+
* @throws {Error} If liquidity is not available when using swapOptions.
|
|
299
|
+
*
|
|
300
|
+
* @example **Using a pre-created swap quote**
|
|
301
|
+
* ```ts
|
|
302
|
+
* // First create a swap quote
|
|
303
|
+
* const swapQuote = await cdp.evm.createSwapQuote({
|
|
304
|
+
* network: "base",
|
|
305
|
+
* buyToken: "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48", // USDC
|
|
306
|
+
* sellToken: "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2", // WETH
|
|
307
|
+
* sellAmount: BigInt("1000000000000000000"), // 1 WETH in wei
|
|
308
|
+
* taker: account.address
|
|
309
|
+
* });
|
|
310
|
+
*
|
|
311
|
+
* // Check if liquidity is available
|
|
312
|
+
* if (!swapQuote.liquidityAvailable) {
|
|
313
|
+
* console.error("Insufficient liquidity for swap");
|
|
314
|
+
* return;
|
|
315
|
+
* }
|
|
316
|
+
*
|
|
317
|
+
* // Execute the swap
|
|
318
|
+
* const { transactionHash } = await account.swap({
|
|
319
|
+
* swapQuote: swapQuote
|
|
320
|
+
* });
|
|
321
|
+
*
|
|
322
|
+
* console.log(`Swap executed with transaction hash: ${transactionHash}`);
|
|
323
|
+
* ```
|
|
324
|
+
*
|
|
325
|
+
* @example **Using swap options (all-in-one)**
|
|
326
|
+
* ```ts
|
|
327
|
+
* // Create and execute swap in one call
|
|
328
|
+
* const { transactionHash } = await account.swap({
|
|
329
|
+
* network: "base",
|
|
330
|
+
* buyToken: "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48", // USDC
|
|
331
|
+
* sellToken: "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2", // WETH
|
|
332
|
+
* sellAmount: BigInt("1000000000000000000"), // 1 WETH in wei
|
|
333
|
+
* taker: account.address
|
|
334
|
+
* });
|
|
335
|
+
*
|
|
336
|
+
* console.log(`Swap executed with transaction hash: ${transactionHash}`);
|
|
337
|
+
* ```
|
|
338
|
+
*/
|
|
339
|
+
swap: (options: SwapOptions) => Promise<SwapResult>;
|
|
252
340
|
};
|
|
253
341
|
|
|
254
342
|
export type SmartAccountActions = Actions & {
|
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:
|
|
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
|
*
|