Javascript SDK V2

Functions that are not signing a transaction.

Introduction

You can get our SDK from here: Overledger SDK

Prerequisite

Before you start, be sure to complete the following prerequisites:

  • Since our SDK encapsulates the Overledger API, authentication requires a Quant Connect account, utilizing credentials from a developer App.
  • Ensure that the following libraries are installed:
    • Node.js v14 with a minimum version of 14.18.0, or v16 with a minimum version of 16.13.0
    • yarn
    • lerna

Technologies

The Overledger SDK is a collection of node packages written in Typescript. Currently, the supported DLTs are Bitcoin, Substrate, Ethereum and the XRP Ledger.

Installing

The Overledger SDK can be installed as node modules.
If all supported Distributed Ledger Technologies (DLTs) are necessary, the bundle package can be installed via the following command, which will include all the required dependencies.

npm install @quantnetwork/overledger-bundle

Alternatively, the suite of node packages allows developers to choose which DLTs they would like to utilise by installing the core Overledger package and the individual DLT packages. For example, if you only want to use the Ethereum blockchain, you will need only the Overledger-Core and Overledger-Ethereum packages, which you can install by running the command:

npm install @quantnetwork/overledger-core  
npm install @quantnetwork/overledger-dlt-ethereum

Getting started

Setting configuration values in .env file

This SDK is designed to be configurable via a .env file, which allows for easy management of environment-specific settings. This file should be located in the root directory of your project. Below are the steps to set configuration values in the .env file:

Step 1: Create a .env file

If you don't have a .env file already, create one in the root directory of your project.

Step 2: Define and set configuration values

In the .env file, define the following configuration variables and replace USER_NAME, PASSWORD, CLIENT_ID, CLIENT_SECRET and other values with your actual values.

USER_NAME=your-overledger-devportal-email-address-here
PASSWORD=your-overledger-devportal-password-here
CLIENT_ID=your-overledger-devportal-application-client-id-here
CLIENT_SECRET=your-overledger-devportal-application-client-secret-here

PARTY_A_BITCOIN_SECRET=your-bitcoin-private-key-here
PARTY_A_ETHEREUM_SECRET=your-ethereum-private-key-here
PARTY_A_XRP_LEDGER_SECRET=your-xrp-ledger-private-key-here
PARTY_A_SUBSTRATE_SECRET=your-substrate-private-key-here

PARTY_A_BITCOIN_ADDRESS=your-bitcoin-public-address-here
PARTY_A_ETHEREUM_ADDRESS=your-ethereum-public-address-here
PARTY_A_XRP_LEDGER_ADDRESS=your-xrp-ledger-public-address-here
PARTY_A_SUBSTRATE_ADDRESS=your-substrate-public-address-here

Then to encrypt your .env file, use the following command:

secure-env .env -s mySecretPassword

This will create a .env.enc file in your project's root directory. For security reasons, you can delete the original .env file after encryption to prevent unauthorised access.

Getting your JWT

Once the SDK is installed and you have the correct values in your .env file, to access any Overledger API, you must obtain an authorisation token, which Overledger utilises for authentication purposes. Below is a code example demonstrating retrieving your authorisation tokens using our SDK.

const OverledgerSDK = require('@quantnetwork/overledger-bundle').default;
const DltNameOptions = require('@quantnetwork/overledger-types').DltNameOptions;
(async () => {
  try {
    const overledger = new OverledgerSDK({
      dlts: [{ dlt: DltNameOptions.BITCOIN },
             { dlt: DltNameOptions.ETHEREUM },
             { dlt: DltNameOptions.XRP_LEDGER }],
      userPoolID: 'us-east-1_xfjNg5Nv9', //your userpool id
      provider: { network: 'https://api.sandbox.overledger.io/' },
      envFilePassword: 'password',
    });
    const refreshTokensResponse = await overledger.getTokensUsingClientIdAndSecret(
      process.env.USER_NAME,
      process.env.PASSWORD,
      process.env.CLIENT_ID,
      process.env.CLIENT_SECRET,
    );
    console.log('accessToken:\n', refreshTokensResponse.accessToken);
    console.log('expiresIn:\n', refreshTokensResponse.expiresIn);
    console.log('tokenType:\n', refreshTokensResponse.tokenType);
  } catch (e) {
    console.error('error', e);
  }
})();

The refreshTokensResponse.accessToken contains the OAuth 2.0 access token, which you can utilise in your subsequent Overledger API calls.

Sending a request

Now that you have an authentication token, you can use it to access Overledger's functionalities. Here's an example of how you might use the SDK to search for an account balance:

const OverledgerSDK = require('@quantnetwork/overledger-bundle').default;
const DltNameOptions = require('@quantnetwork/overledger-types').DltNameOptions;


// Create a new instance of the SDK
const sdk = new OverledgerSDK({
  dlts: [{ dlt: 'ethereum' }],
	userPoolID: 'us-east-1_xfjNg5Nv9', //your userpool id
  provider: { network: 'https://api.sandbox.overledger.io/v2' },
  envFilePassword: 'password',
});

//get required token
const refreshTokensResponse = await overledger.getTokensUsingClientIdAndSecret(
  process.env.USER_NAME,
  process.env.PASSWORD,
  process.env.CLIENT_ID,
  process.env.CLIENT_SECRET,
);

//setup overledger request
const overledgerRequest = {
  "location": {
    "technology": "Ethereum",
    "network": "Ethereum Goerli Testnet"
  }
};

// Send your request
const overledgerInstance = overledger.provider.createRequest(refreshTokensResponse.accessToken.toString());
const overledgerResponse = await overledgerInstance.post("/autoexecution/search/address/balance/0x650A87cfB9165C9F4Ccc7B971D971f50f753e761",overledgerRequest);

console.log(overledgerResponse);

You can locate this code in our SDK's examples folder here.

For more information on how to use our SDK, please refer to the other examples in our SDK's github repository here as well as the examples detailed in our future learn course here. Both folders contains various examples that demonstrate how to interact with different DLTs and perform common tasks, such as sending transactions, querying balances, and subscribing to events.