sendTransaction - SDK

Submits transaction to the network to be mined. The transaction must be signed, and be valid (i.e. the nonce is correct and the account has sufficient balance to pay for the transaction).

Don’t have an API key?

Start using this method in your app today.

Description

Submits transaction to the network to be mined. The transaction must be signed, and valid (i.e. the nonce is correct and the account has sufficient balance to pay for the transaction).

Parameters

NameTypeDescription
signedTransactionstringThe signed transaction to send.

Response

PropertyTypeDescription
Promise<TransactionResponse>objectReturns the transaction response.

TransactionResponse object parameters

ParameterTypeDescription
tostringDATA, 20 Bytes - The address the transaction is directed to.
hashstringThe transaction hash.
fromstringDATA, 20 Bytes - (optional) The address the transaction is sent from.
blockNumbernumberThe block number where this log was in. null when its pending. null when its pending log.
blockHashstring32 Bytes - hash of the block where this log was in. null when its pending. null when its pending log.
gasPrice?objectAn object containing the type and hex parameters of the gasPrice used for each paid gas.
data?stringDATA - (optional) Hash of the method signature and encoded parameters. For details see Ethereum Contract ABI
value?objectAn object containing the type and hex parameters of the value sent with this transaction
nonce?stringA nonce is a value that represents the number of transactions sent by the sender's address, ensuring the transactions are processed in order and preventing replay attacks.
gasLimit?objectAn object containing the type and hex parameters of the the maximum gas allowed.
chainId?numberoptional the Id of the transaction chain.
timestampnumberOptional. Returns only if a transaction has been mined.
confirmationsnumbernumber of transaction confirmations.
type?numberOptional This is the type of transaction.
accessList?array of stringsThe accessList is an optional transaction parameter that specifies addresses and storage keys a transaction interacts with, enabling gas cost optimization and compatibility with future Ethereum upgrades.
maxPriorityFee?numberThis describes the maximum priority fee for this transaction.
maxFeePerGas?numberThis is the maximum fee per gas for this transaction.
rawstringThe raw transaction.
ccipReadEnabled?booleanThis specifies if the CCIP read is enabled.

Example Request and Response

Prerequisite: You will need to install the Alchemy SDK before making requests with it.

The commands for installing it using npm or yarn are given below:

npm install alchemy-sdk@latest
yarn add alchemy-sdk@latest

Request

// Imports the Alchemy SDK
const { Alchemy, Network, Wallet, Utils } = require("alchemy-sdk");
const dotenv = require("dotenv");
dotenv.config();

//Replace with your own private key
const {PRIVATE_KEY} = process.env;

// Configures the Alchemy SDK
const config = {
    apiKey: "alchemy-replit", // Replace with your API key
    network: Network.ETH_MAINNET, // Replace with your network
};

// Creates an Alchemy object instance with the config to use for making requests
const alchemy = new Alchemy(config);
const wallet = new Wallet(PRIVATE_KEY);

const main = async () => {
    // define the transaction
    const transaction = {
        to: "0xa238b6008Bc2FBd9E386A5d4784511980cE504Cd",
        value: Utils.parseEther("0.001"),
        gasLimit: "21000",
        maxPriorityFeePerGas: Utils.parseUnits("5", "gwei"),
        maxFeePerGas: Utils.parseUnits("20", "gwei"),
        nonce: await alchemy.core.getTransactionCount(wallet.getAddress()),
        type: 2,
        chainId: 5, // Corresponds to ETH_GOERLI
      };

    const rawTransaction = await wallet.signTransaction(transaction);
    const response = await alchemy.transact.sendTransaction(rawTransaction)

    //Logging the response to the console
    console.log(response)
}

main();

Response

{ 
 value: {
    to: '0xa238b6008Bc2FBd9E386A5d4784511980cE504Cd',
    value: {
      "type": "BigNumber",
      "hex": '0x038d7ea4c68000'
    },
    gasLimit: '21000',
    maxPriorityFeePerGas:  { 
      "type": "BigNumber",
      "hex": '0x012a05f200'
    },
    maxFeePerGas: {
      "type": "BigNumber"
      "hex": '0x04a817c800'
    },
    type: 2,
    chainId: 5
  }
}

Code Sandbox

You can test out the sendTransaction method using the code sandbox below:

Use Cases

Here are some potential use cases for the sendTransaction method:

  • Sending ETH: sendTransaction can be used to send Ether from one Ethereum address to another. This is one of the most common use cases for sendTransaction.

  • Deploying a smart contract: When you deploy a smart contract to the Ethereum blockchain, you need to send a transaction that includes the bytecode of the contract. sendTransaction can be used to send this transaction.

  • Interacting with a smart contract: Once a smart contract has been deployed, you can interact with it by sending transactions that call its functions. sendTransaction can be used to send these transactions.

  • Token transfers: Tokens on the Ethereum blockchain are often built using smart contracts. sendTransaction can be used to transfer tokens from one Ethereum address to another.

Related Methods

Here are the methods related to sendTransaction:

  • sendPrivateTransaction: Used to send a single transaction to Flashbots. Flashbots will attempt to send the transaction to miners for the next 25 blocks.
ReadMe