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.- ethers.js
- viem
- wagmi
ethers.js
ethers.js is one of the most widely adopted Ethereum libraries, offering a clean API for wallets, providers, and contract interaction.Installation
npm install ethers
Signing a Transaction
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;
}
ethers.Wallet handles nonce management and gas estimation automatically when connected to a provider.viem
viem is a modern, type-safe alternative focused on performance and tree-shaking.Installation
npm install viem
Signing a Transaction
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;
}
viem provides full TypeScript inference — hover over any method for complete type information without leaving your editor.
wagmi
wagmi is a React hooks library built on top of viem, ideal for front-end dApps that connect browser wallets.Installation
npm install wagmi viem @tanstack/react-query
Signing a Transaction
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>
);
}
wagmi delegates signing to the user’s connected wallet (MetaMask, WalletConnect, etc.), so you never handle private keys directly in the browser.

