Last updated
Was this helpful?
Last updated
Was this helpful?
Let's try sending a transaction as a simple warm-up. In this short example, we will be creating a keystore, connecting to Klaytn node, as well as creating a transaction - all of this using caver-js!
Don't worry if this is your first time using caver-js. Just follow the simple steps below.
First install the following packages.
version ()
Note: If you get an nvm: command not found
error after installing nvm, refer to this .
The most simple way to create an account is using the .
Download the keystore file, and let's change the name to something more simple, like keystore.json
.
First, let's create a folder for our project. We will simply call it test
. Navigate to your command line and type:
Now let's navigate to our folder.
We are in our folder, where we will download caver-js. But before that, we have to check our node.js
version, because we have to use 12 or 14.
You can check the version like this:
Now let's initialize our project:
Since we are just doing a simple test, it doesn't matter how you answer the questions. Keep pressing enter
.
Alternatively, you can simply type the command below to skip hitting enter
:
And now we are ready to install caver-js.
Also, add the below module because we need it:
Let's create a test file named testcaver.js
like so:
We will be writing our code in this file to send a transaction to transfer KLAY.
Since we are sending a transaction to the blockchain network, we need to connect to a Klaytn node. We will be using Klaytn's testnet Baobab.
We will import the caver-js
and read
module and connect to a Klaytn node in the Baobab network as shown below:
You need an account to make transactions on the blockchain. That account information is included in the keystore. Using the loadPassword()
function, we can implement a password prompt on the terminal. The function looks like this:
The password entered from the prompt, along with the keystore file existing in the same directory, will be decrypted and stored as keyring
.
After that, the keyring
will be stored in the wallet. Add the lines below:
We will now create a transaction to transfer some KLAY. This type of transaction is called "value transfer transaction". Let's break down each parameter.
The from
address is derived from the keystore we uploaded. The to
address is the receiver of the KLAY, and you can use any address. For value
, you can conveniently use caver.utils.toPeb()
to convert KLAY into peb. Here, we will send 10 KLAY. For gas
,
Don't forget to add in the end:
Let's run the code that we've just written:
The result will look something like this:
You need KLAY to send a transaction. You can get test KLAY for the Baobab testnet from . Refer to for detailed instructions.
If the version is not 12 or 14, make sure to change it. Here, we will use the version (). So let's type nvm use 14.16.0
to change our node version.
You can view the transaction details in or using the transactionHash
.
I hope you are feeling confident about having submitted a transacion using caver-js. If you are stuck, or have any questions, feel free to visit our for help.