Docs

Docs

  • Develop
  • Validate
  • Integrate
  • Learn

›erdjs

Welcome to Elrond

  • Welcome to Elrond

Technology

  • Architecture Overview
  • Glossary
  • Entities
  • Chronology
  • Secure Proof of Stake
  • Adaptive State Sharding
  • The Elrond WASM VM
  • Cross Shard Transactions

Wallet

  • Wallets - Overview
  • Web Wallet
  • Maiar Web Wallet Extension
  • Webhooks
  • Ledger

Tokens

  • Native Tokens
  • ESDT tokens
  • NFT tokens

Validators

  • Validators - Overview
  • System Requirements
  • Install a Mainnet Node

    • Scripts & User config
    • Installing a Validator Node
    • Optional Configurations
    • How to use the Docker Image

    Install a Testnet/Devnet Node

    • Scripts & User config
    • Installing a Validator Node
    • Manage a validator node
    • How to use the Docker Image

    Manage your keys

    • Validator Keys
    • Wallet Keys
    • Protecting your keys

    Staking, Unstaking, Unjailing

    • Staking, unstaking and unjailing
    • Staking
    • Unjailing
    • The Staking Smart Contract
  • The Delegation Manager
  • Convert An Existing Validator Into A Staking Pool
  • Merging A Validator Into An Existing Delegation Smart Contract
  • Rating
  • Elrond Node upgrades
  • Node redundancy
  • Import DB
  • Node CLI
  • Node Databases
  • Useful Links & Tools
  • FAQs

Developers

  • Developers - Overview
  • Tutorials

    • Build a dApp in 15 minutes
    • Build a Microservice for your dApp
    • The Crowdfunding Smart Contract (part 1)
    • The Crowdfunding Smart Contract (part 2)
    • The Counter Smart Contract
    • Custom Wallet Connect

    Signing Transactions

    • Signing Transactions
    • Tools for signing
    • Signing programmatically

    Gas and Fees

    • Overview
    • EGLD transfers (move balance transactions)
    • System Smart Contracts
    • User-defined Smart Contracts

    Developer reference

    • The Elrond Serialization Format
    • Smart contract annotations
    • Smart contract modules
    • Smart contract to smart contract calls
    • Smart Contract Developer Best Practices
    • Code Metadata
    • Smart Contract API Functions
    • Storage Mappers
    • Rust Testing Framework
    • Rust Testing Framework Functions Reference
    • Rust Smart Contract Debugging
    • Random Numbers in Smart Contracts

    Developers Best Practices

    • Basics
    • BigUint Operations
    • The dynamic allocation problem
    • Multi-values

    Mandos tests reference

    • Mandos Overview
    • Mandos Structure
    • Mandos Simple Values
    • Mandos Complex Values
    • Embedding Mandos code in Go
  • Constants
  • Built-In Functions
  • Account storage
  • Setup a Local Testnet
  • Set up a Local Testnet (advanced)
  • Creating Wallets

SDK and Tools

  • SDKs and Tools - Overview
  • REST API

    • REST API overview
    • api.elrond.com
    • Gateway overview
    • Addresses
    • Transactions
    • Network
    • Nodes
    • Blocks
    • Virtual Machine
    • Versions and Changelog
  • Proxy
  • Elasticsearch
  • erdpy

    • erdpy
    • Installing erdpy
    • Configuring erdpy
    • erdpy CLI
    • Deriving the Wallet PEM file
    • Sending bulk transactions
    • Writing and running erdpy scripts
    • Smart contract interactions

    erdjs

    • erdjs
    • Cookbook
    • Extending erdjs
    • Writing and testing interactions
    • Migration guides
    • Signing Providers for dApps
  • erdgo
  • erdcpp
  • erdjava
  • erdkotlin
  • erdwalletjs-cli

Integrators

  • Integrators - Overview
  • EGLD integration guide
  • ESDT tokens integration guide
  • Observing Squad
  • Accounts Management
  • Creating Transactions
  • Querying the Blockchain

Cookbook

This page will guide you through the process of handling common tasks using erdjs.

important

This cookbook makes use of erdjs 10. In order to migrate from erdjs 9.x to erdjs 10, please follow the migration guide.

Creating network providers

Creating an API provider:

import { ApiNetworkProvider } from "@elrondnetwork/erdjs-network-providers";

let networkProvider = new ApiNetworkProvider("https://devnet-api.elrond.com");

Creating a Proxy provider:

import { ProxyNetworkProvider } from "@elrondnetwork/erdjs-network-providers";

let networkProvider = new ProxyNetworkProvider("https://devnet-gateway.elrond.com");
important

Use the classes from @elrondnetwork/erdjs-network-providers only as a starting point. As your dApp matures, make sure you switch to using your own network provider, tailored to your requirements (whether deriving from the default ones or writing a new one, from scratch) that directly interacts with the Elrond API (or Gateway).

On this topic, please see extending erdjs.

Fetching network parameters

let networkConfig = await networkProvider.getNetworkConfig();
console.log(networkConfig.MinGasPrice);
console.log(networkConfig.ChainID);

Working with accounts

Synchronizing an account object

The following snippet fetches (from the Network) the nonce and the balance of an account, and updates the local representation of the account.

let addressOfAlice = new Address("erd1...");
let alice = new Account(addressOfAlice);
let aliceOnNetwork = await networkProvider.getAccount(addressOfAlice);
alice.update(aliceOnNetwork);

console.log(alice.nonce);
console.log(alice.balance);

Managing the sender nonce locally

When sending a bunch of transactions, you usually have to first fetch the account nonce from the network (see above), then manage it locally (e.g. increment upon signing & broadcasting a transaction):

alice.incrementNonce();

Alternatively, you can also use:

transaction.setNonce(alice.getNonceThenIncrement());

For further reference, please see nonce management.

Preparing payment objects

important

In erdjs 9x, the payments were prepared using the classes Balance and BalanceBuilder. In erdjs 10, we use TokenPayment.

A TokenPayment object for EGLD transfers (value movements):

let firstPayment = TokenPayment.egldFromAmount("1.5");
let secondPayment = TokenPayment.egldFromBigInteger("1500000000000000000");
console.log(firstPayment.valueOf(), secondPayment.valueOf());
console.log(firstPayment.toPrettyString(), secondPayment.toPrettyString());

A TokenPayment object for transferring fungible tokens:

let identifier = "FOO-123456";
let numDecimals = 2;
let firstPayment = TokenPayment.fungibleFromAmount(identifier, "1.5", numDecimals);
let secondPayment = TokenPayment.fungibleFromBigInteger(identifier, "4000", numDecimals);

console.log(firstPayment.toString()); // Will output: 150.
console.log(firstPayment.toPrettyString()); // Will output: 1.50 FOO-123456.
console.log(secondPayment.toString()); // Will output: 4000.
console.log(secondPayment.toPrettyString()); // Will output: 40.00 FOO-123456.

A TokenPayment object for transferring semi-fungible tokens:

let nonce = 3;
let quantity = 50;
let payment = TokenPayment.semiFungible(identifier, nonce, quantity);

A TokenPayment object for transferring non-fungible tokens (the quantity doesn't need to be specified for NFTs, as the token is only one of its kind):

let nonce = 7;
let payment = TokenPayment.nonFungible(identifier, nonce);

A TokenPayment object for transferring meta-esdt tokens:

let payment = TokenPayment.metaEsdtFromAmount(identifier, nonce, "0.1", numDecimals);

Broadcasting transactions

Preparing a simple transaction

let tx = new Transaction({
    data: new TransactionPayload("helloWorld"),
    gasLimit: 70000,
    receiver: new Address("erd1spyavw0956vq68xj8y4tenjpq2wd5a9p2c6j8gsz7ztyrnpxrruqzu66jx"),
    value: TokenPayment.egldFromAmount(1),
    chainID: "D"
});

Broadcast using a network provider

let txHash = await networkProvider.sendTransaction(tx);

Note that the transaction needs to be signed before broadcasting it. Signing can be achieved using a signing provider.

important

Note that, for all purposes, we recommend using dapp-core instead of integrating the signing providers on your own.

Broadcast using axios

let data = tx.toSendable();
let url = "https://devnet-api.elrond.com/transactions";
let response = await axios.post(url, data, {
    headers: {
        "Content-Type": "application/json",
    },
});
let txHash = response.data.txHash;

Wait for transaction completion

let watcher = new TransactionWatcher(networkProvider);
let transactionOnNetwork = await watcher.awaitCompleted(tx);

If only the txHash is available, then:

let transactionOnNetwork = await watcher.awaitCompleted({ getHash: () => txHash });
console.log(transactionOnNetwork);

In order to wait for multiple transactions:

await Promise.all([watcher.awaitCompleted(tx1), watcher.awaitCompleted(tx2), watcher.awaitCompleted(tx3)]);

For a different awaiting strategy, also see extending erdjs.

Token transfers

Single ESDT transfer

let payment = TokenPayment.fungibleFromAmount("COUNTER-8b028f", "100.00", 0);
let data = new ESDTTransferPayloadBuilder()
    .setPayment(payment)
    .build();

transactions.push(new Transaction({
    nonce: 7,
    receiver: new Address("erd1..."),
    data: data,
    gasLimit: 50000 + 1500 * data.length() + 300000,
    chainID: "D"
}));

Single NFT transfer

let payment = TokenPayment.nonFungible("ERDJS-38f249", 1);
let payload = new ESDTNFTTransferPayloadBuilder()
    .setPayment(payment)
    .setDestination(new Address("erd1..."))
    .build();

transactions.push(new Transaction({
    nonce: 7,
    // Same as sender address!
    receiver: new Address("erd1..."),
    data: data,
    gasLimit: 50000 + 1500 * data.length() + 1000000,
    chainID: "D"
}));

Single SFT transfer

let payment = TokenPayment.semiFungible("SEMI-9efd0f", 1, 5);
let payload = new ESDTNFTTransferPayloadBuilder()
    .setPayment(payment)
    .setDestination(new Address("erd1..."))
    .build();

transactions.push(new Transaction({
    nonce: 7,
    // Same as sender address!
    receiver: new Address("erd1..."),
    data: data,
    gasLimit: 50000 + 1500 * data.length() + 1000000,
    chainID: "D"
}));

Multi ESDT / NFT transfer

let paymentOne = TokenPayment.nonFungible("ERDJS-38f249", 1);
let paymentTwo = TokenPayment.fungibleFromAmount("BAR-c80d29", "10.00", 18);
let payments = [paymentOne, paymentTwo];
let payload = new MultiESDTNFTTransferPayloadBuilder()
    .setPayments(payments)
    .setDestination(new Address("erd1..."))
    .build();

transactions.push(new Transaction({
    nonce: 7,
    // Same as sender address!
    receiver: new Address("erd1..."),
    data: data,
    gasLimit: 50000 + 1500 * data.length() + 1000000 * payments.length,
    chainID: "D"
}));

Contract deployments

Load the bytecode from a file

import { Code } from "@elrondnetwork/erdjs";
import { promises } from "fs";

let buffer: Buffer = await promises.readFile(file);
let code = Code.fromBuffer(buffer);

Load the bytecode from an URL

import axios, { AxiosResponse } from "axios";

let response: AxiosResponse<ArrayBuffer> = await axios.get("https://.../myContract.wasm", {
    responseType: "arraybuffer",
    transformResponse: [],
    headers: {
        "Accept": "application/wasm"
    }
});

let buffer = Buffer.from(response.data);
let code = Code.fromBuffer(buffer);

Perform a contract deployment

Create a SmartContract object:

let contract = new SmartContract();

Prepare the deploy transaction:

let transaction = contract.deploy({
    code: code,
    codeMetadata: new CodeMetadata(/* set the parameters accordingly */),
    initArguments: [/* set the initial arguments, if any */],
    gasLimit: 20000000,
    chainID: "D"
});

Then, set the transaction nonce.

Note that the account nonce must be synchronized beforehand. Also, locally increment the nonce of the deployer (optional).

transaction.setNonce(deployer.getNonceThenIncrement());

Then sign the transaction using a wallet / signing provider of your choice. Upon signing, you would usually compute the contract address (deterministically computable), as follows:

let contractAddress = SmartContract.computeAddress(transaction.getSender(), transaction.getNonce());

In order to broadcast the transaction and await its completion, use a network provider and a transaction watcher:

await networkProvider.sendTransaction(transaction);
let transactionOnNetwork = await new TransactionWatcher(networkProvider).awaitCompleted(transaction);

In the end, parse the results:

let { returnCode } = new ResultsParser().parseUntypedOutcome(transactionOnNetwork);

ABI

Load the ABI from a file

import { AbiRegistry } from "@elrondnetwork/erdjs";
import { promises } from "fs";

let jsonContent: string = await promises.readFile("myAbi.json", { encoding: "utf8" });
let json = JSON.parse(jsonContent);
let abiRegistry = AbiRegistry.create(json);
let abi = new SmartContractAbi(abiRegistry, ["MyContract"]);
...
let contract = new SmartContract({ address: new Address("erd1..."), abi: abi });

Load the ABI from an URL

import axios, { AxiosResponse } from "axios";

let response: AxiosResponse = await axios.get("https://.../myAbi.json");
let abiRegistry = AbiRegistry.create(response.data);
let abi = new SmartContractAbi(abiRegistry, ["MyContract"]);
...
let contract = new SmartContract({ address: new Address("erd1..."), abi: abi });

Contract queries

When the ABI is not available

let contractAddress = new Address("erd1qqq...");
let contract = new SmartContract({ address: contractAddress });
let addressOfAlice = new Address("erd1...");

let query = contract.createQuery({
    func: new ContractFunction("getClaimableRewards"),
    args: [new AddressValue(addressOfAlice)],
    caller: new Address("erd1...")
});

let queryResponse = await networkProvider.queryContract(query);
let bundle = resultsParser.parseUntypedQueryResponse(queryResponse);
console.log(bundle.returnCode);
console.log(bundle.returnMessage);
console.log(bundle.values);

Using Interaction, when the ABI is not available

let func = new ContractFunction("getClaimableRewards");
let args = [new AddressValue(addressOfAlice)];
let query = new Interaction(contract, func, args)
    .withQuerent(new Address("erd1..."))
    .buildQuery();

let queryResponse = await networkProvider.queryContract(query);

Then, parse the response as above.

When the ABI is available

let query = contract.createQuery({
    func: new ContractFunction("getClaimableRewards"),
    args: [new AddressValue(addressOfAlice)],
    caller: new Address("erd1...")
});

let queryResponse = await networkProvider.queryContract(query);
let endpointDefinition = contract.getEndpoint("getClaimableRewards");
let { firstValue, secondValue, returnCode } = resultsParser.parseQueryResponse(queryResponse, endpointDefinition);

Using Interaction, when the ABI is available

Prepare the interaction, check it, then build the query:

let interaction = <Interaction>contract.methods.getLotteryInfo(["myLottery]);
let query = interaction.check().buildQuery();

Then, run the query and parse the results:

let queryResponse = await networkProvider.queryContract(query);
let endpointDefinition = interaction.getEndpoint();
let { firstValue, secondValue, returnCode } = resultsParser.parseQueryResponse(queryResponse, endpointDefinition);

Depending on the context, reinterpret (cast) the results:

let firstValueAsStruct = <Struct>firstValue;
return firstValueAsStruct;

Contract interactions

When the ABI is not available

let contractAddress = new Address("erd1qqq...");
let contract = new SmartContract({ address: contractAddress });
let addressOfCarol = new Address("erd1...");

let tx = contract.call({
    func: new ContractFunction("transferToken"),
    gasLimit: 5000000,
    args: [new AddressValue(addressOfCarol), new U64Value(1000)],
    chainID: "D"
});

tx.setNonce(alice.nonce);

Then, sign, broadcast tx and wait for its completion.

Using Interaction, when the ABI is not available

let contract = new SmartContract({ address: contractAddress });
let dummyFunction = new ContractFunction("dummy");
let args = [new U32Value(100)];
let interaction = new Interaction(contract, dummyFunction, args);

let tx = interaction
    .withNonce(7)
    .withValue(TokenPayment.egldFromAmount(1))
    .withGasLimit(20000000)
    .withChainID("D")
    .buildTransaction();

Then, sign, broadcast tx and wait for its completion.

Using Interaction, when the ABI is available

let contract = new SmartContract({ address: contractAddress, abi: abi });
let tx = contract.methods.dummy([new U32Value(100)])
    .withNonce(7)
    .withValue(TokenPayment.egldFromAmount(1))
    .withGasLimit(20000000)
    .withChainID("D")
    .buildTransaction();

Transfer & execute

Given an interaction:

let interaction = contract.methods.doStuff([]);

One can apply token transfers to the smart contract call, as well.

For single payments, do as follows:

// Fungible token
interaction.withSingleESDTTransfer(TokenPayment.fungibleFromAmount("FOO-6ce17b", "1.5", 18));

// Non-fungible token
interaction.withSingleESDTNFTTransfer(TokenPayment.nonFungible("ERDJS-38f249", 1));

For multiple payments:

interaction.withMultiESDTNFTTransfer([
    TokenPayment.fungibleFromAmount("FOO-6ce17b", "1.5", 18)
    TokenPayment.nonFungible("ERDJS-38f249", 1)
]);

Parsing contract results

important

When the default ResultsParser misbehaves, please open an issue on GitHub, and also provide as much details as possible about the unparsable results (e.g. provide a dump of the transaction object if possible - make sure to remove any sensitive information).

When the ABI is not available

let parser = new ResultsParser();
let transactionOnNetwork = await networkProvider.getTransaction(txHash);
let { returnCode, returnMessage, values } = resultsParser.parseUntypedOutcome(transactionOnNetwork, endpointDefinition);

When the ABI is available

let parser = new ResultsParser();
let transactionOnNetwork = await networkProvider.getTransaction(txHash);
let { returnCode } = resultsParser.parseOutcome(transactionOnNetwork, endpointDefinition);

The endpointDefinition can be obtained from the Interaction object, if available in the context:

let endpointDefinition = interaction.getEndpoint();

Alternatively, the endpointDefinition can be obtained from the SmartContract object:

let endpointDefinition = smartContract.getEndpoint("myFunction");

For customizing the default parser, also see extending erdjs.

Decoding transaction metadata

Using the transaction-decoder

In order to decode the metadata (function, arguments, transfers) from a transaction payload, do as follows:

import { TransactionDecoder, TransactionMetadata } from "@elrondnetwork/transaction-decoder";

let transactionOnNetwork = await networkProvider.getTransaction(txHash);

let metadata = new TransactionDecoder().getTransactionMetadata({
    sender: transactionOnNetwork.sender.bech32(),
    receiver: transactionOnNetwork.receiver.bech32(),
    data: transactionOnNetwork.data.toString("base64"),
    value: transactionOnNetwork.value.toString(),
    type: transactionOnNetwork.type
});

Using the esdtHelpers and scArgumentsParser of erdjs 9x

The classes esdtHelpers and scArgumentsParser have been removed in erdjs 10, in favor of the @elrondnetwork/transaction-decoder (see above).

However, you can still find the previous implementations at the following location:

  • esdtHelpers
  • esdtHelpers examples
  • scArgumentsParser
  • scArgumentsParser examples
← erdjsExtending erdjs →
  • Creating network providers
  • Fetching network parameters
  • Working with accounts
    • Synchronizing an account object
    • Managing the sender nonce locally
  • Preparing payment objects
  • Broadcasting transactions
    • Preparing a simple transaction
    • Broadcast using a network provider
    • Broadcast using axios
    • Wait for transaction completion
  • Token transfers
    • Single ESDT transfer
    • Single NFT transfer
    • Single SFT transfer
    • Multi ESDT / NFT transfer
  • Contract deployments
    • Load the bytecode from a file
    • Load the bytecode from an URL
    • Perform a contract deployment
  • ABI
    • Load the ABI from a file
    • Load the ABI from an URL
  • Contract queries
    • When the ABI is not available
    • Using Interaction, when the ABI is not available
    • When the ABI is available
    • Using Interaction, when the ABI is available
  • Contract interactions
    • When the ABI is not available
    • Using Interaction, when the ABI is not available
    • Using Interaction, when the ABI is available
    • Transfer & execute
  • Parsing contract results
    • When the ABI is not available
    • When the ABI is available
  • Decoding transaction metadata
    • Using the transaction-decoder
    • Using the esdtHelpers and scArgumentsParser of erdjs 9x
Made withby the Elrond team.
GithubChat
Main siteWalletExplorerBridgeDocsGrowthMaiarMaiar Exchange