> ## Documentation Index
> Fetch the complete documentation index at: https://yieldxyz.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# EVM Signing

> Sign EVM transactions using ethers.js, viem, or wagmi

## Overview

EVM transactions returned by the Yield.xyz API can be signed using any Ethereum-compatible library. Below you'll find examples for the three most popular options.

<Tabs>
  <Tab title="ethers.js">
    ### ethers.js

    [ethers.js](https://docs.ethers.org/) is one of the most widely adopted Ethereum libraries, offering a clean API for wallets, providers, and contract interaction.

    #### Installation

    ```bash theme={null}
    npm install ethers
    ```

    #### Signing a Transaction

    ```javascript theme={null}
    import { ethers } from "ethers";

    const provider = new ethers.JsonRpcProvider(process.env.RPC_URL);
    const wallet = new ethers.Wallet(process.env.PRIVATE_KEY, provider);

    async function signAndSend(unsignedTx) {
      const tx = await wallet.sendTransaction({
        to: unsignedTx.to,
        data: unsignedTx.data,
        value: unsignedTx.value ?? 0,
        gasLimit: unsignedTx.gasLimit,
      });

      const receipt = await tx.wait();
      console.log("Transaction hash:", receipt.hash);
      return receipt;
    }
    ```

    <Tip>
      `ethers.Wallet` handles nonce management and gas estimation automatically when connected to a provider.
    </Tip>
  </Tab>

  <Tab title="viem">
    ### viem

    [viem](https://viem.sh/) is a modern, type-safe alternative focused on performance and tree-shaking.

    #### Installation

    ```bash theme={null}
    npm install viem
    ```

    #### Signing a Transaction

    ```typescript theme={null}
    import { createWalletClient, http } from "viem";
    import { privateKeyToAccount } from "viem/accounts";
    import { mainnet } from "viem/chains";

    const account = privateKeyToAccount(process.env.PRIVATE_KEY as `0x${string}`);

    const client = createWalletClient({
      account,
      chain: mainnet,
      transport: http(process.env.RPC_URL),
    });

    async function signAndSend(unsignedTx: {
      to: `0x${string}`;
      data: `0x${string}`;
      value?: bigint;
    }) {
      const hash = await client.sendTransaction({
        to: unsignedTx.to,
        data: unsignedTx.data,
        value: unsignedTx.value ?? 0n,
      });

      console.log("Transaction hash:", hash);
      return hash;
    }
    ```

    <Tip>
      viem provides full TypeScript inference — hover over any method for complete type information without leaving your editor.
    </Tip>
  </Tab>

  <Tab title="wagmi">
    ### wagmi

    [wagmi](https://wagmi.sh/) is a React hooks library built on top of viem, ideal for front-end dApps that connect browser wallets.

    #### Installation

    ```bash theme={null}
    npm install wagmi viem @tanstack/react-query
    ```

    #### Signing a Transaction

    ```tsx theme={null}
    import { useSendTransaction, useWaitForTransactionReceipt } from "wagmi";

    function SignTransaction({ unsignedTx }) {
      const { data: hash, sendTransaction } = useSendTransaction();

      const { isLoading, isSuccess } = useWaitForTransactionReceipt({ hash });

      const handleSign = () => {
        sendTransaction({
          to: unsignedTx.to,
          data: unsignedTx.data,
          value: unsignedTx.value ?? 0n,
        });
      };

      return (
        <div>
          <button onClick={handleSign} disabled={isLoading}>
            {isLoading ? "Confirming…" : "Sign & Send"}
          </button>
          {isSuccess && <p>Transaction confirmed: {hash}</p>}
        </div>
      );
    }
    ```

    <Note>
      wagmi delegates signing to the user's connected wallet (MetaMask, WalletConnect, etc.), so you never handle private keys directly in the browser.
    </Note>
  </Tab>
</Tabs>
