Elrond SDK for JavaScript and TypeScript (written in TypeScript).
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.dev.net.spec.ts
- address.spec.ts
- transactionPayloadBuilders.spec.ts
- smartContract.spec.ts
- smartContract.dev.net.spec.ts
- query.spec.ts
- query.main.net.spec.ts
For advanced smart contract interaction, using ABIs, please see the following test files:
More examples and documentation on writing ABI-aware custom interactors for smart contracts is coming soon!
Additional examples (please note that they are slighly out-of-date though) can be found here:
let provider = new ProxyProvider("https://localhost:7950");
await NetworkConfig.getDefault().sync(provider);
console.log(NetworkConfig.getDefault().MinGasPrice);
console.log(NetworkConfig.getDefault().ChainID);
let addressOfAlice = new Address("erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th");
let alice = new Account(addressOfAlice);
await alice.sync(provider);
console.log(alice.nonce);
console.log(alice.balance);
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);
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 tx.send(provider);
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);
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());
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);
erdjs
is delivered via npm, therefore it can be installed as follows:
npm install @elrondnetwork/erdjs
Feel free to skip this section if you are not a contributor.
browserify
is required to compile the browser-friendly versions of erdjs
. It can be installed as follows:
npm install --global browserify
In order to compile erdjs
, run the following:
npm install
npm run compile
npm run compile-browser
npm run compile-browser-min
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-mainnet
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
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.