Installation and usage of the platform

JS SDK installation and initialization

If you are going to use the JS SDK in a Node.js environment, install the Node.js package from the official website.

Install the js-sdk package using npm:

npm install @wavesenterprise/js-sdk --save

In the selected development environment, import the package containing the JS SDK library:

import WeSdk from '@wavesenterprise/js-sdk'

In addition to importing a package, you can use the require function:

const WeSdk = require('@wavesenterprise/js-sdk');

Then initialize the library:

const config = {
    ...WeSdk.MAINNET_CONFIG,
    nodeAddress: 'https://hoover.welocal.dev/node-0',
    crypto: 'waves',
    networkByte: 'V'.charCodeAt(0)
}

const Waves = WeSdk.create({
    initialConfiguration: config,
    fetchInstance: window.fetch // Browser feature. For Node.js use node-fetch
});

When working in a browser, use the window.fetch function as fetchInstance. If you work in Node.js, use the module node-fetch.

Once the JavaScript SDK is initialized, you can start creating and sending transactions.

Below is a complete listing with the creation of a typical transaction:

import WeSdk from '@wavesenterprise/js-sdk'

const config = {
    ...WeSdk.MAINNET_CONFIG,
    nodeAddress: 'https://hoover.welocal.dev/node-0',
    crypto: 'waves',
    networkByte: 'V'.charCodeAt(0)
}

const Waves = WeSdk.create({
    initialConfiguration: config,
    fetchInstance: window.fetch
});

// Create a seed phrase from an existing one
const seed = Waves.Seed.fromExistingPhrase('examples seed phrase');

const txBody = {
    recipient: seed.address, // Send tokens to the same address
    assetId: '',
    amount: '10000',
    fee: '1000000',
    attachment: 'Examples transfer attachment',
    timestamp: Date.now()
};

const tx = Waves.API.Transactions.Transfer.V3(txBody);

await tx.broadcast(seed.keyPair)

A description of the transaction parameters, as well as examples, is available in the “Creating and sending transactions” section.

See also