@anmolbansal/cactus-plugin-ledger-connector-polkadot
TypeScript icon, indicating that this package has built-in type declarations

100.0.3 • Public • Published

@hyperledger/cactus-plugin-ledger-connector-polkadot

Table of Contents

1. Usage

This plugin provides a way to interact with Substrate networks. Using this one can perform:

  • Deploy smart contracts (ink! contract).
  • Execute transactions on the ledger.
  • Invoke ink! contract functions.

The above functionality can either be accessed by importing the plugin directly as a library (embedding) or by hosting it as a REST API through the Cactus API server

We also publish the Cactus API server as a container image to the GitHub Container Registry that you can run easily with a one liner. The API server is also embeddable in your own NodeJS project if you choose to do so.

1.1. Installation

npm

npm install @hyperledger/cactus-plugin-ledger-connector-polkadot

yarn

yarn add @hyperledger/cactus-plugin-ledger-connector-polkadot

1.2. Using as a Library

import {
  PluginLedgerConnectorPolkadot,
} from "@hyperledger/cactus-plugin-ledger-connector-polkadot";

const plugin = new PluginLedgerConnectorPolkadot({
  // See test cases for exact details on what parameters are needed
});

const req: RunTransactionRequest = {
  // See tests for specific examples on request properties
};

try {
  const res = await plugin.transact(req);
} catch (ex: Error) {
  // Make sure to handle errors gracefully (which is dependent on your use-case)
  console.error(ex);
  throw ex;
}

1.3. Using Via The API Client

Prerequisites

  • A running Substrate ledger (network)
  • You have a running Cactus API server on $HOST:$PORT with the Polkadot connector plugin installed on it (and the latter configured to have access to the Substrate ledger from point 1)
import {
  PluginLedgerConnectorPolkadot,
  DefaultApi as PolkadotApi,
} from "@hyperledger/cactus-plugin-ledger-connector-polkadot";

// Step zero is to deploy your Substrate ledger and the Cactus API server

const apiHost = `http://${address}:${port}`;

const apiConfig = new Configuration({ basePath: apiHost });

const apiClient = new PolkadotApi(apiConfig);

const req: RunTransactionRequest = {
  // See tests for specific examples on request properties
};

try {
  const res = await apiClient.runTransaction(req);
} catch (ex: Error) {
  // Make sure to handle errors gracefully (which is dependent on your use-case)
  console.error(ex);
  throw ex;
}

2. Architecture

The sequence diagrams for various endpoints are mentioned below

2.1. run-transaction-endpoint

run-transaction-endpoint sequence diagram
The above diagram shows the sequence diagram of run-transaction-endpoint. User A (One of the many Users) interacts with the API Client which in turn, calls the API server. API server then executes transact() method which is explained in detailed in the subsequent diagrams.
run-transaction-endpoint transact() method
The above diagram shows the sequence diagram of transact() method of the PluginLedgerConnectorPolkadot class. The caller to this function, which in reference to the above sequence diagram is API server, sends RunTransactionRequest object as an argument to the transact() method. Based on the type of Web3SigningCredentialType, corresponding responses are sent back to the caller.
run-transaction-endpoint transactCactusKeychainRef() method
The above diagram shows transactCactusKeychainReference() method being called by the transact() method of the PluginLedgerConnector class when the Web3SigningCredentialType is CACTUSKEYCHAINREF. This method inturn calls transactMnemonicString() which calls the signAndSend() method of the Polkadot library.
runtransaction-endpoint transactMnemonicString() method
The above diagram shows transactMnemonicString() method being called by the transact() method of the PluginLedgerConnector class when the Web3SigningCredentialType is MNEMONICSTRING. This method then calls the signAndSend() method of the Polkadot library.
run-transaction-endpoint transactSigned() method
The above diagram shows transactSigned() method being called by the transact() method of the PluginLedgerConnector class when the Web3SigningCredentialType is NONE. This method calls the api.rpc.author.submitAndWatchExtrinsic() of the Polkadot library.

3. Containerization

3.1. Building/running the container image locally

In the Cactus project root say:

DOCKER_BUILDKIT=1 docker build -f ./packages/cactus-plugin-ledger-connector-polkadot/Dockerfile . -t cplcb

Build with a specific version of the npm package:

DOCKER_BUILDKIT=1 docker build --build-arg NPM_PKG_VERSION=2.0.0-alpha.4 -f ./packages/cactus-plugin-ledger-connector-polkadot/Dockerfile . -t cplcb

3.2. Running the container

Launch container with plugin configuration as an environment variable:

docker run \
  --rm \
  --publish 3000:3000 \
  --publish 4000:4000 \
  --env AUTHORIZATION_PROTOCOL='NONE' \
  --env AUTHORIZATION_CONFIG_JSON='{}' \
  --env GRPC_TLS_ENABLED=false \
  --env PLUGINS='[{"packageName": "@hyperledger/cactus-plugin-ledger-connector-polkadot", "type": "org.hyperledger.cactus.plugin_import_type.LOCAL", "action": "org.hyperledger.cactus.plugin_import_action.INSTALL",  "options": {"wsProviderUrl":"ws://127.0.0.1:9944", "instanceId": "some-unique-polkadot-connector-instance-id"}}]' \
  cplcb

Launch container with plugin configuration as a CLI argument:

docker run \
  --rm \
  --publish 3000:3000 \
   --publish 4000:4000 \
  cplcb \
    ./node_modules/.bin/cactusapi \
    --plugins='[{"packageName": "@hyperledger/cactus-plugin-ledger-connector-polkadot", "type": "org.hyperledger.cactus.plugin_import_type.LOCAL", "action": "org.hyperledger.cactus.plugin_import_action.INSTALL",  "options": {"wsProviderUrl":"ws://127.0.0.1:9944", "instanceId": "some-unique-polkadot-connector-instance-id"}}]'

Launch container with configuration file mounted from host machine:

echo '[{"packageName": "@hyperledger/cactus-plugin-ledger-connector-polkadot", "type": "org.hyperledger.cactus.plugin_import_type.LOCAL", "action": "org.hyperledger.cactus.plugin_import_action.INSTALL",  "options": {"wsProviderUrl":"ws://127.0.0.1:9944", "instanceId": "some-unique-polkadot-connector-instance-id"}}]' > cactus.json

docker run \
  --rm \
  --publish 3000:3000 \
  --publish 4000:4000 \
  --mount type=bind,source="$(pwd)"/cactus.json,target=/cactus.json \
  cplcb \
    ./node_modules/.bin/cactusapi \
    --config-file=/cactus.json

4. Prometheus Exporter

This class creates a Prometheus exporter, which scraps the transactions (total transaction count) for the use cases incorporating the use of Fabric connector plugin.

4.1. Usage Prometheus

The Prometheus exporter object is initialized in the PluginLedgerConnectorPolkadot class constructor itself, so instantiating the object of the PluginLedgerConnectorPolkadot class, gives access to the exporter object. You can also initialize the Prometheus exporter object separately and then pass it to the IPluginLedgerConnectorPolkadotOptions interface for PluginLedgerConnectorPolkadot constructor.

getPrometheusExporterMetricsEndpoint function returns the Prometheus exporter metrics, currently displaying the total transaction count, which currently increments every time the transact() method of the PluginLedgerConnectoPolkadot class is called.

4.2. Prometheus Integration

To use Prometheus with this exporter make sure to install Prometheus main component. Once Prometheus is setup, the corresponding scrape_config needs to be added to the prometheus.yml

- job_name: 'polkadot_ledger_connector_exporter'
  metrics_path: api/v1/plugins/@hyperledger/cactus-plugin-ledger-connector-polkadot/get-prometheus-exporter-metrics
  scrape_interval: 5s
  static_configs:
    - targets: ['{host}:{port}']

Here the host:port is where the Prometheus exporter metrics are exposed. The test cases (For example, packages/cactus-plugin-ledger-connector-polkadot/src/test/typescript/integration/run-transaction.test.ts) exposes it over 0.0.0.0 and a random port(). The random port can be found in the running logs of the test case and looks like (42379 in the below mentioned URL) Metrics URL: http://0.0.0.0:42379/api/v1/plugins/@hyperledger/cactus-plugin-ledger-connector-polkadot/get-prometheus-exporter-metrics

Once edited, you can start the Prometheus service by referencing the above edited prometheus.yml file. On the Prometheus graphical interface (defaulted to http://localhost:9090), choose Graph from the menu bar, then select the Console tab. From the Insert metric at cursor drop down, select cactus_Polkadot_total_tx_count and click execute

4.3. Helper code

4.3.1. response.type.ts

This file contains the various responses of the metrics.

4.3.2. data-fetcher.ts

This file contains functions encasing the logic to process the data points

4.3.3. metrics.ts

This file lists all the Prometheus metrics and what they are used for.

5. Contributing

We welcome contributions to Hyperledger Cactus in many forms, and there’s always plenty to do!

Please review CONTIRBUTING.md to get started.

6. License

This distribution is published under the Apache License Version 2.0 found in the LICENSE file.

7. Acknowledgments

Package Sidebar

Install

npm i @anmolbansal/cactus-plugin-ledger-connector-polkadot

Weekly Downloads

1

Version

100.0.3

License

Apache-2.0

Unpacked Size

326 kB

Total Files

58

Last publish

Collaborators

  • anmolbansal