CDP SDK TypeScript - v1.31.0
    Preparing search index...

    Interface CreateSwapQuoteOptions

    Options for creating a swap quote between two tokens on an EVM network.

    interface CreateSwapQuoteOptions {
        network: EvmSwapsNetwork;
        toToken: `0x${string}`;
        fromToken: `0x${string}`;
        fromAmount: bigint;
        taker: `0x${string}`;
        signerAddress?: `0x${string}`;
        smartAccount?: {
            address: `0x${string}`;
            name?: string;
            owners: EvmAccount[];
            type: "evm-smart";
            policies: undefined | string[];
            useNetwork: <Network extends KnownEvmNetworks>(
                network: Network,
            ) => Promise<
                {
                    [K in string
                    | number
                    | symbol]: (
                        Omit<EvmSmartAccountProperties, "useNetwork"> & {
                            [K in string | number | symbol]: (
                                {
                                    sendUserOperation: ...;
                                    waitForUserOperation: ...;
                                    getUserOperation: ...;
                                } & ((...) extends (...) ? (...) : (...)) & (
                                    (...) extends (...) ? (...) : (...)
                                ) & ((...) extends (...) ? (...) : (...)) & (
                                    (...) extends (...) ? (...) : (...)
                                ) & ((...) extends (...) ? (...) : (...)) & (
                                    (...) extends (...) ? (...) : (...)
                                ) & ((...) extends (...) ? (...) : (...)) & (
                                    (...) extends (...) ? (...) : (...)
                                )
                            )[K]
                        } & { network: Network }
                    )[K]
                },
            >;
            listTokenBalances: (
                options: Omit<ListTokenBalancesOptions, "address">,
            ) => Promise<ListTokenBalancesResult>;
            requestFaucet: (
                options: Omit<RequestFaucetOptions, "address">,
            ) => Promise<RequestFaucetResult>;
            quoteFund: (
                options: Omit<EvmQuoteFundOptions, "address">,
            ) => Promise<EvmQuote>;
            fund: (
                options: Omit<EvmFundOptions, "address">,
            ) => Promise<FundOperationResult>;
            waitForFundOperationReceipt(
                options: WaitForFundOperationOptions,
            ): Promise<WaitForFundOperationResult>;
            transfer: (
                options: SmartAccountTransferOptions,
            ) => Promise<SendUserOperationReturnType>;
            sendUserOperation: (
                options: Omit<SendUserOperationOptions<unknown[]>, "smartAccount">,
            ) => Promise<SendUserOperationReturnType>;
            waitForUserOperation: (
                options: Omit<WaitForUserOperationOptions, "smartAccountAddress">,
            ) => Promise<WaitForUserOperationReturnType>;
            getUserOperation: (
                options: Omit<GetUserOperationOptions, "smartAccount">,
            ) => Promise<UserOperation>;
            quoteSwap: (
                options: SmartAccountQuoteSwapOptions,
            ) => Promise<SmartAccountQuoteSwapResult>;
            swap: (
                options: SmartAccountSwapOptions,
            ) => Promise<SmartAccountSwapResult>;
            signTypedData: (
                options: Omit<SignTypedDataOptions, "address"> & {
                    network: KnownEvmNetworks;
                },
            ) => Promise<`0x${string}`>;
            __experimental_useSpendPermission: (
                options: UseSpendPermissionOptions,
            ) => Promise<SendUserOperationReturnType>;
        };
        gasPrice?: bigint;
        slippageBps?: number;
        idempotencyKey?: string;
    }
    Index

    Properties

    network: EvmSwapsNetwork

    The network to create a swap quote on.

    toToken: `0x${string}`

    The token to receive (destination token).

    fromToken: `0x${string}`

    The token to send (source token).

    fromAmount: bigint

    The amount to send in atomic units of the token.

    taker: `0x${string}`

    The address receiving the output of the swap.

    signerAddress?: `0x${string}`

    The address signing the swap (only needed if taker is a smart contract, i.e. for smart account swaps).

    smartAccount?: {
        address: `0x${string}`;
        name?: string;
        owners: EvmAccount[];
        type: "evm-smart";
        policies: undefined | string[];
        useNetwork: <Network extends KnownEvmNetworks>(
            network: Network,
        ) => Promise<
            {
                [K in string
                | number
                | symbol]: (
                    Omit<EvmSmartAccountProperties, "useNetwork"> & {
                        [K in string | number | symbol]: (
                            {
                                sendUserOperation: ...;
                                waitForUserOperation: ...;
                                getUserOperation: ...;
                            } & ((...) extends (...) ? (...) : (...)) & (
                                (...) extends (...) ? (...) : (...)
                            ) & ((...) extends (...) ? (...) : (...)) & (
                                (...) extends (...) ? (...) : (...)
                            ) & ((...) extends (...) ? (...) : (...)) & (
                                (...) extends (...) ? (...) : (...)
                            ) & ((...) extends (...) ? (...) : (...)) & (
                                (...) extends (...) ? (...) : (...)
                            )
                        )[K]
                    } & { network: Network }
                )[K]
            },
        >;
        listTokenBalances: (
            options: Omit<ListTokenBalancesOptions, "address">,
        ) => Promise<ListTokenBalancesResult>;
        requestFaucet: (
            options: Omit<RequestFaucetOptions, "address">,
        ) => Promise<RequestFaucetResult>;
        quoteFund: (
            options: Omit<EvmQuoteFundOptions, "address">,
        ) => Promise<EvmQuote>;
        fund: (
            options: Omit<EvmFundOptions, "address">,
        ) => Promise<FundOperationResult>;
        waitForFundOperationReceipt(
            options: WaitForFundOperationOptions,
        ): Promise<WaitForFundOperationResult>;
        transfer: (
            options: SmartAccountTransferOptions,
        ) => Promise<SendUserOperationReturnType>;
        sendUserOperation: (
            options: Omit<SendUserOperationOptions<unknown[]>, "smartAccount">,
        ) => Promise<SendUserOperationReturnType>;
        waitForUserOperation: (
            options: Omit<WaitForUserOperationOptions, "smartAccountAddress">,
        ) => Promise<WaitForUserOperationReturnType>;
        getUserOperation: (
            options: Omit<GetUserOperationOptions, "smartAccount">,
        ) => Promise<UserOperation>;
        quoteSwap: (
            options: SmartAccountQuoteSwapOptions,
        ) => Promise<SmartAccountQuoteSwapResult>;
        swap: (options: SmartAccountSwapOptions) => Promise<SmartAccountSwapResult>;
        signTypedData: (
            options: Omit<SignTypedDataOptions, "address"> & {
                network: KnownEvmNetworks;
            },
        ) => Promise<`0x${string}`>;
        __experimental_useSpendPermission: (
            options: UseSpendPermissionOptions,
        ) => Promise<SendUserOperationReturnType>;
    }

    The smart account object (required for smart account execution context only).

    Type declaration

    • address: `0x${string}`

      The smart account's address.

    • Optionalname?: string

      The name of the smart account.

    • owners: EvmAccount[]

      Array of accounts that own and can sign for the smart account (currently only supports one owner but will be extended to support multiple owners in the future).

    • type: "evm-smart"

      Identifier for the smart account type.

    • policies: undefined | string[]

      The list of policy IDs that apply to the smart account. This will include both the project-level policy and the account-level policy, if one exists.

    • useNetwork: <Network extends KnownEvmNetworks>(
          network: Network,
      ) => Promise<
          {
              [K in string
              | number
              | symbol]: (
                  Omit<EvmSmartAccountProperties, "useNetwork"> & {
                      [K in string | number | symbol]: (
                          {
                              sendUserOperation: ...;
                              waitForUserOperation: ...;
                              getUserOperation: ...;
                          } & ((...) extends (...) ? (...) : (...)) & (
                              (...) extends (...) ? (...) : (...)
                          ) & ((...) extends (...) ? (...) : (...)) & (
                              (...) extends (...) ? (...) : (...)
                          ) & ((...) extends (...) ? (...) : (...)) & (
                              (...) extends (...) ? (...) : (...)
                          ) & ((...) extends (...) ? (...) : (...)) & (
                              (...) extends (...) ? (...) : (...)
                          )
                      )[K]
                  } & { network: Network }
              )[K]
          },
      >

      A function that returns a network-scoped smart account.

      // For known networks, type is inferred automatically:
      const baseAccount = await smartAccount.useNetwork("base");

      // For custom RPC URLs with type hints (requires casting):
      const typedAccount = await smartAccount.useNetwork<"base">("https://mainnet.base.org" as "base");

      // For custom RPC URLs without type hints (only sendTransaction, transfer and waitForTransactionReceipt methods available):
      const customAccount = await smartAccount.useNetwork("https://mainnet.base.org");
    • listTokenBalances: (
          options: Omit<ListTokenBalancesOptions, "address">,
      ) => Promise<ListTokenBalancesResult>

      List the token balances of an account.

      const balances = await account.listTokenBalances({
      network: "base-sepolia",
      });
    • requestFaucet: (options: Omit<RequestFaucetOptions, "address">) => Promise<RequestFaucetResult>

      Requests funds from an EVM faucet.

      const result = await account.requestFaucet({
      network: "base-sepolia",
      token: "eth",
      });
    • quoteFund: (options: Omit<EvmQuoteFundOptions, "address">) => Promise<EvmQuote>

      Gets a quote to fund an EVM account.

      const quote = await account.quoteFund({
      network: "base",
      token: "usdc",
      amount: 1000000n,
      });
    • fund: (options: Omit<EvmFundOptions, "address">) => Promise<FundOperationResult>

      Funds an EVM account with the specified token amount.

      const fundOperation = await account.fund({
      network: "base",
      token: "usdc",
      amount: 1000000n,
      });
    • waitForFundOperationReceipt: function
      • Waits for a fund operation to complete and returns the transfer receipt.

        Parameters

        • options: WaitForFundOperationOptions

          The options for the wait for fund operation.

          • transferId

            The ID of the transfer to wait for.

        Returns Promise<WaitForFundOperationResult>

        A promise that resolves to the completed transfer receipt containing details about the funding operation.

        const completedTransfer = await account.waitForFundOperationReceipt({
        transferId: "transfer_123",
        });
    • transfer: (options: SmartAccountTransferOptions) => Promise<SendUserOperationReturnType>

      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",
      });
    • sendUserOperation: (
          options: Omit<SendUserOperationOptions<unknown[]>, "smartAccount">,
      ) => Promise<SendUserOperationReturnType>

      Sends a user operation.

      const userOp = await smartAccount.sendUserOperation({
      network: "base-sepolia",
      calls: [
      {
      to: "0x1234567890123456789012345678901234567890",
      value: parseEther("0.000001"),
      data: "0x",
      },
      ],
      });
    • waitForUserOperation: (
          options: Omit<WaitForUserOperationOptions, "smartAccountAddress">,
      ) => Promise<WaitForUserOperationReturnType>

      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,
      });
    • getUserOperation: (
          options: Omit<GetUserOperationOptions, "smartAccount">,
      ) => Promise<UserOperation>

      Gets a user operation by its hash.

      const userOp = await smartAccount.getUserOperation({
      userOpHash: "0x1234567890123456789012345678901234567890",
      });
    • quoteSwap: (options: SmartAccountQuoteSwapOptions) => Promise<SmartAccountQuoteSwapResult>

      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`);
      }
    • swap: (options: SmartAccountSwapOptions) => Promise<SmartAccountSwapResult>

      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.

      If liquidity is not available when using inline options.

      // 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}`);
    • signTypedData: (
          options: Omit<SignTypedDataOptions, "address"> & {
              network: KnownEvmNetworks;
          },
      ) => Promise<`0x${string}`>

      Signs a typed data message.

      const signature = await smartAccount.signTypedData({
      network: "base-sepolia",
      typedData: {
      domain: {
      name: "Test",
      chainId: 84532,
      verifyingContract: "0x0000000000000000000000000000000000000000",
      },
      types: {
      Test: [{ name: "name", type: "string" }],
      },
      primaryType: "Test",
      message: {
      name: "John Doe",
      },
      },
      });
    • __experimental_useSpendPermission: (options: UseSpendPermissionOptions) => Promise<SendUserOperationReturnType>

      Experimental! This method name will change, and is subject to other breaking changes.

      Uses a spend permission to execute a transaction via user operation. This allows the smart account to spend tokens that have been approved via a spend permission.

      If the network doesn't support spend permissions via CDP API.

      const spendPermission = {
      account: "0x1234...", // Smart account that owns the tokens
      spender: smartAccount.address, // This smart account that can spend
      token: "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE", // ETH
      allowance: parseEther("0.01"),
      period: 86400, // 1 day
      start: 0,
      end: 281474976710655,
      salt: 0n,
      extraData: "0x",
      };

      const result = await smartAccount.__experimental_useSpendPermission({
      spendPermission,
      value: parseEther("0.001"), // Spend 0.001 ETH
      network: "base-sepolia",
      });
    gasPrice?: bigint

    The price per unit of gas in wei.

    slippageBps?: number

    The slippage tolerance in basis points (0-10000).

    idempotencyKey?: string

    The idempotency key.