UNPKG

@syncswap/sdk

Version:

SyncSwap TypeScript SDK for building DeFi applications

107 lines (87 loc) 3.54 kB
import SyncSwapSDK from "@syncswap/sdk"; import { ethers } from "ethers"; import { Provider, Wallet } from "zksync-web3"; import { Token } from "@syncswap/sdk/tokens/token"; import { UserContext } from "@syncswap/sdk/types/UserContext"; const PRIVATE_KEY = process.env.PRIVATE_KEY; /** * Provider-Only Mode Example (Recommended for Production) * * This approach separates transaction generation from signing, * enabling secure backend/frontend architecture and concurrent operations. */ async function providerModeExample() { console.log("\n=== Provider-Only Mode (Recommended) ==="); const provider = new ethers.providers.JsonRpcProvider("https://rpc.sophon.xyz"); const sdk = new SyncSwapSDK({ network: "sophonMainnet", allowUniswapV3Pools: true, enableHops: true, enableSplits: true }); // Initialize with provider only (no private keys needed on backend) await sdk.initialize(provider); const SOPHON_MAINNET_USDC = "0x9aa0f72392b5784ad86c6f3e899bcc053d00db4f"; const SOPHON_MAINNET_ETH = "0x72af9f169b619d85a47dfa8fefbcd39de55c567d"; const tokenIn = Token._SOPH.address; const tokenOut = SOPHON_MAINNET_ETH; const amountIn = ethers.utils.parseUnits("1", 18); // Create user context with custom settings const userContext: UserContext = { address: "0x0a225eFEFe50382EA7C30EDb0640e8B1311Db998", settings: { slippage: 0.5, // 0.5% gasPrice: 30, }, enableLimitedUnlock: true, allowSponsoredPaymaster: false }; console.log("Fetching swap route pools..."); const routePools = await sdk.fetchSyncSwapRouteData(tokenIn, tokenOut, userContext); if (!routePools) { console.log("No route pools found for the given tokens"); return; } const route = await sdk.calculateSyncSwapRoute(routePools, tokenIn, tokenOut, amountIn); console.log("Route calculated:", route); // Get permit signature data for frontend to sign const permitSignatureData = await sdk.getPermitSignatureData(tokenIn, amountIn, userContext); if (permitSignatureData) { console.log("Permit signature data generated for frontend signing"); console.log("Send this to frontend for user signing"); } else { console.log("Token does not support permit, checking approval..."); // Check if approval is needed const approved = await sdk.checkApproval(tokenIn, amountIn, userContext); if (!approved) { console.log("Approval needed, generating approve transaction..."); const approveTx = await sdk.approve(tokenIn, amountIn, userContext); console.log("Approval transaction data generated"); } else { console.log("Token already approved"); } } // Generate swap transaction data const swapTx = await sdk.swapExactInput(route, null, userContext); console.log("Swap transaction data generated"); console.log("Ready to send to frontend for execution"); const wallet = new Wallet("d48adce067f5be663c70d83f0968a8551ba4b1993d927a6e48b578bdbd0ec839", provider as any); const receipt = await wallet.sendTransaction(swapTx); console.log("Transaction receipt:", receipt); } async function main() { console.log("SyncSwap SDK Examples"); console.log("=".repeat(60)); try { await providerModeExample(); } catch (error) { console.error("Error in examples:", error); process.exit(1); } } main() .then(() => console.log("\nSDK Examples completed successfully")) .catch((error) => { console.error("Error in examples:", error); process.exit(1); });