Using Ethereum Tools: How to Set Gas Price on Klaytn

·

Klaytn’s v1.8.0 upgrade marks a major leap toward Ethereum equivalence, enabling developers to use popular Ethereum development tools—like web3.js, ethers.js, Hardhat, and Truffle—with minimal modifications. While this compatibility streamlines dApp development, a critical difference remains: Klaytn uses a fixed gas price model, unlike Ethereum’s dynamic EIP-1559 fee market.

Understanding how to correctly set gas parameters is essential to avoid transaction failures when deploying smart contracts or interacting with decentralized applications on Klaytn. This guide walks you through the necessary configurations for each major Ethereum tool, ensuring your transactions are compatible and efficiently processed.

Core Keywords


Why Gas Price Matters on Klaytn

With Ethereum’s London hard fork and EIP-1559, gas pricing became dynamic, using baseFeePerGas, maxFeePerGas, and maxPriorityFeePerGas. In contrast, Klaytn maintains a fixed gas price—250 ston (0.00000025 KLAY) on both Baobab (testnet) and Cypress (mainnet) at the time of writing.

Ethereum-native tools like web3.js and ethers.js automatically estimate gas fees based on network congestion. However, these estimates don’t apply to Klaytn’s fixed model. If you don’t explicitly define gas values, the tools may inject Ethereum-style dynamic fee fields, leading to rejected transactions.

👉 Discover how to optimize gas settings for seamless blockchain development on Klaytn.

To ensure compatibility, developers must manually set the correct gas price fields depending on the transaction type and tool used.


Setting Gas Price in web3.js

Legacy Transactions (Pre-EIP-1559)

For legacy-style transactions, explicitly define the gasPrice field using Klaytn’s fixed rate (250,000,000,000 wei):

const url = "your-provider-url";
const privateKey = "your-private-key";
const Web3 = require("web3");
const web3 = new Web3(url);

const tx = await web3.eth.accounts.signTransaction({
  to: "0x8a726c5d1b9b7796df6836f24ef9ea50ab5178c6",
  value: 90000000000,
  gasPrice: 250000000000,
  gas: 21000,
}, privateKey);

const receipt = await web3.eth.sendSignedTransaction(tx.rawTransaction);

Dynamic Fee Transactions (Post-EIP-1559)

For EIP-1559 compatible transactions, set both maxFeePerGas and maxPriorityFeePerGas to the fixed gas price:

const tx = await web3.eth.accounts.signTransaction({
  to: "0x8a726c5d1b9b7796df6836f24ef9ea50ab5178c6",
  value: 90000000000,
  maxFeePerGas: 250000000000,
  maxPriorityFeePerGas: 250000000000,
  gas: 21000,
}, privateKey);

Setting Gas Price in ethers.js

Legacy Transactions

Use the gasPrice field when sending transactions via an ethers wallet:

const { ethers } = require("ethers");

const url = "your-provider-url";
const provider = new ethers.providers.JsonRpcProvider(url);
const wallet = new ethers.Wallet("private-key", provider);

const tx = await wallet.sendTransaction({
  to: "recipient-address",
  value: ethers.utils.parseEther("0.09"),
  gasPrice: 250000000000,
  gasLimit: 21000,
});

await tx.wait();

Dynamic Fee Transactions

Set both maxFeePerGas and maxPriorityFeePerGas to the fixed value:

const tx = await wallet.sendTransaction({
  to: "recipient-address",
  value: ethers.utils.parseEther("0.09"),
  maxFeePerGas: 250000000000,
  maxPriorityFeePerGas: 250000000000,
  gasLimit: 21000,
});

Configuring Gas Price in Hardhat

Hardhat supports two methods for setting gas prices on Klaytn.

Method 1: Set Default in hardhat.config.js

Define the gas price globally for the Klaytn network:

module.exports = {
  networks: {
    klaytn: {
      url: "https://public-en-cypress.klaytn.net",
      gasPrice: 250000000000,
      accounts: ["your-private-key"],
    },
  },
};

This ensures all transactions default to the correct gas price.

Method 2: Inline in Transaction Code

You can override config settings by specifying gas parameters directly in your scripts using either web3.js or ethers.js patterns.

👉 Learn how to configure Hardhat for cross-chain development with fixed fee models.

Tip: Forking Considerations

When forking mainnet using Hardhat’s forking feature:


Setting Gas Price in Truffle

Like Hardhat, Truffle offers two configuration paths.

Method 1: Global Settings in truffle-config.js

Truffle requires explicit definition of either legacy or dynamic fee fields—not both.

For Legacy Transactions:

module.exports = {
  networks: {
    klaytn: {
      provider: () => new HDWalletProvider({
        providerOrUrl: "https://public-en-baobab.klaytn.net",
        privateKeys: ["your-private-key"],
      }),
      gas: 50000000,
      gasPrice: 250000000000,
      network_id: "1001",
    },
  },
};

For Dynamic Fee Transactions:

module.exports = {
  networks: {
    klaytn: {
      provider: () => new HDWalletProvider({
        providerOrUrl: "https://public-en-cypress.klaytn.net",
        privateKeys: ["your-private-key"],
      }),
      gas: 5000000,
      maxFeePerGas: 250000000000,
      maxPriorityFeePerGas: 25000000000,
      network_id: "8217",
    },
  },
};
Warning: If you use Truffle migrations, prefer config-level gas settings. Inline gas definitions won’t apply to migration history recording, potentially causing deployment failures.

Method 2: Inline in Deployment Scripts

When deploying contracts, pass gas options directly:

Legacy:

deployer.deploy(MetaCoin, {
  gasPrice: 2500000000,
  from: deployer.address,
  gas: 3e6
});

Dynamic:

deployer.deploy(MetaCoin, {
  maxFeePerGas: 25e9,
  maxPriorityFeePerGas: 2e9,
  from: deployer.address,
  gas: 3e6
});

Frequently Asked Questions (FAQ)

Q1: Why do my Ethereum tool transactions fail on Klaytn?

Transactions fail if gas fields aren’t explicitly set. Ethereum tools auto-generate dynamic fees incompatible with Klaytn’s fixed model. Always define gasPrice, maxFeePerGas, or maxPriorityFeePerGas manually.

Q2: What is the current gas price on Klaytn?

As of now, both Baobab (testnet) and Cypress (mainnet) use a fixed gas price of 25 Gpeb (25, or 25e9 wei). Confirm current values via official Klaytn documentation or network explorers.

Q3: Can I use EIP-1559 transactions on Klaytn?

Yes. Klaytn supports EIP-1559-style transactions but treats maxFeePerGas and maxPriorityFeePerGas as fixed values—set both to the network’s standard gas price.

Q4: Does Truffle support Klaytn natively?

Truffle doesn’t natively support Klaytn but works seamlessly via custom network configurations and HDWalletProvider. Ensure proper RPC endpoints and gas settings.

Q5: Do I need different configs for testnet and mainnet?

Yes. While gas prices are currently identical, network IDs differ:

Update network_id accordingly in your config files.


Final Notes

Klaytn’s Ethereum equivalence opens the door for developers to leverage familiar tools across ecosystems. However, success hinges on understanding key differences—especially around gas pricing. By explicitly setting gas parameters in web3.js, ethers.js, Hardhat, or Truffle, you ensure reliable transaction execution.

Whether building DeFi protocols, NFT marketplaces, or enterprise dApps, proper configuration is foundational. As blockchain interoperability grows, mastering these nuances gives developers a competitive edge.

👉 Start building on Klaytn with optimized Ethereum tooling today.