Package Exports
- @elrondnetwork/erdjs
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).
Under development, stay tuned!
Features:
- Transaction construction, signing, broadcasting and querying.
- Smart Contracts deployment and interaction (execution and querying).
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:
- transaction.devnet.spec.ts
- address.spec.ts
- transactionPayloadBuilders.spec.ts
- smartContract.spec.ts
- smartContract.devnet.spec.ts
- query.spec.ts
- query.mainnet.spec.ts
Additional examples (please note that they are slighly out-of-date though) can be found here:
Synchronizing network parameters
let provider = new ProxyProvider("https://localhost:7950");
await NetworkConfig.getDefault().sync(provider);
console.log(NetworkConfig.getDefault().MinGasPrice);
console.log(NetworkConfig.getDefault().ChainID);Synchronizing an account object
let addressOfAlice = new Address("erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th");
let alice = new Account(addressOfAlice);
await alice.sync(provider);
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 tx.send(provider);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: [Argument.pubkey(addressOfCarol), Argument.number(1000)]
});
tx.setNonce(alice.nonce);
await signer.sign(tx);
await tx.send(provider);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: [Argument.pubkey(addressOfAlice)]
});
console.log(response.isSuccess());
console.log(response.returnData);Waiting for transactions to be processed
await tx1.send(provider);
await tx2.send(provider);
await tx3.send(provider);
await tx1.awaitExecuted(provider);
await tx2.awaitPending(provider);
let watcher = new TransactionWatcher(tx3.hash, provider);
await watcher.awaitStatus(status => status.isExecuted());Managing the sender nonce locally
await alice.sync(provider);
txA.setNonce(alice.nonce);
alice.incrementNonce();
txB.setNonce(alice.nonce);
alice.incrementNonce();
await signer.sign(txA);
await signer.sign(txB);
await txA.send(provider);
await txB.send(provider);
await txA.awaitExecuted(provider);
await txB.awaitExecuted(provider);Installation
erdjs is delivered via npm, therefore it can be installed as follows:
npm install @elrondnetwork/erdjsDevelopment
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 browserifyBuilding the library
In order to compile erdjs, run the following:
npm install
npm run compile
npm run compile-browser
npm run compile-browser-minRunning the tests
On NodeJS
In order to run the tests on NodeJS, do as follows:
npm run tests-unit
npm run tests-devnet
npm run tests-testnet
npm run tests-mainnetIn the browser
Make sure you have the package http-server installed globally.
npm install --global http-serverIn order to run the tests in the browser, do as follows:
make clean && npm run browser-testsNotes
For the devnet tests, make sure you have a devnet running locally. A local devnet can be started from the Elrond IDE or from erdpy.