Nodejs

Getting started

This package acts as a typescript wrapper on top of the Ston.fi Omniston protocol API. It uses RxJs to provide observables on top of the WebSocket API connection

Installation

via NPM

npm install @ston-fi/omniston-sdk

via YARN

yarn add @ston-fi/omniston-sdk

via PNPM

pnpm install @ston-fi/omniston-sdk

Create an instance

Create an Omniston instance, specifying the API URL.

import { Omniston } from "@ston-fi/omniston-sdk";

const omniston = new Omniston({
  apiUrl: "wss://omni-ws.ston.fi",
});

The constructor takes the following parameters:

Get list of supported assets

const assetList = await omniston.assetList();

An AssetsResponse has the following properties:

An AssetInfo has the following properties:

An Address has the following properties:

Send a quote request

Send a request for quote to swap an asset to another asset.

omniston
  .requestForQuote({
    settlementMethods: [SettlementMethod.SETTLEMENT_METHOD_SWAP],
    askAssetAddress: {
      blockchain: Blockchain.TON,
      address: "EQA2kCVNwVsil2EM2mB0SkXytxCqQjS4mttjDpnXmwG9T6bO", // STON
    },
    offerAssetAddress: {
      blockchain: Blockchain.TON,
      address: "EQCxE6mUtQJKFnGfaROTKOt1lZbDiiX1kCixRv7Nw2Id_sDs", // USDT
    },
    amount: {
      offerUnits: "1000000", // 1 USDT
    },
  })
  .subscribe((quote) => {
    if (!quote) return;

    // process the quote
  });

A QuoteRequest has the following properties:

The server returns Observable<Quote | null>, which is a stream of quotes. Learn more about Observable types in the official RxJS documentation.

A Quote has the following properties:

Build a transaction

Now that we have a quote, we should request a server to build a transaction to initiate the trade that the user can verify and sign.

const tx = await omniston.buildTransfer({
  quote,
  sourceAddress: {
    blockchain: Blockchain.TON,
    address: "", // sender wallet address on `offerBlockchain`
  },
  destinationAddress: {
    blockchain: Blockchain.TON,
    address: "", // receiver wallet address on `askBlockchain`
  },
  maxSlippageBps: 100, // 1%
});

const messages = tx.transaction!.ton!.messages;

The buildTransfer method takes a TransactionRequest object as a parameter, having the following properties:

Sign the transaction

You can send messages to any library of your choice. Take a look at our transaction sending guide with examples of different popular packages

Listen for trade status updates

After the user has signed and initiated the transaction, we can track the trade status.

const tradeStatus = omniston.trackTrade({
  quoteId: quote.quoteId,
  traderWalletAddress: {
    blockchain: Blockchain.TON,
    address: "", // sender wallet address on `offerBlockchain`
  },
});

tradeStatus.subscribe((status) => {
  console.log(JSON.stringify(status));
});

The trackTrade method has the following parameters:

It returns Observable<TrackTradeStatus>. For the different trade status values, see the source code. We are interested in the trade result enum which can be read from status.tradeSettled?.result? field. The enum has the following values:

Last updated