Alchemy Smart Wallets SDK

repository·main·Indexed 43 days ago

https://github.com/alchemyplatform/aa-sdk

A modular SDK for building account abstraction-based applications implementing ERC-4337 and EIP-7702 standards. It provides tools for social login, gas sponsorship, and transaction batching via packages including @alchemy/common, @alchemy/aa-infra for Rundler fee estimation, @alchemy/smart-accounts for LightAccount and Modular Account v1/v2, and @alchemy/wallet-apis for high-level SmartWalletClient interactions.

Tokens
128.8K
Snippets
434
Records
576
Agent score
55%

What's inside Alchemy Smart Wallets SDK

  1. Overview of Alchemy Smart Wallets SDK

    main

    The Alchemy Smart Wallets SDK is designed to build zero-friction sign-up and transaction flows using smart accounts. It leverages the ERC-4337 standard and is built on top of viem. Key features include:

    • Zero-friction onboarding: Sign up via email and login using passkeys.
    • Gas abstraction: Transact with sponsored gas.
    • Efficient transactions: One-click checkout using transaction batching.

    The SDK is modular and extensible, allowing developers to customize layers to meet specific requirements.

  2. Overview of Alchemy Smart Wallets SDK modules

    main

    The Alchemy Smart Wallets SDK is organized into several specialized modules. Depending on your requirements, you may need to interact with different parts of the SDK:

    • @alchemy/aa-infra: Infrastructure-level components for Account Abstraction.
    • @alchemy/common: Shared utilities and common logic used across the SDK.
    • @alchemy/smart-accounts: Core logic for managing and interacting with smart accounts.
    • @alchemy/wallet-apis: High-level APIs for wallet interactions, including:
      • Standard wallet APIs.
      • Experimental wallet APIs.
      • Solana-specific wallet APIs.
  3. Use experimental wallet API features

    main

    The @alchemy/wallet-apis/experimental module provides early-access features for wallet API clients, specifically focusing on swap actions and quoting.

    Warning: This API is experimental and may change in a future release.

    Key capabilities include:

    • Token Swap Quoting: Using requestQuoteV0 to get quotes for token swaps.
    • Swap Actions: Using the swapActions decorator to augment client instances with swap capabilities.
  4. Manage transaction lifecycles with prepare, sign, and send methods

    main

    For more granular control over the transaction lifecycle, use the following sequence of functions:

    1. prepareCalls: Builds a user operation from a set of contract calls. It returns the built user operation and a signature request.
    2. signPreparedCalls: Signs the prepared calls using the provided signer.
    3. sendPreparedCalls: Submits the signed user operation to the network.

    Alternatively, you can use prepareSign and formatSign to handle signature requests for messages or transactions manually.

  5. Quickstart: Create and use a Modular Account v2 with viem

    main

    This example demonstrates how to initialize a Modular Account v2 (MAv2), set up a bundler client with gas estimation and optional paymaster sponsorship, and send a user operation on the Sepolia network.

    Note: This package is a low-level interface for interacting with Smart Accounts via viem. Most users should prefer @alchemy/wallet-apis for higher-level functionality.

    import { createPublicClient } from "viem";
    import {
      createBundlerClient,
      createPaymasterClient,
    } from "viem/account-abstraction";
    import { sepolia } from "viem/chains";
    import { generatePrivateKey, privateKeyToAccount } from "viem/accounts";
    import { alchemyTransport } from "@alchemy/common";
    import { estimateFeesPerGas } from "@alchemy/aa-infra";
    import { toModularAccountV2 } from "@alchemy/smart-accounts";
    
    const transport = alchemyTransport({ apiKey: "YOUR_API_KEY" });
    
    // 1. Create a MAv2 smart account
    const account = await toModularAccountV2({
      client: createPublicClient({ chain: sepolia, transport }),
      owner: privateKeyToAccount(generatePrivateKey()),
    });
    
    // 2. Create a bundler client with the account
    const bundlerClient = createBundlerClient({
      account,
      chain: sepolia,
      transport,
      userOperation: {
        estimateFeesPerGas,
      },
      // Optional: sponsor gas with a paymaster
      paymaster: createPaymasterClient({ transport }),
      paymasterContext: { policyId: "YOUR_POLICY_ID" },
    });
    
    // 3. Send a user operation
    const hash = await bundlerClient.sendUserOperation({
      calls: [{ to: "0x...", value: 0n, data: "0x" }],
    });
    
    const receipt = await bundlerClient.waitForUserOperationReceipt({ hash });
  6. Quickstart: Create and use a Modular Account v2

    main

    This example demonstrates how to initialize a Modular Account v2 (MAv2), set up a bundler client with gas estimation and optional paymaster sponsorship, and send a user operation on the Sepolia network.

    Note: This package is a low-level SDK. Most users should prefer @alchemy/wallet-apis unless they need direct interface with Smart Accounts via viem.

    import { createPublicClient } from "viem";
    import {
      createBundlerClient,
      createPaymasterClient,
    } from "viem/account-abstraction";
    import { sepolia } from "viem/chains";
    import { generatePrivateKey, privateKeyToAccount } from "viem/accounts";
    import { alchemyTransport } from "@alchemy/common";
    import { estimateFeesPerGas } from "@alchemy/aa-infra";
    import { toModularAccountV2 } from "@alchemy/smart-accounts";
    
    const transport = alchemyTransport({ apiKey: "YOUR_API_KEY" });
    
    // 1. Create a MAv2 smart account
    const account = await toModularAccountV2({
      client: createPublicClient({ chain: sepolia, transport }),
      owner: privateKeyToAccount(generatePrivateKey()),
    });
    
    // 2. Create a bundler client with the account
    const bundlerClient = createBundlerClient({
      account,
      chain: sepolia,
      transport,
      userOperation: {
        estimateFeesPerGas,
      },
      // Optional: sponsor gas with a paymaster
      paymaster: createPaymasterClient({ transport }),
      paymasterContext: { policyId: "YOUR_POLICY_ID" },
    });
    
    // 3. Send a user operation
    const hash = await bundlerClient.sendUserOperation({
      calls: [{ to: "0x...", value: 0n, data: "0x" }],
    });
    
    const receipt = await bundlerClient.waitForUserOperationReceipt({ hash });
  7. Execute Solana instructions using the prepare-sign-send pattern

    main

    For more granular control over the transaction lifecycle (e.g., if you need to perform other actions between signing and sending), use the following sequence:

    1. prepareCalls: Builds a versioned transaction from Solana instructions. It returns the compiled transaction and a signature request.
    2. signPreparedCalls: Signs the prepared transaction using the client's Ed25519 signer.
    3. sendPreparedCalls: Submits the signed transaction to the network.