npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@stellaswap/swap-sdk

v0.0.16

Published

StellaSwap Swap SDK

Downloads

19

Readme

StellaSwap SDK

Description

The @stellaswap/swap-sdk provides functionality for integrating swap on Moonbeam into any app or plugin. StellaSwap SDK allows end-users to exchange tokens seamlessly on Moonbeam network.

Features

  • Uses state-of-the-art Hybrid Router
  • Utilizes Stable, V2, V3 AMMs
  • Error Handling

Installation

To install the package, use npm or yarn:

npm install @stellaswap/swap-sdk
# OR
yarn add @stellaswap/swap-sdk

Usage

Importing the SDK

First, import the SDK.

import stellaSwap from '@stellaswap/swap-sdk';

Allowance

This helps to check allowance of tokenAddress against spender, it will return allowed number if there is any.

const addresses = await stellaSwap.getAddresses();
const spender = addresses.permit2;
const allowance = await stellaSwap.checkAllowance(
  account,
  erc20Instance,
  spender
);
Response: 0

Approve

To perform approve pass desired value as amountIn and for unlimited approval use 0. In response, it returns transaction hash.

const addresses = await stellaSwap.getAddresses();
const spender = addresses.permit2;
const tx = await stellaSwap.approve(amountIn, erc20Instance, spender);

Get Quote

To get amountOut of a trade use getQuote. For account it can be null if user is not connected. For native asset pass ETH as token0Addr or token1Addr.

const quote = await stellaSwap.getQuote(
  token0Addr,
  token1Addr,
  amountIn,
  account,
  slippage
);
Response

To filter out amountOut use quote.result.amountOut. For the rest of the response, it includes;

  • Complete trade path.
  • Execution with commands and inputs

Swap

This can executes actual swap, for native asset pass ETH as token0Addr or token1Addr.

const tx = await stellaSwap.executeSwap(
  token0Addr,
  token1Addr,
  amountIn,
  signer,
  slippage
);

Swap Native to ERC20

To swap native to ERC20, pass token0Addr as ETH and token1Addr as erc20 address.

const encodedTxData = await stellaSwap.executeNativeSwap(
  token0Addr,
  token1Addr,
  amountIn,
  slippage,
  aggregatorContractInstance
);
const txParams = {
  from: ACCOUNT,
  value: amountIn,
  to: aggregatorContractInstance.address,
  data: encodedTxData,
  gasLimit: 1_500_000,
  gasPrice: await signer.getGasPrice(),
};

const txResponse = await signer.sendTransaction(txParams);
await txResponse.wait();
return txResponse.hash;

Swap ERC20 to ERC20

To swap native to ERC20, pass token0Addr as erc20 address and token1Addr as erc20 address.

const { signature, permit } = await permit2.getPermit2Signature(
  token0Addr,
  amountIn,
  signer
);

const encodedTxData = await stellaSwap.executeERC20Swap(
  token0Addr,
  token1Addr,
  amountIn,
  slippage,
  aggregatorContractInstance,
  permit,
  signature
);
const txParams = {
  to: aggregatorContractInstance.address,
  data: encodedTxData,
  gasLimit: 1_500_000,
  gasPrice: await signer.getGasPrice(),
};

const txResponse = await signer.sendTransaction(txParams);
await txResponse.wait();

console.log("Transaction hash:", txResponse.hash);
return txResponse.hash;

Exampel for getPermit2Signature

import {
  PermitTransferFrom,
  Witness,
  SignatureTransfer,
  MaxUint256,
} from "@uniswap/permit2-sdk";
import { joinSignature, splitSignature } from "@ethersproject/bytes";
import stellaSwap from "@stellaswap/swap-sdkÏ";

const permit2 = {
  async getPermit2Signature(token0Addr: string, amountIn: string, signer: any) {
    const addresses = await stellaSwap.getAddresses();
    const AGGREGATOR_ADDRESS = addresses.aggregator;
    const PERMIT2_ADDRESS = addresses.permit2;

    const spender = AGGREGATOR_ADDRESS;

    const permit: PermitTransferFrom = {
      permitted: {
        token: token0Addr,
        amount: amountIn,
      },
      spender: spender,
      nonce: await utils.calcNonces(signer),
      deadline: MaxUint256,
    };

    const witness: Witness = {
      witnessTypeName: "Witness",
      witnessType: { Witness: [{ name: "user", type: "address" }] },
      witness: { user: spender },
    };

    const { domain, types, values } = SignatureTransfer.getPermitData(
      permit,
      PERMIT2_ADDRESS,
      await signer.getChainId(),
      witness
    );

    const signature = await signer._signTypedData(domain, types, values);

    let { r, s, v } = splitSignature(signature);

    if (v == 0) v = 27;
    if (v == 1) v = 28;

    const joined = joinSignature({ r, s, v });

    return { signature: joined, permit, witness };
  },
};

export default permit2;

Dependencies

  • axios
  • @uniswap/permit2-sdk

Configuration

The SDK is pre-configured to be used with the Moonbeam mainnet and doesn't require an API key.

Error Handling

The SDK includes basic error handling. All methods return Promises, so you can use .catch() to handle errors as you see fit.

stellaSwap.checkAllowance(tokenAddress, signer, spender).catch((error) => {
  console.error("Check Allowance failed:", error.message);
});

Contribution

Feel free to submit issues and enhancement requests.