Installation and usage of the platform

Creating and sending transactions with the use of the JS SDK

Principles of transaction creation

Any transaction is called using the function Waves.API.Transactions.<TRANSACTION_Name>.<TRANSACTION_VERSION>.

For example, a transaction call for a version 3 token transfer transaction can be done as follows:

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

txBody – transaction body, which contains the necessary parameters. For example, for the above Transfer transaction it may look like this:

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

{
  "sender": "3M6dRZXaJY9oMA3fJKhMALyYKt13D1aimZX",
  "password": "",
  "recipient": "3M6dRZXaJY9oMA3fJKhMALyYKt13D1aimZX",
  "amount": 40000000000,
  "fee": 100000
}

You can leave the transaction body blank and fill in the necessary parameters later by accessing the variable where the result of the transaction call function is returned (in the example, the tx variable):

const tx = Waves.API.Transactions.Transfer.V3({});
tx.recipient = '12afdsdga243134';
tx.amount = 10000;
//...
tx.sender = "3M6dRZXaJY9oMA3fJKhMALyYKt13D1aimZX";
//...
tx.amount = 40000000000;
tx.fee = 10000;

This way of calling a transaction allows more flexibility in making numerical operations in the code and using separate functions to define certain parameters.

3, 13, 14 and 112 transactions use the description text field, and 4 and 6 transactions use the attachment text field. Messages sent in these transaction fields need to be converted into base58 format before being sent. There are two functions in the JS SDK for that:

  • ``base58.encode’’ – translates the text string into base58 format;

  • base58.decode – reverse decode the base58 format string into text.

An example of a transaction body using base58.encode:

const txBody = {
  recipient: seed.address,
  assetId: '',
  amount: 10000,
  fee: minimumFee[4],
  attachment: Waves.tools.base58.encode('Examples transfer attachment'),
  timestamp: Date.now()
}

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

Attention

When calling transactions with the use of JS SDK, you need to fill all necessary parameters of transaction body except type, version, id, proofs and senderPublicKey. These parameters are filled in automatically when the key pair is generated.

For a description of the parameters included in the body of each transaction, see Transaction Description.

Broadcasting a transaction

The broadcast method is used to broadcast a transaction to the network via the JS SDK:

await tx.broadcast(seed.keyPair);

This method is called after creating a transaction and filling its parameters. The result of its execution can be assigned to a variable to display the result of sending the transaction to the network (in the example, the result variable):

try {
    const result = await tx.broadcast(seed.keyPair);
    console.log('Broadcast PolicyCreate result: ', result)
} catch (err) {
    console.log('Broadcast error:', err)
}

Below is the full listing of the token transfer transaction call and its broadcasting:

const { create: createApiInstance, MAINNET_CONFIG } = require('..');
const nodeFetch = require('node-fetch');

const nodeAddress = 'https://hoover.welocal.dev/node-0';
const seedPhrase = 'examples seed phrase';

const fetch = (url, options = {}) => {
  const headers = options.headers || {}
  return nodeFetch(url, { ...options, headers: {...headers, 'x-api-key': 'vostok'} });
}

(async () => {
  const { chainId, minimumFee, gostCrypto } = await (await fetch(`${nodeAddress}/node/config`)).json();

  const wavesApiConfig = {
    ...MAINNET_CONFIG,
    nodeAddress,
    crypto: gostCrypto ? 'gost' : 'waves',
    networkByte: chainId.charCodeAt(0),
  };

  const Waves = createApiInstance({
    initialConfiguration: wavesApiConfig,
    fetchInstance: fetch
  });

  const seed = Waves.Seed.fromExistingPhrase(seedPhrase);

  const txBody = {
    recipient: seed.address,
    assetId: '',
    amount: 10000,
    fee: minimumFee[4],
    attachment: Waves.tools.base58.encode('Examples transfer attachment'),
    timestamp: Date.now()
  }

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

  try {
    const result = await tx.broadcast(seed.keyPair);
    console.log('Broadcast transfer result: ', result)
  } catch (err) {
    console.log('Broadcast error:', err)
  }

})();

For examples of calling and sending other transactions, see “Examples of JavaScript SDK usage” Additional methods available when creating and sending a transaction

In addition to the broadcast method, the following methods are available for debugging and defining transaction parameters:

  • isValid – transaction body check, returns 0 or 1;

  • getErrors – returns a string array containing the description of errors made when filling the fields;

  • getSignature – returns a string with the key with which the transaction was signed;

  • getId – returns a string with the ID of the transaction to be sent;

  • getBytes – an internal method that returns an array of bytes to sign.

See also