Tutorial

This tutorial demonstrates the complete process of deploying a market using the MarketSDK. It includes initializing the SDK, preparing for market deployment, configuring the market, generating the necessary actions, and executing those actions.

Installation

To install the SDK, you can use npm or yarn:

npm i @evergonlabs/sdk

Initialization

Initializes the MarketSDK with the necessary configuration, including the environment (EnvironmentEnum.PROD), credentials (public key), and signer. This sets up the SDK to interact with the specified blockchain environment and API.

import { createWalletClient } from "viem";
import { hardhat } from "viem/chains";
import {
  CapitalisationKind,
  CredentialsTypeEnum,
  EnvironmentEnum,
  MarketSDK,
  MarketTraitEnum,
  TimeBoundariesKinds,
  Wallet,
  IMarketFractionTraits,
  IWallet,
  TokenTypeEnum,
  AssetUseCaseEnum,
} from "@evergonlabs/tokenizer-sdk";

const [aliceClient] = createWalletClient({
  chain: mainnet,
  transport: custom(window.ethereum!),
});

// You can pass your own implementation of IWallet
const signer: IWallet = new Wallet(aliceClient!);

const sdk = new MarketSDK({
  env: EnvironmentEnum.DEV,
  credentials: {
    type: CredentialsTypeEnum.CLIENT_SIDE,
    publicKey: "[ADD_YOUR_API_KEY]",
  },
  signer: signer,
});
  1. Optional, depending on the use case, call the prepare method to retrieve the available blockchain networks (display user input).
  2. Defines the market deployment configuration, specifying the types of assets to be wrapped, fraction assets, funding assets, admin address, capitalisation kind, time boundaries, and metadata.
  3. Call the getActions.
  4. Call the Run function with the market configuration to generate the set of actions required for deploying the market (send transaction and register metadata).

Define Market Deployment Configuration

Defines the market deployment configuration, specifying the types of assets to be wrapped, fraction assets, funding assets, admin address, time boundaries, and metadata.

const traits = {
  [MarketTraitEnum.CAPITALISATION]: CapitalisationKind.SOFTCAP_HARDCAP,
  [MarketTraitEnum.TIME]: TimeBoundariesKinds.START_AND_END_DATE,
  [MarketTraitEnum.WRAPPED_ASSET_KIND]: [TokenTypeEnum.ERC20],
  [MarketTraitEnum.FRACTION_ASSET_KINDS]: [TokenTypeEnum.ERC20],
} satisfies IMarketFractionTraits;

Generate Actions for Market Deployment

Calls the getActions method with the market configuration to generate the set of actions required for deploying the market. These actions include the necessary steps and transactions to set up the market on the blockchain.

// 3. Generate and run actions
const deployActions = sdk.deployMarket.getActions(traits);

// Choose payment assets from available
const availablePaymentAssets = await sdk.assets.getAssets({
  chainIds: [chain.id],
  usage: [AssetUseCaseEnum.PAYMENT],
});

// Choose sector from available
const availableSectors = await sdk.sectors.getSectors();

deployActions.on("nextAction", (action) => {
  // You may use this to display the progress in the UI
  // do something with action here, each action has it's own key and metadata
});
const market = await deployActions.run({
  chainId: chain.id,
  adminAddress: aliceClient!.account.address,
  sectorId: availableSectors[0]!.id,
  paymentAssetIds: availablePaymentAssets.map((x) => x.id),
  metadata: {
    title: "test",
    description: "test",
    logo: "test",
    email: "test",
  },
});

Full Code Example

import { createWalletClient } from "viem";
import { hardhat } from "viem/chains";
import {
  CapitalisationKind,
  CredentialsTypeEnum,
  EnvironmentEnum,
  MarketSDK,
  MarketTraitEnum,
  TimeBoundariesKinds,
  Wallet,
  IMarketFractionTraits,
  IWallet,
  TokenTypeEnum,
  AssetUseCaseEnum,
} from "@evergonlabs/tokenizer-sdk";

const [aliceClient] = createWalletClient({
  chain: mainnet,
  transport: custom(window.ethereum!),
});

// You can pass your own implementation of IWallet
const signer: IWallet = new Wallet(aliceClient!);

const sdk = new MarketSDK({
  env: EnvironmentEnum.DEV,
  credentials: {
    type: CredentialsTypeEnum.CLIENT_SIDE,
    publicKey: "[ADD_YOUR_API_KEY]",
  },
  signer: signer,
});

const traits = {
  [MarketTraitEnum.CAPITALISATION]: CapitalisationKind.SOFTCAP_HARDCAP,
  [MarketTraitEnum.TIME]: TimeBoundariesKinds.START_AND_END_DATE,
  [MarketTraitEnum.WRAPPED_ASSET_KIND]: [TokenTypeEnum.ERC20],
  [MarketTraitEnum.FRACTION_ASSET_KINDS]: [TokenTypeEnum.ERC20],
} satisfies IMarketFractionTraits;

const deployActions = sdk.deployMarket.getActions(traits);

// Choose payment assets from available
const availablePaymentAssets = await sdk.assets.getAssets({
  chainIds: [chain.id],
  usage: [AssetUseCaseEnum.PAYMENT],
});

// Choose sector from available
const availableSectors = await sdk.sectors.getSectors();

deployActions.on("nextAction", (action) => {
  // You may use this to display the progress in the UI
  // do something with action here, each action has it's own key and metadata
});

const market = await deployActions.run({
  chainId: chain.id,
  adminAddress: aliceClient!.account.address,
  sectorId: availableSectors[0]!.id,
  paymentAssetIds: availablePaymentAssets.map((x) => x.id),
  metadata: {
    title: "test",
    description: "test",
    logo: "test",
    email: "test",
  },
});