Create Transactions
To construct & process transactions you will need our API JavaScript library: near-api-js
. There are many ways to create transactions but for this example we'll show you two ways to create a simple token transfer transaction.
- HIGH LEVEL - easiest way to create a transaction
- LOW LEVEL - performs the exact same transaction as above, but deconstructs the entire process for those curious about each step
At the core, all transactions require the following:
signerId
(account ID of the transaction originator)signerPublicKey
receiverId
(account ID of the transaction recipient)nonceForPublicKey
(each time a key is used the nonce value should be incremented by 1)actions
( [click here] for supported arguments)blockHash
(a current block hash (within 24hrs) to prove the transaction was recently created)
See Transaction Class for a more in depth outline.
HIGH LEVEL -- Create a transaction
Setup
- Clone the transaction-examples repository by running:
git clone https://github.com/near-examples/transaction-examples.git
- Follow setup instructions
Imports
In send-tokens-easy.js
we use two dependencies:
- NEAR API JavaScript library
dotenv
(used to load environment variables for private key)
const nearAPI = require("near-api-js");
const { connect, KeyPair, keyStores, utils } = nearAPI;
require("dotenv").config();
The second line above deconstructs several utilities from nearAPI that you will use to interact with the blockchain.
connect
- create a connection to NEAR passing configuration variablesKeyPair
- creates a keyPair from the private key you'll provide in an.env
filekeyStores
- stores the keyPair that you will create from the private key and used to sign Transactionsutils
- used to format NEAR amounts