GenLayerJS SDK Reference
This document describes the key components and methods available in the GenLayerJS SDK for interacting with the GenLayer network.
Client Creation
createClient
Creates a new GenLayer client instance.
import { createClient } from 'genlayer-js';
const client = createClient({
chain: simulator,
account: account, // Optional: Use this account for subsequent calls
});
Parameters:
chain
: The chain configuration (e.g., simulator)account
: (Optional) Sets an account to be used in subsequent calls
Returns: A GenLayer client instance
Transaction Handling
getTransaction
Retrieves transaction details by hash.
const transaction = await client.getTransaction({ hash: transactionHash });
Parameters:
hash
: The transaction hash
Returns: Transaction details object
waitForTransactionReceipt
Waits for a transaction receipt.
const receipt = await client.waitForTransactionReceipt({
hash: transactionHash,
status: 'FINALIZED', // or 'ACCEPTED'
});
Parameters:
hash
: The transaction hashstatus
: The desired transaction status ('FINALIZED' or 'ACCEPTED')
Returns: Transaction receipt object
Contract Interaction
readContract
Reads data from a deployed contract.
const result = await client.readContract({
address: contractAddress,
functionName: 'get_complete_storage',
args: [],
});
Parameters:
address
: The contract addressfunctionName
: The name of the function to callargs
: An array of arguments for the function call
Returns: The result of the contract function call
writeContract
Writes data to a deployed contract.
const transactionHash = await client.writeContract({
address: contractAddress,
functionName: 'storeData',
args: ['new_data'],
value: 0, // Optional: amount of native token to send with the transaction
});
Parameters:
address
: The contract addressfunctionName
: The name of the function to callargs
: An array of arguments for the function callvalue
: (Optional) Amount of native token to send with the transaction
Returns: The transaction hash
Account Management
generatePrivateKey
Generates a new private key.
import { generatePrivateKey } from 'genlayer-js';
const privateKey = generatePrivateKey();
Parameters: None
Returns: A new private key as string
createAccount
Creates a new account, optionally using a provided private key.
import { createAccount } from 'genlayer-js';
const account = createAccount();
// Or with a specific private key:
const accountWithKey = createAccount('0x1234...'); // Replace with actual private key
Parameters:
accountPrivateKey
: (Optional) A string representing the private key
Returns: A new account object
Chain Information
simulator
Provides configuration for the GenLayer Studio chain (the Studio used to be called "Simulator").
import { simulator } from 'genlayer-js/chains';
Usage: Used when creating a client to specify the chain