@velarprotocol/velar-sdk
TypeScript icon, indicating that this package has built-in type declarations

0.6.8 • Public • Published

VELAR SDK

Velar-SDK is a user-friendly library to integrate swap functionality.

Velar SDK Types

  import { ContractPrincipalCV, PostCondition, PostConditionMode, UIntCV } from "@stacks/transactions";

  export interface SwapConfig {
      account: string;
      inToken: string;  // Token Symbol or Token Contract Address
      outToken: string; // Token Symbol or Token Contract Address
  }

  export interface ISwapService {
    swap(args: SwapPayload): Promise<SwapResponse>;
    getComputedAmount(args: ComputedAmountPayload): Promise<AmountOutResponse>;
  }

  export enum SwapType {
    ONE = 1,
    TWO = 2
  }

  export interface ComputedAmountPayload {
    amount: Number;
    slippage?: Number;
    type?: SwapType;
  }

  export interface AmountOutResponse {
    value: Number,
    route?: Array<string>, // multihop optimal route
    result?: any // multihop routes results
  }

  export interface SwapResponse {
    contractAddress: string,
    contractName: string,
    functionName: string,
    functionArgs: [
      UIntCV,                  // pool id
      ContractPrincipalCV,    // pool token0 address
      ContractPrincipalCV,    // pool token1 address
      ContractPrincipalCV,    // in token address
      ContractPrincipalCV,    // out token address
      ContractPrincipalCV,    // staking contract
      UIntCV,                 // amount in
      UIntCV                  // amount out
    ],
    postConditions: Array<PostCondition>,
    postConditionMode: PostConditionMode,
  }

  export declare class SwapService {
    constructor(args: SwapConfig);
    swap(args: SwapPayload): Promise<SwapResponse>;
    getComputedAmount(args: ComputedAmountPayload): Promise<AmountOutResponse>;
  }

VELAR SDK Interface

  export declare class VelarSDK {
    getPairs (symbol: string): Promise<Array<string>>;
    getSwapInstance(args: SwapConfig): SwapService;
  }

Swap Exact Tokens For Tokens and Multihop

import {
  SwapType,
  VelarSDK,
  getTokens,
  ISwapService,
  SwapResponse,
} from '@velarprotocol/velar-sdk';
import { openContractCall } from '@stacks/connect';

const sdk = new VelarSDK();

async () => {
  // getTokens will return you all of the tokens symbols which currently Velar supporting
  const { VELAR, STX } = await getTokens();
  const account = '';

  // setup swap instance
  const swapInstance: ISwapService = await sdk.getSwapInstance({
    account: account,
    inToken: VELAR,
    outToken: STX,
  });

  // to display amount out
  const amount: AmountOutResponse = await swapInstance.getComputedAmount({
    type: SwapType.ONE,
    amount: 10,
  });

  // get swap calculated arguments for contract call
  const swapOptions: SwapResponse = await swapInstance.swap({
    amount: 10,
    type: SwapType.ONE,
  });

  // build options for contract call
  const options = {
    ...swapOptions,
    network: AppService.getNetwork(),
    appDetails: AppService.getAppDetails(),
    anchorMode: AnchorMode.Any,
    onFinish: data => {},
    onCancel: ex => {},
  };

  // open contract call
  await openContractCall(options);
};

Swap Tokens For Exact Tokens

import {
  SwapType,
  VelarSDK,
  getTokens,
  ISwapService,
  SwapResponse,
} from '@velarprotocol/velar-sdk';
import { openContractCall } from '@stacks/connect';

const sdk = new VelarSDK();

async () => {
  // getTokens will return you all of the tokens symbols which currently Velar supporting
  const { VELAR, STX } = await getTokens();
  const account = '';

  // setup swap instance
  const swapInstance: ISwapService = await sdk.getSwapInstance({
    account: account,
    inToken: VELAR,
    outToken: STX,
  });

  // to display amount in
  const amount: AmountOutResponse = await swapInstance.getComputedAmount({
    type: SwapType.TWO,
    amount: 10,
  });

  // get swap calculated arguments for contract call
  const swapOptions: SwapResponse = await swapInstance.swap({
    amount: 10,
    type: SwapType.TWO,
  });

  // build options for contract call
  const options = {
    ...swapOptions,
    network: AppService.getNetwork(),
    appDetails: AppService.getAppDetails(),
    anchorMode: AnchorMode.Any,
    onFinish: data => {},
    onCancel: ex => {},
  };

  // open contract call
  await openContractCall(options);
};

Available tokens

https://sdk.velar.network/tokens/symbols

Dependents (0)

Package Sidebar

Install

npm i @velarprotocol/velar-sdk

Weekly Downloads

62

Version

0.6.8

License

MIT

Unpacked Size

597 kB

Total Files

38

Last publish

Collaborators

  • zivelar
  • velarbtc