0.1.28 • Published 26 days ago

alex-sdk v0.1.28

Weekly downloads
-
License
MIT
Repository
-
Last release
26 days ago

Alex-SDK

Alex-SDK is a easy-to-use library that exposes the swap functionality from alexlab.co to be integrated into any app or wallet. It enables users to perform swaps with a wide variety of supported currencies.

Supported Currencies

The SDK supports the following currencies:

export enum Currency {
  ALEX = 'age000-governance-token',
  USDA = 'token-wusda',
  STX = 'token-wstx',
  BANANA = 'token-wban',
  XBTC = 'token-wbtc',
  DIKO = 'token-wdiko',
  SLIME = 'token-wslm',
  XUSD = 'token-wxusd',
  MIA = 'token-wmia',
  NYCC = 'token-wnycc',
  CORGI = 'token-wcorgi',
}

Functions

The AlexSDK class includes the following functions:

export declare class AlexSDK {
    getFeeRate(from: Currency, to: Currency): Promise<bigint>;
    getRouter(from: Currency, to: Currency): Promise<Currency[]>;
    getAmountTo(from: Currency, fromAmount: bigint, to: Currency): Promise<bigint>;
    runSwap(stxAddress: string, currencyX: Currency, currencyY: Currency, fromAmount: bigint, minDy: bigint, router: Currency[]): TxToBroadCast;
    getCurrencyFrom(address: string): Currency | undefined;
    getLatestPrices(): Promise<
      Partial<{
        [currency in Currency]: number;
      }>
    >
  }

getFee

Rate Get the swap fee (liquidity provider fee) between two currencies.

async function getFeeRate(from: Currency, to: Currency): Promise<bigint>;

getRouter

Get the router path for swapping between two currencies.

async function getRouter(from: Currency, to: Currency): Promise<Currency[]>;

getAmountTo

Get the amount of destination currency that will be received when swapping from one currency to another.

async function getAmountTo(from: Currency, fromAmount: bigint, to: Currency): Promise<bigint>;

runSwap

Perform a swap between two currencies using the specified route and amount.

function runSwap(stxAddress: string, currencyX: Currency, currencyY: Currency, fromAmount: bigint, minDy: bigint, router: Currency[]): TxToBroadCast;

getCurrencyFrom

Get the corresponding currency for a given address.

function getCurrencyFrom(address: string): Currency | undefined;

getAddressFrom

Get the corresponding currency for a given address.

function getAddressFrom(currency: Exclude<Currency, Currency.STX>): string;

isAlexSwapTransaction

getLatestPrices

Get a list of token prices from Alex's price endpoint

getLatestPrices(): Promise<
  Partial<{
    [currency in Currency]: number;
  }>
>

It will do it's best to find the price, and where it can't, it will return undefined.

Check if a transaction is a swap transaction from Alex

function isAlexSwapTransaction(deployer: string, contractName: string, functionName: string): boolean;

broadcastSponsoredTx

Broadcast a sponsored transaction to Alex's sponsored tx services

function broadcastSponsoredTx(txRaw: string): Promise<string>;

isSponsoredSwapEnabled

Check if alex's swap sponsor service is activated

function isSponsoredSwapEnabled(): Promise<boolean>;

Installation

You can install Alex-SDK using npm:

npm install alex-sdk

Usage

To use the AlexSDK, you can import it into your project and instantiate a new object:

import { AlexSDK, Currency } from 'alex-sdk';

const alex = new AlexSDK();

(async () => {
  // Get swap fee between ALEX and USDA
  const feeRate = await alex.getFeeRate(Currency.ALEX, Currency.USDA);
  console.log('Swap fee:', feeRate);

  // Get the router path for swapping ALEX to USDA
  const router = await alex.getRouter(Currency.ALEX, Currency.USDA);
  console.log('Router path:', router);

  // Get the amount of USDA that will be received when swapping 100 ALEX
  const amountTo = await alex.getAmountTo(
    Currency.ALEX,
    BigInt(100),
    Currency.USDA
  );
  console.log('Amount to receive:', amountTo);

  // To get the transaction to broadcast
  const tx = await alex.runSwap(
    stxAddress,
    Currency.ALEX,
    Currency.USDA,
    BigInt(Number(amount) * 1e8),
    BigInt(0),
    router
  );

  // Then broadcast the transaction yourself
  await openContractCall(tx);
})();

There is a fully working example in the alex-sdk-example

Contributing

Contributions to the project are welcome. Please fork the repository, make your changes, and submit a pull request. Ensure your changes follow the code style and conventions used

0.1.28

26 days ago

0.1.27

1 month ago

0.1.26

2 months ago

0.1.25

3 months ago

0.1.24

4 months ago

0.1.23

5 months ago

0.1.14

10 months ago

0.1.15

9 months ago

0.1.20

9 months ago

0.1.21

8 months ago

0.1.22

8 months ago

0.1.16

9 months ago

0.1.17

9 months ago

0.1.18

9 months ago

0.1.19

9 months ago

0.1.10

1 year ago

0.1.11

1 year ago

0.1.12

1 year ago

0.1.13

1 year ago

0.1.8

1 year ago

0.1.7

1 year ago

0.1.9

1 year ago

0.0.5

1 year ago

0.1.6

1 year ago

0.1.5

1 year ago

0.0.4

1 year ago

0.0.3

1 year ago

0.0.2

1 year ago

0.0.1

1 year ago