Getting started

To get started with the @evergonlabs/tmi-protocol-api-client library, there are a few simple installation steps to follow. You'll also learn how the library works from a conceptual perspective, helping you understand its core functionality.

Installation

Install the the library by running the following command in your terminal:

npm install --save-dev @evergonlabs/tmi-protocol-api-client viem
yarn add @evergonlabs/tmi-protocol-api-client viem
pnpm add @evergonlabs/tmi-protocol-api-client viem
bun add @evergonlabs/tmi-protocol-api-client viem

As you can see, the viem library is also installed in order to create a client instance that interacts with the blockchain.

📘

Note

Developers are free to use any other alternative to the viem such as ethers.js, web3.js and other.

viem

The viem library is mostly used to create a client instance that provides the functionality to send transactions. Below you can find the code example where a new client instance is created:

import { sepolia } from "viem/chains";
import { createWalletClient, http} from "viem";
import { privateKeyToAccount } from "viem/accounts"; 

const myClient = createWalletClient({
  account: privateKeyToAccount("0xffffffffffffffffffffffffffffffffffffffff"),
  chain: sepolia,
  transport: http("https://your-provider.com/api-key"),
});

Library Specifics

The @evergonlabs/tmi-protocol-api-client library represents a high-level abstraction based on the smart contracts provided by Nexera ; It significantly simplifies the development process by providing the predefined use case templates that guide developers step-by-step in deploying and running a new staking or fraction platform with a predefined configuration.

The library provides an ApiClientthat allows generating transactions that are then passed to the viem client. You are required to create an instance of the ApiClient when using the library:

import { api} from "@evergonlabs/tmi-protocol-api-client";

const apiClient = api.createClient({
    env: getEnv("test"),
  });

Most functions provided by the library return only a data object, which you need to pass to a client like viem (or any other) to broadcast a transaction to the blockchain. For example, below is the invocation of the createPlatform function from the rwa template:

import { sepolia } from "viem/chains";
import { api, handleApiResponse} from "@evergonlabs/tmi-protocol-api-client";

const transactionData = handleApiResponse(
  await api.stakingTemplates.rwa.createPlatform({
    client: apiClient,
    body: {
      // pay attention: we are using string literals here
      chainId: `${sepolia.id}`,
      isSoulbound: true,
      adminAddress: myClient.account.address,
      erc721: {
        symbol: "TT",
        name: "Test",
        baseUri: "ipfs://QmYwAPJzv5CZsnAzt8QvV6pMMN4HgfMNXYxa9avYo5bFwK",
      },
    },
}));

The handleApiResponse function is responsible for processing the return values of library functions like createPlatform, which typically return either data or an error.

When a data object is created and assigned to the transactionData variable, you can define an arbitrary function that uses the viem client's writeContract function to send transactions. In this context, the this function is called sendTransactionwhich expects the transaction argument of the Transaction type with the corresponding structure defined below:

import {Transaction} from "@evergonlabs/tmi-protocol-api-client";

async function sendTransaction(viemClient:any, transaction: Transaction): Promise<Hash> {
  return await viemClient.writeContract({
    args: transaction.details.args,
    functionName: transaction.details.functionName,
    abi: transaction.details.abi,
    address: transaction.details.address as Address,
  });
}

The library provides functions like getMarketDeployedEvent that extract data from events, offering additional details about the operation's result, which can be used for other purposes later on. These functions require a valid hash which can be obtained by calling an arbitraty function sendTransaction:

const platformDetails = handleApiResponse(
      await api.stakingTemplates.rwa.getPlatformDeployEvent({
        query: {
          chainId: `${sepolia.id}`,
          hash: await sendTransaction(myClient, transactionData),
        },
      }),
    );

Finally, the execution of the sendTransaction function with the transactionData deploys the staking platform with the predefined configuration, and resulting hash is used in the getPlatformDeployEvent invocation to get the platform details such as diamondAddress:

const platformReceipt = await deployPlatform(apiClient)
const diamondAddress = platformReceipt.diamondAddress

The code examples and descriptions above highlight the key principles for effectively using the library to deploy staking and fraction platforms.