Documentation Index
Fetch the complete documentation index at: https://yieldxyz.mintlify.app/llms.txt
Use this file to discover all available pages before exploring further.
Sign EVM transactions using ethers.js, viem, or wagmi
Documentation Index
Fetch the complete documentation index at: https://yieldxyz.mintlify.app/llms.txt
Use this file to discover all available pages before exploring further.
npm install ethers
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.npm install viem
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;
}
npm install wagmi viem @tanstack/react-query
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>
);
}