
Company News
Socket Named Top Sales Organization by RepVue
Socket won two 2026 Reppy Awards from RepVue, ranking in the top 5% of all sales orgs. AE Alexandra Lister shares what it's like to grow a sales career here.
@zenswap-labs/sdk
Advanced tools
A comprehensive SDK for interacting with the Zenswap protocol and various blockchain networks
A comprehensive SDK for interacting with the Zenswap protocol and various blockchain networks.
npm install @zenswap-analog/sdk
# or
yarn add @zenswap-analog/sdk
# or
pnpm add @zenswap-analog/sdk
The Zenswap SDK provides a powerful interface for interacting with Zenswap's smart contracts directly, enabling cross-chain swaps, liquidity provision, and more without requiring a UI.
The SwapRouter class is the main entry point for interacting with Zenswap's smart contracts:
import {
SwapRouter,
NetworkId,
ChainId,
getNetworkId,
getChainId,
} from "@zenswap-analog/sdk";
// The SDK comes with default configurations for:
// - Contract addresses for Sepolia and Arbitrum Sepolia testnet deployments
// - GMP IDs for cross-chain messaging
// - Fee parameters (0.25% fee, default recipient)
// - Network configurations (RPC URLs, chain IDs, etc.)
// - Native token configurations
// - Token standards and interfaces
// - Cross-chain protocols
// Create router instance with default configurations
const router = new SwapRouter();
// Or override specific configurations
const customContracts = {
[ChainId.EthereumMainnet]: {
zenswap: "0x...", // Your deployed Zenswap contract address
gmpPlugin: "0x...", // Your deployed GMP plugin address
gmpGateway: "0x...", // Your deployed GMP gateway address
},
};
const customGmpIds = {
[ChainId.EthereumMainnet]: 1,
};
const customFeePercent = 0.3; // 0.3%
const customFeeRecipient = "0x...";
// Create router instance with custom configurations
const customRouter = new SwapRouter(
customContracts, // null to use defaults
customGmpIds, // null to use defaults
customFeePercent, // null to use defaults
customFeeRecipient // null to use defaults
);
The SDK supports various swap strategies:
import { SwapRouter, NetworkToken } from "@zenswap-analog/sdk";
// Define input parameters
const swapParams = {
input: tokenIn, // NetworkToken object for input token
output: tokenOut, // NetworkToken object for output token
amount: "1.5", // Amount to swap as string
recipient: "0x...", // Recipient address
slippagePercent: 0.5, // Slippage tolerance (0.5%)
deadlineInSeconds: 1800, // Transaction deadline (30 minutes)
};
// Find the best route
const route = await router.findRoute(swapParams);
if (route) {
console.log("Swap route found:");
console.log("Input amount:", route.input.amount);
console.log("Output amount:", route.output.amount);
console.log("Price impact:", route.priceImpact, "%");
console.log("Strategy:", route.strategy);
// The route.method contains the transaction parameters
const txParams = route.method?.parameters;
// Execute the transaction using your preferred web3 provider
// e.g., with ethers.js:
// const tx = await signer.sendTransaction(txParams);
}
// Example of a cross-chain transfer from Ethereum to Polygon
const transferParams = {
input: usdcEthereum, // USDC on Ethereum
output: usdcPolygon, // USDC on Polygon
amount: "100", // Amount to transfer
recipient: "0x...", // Recipient address
slippagePercent: 0.1,
deadlineInSeconds: 1800,
};
const transferRoute = await router.findRoute(transferParams);
// The transferRoute.method contains the transaction parameters
// The transferRoute.fee contains the estimated gas fee
// The transferRoute.transferFee contains the cross-chain transfer fee
// Get a quote for a swap
const quote = await router.quote(swapParams);
if (quote) {
console.log("Estimated output:", quote.output.amount);
console.log("Fee in native currency:", quote.fee.amount);
if (quote.transferFee) {
console.log("Transfer fee:", quote.transferFee.amount);
}
if (quote.gmpFee) {
console.log("GMP fee:", quote.gmpFee.amount);
}
}
// For cross-chain operations, there are two types of fees:
// 1. Transfer Fee: Gas fee for executing the transaction on the destination chain
// 2. GMP Fee: Fee for cross-chain message passing
// The GMP fee is calculated by the prepareGmpFee method
const gmpFee = await router.prepareGmpFee(proxyToken);
// The GMP fee is then applied when creating a transfer route
const transferRoute = router.createDirectRoute(
amount, // Amount to transfer
tokenIn, // Source token
tokenOut, // Destination token
RouteType.Transfer,
gmpFee.amount // GMP fee amount to deduct
);
// The output amount will be reduced by the GMP fee
console.log("Input amount:", amount);
console.log("GMP fee:", gmpFee.amount);
console.log("Output amount after GMP fee:", transferRoute.output.amount);
// 1. Estimate gas for a specific transaction
const gasUsed = await SwapRouter.estimateGasUsed(networkId, {
data,
from,
to,
value,
});
// 2. Create a SwapFee instance with custom coefficients
const swapFee = new SwapFee(
networkFee.token,
networkFee.gasLimit,
networkFee.baseFeePerGas
);
swapFee.baseCoeff = baseCoeff; // For different speed preferences
swapFee.priorityCoeff = priorityCoeff;
swapFee.additionalValue = transferFee?.value ?? BigInt(0);
// 3. Get maximum available amount considering fees
const maxAmount = getMaxValue(balance, combinedFee.amount, isNativeToken);
// 1. Create transaction request from router result
const { data, value, to } = route.method.parameters;
const txRequest = {
data,
value: value.toString(),
to,
maxFeePerGas: maxFeePerGas?.toString(),
maxPriorityFeePerGas: maxPriorityFeePerGas?.toString(),
};
// 2. Track transaction details
const txDetails = {
id: generateTxId(),
input: {
token: tokenIn,
amount: amountIn,
},
output: {
token: tokenOut,
amount: amountOut,
},
networkFee,
transferFee,
gmpFee,
routerFee,
priceImpact,
estimatedTime,
startTime: Date.now(),
state: TxState.Pending,
};
// 3. Execute and monitor transaction
const tx = await signer.sendTransaction(txRequest);
await waitForEvmTransactionMined(tx);
// 4. Update transaction state and details
txDetails.state = TxState.Done;
txDetails.endTime = Date.now();
txDetails.executionTime = txDetails.endTime - txDetails.startTime;
// 5. Handle cross-chain transactions
if (transferFee) {
// Monitor source chain confirmation
txDetails.sourceHash = tx.hash;
// Monitor destination chain execution
const destTx = await waitForDestinationTx(tx.hash);
txDetails.destHash = destTx.hash;
txDetails.received = {
token: tokenOut,
amount: destTx.amount,
};
}
The SDK provides comprehensive transaction tracking capabilities:
Transaction States:
Transaction Details:
Progress Monitoring:
Fee Management:
prepareGmpFee method which queries the GMP plugin contractcreateDirectRoute method// 1. Check if approval is needed
const isApproved = BigNumber(allowance).gte(BigNumber(amount));
// 2. Approve if needed
if (!isApproved) {
const tx = await approveToken(contractAddress, tokenAddress);
await waitForEvmTransactionMined(tx);
}
const QUOTE_UPDATE_INTERVAL = 15000; // 15 seconds
const QUOTE_DEBOUNCE_TIMEOUT = 1000; // 1 second
// 1. Create a debounced quote function
const debouncedQuote = debouncedHandler(updateQuote, QUOTE_DEBOUNCE_TIMEOUT);
// 2. Schedule periodic updates
function scheduleQuoteUpdate(quoteId) {
setTimeout(() => {
if (isCurrentQuoteId(quoteId)) {
updateQuote(quoteId);
}
}, QUOTE_UPDATE_INTERVAL);
}
// 3. Update quote with proper error handling
async function updateQuote(quoteId) {
try {
const result = await router.findRoute(params);
if (result && isCurrentQuoteId(quoteId)) {
await updateGasEstimates(result);
setQuoteResult(result);
scheduleQuoteUpdate(quoteId);
}
} catch (error) {
console.error("Quote update failed:", error);
}
}
Quote Management:
Fee Handling:
Transaction Flow:
Error Handling:
The Zenswap SDK supports a Boost Mode feature, which allows for enhanced performance and reduced fees during cross-chain swaps. To use Boost Mode, you can pass a boostParams object to the findRoute method.
import { SwapRouter, NetworkToken } from "@zenswap-analog/sdk";
// Define input parameters
const swapParams = {
input: tokenIn, // NetworkToken object for input token
output: tokenOut, // NetworkToken object for output token
amount: "1.5", // Amount to swap as string
recipient: "0x...", // Recipient address
slippagePercent: 0.5, // Slippage tolerance (0.5%)
deadlineInSeconds: 1800, // Transaction deadline (30 minutes)
};
// Define boost parameters
const boostParams = {
rewardAmount: "1", // Reward amount for the swap
};
// Find the best route with boost mode
const route = await router.findRoute(swapParams, boostParams);
if (route) {
console.log("Boost mode route found:");
console.log("Input amount:", route.input.amount);
console.log("Output amount:", route.output.amount);
console.log("Price impact:", route.priceImpact, "%");
console.log("Strategy:", route.strategy);
// The route.method contains the transaction parameters
const txParams = route.method?.parameters;
// Execute the transaction using your preferred web3 provider
// e.g., with ethers.js:
// const tx = await signer.sendTransaction(txParams);
}
This feature allows you to optimize your swaps for better performance and lower fees. Adjust the boostParams as needed to fit your specific use case.
The SDK includes several examples to help you get started:
# Build the SDK
npm run build
# Run the JavaScript example
npm run example:js
# Build the SDK
npm run build
# Run the TypeScript example
npm run example:ts
The SDK includes test scripts that demonstrate how to use it with real contract addresses:
# Run the basic test script
npm run test-sdk
You can also run the SDK in development mode, which provides a REPL for interactive testing:
npm run dev
This will:
SDK variableFor detailed documentation, see the API Reference.
You can also generate the documentation locally:
# Generate documentation
npm run docs
# The documentation will be available in the docs directory
Contributions are welcome! Please feel free to submit a Pull Request.
The SDK includes a comprehensive test suite. To run all tests, linting, and type checking:
npm run test:all
This will:
You can also run individual test commands:
# Run tests
npm test
# Run tests in watch mode
npm run test:watch
# Run tests with coverage
npm run test:coverage
This project is licensed under the MIT License - see the LICENSE file for details.
FAQs
A comprehensive SDK for interacting with the Zenswap protocol and various blockchain networks
The npm package @zenswap-labs/sdk receives a total of 1 weekly downloads. As such, @zenswap-labs/sdk popularity was classified as not popular.
We found that @zenswap-labs/sdk demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Company News
Socket won two 2026 Reppy Awards from RepVue, ranking in the top 5% of all sales orgs. AE Alexandra Lister shares what it's like to grow a sales career here.

Security News
NIST will stop enriching most CVEs under a new risk-based model, narrowing the NVD's scope as vulnerability submissions continue to surge.

Company News
/Security News
Socket is an initial recipient of OpenAI's Cybersecurity Grant Program, which commits $10M in API credits to defenders securing open source software.