### Sui Same-Chain Swap Example Source: https://kanalabs.github.io/kanalabs-aggregator-sdk-v2/index Demonstrates how to initialize the kanaLabs Aggregator SDK for Sui, fetch swap quotes, and execute a swap instruction on the Sui network. This example uses `JsonRpcProvider` and `RawSigner` for provider and signer setup. ```typescript const suiProvider = new JsonRpcProvider(new SuiConnection({ fullnode: 'SUI RPC NODE ' })); const suiSigner = new RawSigner(Ed25519Keypair.deriveKeypair(process.env.SUIMNEMONICS || ''), suiProvider); const swap = new SwapAggregator(Environment.production, { providers: { sui: suiProvider, }, signers: { sui: suiSigner, }, }); ``` ```typescript const quote = await swap.swapQuotes({ apiKey: APIKEY, inputToken: '0x2::sui::SUI', outputToken: '0xf0fe2210b4f0c4e3aff7ed147f14980cf14f1114c6ad8fd531ab748ccf33373b::bswt::BSWT', amountIn: '10000', slippage: 1, network: NetworkId.sui, }); const optimalQuote = quote.data[0]; ``` ```typescript const executeSwap = await swap.executeSwapInstruction({ quote: optimalQuote, address: await swap.signer?.sui?.getAddress(), }); ``` -------------------------------- ### Aptos Same-Chain Swap Example Source: https://kanalabs.github.io/kanalabs-aggregator-sdk-v2/index Demonstrates how to initialize the kanaLabs Aggregator SDK for Aptos, fetch swap quotes, and execute a swap instruction on the Aptos network. This example uses `AptosClient` and `AptosAccount` for provider and signer setup. ```typescript const aptosProvider = new AptosClient('RPC ENDPOINT'); const aptosSigner = AptosAccount.fromAptosAccountObject({ address: process.env.APTOS_ADDRESS || '', publicKeyHex: process.env.APTOS_PUBLICKEY || '', privateKeyHex: process.env.APTOS_PRIVATEKEY || '', }); const swap = new SwapAggregator(Environment.production, { providers: { aptos: aptosProvider, }, signers: { aptos: aptosSigner, }, }); ``` ```typescript const quote = await swap.swapQuotes({ apiKey: APIKEY, inputToken: '0x1::aptos_coin::AptosCoin', outputToken: '0x6f986d146e4a90b828d8c12c14b6f4e003fdff11a8eecceceb63744363eaac01::mod_coin::MOD', amountIn: '100000', slippage: 0.5, network: NetworkId.aptos, }); const optimalQuote = quote.data[0]; ``` ```typescript const executeSwap = await swap.executeSwapInstruction({ quote: optimalQuote, address: swap.signer?.aptos?.address().toString(), }); ``` -------------------------------- ### Solana SDK Initialization Example Source: https://kanalabs.github.io/kanalabs-aggregator-sdk-v2/index Illustrates how to initialize the kanaLabs Aggregator SDK for the Solana network, setting up the connection provider and signer using `Connection` and `Keypair`. ```typescript const solanaProvider = new Connection(clusterApiUrl('mainnet-beta')); const solanaSigner = Keypair.fromSecretKey(bs58.decode(process.env.SOLANAPAYER || '')); const swap = new SwapAggregator(Environment.production, { providers: { solana: solanaProvider, }, signers: { solana: solanaSigner, }, }); ``` -------------------------------- ### Initialize Kanalabs SDK for EVM Chains (Polygon Example) Source: https://kanalabs.github.io/kanalabs-aggregator-sdk-v2/index Demonstrates how to initialize the Kanalabs SwapAggregator SDK for EVM chains, specifically Polygon, using Ethers.js providers and signers. This setup is crucial for interacting with EVM-based decentralized exchanges and managing transactions. ```javascript const polygonRpc = process.env.ETH_NODE_URI_POLYGON as string; const polygonProvider = ethers.getDefaultProvider(polygonRpc); const polygonSigner = new ethers.Wallet(privateKey, polygonProvider); const swap = new SwapAggregator(Environment.production, { providers: { polygon: polygonProvider, }, signers: { polygon: polygonSigner, }, }); ``` -------------------------------- ### Install kanaLabs Aggregator SDK Source: https://kanalabs.github.io/kanalabs-aggregator-sdk-v2/index Instructions for installing the kanaLabs Aggregator SDK using popular package managers like NPM and Yarn. These commands add the SDK to your project's dependencies. ```shell npm i @kanalabs/aggregator ``` ```shell yarn add @kanalabs/aggregator ``` -------------------------------- ### Kanalabs Cross-Chain Swap API Reference Source: https://kanalabs.github.io/kanalabs-aggregator-sdk-v2/index Comprehensive API documentation for performing cross-chain swaps using the Kanalabs SDK. This process involves three main steps: getting quotes, executing the transfer, and claiming tokens on the destination chain. Includes detailed parameter descriptions for each method and an overview of the cross-chain swap flow. ```APIDOC crossChainQuote = async (params: CrossChainSwapParams) - Description: Fetches quotes for a cross-chain swap, providing details on potential routes and costs. - Parameters: - sourceToken: The token to be swapped from the source chain. - targetToken: The token to be received on the target chain after the swap. - sourceChain: The network ID or chain from which the swap originates. - targetChain: The network ID or chain where the swap will conclude. - amountIn: The quantity of source tokens to be swapped. - sourceSlippage: The maximum allowed slippage percentage on the source chain. - targetSlippage: The maximum allowed slippage percentage on the target chain. - options: (Optional) An object containing additional configuration options. executeTransfer = async (params: ExecuteCrossChainTransferParams) - Description: Executes the token transfer across chains, typically utilizing a bridge mechanism. - Parameters: - sourceProvider: RPC Provider for the source chain. - sourceSigner: Signer object for the source chain, authorizing the transaction. - quote: The selected cross-chain swap quote obtained from `crossChainQuote`. - sourceAddress: The user's address on the source chain. - targetAddress: The user's address on the target chain. - targetSigner: Signer object for the target chain (if required for transfer). - targetProvider: RPC provider for the target chain. executeClaim = async (params: ExecuteCrossChainClaimParams) - Description: Allows users to claim their tokens on the destination chain after a successful bridge transfer. This step may not be necessary for bridges that utilize a relayer. - Parameters: - txHash: The transaction hash of the transfer transaction on the source chain. - sourceProvider: RPC Provider for the source chain. - targetProvider: RPC provider for the target chain. - targetSigner: Signer object for the target chain, used for claiming. - quote: The selected cross-chain swap quote. - sourceAddress: The user's address on the source chain. - targetAddress: The user's address on the target chain. Cross-Chain Swap Flow: 1. Getting Swap Quotes: Obtain quotes for the selected chains and tokens. 2. Executing Transfer Instruction: Transfer tokens across chains using a bridge to reach the selected destination chain. 3. Executing Claim Instruction: After tokens are bridged to the destination chain, this step allows users to claim their tokens. Note that bridges utilizing a relayer may not require this specific claim step. Failure Handling: In the event of a claim failure, users can redeem their tokens that were transferred via the bridge. Kanalabs exclusively transfers native stable coins or wrapped stable coins via the bridge. Therefore, users have the option to redeem their tokens and manually initiate the swap on the destination chain. ``` -------------------------------- ### Get Cross-Chain Swap Quote for Aptos and Sui Chains Source: https://kanalabs.github.io/kanalabs-aggregator-sdk-v2/index This snippet demonstrates how to obtain a cross-chain swap quote using the `crossChainAggregator` for Aptos and Sui chains. It specifies source and target tokens, chains, amount, and slippage, returning an optimal route. ```javascript const crossChainQuotes = await crossChainAggregator.crossChainQuote({ sourceToken, targetToken, sourceChain: NetworkId.aptos, targetChain: NetworkId.sui, amountIn: '100000', sourceSlippage: 0.1, targetSlippage: 0.1, }); const optimalRoute = crossChainQuotes.data[0]; ``` -------------------------------- ### Get Cross-Chain Swap Quote for EVM Chains Source: https://kanalabs.github.io/kanalabs-aggregator-sdk-v2/index This snippet demonstrates how to obtain a cross-chain swap quote using the `crossChainAggregator` for EVM-compatible chains like Polygon and BSC. It specifies source and target tokens, chains, amount, and slippage, returning an optimal quote. ```javascript const quotes = await crossChainAggregator.crossChainQuote({ sourceToken: '0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee', targetToken: '0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee', sourceChain: NetworkId.polygon, targetChain: NetworkId.bsc, amountIn: utils.parseUnits('0.01', 18).toString(), sourceSlippage: 0.1, targetSlippage: 0.1, }); const optimalQuote = quotes.data[0]; ``` -------------------------------- ### Get Cross-Chain Swap Quote (Kanalabs Aggregator SDK) Source: https://kanalabs.github.io/kanalabs-aggregator-sdk-v2/index This snippet demonstrates how to obtain a cross-chain swap quote using the `crossChainAggregator.crossChainQuote` method. It specifies source and target chains, input amount, and slippage tolerances to find optimal routes for token swaps across different blockchain networks. The result provides an `optimalRoute` object for subsequent operations. ```javascript const crossChainQuotes = await crossChainAggregator.crossChainQuote({ 'So11111111111111111111111111111111111111112', '0x1::aptos_coin::AptosCoin', sourceChain: NetworkId.solana, targetChain: NetworkId.aptos, amountIn: '100000', sourceSlippage: 0.5, targetSlippage: 0.1 }); const optimalRoute = crossChainQuotes.data[0]; ``` -------------------------------- ### Initialize Kanalabs SDK for Aptos and Sui Chains Source: https://kanalabs.github.io/kanalabs-aggregator-sdk-v2/index This snippet shows the initialization of the Kanalabs SwapAggregator SDK along with setting up providers and signers for Aptos and Sui blockchains. It uses `JsonRpcProvider` for Sui and `AptosClient` for Aptos, deriving keypairs from environment variables. ```javascript const suiProvider = new JsonRpcProvider(new SuiConnection({ fullnode: 'SUI RPC NODE' })); const aptosProvider = new AptosClient('APTOS RPC NODE'); const suiSigner = new RawSigner(Ed25519Keypair.deriveKeypair(process.env.SUIMNEMONICS || ''), suiProvider); const aptosSigner = AptosAccount.fromAptosAccountObject({ address: process.env.APTOS_ADDRESS || '', publicKeyHex: process.env.APTOS_PUBLICKEY || '', privateKeyHex: process.env.APTOS_PRIVATEKEY || '', }); const crossChainAggregator = new SwapAggregator(Environment.production); ``` -------------------------------- ### Initialize Kanalabs SDK for Solana and Aptos Chains Source: https://kanalabs.github.io/kanalabs-aggregator-sdk-v2/index This snippet shows the initialization of the Kanalabs SwapAggregator SDK along with setting up providers and signers for Solana and Aptos blockchains. It uses `Connection` for Solana and `AptosClient` for Aptos, deriving keypairs from environment variables. ```javascript const solanaProvider = new Connection('SOLANA RPC NODE); const solanaSigner = Keypair.fromSecretKey(bs58.decode(process.env.SOLANAPAYER || '')); const aptosProvider = new AptosClient('APTOS RPC NODE'); const aptosSigner = AptosAccount.fromAptosAccountObject({ address: process.env.APTOS_ADDRESS || '', publicKeyHex: process.env.APTOS_PUBLICKEY || '', privateKeyHex: process.env.APTOS_PRIVATEKEY || '', }); const crossChainAggregator = new SwapAggregator(Environment.production); ``` -------------------------------- ### Same Chain Swap API Reference Source: https://kanalabs.github.io/kanalabs-aggregator-sdk-v2/index Detailed API reference for performing same-chain swaps using the kanaLabs Aggregator SDK. This includes the `swapQuotes` function for fetching available swap options and the `executeSwapInstruction` function for executing a chosen swap. ```APIDOC swapQuotes = async (params: SameChainSwapParams) - Description: Fetches available swap quotes for a same-chain swap operation. - Parameters: - inputToken: The input token for the swap operation. - outputToken: The output token to receive after the swap. - amountIn: The amount of input tokens to be swapped. - slippage: The allowed slippage percentage for the swap. - network: The network ID for the swap operation. - options: (Optional) Additional options for the swap. executeSwapInstruction = async (params: SameChainInstructionParams) - Description: Executes a previously obtained swap instruction. - Parameters: - quote: A `CommonRouteInterface` representing the quote response format. - address: The address related to the instruction. - options: (Optional) An object containing additional options. - options.provider: (Optional) The connection provider to use. - options.integrator: (Optional) A string identifying the integrator. ``` -------------------------------- ### Execute Cross-Chain Transfer Instruction for Aptos and Sui Chains Source: https://kanalabs.github.io/kanalabs-aggregator-sdk-v2/index This code shows how to execute a cross-chain transfer using a previously obtained `optimalRoute` for Aptos and Sui. It requires source provider, source address, source signer, the quote, and the target address to initiate the transfer. ```javascript const transfer = await crossChainAggregator.executeTransfer({ sourceProvider: aptosProvider, sourceAddress: aptosSigner.address().toString(), sourceSigner: aptosSigner, quote: optimalRoute, targetAddress: await suiSigner.getAddress(), }); ``` -------------------------------- ### Execute Cross-Chain Claim Instruction for Aptos and Sui Chains Source: https://kanalabs.github.io/kanalabs-aggregator-sdk-v2/index This snippet illustrates how to execute a claim instruction after a cross-chain transfer between Aptos and Sui. It uses the transaction hash from the transfer, provider information for both chains, signer details for the target chain, and the optimal route. ```javascript const claim = await crossChainAggregator.executeClaim({ txHash: transfer.txHash, sourceProvider: aptosProvider, targetProvider: suiProvider, targetSigner: suiSigner, quote: optimalRoute, sourceAddress: aptosSigner.address().toString(), targetAddress: await suiSigner.getAddress(), }); ``` -------------------------------- ### Initialize Kanalabs SDK for Cross-Chain Swaps (Polygon to BSC) Source: https://kanalabs.github.io/kanalabs-aggregator-sdk-v2/index Demonstrates how to initialize the Kanalabs SwapAggregator SDK for cross-chain operations. This involves setting up separate providers and signers for both the source chain (Polygon) and the target chain (BSC) to facilitate multi-chain interactions. ```javascript const polygonRpc = process.env.ETH_NODE_URI_POLYGON as string; const polygonProvider = ethers.getDefaultProvider(polygonRpc); const polygonSigner = new ethers.Wallet(privateKey, polygonProvider); const bscRpc = process.env.ETH_NODE_URI_BSC as string; const bscProvider = ethers.getDefaultProvider(bscRpc); const bscSigner = new ethers.Wallet(privateKey, bscProvider); const crossChainAggregator = new SwapAggregator(Environment.production); ``` -------------------------------- ### Execute Cross-Chain Transfer Instruction for EVM Chains Source: https://kanalabs.github.io/kanalabs-aggregator-sdk-v2/index This code shows how to execute a cross-chain transfer using a previously obtained `optimalQuote`. It requires source and target addresses, and provider/signer information for the source chain to initiate the transfer. ```javascript const transfer = await crossChainAggregator.executeTransfer({ quote: optimalQuote, sourceAddress: polygonSigner.getAddress() as string, targetAddress: bscSigner.getAddress() as string, sourceProvider: polygonProvider as providers.BaseProvider, sourceSigner: polygonSigner as EvmSignerType, }); ``` -------------------------------- ### Retrieve Swap Quotes for Solana Source: https://kanalabs.github.io/kanalabs-aggregator-sdk-v2/index Fetches optimal swap quotes for a given input token, output token, and amount on the Solana network. This process includes specifying slippage tolerance and utilizing an API key. ```javascript const quote = await swap.swapQuotes({ apiKey: APIKEY, inputToken: 'So11111111111111111111111111111111111111112', outputToken: 'EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v', amountIn: '1000', slippage: 1, network: NetworkId.solana, }); const optimalQuote = quote.data[0]; ``` -------------------------------- ### Execute Swap Instruction on Solana Source: https://kanalabs.github.io/kanalabs-aggregator-sdk-v2/index Executes a previously obtained optimal swap quote on the Solana network. This function requires the full quote object and the user's public address to initiate the transaction. ```javascript const executeSwap = await swap.executeSwapInstruction({ quote: optimalQuote, address: swap.signer?.solana?.publicKey.toString(), }); ``` -------------------------------- ### Execute Cross-Chain Claim Instruction for EVM Chains Source: https://kanalabs.github.io/kanalabs-aggregator-sdk-v2/index This snippet illustrates how to execute a claim instruction after a cross-chain transfer, particularly for bridges that require a separate claim step. It uses the transaction hash from the transfer, provider information for both chains, and signer details for the target chain. ```javascript const claim = await crossChainAggregator.executeClaim({ txHash: transfer.txHash, sourceProvider: polygonProvider as providers.BaseProvider, targetProvider: bscProvider as providers.BaseProvider, targetSigner: bscSigner as EvmSignerType, quote: optimalQuote, sourceAddress: polygonSigner.getAddress() as string, targetAddress: bscSigner.getAddress() as string, }); ``` -------------------------------- ### Retrieve Swap Quotes for EVM Chains (Polygon) Source: https://kanalabs.github.io/kanalabs-aggregator-sdk-v2/index Fetches optimal swap quotes for a given input token, output token, and amount on an EVM network like Polygon. This snippet illustrates handling EVM token addresses and proper amount formatting using `utils.parseEther`. ```javascript const quote = await swap.swapQuotes({ apiKey: APIKEY, inputToken: '0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee', outputToken: '0xc2132D05D31c914a87C6611C10748AEb04B58e8F', amountIn: utils.parseEther('0.05').toString(), slippage: 1, network: NetworkId.polygon, }); const optimalQuote = quote.data[0]; ``` -------------------------------- ### Execute Cross-Chain Transfer (Kanalabs Aggregator SDK) Source: https://kanalabs.github.io/kanalabs-aggregator-sdk-v2/index This snippet illustrates how to execute a cross-chain token transfer using the `crossChainAggregator.executeTransfer` method. It requires source chain provider, address, and signer details, along with the `optimalRoute` obtained from a quote, and the target recipient's address. The function returns a `transfer` object, typically containing a transaction hash. ```javascript const transfer = await crossChainAggregator.executeTransfer({ sourceProvider: solanaProvider, sourceAddress: solanaSigner.publicKey.toString(), sourceSigner: solanaSigner, quote: optimalRoute, targetAddress: aptosSigner.address().toString() }); ``` -------------------------------- ### Execute Swap Instruction on EVM Chains (Polygon) Source: https://kanalabs.github.io/kanalabs-aggregator-sdk-v2/index Executes a previously obtained optimal swap quote on an EVM network like Polygon. This function requires the quote object and the user's address, typically retrieved from the signer, to complete the swap transaction. ```javascript const executeSwap = await swap.executeSwapInstruction({ quote: optimalQuote, address: await swap.signer?.polygon?.getAddress(), }); ``` -------------------------------- ### Redeem Tokens After Failed Cross-Chain Transfer on Aptos and Sui Source: https://kanalabs.github.io/kanalabs-aggregator-sdk-v2/index This code demonstrates how to redeem tokens in case of a failed cross-chain transfer between Aptos and Sui. It requires source and target chain IDs, provider/signer information, the original transfer's transaction hash, target address, and the specific bridge ID used. ```javascript const redeem = await crossChainAggregator.redeem({ sourceChain: NetworkId.aptos, targetChain: NetworkId.sui, sourceProvider: aptosProvider, targetProvider: suiProvider, targetSigner: suiSigner, SourceHash: transfer.txHash, targetAddress: await suiSigner.getAddress(), BridgeId: BridgeId.wormhole, }); ``` -------------------------------- ### Execute Cross-Chain Claim (Kanalabs Aggregator SDK) Source: https://kanalabs.github.io/kanalabs-aggregator-sdk-v2/index This snippet shows how to execute a claim instruction using `crossChainAggregator.executeClaim`. This step is often necessary for certain bridge types to finalize the transfer on the target chain. It utilizes the transaction hash from the transfer, source/target providers, target signer, the original quote, and both source and target addresses to complete the cross-chain operation. ```javascript const claim = await crossChainAggregator.executeClaim({ txHash: transfer.txHash, sourceProvider: solanaProvider, targetProvider: aptosProvider, targetSigner: aptosSigner, quote: optimalRoute, sourceAddress: solanaSigner.publicKey.toString(), targetAddress: aptosSigner.address().toString() }); ``` -------------------------------- ### Redeem Tokens After Failed Cross-Chain Transfer on EVM Chains Source: https://kanalabs.github.io/kanalabs-aggregator-sdk-v2/index This code demonstrates how to redeem tokens in case of a failed cross-chain transfer. It requires source and target chain IDs, provider/signer information, the original transfer's transaction hash, target address, and the specific bridge ID used. ```javascript const redeem = await crossChainAggregator.redeem({ sourceChain: NetworkId.polygon, targetChain: NetworkId.bsc, sourceProvider: polygonProvider as providers.BaseProvider, targetProvider: bscProvider as providers.BaseProvider, targetSigner: bscSigner as EvmSignerType, SourceHash: transfer.txHash, targetAddress: bscSigner.getAddress() as string, BridgeId: BridgeId.wormhole, }); ``` -------------------------------- ### Redeem Tokens After Failed Cross-Chain Transfer (Kanalabs Aggregator SDK) Source: https://kanalabs.github.io/kanalabs-aggregator-sdk-v2/index This snippet demonstrates how to redeem tokens in case of a failed cross-chain transfer using `crossChainAggregator.redeem`. It requires details such as source and target chains, their respective providers, the target signer, the source transaction hash, the target address, and the specific `BridgeId` used. This function helps recover funds in exceptional scenarios. ```javascript const redeem = await crossChainAggregator.redeem({ sourceChain: NetworkId.solana, targetChain: NetworkId.aptos, sourceProvider: solanaProvider, targetProvider: aptosProvider, targetSigner: aptosSigner, SourceHash: transfer.txHash, targetAddress: aptosSigner.address().toString(), BridgeId: BridgeId.wormhole }); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.