JSPM

  • Created
  • Published
  • Downloads 833
  • Score
    100M100P100Q7446F
  • License GPL-3.0-or-later

Smart Contracts interaction framework

Package Exports

  • @elrondnetwork/erdjs
  • @elrondnetwork/erdjs/out
  • @elrondnetwork/erdjs/out/index.js
  • @elrondnetwork/erdjs/out/signature
  • @elrondnetwork/erdjs/out/signature.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@elrondnetwork/erdjs) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

Elrond SDK for JavaScript

Elrond SDK for JavaScript and TypeScript (written in TypeScript).

Documentation

TypeDoc

CHANGELOG

CHANGELOG

Distribution

npm

Usage

The most comprehensive usage examples are captured within the unit and the integration tests. Specifically, in the *.spec.ts files of the source code. For example:

For advanced smart contract interaction, using ABIs, please see the following test files:

Fetching network parameters

let provider = new ProxyProvider("https://localhost:7950");
let network = await provider.getNetworkConfig();
console.log(network.MinGasPrice);
console.log(network.ChainID);

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("erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th");
let alice = new Account(addressOfAlice);
let aliceOnNetwork = await provider.getAccount(addressOfAlice);
alice.update(aliceOnNetwork);

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

Creating value-transfer transactions

await alice.sync(provider);

let tx = new Transaction({
    data: new TransactionPayload("helloWorld"),
    gasLimit: new GasLimit(70000),
    receiver: new Address("erd1spyavw0956vq68xj8y4tenjpq2wd5a9p2c6j8gsz7ztyrnpxrruqzu66jx"),
    value: Balance.egld(1)
});

tx.setNonce(alice.nonce);
await signer.sign(tx);
await provider.sendTransaction(tx);

Creating Smart Contract transactions

let contract = new SmartContract({ address: new Address("erd1qqqqqqqqqqqqqpgq3ytm9m8dpeud35v3us20vsafp77smqghd8ss4jtm0q") });
let addressOfCarol = new Address("erd1k2s324ww2g0yj38qn2ch2jwctdy8mnfxep94q9arncc6xecg3xaq6mjse8");

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

tx.setNonce(alice.nonce);
await signer.sign(tx);
await provider.sendTransaction(tx);

Querying Smart Contracts

let contract = new SmartContract({ address: new Address("erd1qqqqqqqqqqqqqpgqxwakt2g7u9atsnr03gqcgmhcv38pt7mkd94q6shuwt") });
let addressOfAlice = new Address("erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th");

let response = await contract.runQuery(provider, {
    func: new ContractFunction("getClaimableRewards"),
    args: [new AddressValue(addressOfAlice)]
});

console.log(response.isSuccess());
console.log(response.returnData);

Waiting for transactions to be processed

await provider.sendTransaction(tx1);
await provider.sendTransaction(tx2);
await provider.sendTransaction(tx3);

let watcher = new TransactionWatcher(provider);
await Promise.all([watcher.awaitCompleted(tx1), watcher.awaitCompleted(tx2), watcher.awaitCompleted(tx3)]);

Managing the sender nonce locally

let aliceOnNetwork = await provider.getAccount(alice.address);
alice.update(aliceOnNetwork);

txA.setNonce(alice.nonce);
alice.incrementNonce();
txB.setNonce(alice.nonce);
alice.incrementNonce();

await signer.sign(txA);
await signer.sign(txB);

await provider.sendTransaction(txA);
await provider.sendTransaction(txB);

let watcher = new TransactionWatcher(provider);

await watcher.awaitCompleted(txA);
await watcher.awaitCompleted(txB);

Installation

erdjs is delivered via npm, therefore it can be installed as follows:

npm install @elrondnetwork/erdjs

Development

Feel free to skip this section if you are not a contributor.

Prerequisites

browserify is required to compile the browser-friendly versions of erdjs. It can be installed as follows:

npm install --global browserify

Building the library

In order to compile erdjs, run the following:

npm install
npm run compile
npm run compile-browser
npm run compile-browser-min

Running the tests

On NodeJS

In order to run the tests on NodeJS, do as follows:

npm run tests-unit
npm run tests-localnet
npm run tests-devnet
npm run tests-testnet
npm run tests-mainnet

In the browser

Make sure you have the package http-server installed globally.

npm install --global http-server

In order to run the tests in the browser, do as follows:

make clean && npm run browser-tests

Notes

For the localnet tests, make sure you have a local testnet up & running. A local testnet can be started from the Elrond IDE or from erdpy.