Using Ethereum tools in Klaytn

The Klaytn Team has picked out some of the most well-known, and widely used Ethereum tools to test on Klaytn. Here we will explain some things to consider when you are using Ethereum tools on Klaytn.

Define the fields related to `GasPrice`

Klaytn fixed its gas price (Baobab 750ston, Cypress 25ston at the time of writing).

If the fields relating to gas price, such as `gasPrice` or `max_priority_fee_pef_gas`, `max_fee_per_gas`, are not defined, Ethereum tools will create a transaction using values randomly generated by the library. But as mentioned above, since Klaytn uses a fixed `gasPrice`, so a value generated by the library will result in a transaction failure. That is why the fields relating to `gasPrice` have to be defined when you are using Ethereum development tools.

Below we would like to demonstrate how to set the `gasPrice` for the four tools: web3.js ethers.js, Truffle, and Hardhat. The `gasPrice` value used in the code samples are all 25ston. Remember that the `gasPrice` is different for each network.

web3.js

When you want to create a legacy transaction, add the `gasPrice` field.

const url = `provider url`
const privateKey = `privateKey`
const web3 = new Web3(url)

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

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

If you want to create a dynamicFee transaction, add the fields `maxFeePerGas` and `maxPriorityFeePerGas`.

const url = `provider url`
const privateKey = `privateKey`
const web3 = new Web3(url)

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

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

ethers.js

If you want to create a legacy transaction, add the field `gasPrice`.

const url = `provider url`
const provider = new ethers.providers.JsonRpcProvider(url)
const wallet = new ethers.Wallet(privKey, provider)

const tx = await wallet.sendTransaction({
to: account,
value: 90000000000,
gasPrice: 25000000000,
gasLimit: 21000,
})

const receipt = await tx.wait()

If you want to create a dynamicFee transaction, add the fields `maxFeePerGas` and `maxPriorityFeePerGas`.

const url = `provider url`
const provider = new ethers.providers.JsonRpcProvider(url)
const wallet = new ethers.Wallet(privKey, provider)

const tx = await wallet.sendTransaction({
to: account,
value: 90000000000,
maxFeePerGas: 25000000000,
maxPriorityFeePerGas: 25000000000,
gasLimit: 21000,
})

const receipt = await tx.wait()

Hardhat

There are two ways to set `gasPrice` on Hardhat.

  1. Define `gasPrice` to be used as the default value for all transactions in Hardhat.config.js
  2. Define the `gasPrice` fields in the transaction code

1. Defining the values in Hardhat.config.js

In networks options of Hardhat.config, you can define and use the options to be used when you are connecting to the blockchain network. For more details, please refer to the official documentation.

//Hardhat.config.jsmodule.exports = {
networks: {
Hardhat: {
...............
}
},
klaytn: {
url: "provider url",
gasPrice: 25000000000,
account: [...],
}
},

2. Defining the values in the transaction code

Hardhat supports both web3.js and ethers.js. You can use a library that you prefer. For the instructions for each library, refer to the explanations above.

Tips on network forking

You can fork archive mode nodes with Hardhat. For full mode nodes, you have to set an appropriate block number that corresponds to the state storage cycle. (For default setting, a multiple of 128).

Truffle

Just as with Hardhat, there are two ways to set `gasPrice` with Truffle.

  1. Define `gasPrice` to be used as the default value for all transactions in Truffle-config.js
  2. Define the `gasPrice` fields in the transaction code

Warning: If you want to use migration contracts that manage contract deployment history, we recommend using the first method (Truffle-config.js). The second method cannot control the gas price at the time of calling the function that records history on the migration contract, which may prevent it from functioning normally.

1. Defining the values in Truffle-config.js

In the networks options in Truffle-config.js, you can define and use the options to be used when you are connecting to the blockchain network via Truffle. Unlike Hardhat, you can differentiate between `gasPrice` and `maxFeePerGas`, `maxPriorityFeePerGas` used in `DynamicFee`. Please refer to the documentation for more information.

If you want to create legacy transactions every time, add the `gasPrice` field.

module.exports = {
networks: {
development: {
host: "127.0.0.1",
port: 7545,
network_id: "*"
},
klaytn: {
provider: () =>
new HDWalletProvider({
providerOrUrl: "provider url",
privateKeys: [],
}),
gas: 50000000,
gasPrice: 25000000000,
network_id: "*"
}
},
};

If you want to create dynamicFee transactions every time, add the fields `maxFeePerGas` and `maxPriorityFeePerGas`.

module.exports = {
networks: {
development: {
host: "127.0.0.1",
port: 7545,
network_id: "*"
},
klaytn: {
provider: () =>
new HDWalletProvider({
providerOrUrl: "provider url",
privateKeys: [],
}),
gas: 50000000,
maxFeePerGas: 25000000000,
maxPriorityFeePerGas: 25000000000,
network_id: "*"
}
},
};

2. Defining the values in the transaction code

Truffle abstracts contracts, so you can easily deploy and call them. For more information, please refer to the official documentation.

If you look at the contract APIs provided on Truffle, you will see that you can add the transaction parameters. Here you can add the fields related to `gasPrice`.

If you want to create legacy transactions, add the `gasPrice` field.

deployer.deploy(MetaCoin, {gasPrice: 25000000000,
from: ...,
gas: ...,
});

If you want to create DynamicFee transactions, add the fields `maxFeePerGas` and `maxPriorityFeePerGas`.

deployer.deploy(MetaCoin, {
maxFeePerGas: 25000000000,
maxPriorityFeePerGas: 25000000000,
from: ...,
gas: ...,
});

MORE FROM Technology

Announcements

Klaytn Governance Council Votes to Pause KIR Grants

25/11/2022

Ecosystem

Klaytn Square Beta is now available

23/11/2022

Announcements

Announcing the winners of Klaymakers22, Klaytn’s Flagship Web3 Hackathon

21/11/2022

CATEGORIES

© Klaytn Foundation 2022. All rights reserved.
© Klaytn Foundation 2022. All rights reserved.