Alex-SDK
Alex-SDK is an 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.
Methods
fetchSwappableCurrency()
Fetches the list of currencies that can be swapped on the DEX.
- Returns:
Promise<TokenInfo[]>
- A promise that resolves to an array of TokenInfo objects, representing the swappable currencies.
getAllPossibleRoutes(from: Currency, to: Currency)
Retrieves all possible routes for swapping from one currency to another.
getRoute(from: Currency, to: Currency)
Retrieves the best route for swapping from one currency to another.
getWayPoints(route: AMMRoute)
Displays the detailed route information.
getFeeRate(from: Currency, to: Currency, customRoute?: AMMRoute)
Calculates the fee rate for a swap between two currencies.
getAmountTo(from: Currency, fromAmount: bigint, to: Currency, customRoute?: AMMRoute)
Calculates the amount of the destination currency that will be received for a given amount of the source currency.
runSwap(stxAddress: string, currencyX: Currency, currencyY: Currency, fromAmount: bigint, minDy: bigint, customRoute?: AMMRoute)
Executes a swap transaction between two currencies.
getLatestPrices()
Retrieves the latest prices for all supported currencies.
- Returns:
Promise<Partial<{ [currency in Currency]: number }>>
- A promise that resolves to an object containing the latest prices for each currency.
getBalances(stxAddress: string)
Retrieves the balances of all supported currencies for a given Stacks (STX) address.
Usage
import { AlexSDK, Currency } from 'alex-sdk';
const alex = new AlexSDK();
(async () => {
const feeRate = await alex.getFeeRate(Currency.STX, Currency.ALEX);
console.log('Swap fee:', feeRate);
const route = await alex.getRoute(Currency.STX, Currency.ALEX);
const routeDisplay = await alex.getWayPoints(route);
console.log('Route:', routeDisplay.map((x) => x.name).join(' -> '));
const amountTo = await alex.getAmountTo(
Currency.STX,
BigInt(100 * 1e8),
Currency.ALEX
);
console.log('Amount to receive:', Number(amountTo) / 1e8);
const tx = await alex.runSwap(
stxAddress,
Currency.STX,
Currency.ALEX,
BigInt(Number(amount) * 1e8),
BigInt(0)
);
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