Using our SDK

Our SDK is a TypeScript nodejs package that allows you to provide offers to the order book. It simplifies interactions with our API which manages one order book, but you could change the code to interact with other order books

Prerequisites

Install our typescript package in your application by running npm i @kairos-loan/kairos-js

Methods reference

Once the SDK is installed, you can use all the methods documented below


provideOffers({ offers, chainId, wallet }: ProvideOffersParams)Promise<void>

provideOffers() is the main method of the SDK. It is used to provide offers to the order book

It takes 3 parameters:

  • offers: Offer[], an array of offers to post
  • wallet: ethers.Wallet, a wallet to sign the transaction with
  • chainId: SupportedChainIds, an enum of supported chain ids

Custom types used in the SDK

  • NFT is a custom type composed of:
FieldTypeDescription
implem0x${string}the implementation of the NFT
idethers.BigNumberthe id of the NFT
  • Offer is a custom type composed of:
FieldTypeDescription
loanToValueethers.BigNumberthe loan-to-value ratio of the offer
durationethers.BigNumberthe duration of the offer
availabilitySpanethers.BigNumberthe availability span of the offer
collateralNFTthe collateral of the offer

Example

Script example to provide fake offer

import moment from 'moment';
import { WalletClient, createWalletClient, http, parseUnits } from 'viem';
import { PrivateKeyAccount, privateKeyToAccount } from 'viem/accounts';
import { foundry } from 'viem/chains';

import * as kairosSDK from '@kairos-loan/kairos-js';
import { connectToGraphQLSdk, getGraphQLSdk } from '@kairos-loan/kairos-js';
import { AddressString, HexaString } from '@kairos-loan/shared';

/**
 * Main script function
 */
export async function provideFakeOffersScript(
  nftAddresses: Array<`0x${string}`>,
  inBetaTime: boolean,
  providerPrivateKey: string,
  env: kairosSDK.Environement,
): Promise<void> {
  const hexPrivateKey: kairosSDK.HexaType = (
    providerPrivateKey.startsWith('0x') ? providerPrivateKey : `0x${providerPrivateKey}`
  ) as kairosSDK.HexaType;

  const localAccount: PrivateKeyAccount = privateKeyToAccount(hexPrivateKey);
  const wallet: WalletClient = createWalletClient({
    account: localAccount,
    chain: foundry,
    transport: http(process.env.LOCAL_RPC_URL),
  });
  const offers: kairosSDK.CreateOfferByCollection[] = [];
  const wethAddress: AddressString = process.env.WETH_ADDR as AddressString;
  const usdcAddress: AddressString = process.env.USDC_ADDR as AddressString;

  // Connect to graphQL API
  await connectToGraphQLSdk(kairosSDK.Environement.LOCALHOST, wallet);

  // Genereate collection token list
  for (const nftAddress of nftAddresses) {
    await kairosSDK.forceUpdateCollectionTokenList(env, kairosSDK.ChainName.LOCALHOST, nftAddress, false);
  }

  // Generate offers
  for (const nftAddress of nftAddresses) {
    const trancheId: bigint = nftAddress === nftAddresses[0] ? BigInt(1) : BigInt(0);

    offers.push(
      getRandomOffer(nftAddress, nftAddresses.indexOf(nftAddress), inBetaTime, trancheId, wethAddress, 18),
      getRandomOffer(nftAddress, nftAddresses.indexOf(nftAddress), inBetaTime, trancheId, usdcAddress, 6),
    );
  }

  await kairosSDK.provideOffers(offers, wallet, env);
}

/**
 * Get base price for each collection
 */
function getRandomOffer(
  nftAddress: `0x${string}`,
  priceRange: number,
  inBetaTime: boolean,
  trancheId: bigint,
  assetToLendAddress: AddressString,
  assetDecimals: number,
): kairosSDK.CreateOfferByCollection {
  const bigFactor: number = 1_000_000;
  const randFactor1: number = Math.floor(Math.random() * bigFactor);
  const randFactor2: number = Math.floor(Math.random() * bigFactor);
  const availabilitySpanInMs: number = 60 * 60 * 24 * 7 * 2 * 1000; // 2 weeks in ms

  // arbitrary price ranges for collections
  let basePrice: bigint;

  switch (priceRange) {
    case 0: {
      basePrice = parseUnits('1', assetDecimals);
      break;
    }
    case 1: {
      basePrice = parseUnits('8', assetDecimals);
      break;
    }
    default: {
      basePrice = parseUnits('3', assetDecimals);
    }
  }

  const durationInSeconds: number = Math.floor(
    inBetaTime
      ? (60 * 90 * randFactor2) / bigFactor // In minutes
      : (60 * 60 * 24 * 90 * randFactor2) / bigFactor,
  );

  return {
    type: kairosSDK.TokenListType.COLLECTION,
    chainName: kairosSDK.ChainName.LOCALHOST,
    loanToValue: (
      (basePrice + parseUnits('1', assetDecimals) * BigInt(randFactor1)) /
      BigInt(bigFactor)
    ).toString() as HexaString,
    expirationDate: moment(Date.now() + availabilitySpanInMs).toISOString(),
    duration: durationInSeconds,
    collectionAddress: nftAddress,
    trancheId: trancheId.toString() as HexaString,
    assetToLendAddress,
  };
}