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.
- Uses state-of-the-art Hybrid Router
- Utilizes Stable, V2, V3 AMMs
- Error Handling
To install the package, use npm or yarn:
npm install @stellaswap/swap-sdk
# OR
yarn add @stellaswap/swap-sdk
First, import the SDK.
import stellaSwap from '@stellaswap/swap-sdk';
This helps to check allowance of tokenAddress against spender, it will return allowed number if there is any.
const allowance = await stellaSwap.checkAllowance(tokenAddress, signer, spender);
Response: 0
To perform approve pass desired value as amountIn
and for unlimited approval use 0
. In response, it returns transaction hash.
const tx = await stellaSwap.approve(tokenAddress, amountIn, signer, spender);
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);
To filter out amountOut
use quote.result.amountOut
. For the rest of the response, it includes;
- Complete trade path.
- Execution with commands and inputs
This can executes actual swap, for native asset pass ETH
as token0Addr
or token1Addr
.
const tx = await stellaSwap.executeSwap(token0Addr, token1Addr, amountIn, signer, slippage);
- axios
- @uniswap/permit2-sdk
The SDK is pre-configured to be used with the Moonbeam mainnet and doesn't require an API key.
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);
});
Feel free to submit issues and enhancement requests.