Sending a Type 3 Transaction on Ethereum: A Step-by-Step Guide
Starting with Ethereum 2.0, Type 3 (EIP-4844) transactions allow for more advanced and flexible smart contract interactions. In this article, we will cover how to send a Type 3 transaction using a blob transaction.
What is an EIP-4844 Transaction?
An EIP-4844 transaction is a new transaction type that allows for the transfer of data from one address to another, without the need for a separate function call. This means that you can now interact with smart contracts in a more simplified way.
How to Send a Type 3 Transaction
To send a Type 3 transaction on Ethereum, you will need to use the eth_sendTransaction
method provided by the web3
library. Here is an example of how to do it:
const web3 = require('web3');
const network = new web3.providers.HttpProvider('
const contractAddress = '0x...'; // Replace with your contract address
const data = 'Hello, world!';
const gasPrice = 20; // Optional gas price (optional)
web3.eth.sendTransaction({
from: network accounts.get('YOUR accounts ADDRESS'), // You need to get the sender account address
gas: 2000000, // Set a high gas limit to avoid gas errors
nonce: web3.utils.toHex(web3.eth.getTransactionCount(network.accounts[0])), // Get the current nonce (optional)
value: web3.utils.toWei('1', 'ether'), // Set a value of 1 ether
data,
gasPrice: gasPrice
}, (error, transactionHash) => {
if (error) {
console.error(error);
} else {
console.log(Transaction sent successfully! Transaction hash: ${transactionHash}
);
}
});
Raw API calls using a type 3 transaction
Here is an example of how to send a type 3 transaction using the web3
library:
const web3 = require('web3');
const network = new web3.providers.HttpProvider('
const contractAddress = '0x...'; // Replace with your contract address
const data = 'Hello, world!';
const gasPrice = 20; // Optional gas price (optional)
web3.eth.sendTransaction({
from: network accounts.get('YOUR accounts ADDRESS'), // You need to get the sender's account address
gas: 2000000, // Set a high gas limit to avoid gas errors
nonce: web3.utils.toHex(web3.eth.getTransactionCount(network.accounts[0])), // Get the current nonce (optional)
value: web3.utils.toWei('1', 'ether'), // Set a value of 1 ether
data,
gasPrice: gasPrice
}, {
jsonrpc: '2.0',
method: 'eth_sendTransaction'
})
Sending a Type 3 Transaction with Blob Data
To send a Type 3 transaction With blob data, you will need to use the eth_sendTransaction
method and specify the jsonrpc
parameter as 2.0
. You can then pass the blob data as a string:
const web3 = require('web3');
const network = new web3.providers.HttpProvider('
const contractAddress = '0x...'; // Replace with your contract address
const gasPrice = 20; // Optional gas price (optional)
const data = Buffer.from('Hello, world!', 'utf8'); // Create a chain data blob
web3.eth.sendTransaction({
from: network accounts.get('YOUR accounts ADDRESS'), // You need to get the sender account address
jsonrpc: '2.0',
method: 'eth_sendTransaction'
}, {
params: [data],
gasPrice,
gas: 2000000, // Set a high gas limit to avoid gas errors
nonce: web3.utils.toHex(web3.eth.getTransactionCount(network.accounts[0])), // Get the current nonce (optional)
value: web3.utils.toWei('1', 'ether'), // Set a value of 1 ether
data,
jsonrpcVersion: 2.0
})
Note that you will need to replace YOUR accounts ADDRESS
and YOUR PROJECT_ID
with your Ethereum account address and project ID, respectively.
Hope this helps!