GenLayerJS

GenLayerJS SDK

GenLayerJS SDK is a TypeScript library designed for developers building decentralized applications (DApps) on the GenLayer protocol. This SDK provides a comprehensive set of tools to interact with the GenLayer network, including client creation, transaction handling, event subscriptions, and more, all while leveraging the power of Viem as the underlying blockchain client.

Features

  • Client Creation: Easily create and configure a client to connect to GenLayer’s network.
  • Transaction Handling: Send and manage transactions on the GenLayer network.
  • Contract Interaction: Read from and write to smart contracts deployed on GenLayer.
  • Event Subscriptions: Subscribe to events and listen for blockchain updates.
  • TypeScript Support: Benefit from static typing and improved code reliability.

How it's Built

The GenLayerJS SDK is built using TypeScript and leverages the Viem library as the underlying blockchain client. It is designed to provide a high-level, easy-to-use API for interacting with the GenLayer network, abstracting away the complexities of direct blockchain interactions.

Technologies Used

  • TypeScript: A statically typed superset of JavaScript that compiles to plain JavaScript, enhancing code reliability and maintainability.
  • Viem: A modular and extensible blockchain client for JavaScript and TypeScript.
  • ESBuild: A fast JavaScript bundler and minifier used for building the SDK efficiently.
  • Vitest: A Vite-native unit test framework used for testing.

Project Structure

The source code for the GenLayerJS SDK is organized as follows:

  • src/: Contains the main TypeScript source files.
  • tests/: Includes all the test files written using Vitest.
  • dist/: The compiled JavaScript files ready for use.

Requirements

Before using the GenLayerJS SDK, ensure your system meets the following requirements:

  • Node.js: Version 16.x or higher is required.
  • npm: Comes bundled with Node.js, used for managing packages.
  • Operating System: Compatible with macOS, Linux, and Windows.

Installation

To install the GenLayerJS SDK in , use the following command:

npm install genlayer-js

Usage

Here’s how to initialize the client and connect to the GenLayer Simulator:

Reading a Transaction

import { simulator } from 'genlayer-js/chains';
import { createClient } from 'genlayer-js';
 
const client = createClient({
  network: simulator,
});
 
const transaction = await client.getTransaction({ hash: transactionHash });

Reading a Contract

import { simulator } from 'genlayer-js/chains';
import { createClient, createAccount } from 'genlayer-js';
 
const account = createAccount();
const client = createClient({
  network: simulator,
  account: account, // Use this account for subsequent calls
});
 
const result = await client.readContract({
  address: contractAddress,
  functionName: 'get_complete_storage',
  args: [],
});

Writing a Transaction

import { simulator } from 'genlayer-js/chains';
import { createClient, createAccount } from 'genlayer-js';
 
const account = createAccount();
const client = createClient({
  network: simulator,
  account: account,
});
 
const transactionHash = await client.writeContract({
  address: contractAddress,
  functionName: 'update_storage',
  args: ['new_data'],
  value: 0, // Optional: amount of native token to send with the transaction
});
 
const receipt = await client.waitForTransactionReceipt({
  hash: transactionHash,
  status: 'FINALIZED', // or 'ACCEPTED'
});

General Format of Commands

The GenLayerJS SDK provides functions that follow a consistent syntax pattern:

client.functionName(parameters);
  • client: The instance of the GenLayer client.
  • functionName: The primary action you want to perform (e.g., getTransaction, readContract).
  • parameters: An object containing the required parameters for the function.

Further Development

Additional features are planned to enhance interaction with the GenLayer network, including wallet integration and gas estimation. Stay tuned for updates.

Repository

You can find the GenLayerJS SDK repository on GitHub:

GenLayerJS SDK Repository (opens in a new tab)

Full Reference

The full reference for the GenLayerJS SDK is available in the GenLayerJS SDK Reference.