How to Send a Private Transaction on Ethereum

In this tutorial, we will learn how to send a private transaction on Ethereum.

When transactions are sent to the blockchain, they are distributed across the entire network. Transactions that have not been mined yet are classified as "pending" transactions. You can learn more about the various transactions states, here. Pending transactions live in the and are visible to the entire network, which makes them prone to front-running and other forms of arbitrage.

In order to protect your transactions, you can send them through a private endpoint, eth_sendPrivateTransaction, which skips the public mempool and is sent directly to miners. This tutorial will teach you how to use this endpoint to protect your transactions.

3 Steps to Send a Private Transaction

  1. Create free Alchemy account
  2. Create an Alchemy App
  3. Make eth_sendPrivateTransaction API call

Create free Alchemy account

We'll use Alchemy to send our private transaction to Ethereum. You can create an account for free here.

33603360

Create free Alchemy Account

Create an Alchemy App

After creating a new account successfully, we will be redirected to our dashboard, where we can create an App by clicking on the Create App button as shown below.

33193319

Create an Alchemy Ethereum Mainnet App

Next, we will input the following app information:

  • Name: Transaction App
  • Description: Feel free to put anything here.
  • Chain: Ethereum
  • Network: Mainnet
28082808

Create an Alchemy Ethereum Mainnet App

Click on the View Key button as shown below, which will open a popup with our app's HTTP and Websocket URLs. In this tutorial, we will be using the HTTP URL.

17711771

View Alchemy App - API key

Make eth_sendPrivateTransaction API call

Now that we've got everything set up, we'll look at how to send private transactions using eth_sendPrivateTransaction over ethers.js.

Next, we will input the following app information:

Request

const ethers = require("ethers.js");

const {
  FlashbotsBundleProvider,
} = require("@flashbots/ethers-provider-bundle");


const provider = new ethers.providers.JsonRpcProvider({
  url: "https://eth-mainnet.alchemyapi.io/v2/your-api-key", // Our app HTTP URL
});

const authSigner = new ethers.Wallet(
  "ETHEREUM_PRIVATE_KEY" // Our Ethereum Private Key
);


const flashbotsProvider = await FlashbotsBundleProvider.create(
  provider,
  authSigner
);

const signedBundle = await flashbotsProvider.signBundle([
  {
    signer: "SIGNER_TO_SEND_FROM", // Signer adddress to send the transaction from
    transaction: "TRANSACTION_TO_SEND", // The transaction to be sent
  },
]);

const bundleReceipt = await flashbotsProvider.sendRawBundle(
  signedBundle,
  "TARGET_BLOCK_NUMBER" // The target block number
);
URL: https://eth-mainnet.alchemyapi.io/v2/your-api-key // Our app HTTP URL

RequestType: POST

Body: 
{
    "jsonrpc":"2.0",
    "method":"eth_sendPrivateTransaction",
    "params":[{"tx": "0xd46e8dd67c5d32be8d46e8dd67c5d32be8058bb8eb970870f072445675058bb8eb970870f072445675","maxBlockNumber": "0xcd23a0","preferences": { "fast": true }}],"id":1}'
    "id":1
}
curl https://eth-mainnet.alchemyapi.io/v2/your-api-key \
-X POST \
-H "Content-Type: application/json" \
-d '{"jsonrpc":"2.0","method":"eth_sendPrivateTransaction","params":[{"tx": "0xd46e8dd67c5d32be8d46e8dd67c5d32be8058bb8eb970870f072445675058bb8eb970870f072445675","maxBlockNumber": "0xcd23a0","preferences": { "fast": true }}],"id":1}'

Response

{
  "jsonrpc": "2.0",
  "id": 1,
  "result": "0x45df1bc3de765927b053ec029fc9d15d6321945b23cac0614eb0b5e61f3a2f2a" // tx hash
}

Conclusion

This tutorial explained how to send a private transaction on Ethereum using Ethers.js and Alchemy.

If you enjoyed this tutorial for sending a private transaction on Ethereum, give us a tweet @AlchemyPlatform! (Or if you have any questions/feedback)

Don't forget to join our Discord server to meet other blockchain devs, builders, and entrepreneurs!


Did this page help you?