Creates a swap quote without executing the transaction. This is useful when you need to get swap details before executing the swap. The taker is automatically set to the smart account's address.
const swapQuote = await smartAccount.quoteSwap({
network: "base",
fromToken: "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2", // WETH
toToken: "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48", // USDC
fromAmount: BigInt("1000000000000000000"), // 1 WETH in wei
});
if (swapQuote.liquidityAvailable) {
console.log(`Can swap for ${swapQuote.toAmount} USDC`);
}
Sends a user operation.
Executes a token swap on the specified network via a user operation. This method handles all the steps required for a swap, including Permit2 signatures if needed. The taker is automatically set to the smart account's address.
// First create a swap quote
const swapQuote = await cdp.evm.createSwapQuote({
network: "base",
toToken: "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48", // USDC
fromToken: "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2", // WETH
fromAmount: BigInt("1000000000000000000"), // 1 WETH in wei
taker: smartAccount.address,
signerAddress: smartAccount.owners[0].address
});
// Check if liquidity is available
if (!swapQuote.liquidityAvailable) {
console.error("Insufficient liquidity for swap");
return;
}
// Execute the swap
const { userOpHash } = await smartAccount.swap({
swapQuote: swapQuote
});
console.log(`Swap executed with user op hash: ${userOpHash}`);
// Create and execute swap in one call
const { userOpHash } = await smartAccount.swap({
network: "base",
toToken: "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48", // USDC
fromToken: "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2", // WETH
fromAmount: BigInt("1000000000000000000"), // 1 WETH in wei
});
console.log(`Swap executed with user op hash: ${userOpHash}`);
Transfer an amount of a token from an account to another account.
const { userOpHash } = await sender.transfer({
to: "0x9F663335Cd6Ad02a37B633602E98866CF944124d",
amount: 10000n, // equivalent to 0.01 USDC
token: "usdc",
network: "base-sepolia",
});
Using parseUnits to specify USDC amount
import { parseUnits } from "viem";
const { userOpHash } = await sender.transfer({
to: "0x9F663335Cd6Ad02a37B633602E98866CF944124d",
amount: parseUnits("0.01", 6), // USDC uses 6 decimal places
token: "usdc",
network: "base-sepolia",
});
Transfer ETH
import { parseEther } from "viem";
const { userOpHash } = await sender.transfer({
to: "0x9F663335Cd6Ad02a37B633602E98866CF944124d",
amount: parseEther("0.000001"),
token: "eth",
network: "base-sepolia",
});
Using a contract address
import { parseEther } from "viem";
const { userOpHash } = await sender.transfer({
to: "0x9F663335Cd6Ad02a37B633602E98866CF944124d",
amount: parseEther("0.000001"),
token: "0x4200000000000000000000000000000000000006", // WETH on Base Sepolia
network: "base-sepolia",
});
Transfer to another account
const sender = await cdp.evm.createAccount({ name: "Sender" });
const receiver = await cdp.evm.createAccount({ name: "Receiver" });
const { userOpHash } = await sender.transfer({
to: receiver,
amount: 10000n, // equivalent to 0.01 USDC
token: "usdc",
network: "base-sepolia",
});
Waits for a user operation to complete or fail.
// Send a user operation and get the user operation hash
const { userOpHash } = await smartAccount.sendUserOperation({
network: "base-sepolia",
calls: [
{
to: "0x0000000000000000000000000000000000000000",
value: parseEther("0.000001"),
data: "0x",
},
],
});
// Wait for the user operation to complete or fail
const result = await smartAccount.waitForUserOperation({
userOpHash: userOp.userOpHash,
});
Gets a user operation by its hash.