Guides
Dash CoreDash PlatformDash.orgDash User DocsLog In
Guides
These docs are for v0.18.0. Click to read the latest docs for v0.25-redirect.

Submit Documents

Submit data to Dash Platform

Overview

In this tutorial we will submit some data to an application on Dash Platform. Data is stored in the form of documents which are encapsulated in a state transition before being submitted to DAPI.

Prerequisites

Code

πŸ“˜

Wallet Operations

Currently, the JavaScript SDK does not cache wallet information, and therefore, it re-syncs the entire Core chain for some wallet operations (e.g. client.getWalletAccount()). This can result in wait times of 5+ minutes.

An upcoming release will add a persistence feature to cache wallet information during initial sync so that subsequent access is much faster. For now, the skipSynchronizationBeforeHeight option can be used to only sync the wallet starting at a certain block height.

πŸ‘

Initializing the Client with a contract identity

The example below shows how access to contract documents via <contract name>.<contract document> syntax (e.g. tutorialContract.note) can be enabled by passing a contract identity to the constructor. Please refer to the Dash SDK documentation for details.

const Dash = require('dash');

const clientOpts = {
  network: 'testnet',
  wallet: {
    mnemonic: 'a Dash wallet mnemonic with funds goes here',
    unsafeOptions: {
      skipSynchronizationBeforeHeight: 415000, // only sync from start of 2021
    },
  },
  apps: {
    tutorialContract: {
      contractId: 'C96rCVpck4RdBQXG3zzP5KH4RKzfKVTsmTauu8FQenJi',
    },
  },
};
const client = new Dash.Client(clientOpts);

const submitNoteDocument = async () => {
  const { platform } = client;
  const identity = await platform.identities.get('an identity ID goes here');

  const docProperties = {
    message: `Tutorial Test @ ${new Date().toUTCString()}`,
  };

  // Create the note document
  const noteDocument = await platform.documents.create(
    'tutorialContract.note',
    identity,
    docProperties,
  );

  const documentBatch = {
    create: [noteDocument], // Document(s) to create
    replace: [],            // Document(s) to update
    delete: [],             // Document(s) to delete
  };
  // Sign and submit the document(s)
  return platform.documents.broadcast(documentBatch, identity);
};

submitNoteDocument()
  .then((d) => console.log(d))
  .catch((e) => console.error('Something went wrong:\n', e))
  .finally(() => client.disconnect());

What's happening

After we initialize the Client, we create a document that matches the structure defined by the data contract of the application being referenced (e.g. a note document for the contract registered in the data contract tutorial). The platform.documents.create method takes three arguments: a document locator, an identity, and the document data. The document locator consists of an application name (e.g. tutorialContract) and the document type being created (e.g. note). The document data should contain values for each of the properties defined for it in the data contract (e.g. message for the tutorial contract's note).

Once the document has been created, we still need to submit it to DAPI. Documents are submitted in batches that may contain multiple documents to be created, replaced, or deleted. In this example, a single document is being created. The documentBatch object defines the action to be completed for the document (the empty action arrays - replace and delete in this example - may be excluded and are shown for reference only here).

The platform.documents.broadcast method then takes the document batch and an identity parameter. Internally, it creates a State Transition containing the previously created document, signs the state transition, and submits the signed state transition to DAPI.