|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
以程式設計方式將 KLAYswap 整合到智慧合約中,用戶可以在去中心化應用程式中無縫買賣代幣。本教學重點在於如何使用 KLAYswap V2,提供在 Klaytn Cypress(主網)網路上交換代幣的方法。 KLAYswap UniversalRouter 提供了各種方法,例如 swapExactETHForTokens、swapExactTokensForTokens 和 swapExactTokensForETH 來促進這些交換。在啟動交換之前,用戶必須批准路由器存取指定數量的代幣。本指南包括一個完整的工作範例、使用thirdweb CLI 部署MiniKLAYswap 合約的說明,以及使用儀表板和Klaytnscope 執行交換操作的逐步說明。
Integrating KLAYswap V2 into Smart Contracts: A Comprehensive Guide for Developers
將 KLAYswap V2 整合到智能合約中:開發人員綜合指南
Introduction
介紹
For developers seeking to enhance the functionality of their decentralized applications (dApps), integrating automated market maker (AMM) protocols such as KLAYswap becomes crucial. This integration enables the seamless swapping of tokens within dApps, enhancing user experience and expanding service offerings.
對於尋求增強去中心化應用程式 (dApp) 功能的開發人員來說,整合 KLAYswap 等自動化做市商 (AMM) 協議變得至關重要。這種整合可以實現 dApp 內代幣的無縫交換,從而增強用戶體驗並擴展服務範圍。
Understanding KLAYswap
了解 KLAYswap
KLAYswap is an AMM protocol designed specifically for the Klaytn network. It operates as a decentralized exchange (DEX), facilitating instant token swaps by relying on on-chain liquidity pools established by liquidity providers. Unlike traditional order book structures, KLAYswap's liquidity pools allow users to trade tokens directly with the pool, eliminating the need for counterparties.
KLAYswap 是專門為 Klaytn 網路設計的 AMM 協定。它作為去中心化交易所(DEX)運作,依靠流動性提供者建立的鏈上流動性池促進即時代幣交換。與傳統的訂單簿結構不同,KLAYswap 的流動性池允許用戶直接與池交易代幣,從而消除了交易對手的需要。
Benefits of Integrating KLAYswap V2
整合 KLAYswap V2 的好處
Integrating KLAYswap V2 into smart contracts provides numerous advantages:
將 KLAYswap V2 整合到智能合約中具有許多優勢:
- Seamless Token Swapping: Users can conveniently swap tokens directly within your dApp without leaving the interface.
- Enhanced User Experience: By incorporating KLAYswap, you can offer users a more intuitive and efficient trading experience.
- Expanded Functionality: Integrate additional services around specific ERC20 and KIP7 tokens, such as enabling token purchases or lending.
Technical Integration Details
無縫代幣交換:用戶可以在您的dApp 內方便地直接交換代幣,而無需離開介面。增強的用戶體驗:透過整合KLAYswap,您可以為用戶提供更直觀、更有效率的交易體驗。擴展功能:圍繞特定ERC20 和KIP7 代幣整合附加服務,例如啟用代幣購買或借貸。技術整合詳細資訊
To programmatically integrate KLAYswap V2 into your smart contract, follow these steps:
若要以程式設計方式將 KLAYswap V2 整合到您的智能合約中,請按照以下步驟操作:
1. Prerequisites:
1.先決條件:
- Proficiency in smart contract development.
- Familiarity with smart contract development environments (e.g., thirdweb).
2. KLAYswap Universal Router:
熟練智能合約開發。熟悉智能合約開發環境(例如thirdweb)。2. KLAYswap 通用路由器:
- Utilize the KLAYswap router to access a range of methods for secure token swapping.
- Before executing any swap method, the user must authorize the router to access the desired amount of tokens.
3. Token Swap Methods:
利用KLAYswap路由器存取一系列安全令牌交換方法。在執行任何交換方法之前,使用者必須授權路由器存取所需數量的令牌。3.代幣交換方法:
- swapExactETHForTokens: Swaps an exact amount of KLAY for tokens, maximizing the output tokens along the specified path.
- swapExactTokensForTokens: Swaps an exact amount of input tokens for tokens, maximizing the output tokens along the specified path.
- swapExactTokensForETH: Swaps an exact amount of tokens for ETH, maximizing the ETH output along the specified path.
4. Fully Working Example:
swapExactETHForTokens:將精確數量的KLAY 交換為令牌,沿指定路徑最大化輸出令牌。swapExactTokensForTokens:將精確數量的輸入令牌交換為令牌,沿指定路徑最大化輸出令牌。swapExactTokensForETH:將精確數量的輸入令牌交換為令牌,沿指定路徑最大化輸出令牌。swapExactTokensForETH:將精確數量的輸入令牌交換為令牌,沿指定路徑最大化輸出令牌。swapExactTokensForETH:將精確數量的令牌交換為令牌ETH,沿指定路徑最大化ETH輸出。4.完全工作範例:
// Sample smart contract showcasing KLAYswap operations// 展示 KLAYswap 操作的智慧合約範例
pragma solidity ^0.8.20;雜注可靠性^0.8.20;
interface IKlaySwapRouterV2 {介面 IKlaySwapRouterV2 {
struct SwapParams {結構 SwapParams {
address to;寫給;
address[] path;地址[]路徑;
address[] pool;地址[]池;
uint deadline;uint 截止日期;
}}
}}
function swapExactTokensForTokens(函數 swapExactTokensForTokens(
uint256 amountIn,uint256 金額,
uint256 amountOutMin,uint256 金額輸出最小值,
SwapParams calldata pSwapParams 呼叫資料 p
aramsSwapParams 呼叫資料參數
) external returns (uint256[] memory amounts);) 外部回傳(uint256[] 內存量);
function swapExactETHForTokens(函數 swapExactETHForTokens(
uint256 amountOutMin,uint256 金額輸出最小值,
SwapParams calldata pSwapParams 呼叫資料 p
) external payable returns (uint256[] memory amounts);) 外部應付收益(uint256[] 內存量);
function swapExactTokensForETH(函數 swapExactTokensForETH(
uint256 amountIn,uint256 金額,
uint256 amountOutMin,uint256 金額輸出最小值,
SwapParams calldata pSwapParams 呼叫資料 p
) external returns (uint256[] memory amounts);) 外部回傳(uint256[] 內存量);
}}
interface IERC20 {介面 IERC20 {
function transferFrom(address sender, address recipient, uint256 amount) external returns (bool);functiontransferFrom(位址發送者,地址接收者,uint256金額)外部回傳(bool);
function approve(address spender, uint256 amount) external returns (bool);函數批准(地址支出者,uint256金額)外部回報(bool);
}}
contract MiniKLAYSwap {合約 MiniKLAYSwap {
address public constant routerAddress = 0xe0fbB27D0E7F3a397A67a9d4864D4f4DD7cF8cB9;地址公共常數routerAddress = 0xe0fbB27D0E7F3a397A67a9d4864D4f4DD7cF8cB9;
address public constant MBX_TOKEN_ADDRESS = 0xD068c52d81f4409B9502dA926aCE3301cc41f623;位址公共常數 MBX_TOKEN_ADDRESS = 0xD068c52d81f4409B9502dA926aCE3301cc41f623;
address public constant sKAI_TOKEN_ADDRESS = 0x37d46C6813B121d6A27eD263AeF782081ae95434;位址公共常數 sKAI_TOKEN_ADDRESS = 0x37d46C6813B121d6A27eD263AeF782081ae95434;
address public constant WKLAY_ADDRESS = 0x19Aac5f612f524B754CA7e7c41cbFa2E981A4432;位址公共常數WKLAY_ADDRESS = 0x19Aac5f612f524B754CA7e7c41cbFa2E981A4432;
IKlaySwapRouterV2 public immutable swapRouter = IKlaySwapRouterV2(routerAddress);IKlaySwapRouterV2 公共不可變 swapRouter = IKlaySwapRouterV2(routerAddress);
IERC20 public immutable MBX_TOKEN = IERC20(MBX_TOKEN_ADDRESS);IERC20 公共不可變 MBX_TOKEN = IERC20(MBX_TOKEN_ADDRESS);
IERC20 public immutable SKAI_TOKEN = IERC20(sKAI_TOKEN_ADDRESS);IERC20 公共不可變 SKAI_TOKEN = IERC20(sKAI_TOKEN_ADDRESS);
address[] private pool = [address(0)];位址[]私有池=[位址(0)];
address[] private path = [MBX_TOKEN_ADDRESS, WKLAY_ADDRESS];位址[]私有路徑= [MBX_TOKEN_ADDRESS, WKLAY_ADDRESS];
address[] private path1 = [WKLAY_ADDRESS, sKAI_TOKEN_ADDRESS];位址[]私有路徑1 = [WKLAY_ADDRESS, sKAI_TOKEN_ADDRESS];
address[] private path2 = [sKAI_TOKEN_ADDRESS, WKLAY_ADDRESS];位址[]私有路徑2 = [sKAI_TOKEN_ADDRESS, WKLAY_ADDRESS];
function swapExactTokensForTokens(函數 swapExactTokensForTokens(
uint256 amountIn) external returns (uint256[] memory amountOut) {
MBX_TOKEN.transferFrom(msg.sender, address(this), amountIn);MBX_TOKEN.transferFrom(msg.sender, 位址(this), amountIn);
MBX_TOKEN.approve(address(swapRouter), amountIn);MBX_TOKEN.approve(地址(swapRouter), amountIn);
IKlaySwapRouterV2.SwapParams memory params = IKlaySwapRouterV2.SwapParams(IKlaySwapRouterV2.SwapParams 記憶體參數 = IKlaySwapRouterV2.SwapParams(
to: msg.sender,至:msg.sender,
path: path,路徑:路徑,
pool: pool,泳池: 泳池,
deadline: block.timestamp截止日期:區塊.時間戳
););
amountOut = swapRouter.swapExactTokensForTokens(amountIn, 1, params);amountOut = swapRouter.swapExactTokensForTokens(amountIn, 1, params);
}}
}}
function swapExactKLAYForTokens() external payable returns (uint256[] memory amountOut) {function swapExactKLAYForTokens() 外部應付收益 (uint256[] memory amountOut) {
IKlaySwapRouterV2.SwapParams memory params = IKlaySwapRouterV2.SwapParams(IKlaySwapRouterV2.SwapParams 記憶體參數 = IKlaySwapRouterV2.SwapParams(
to: msg.sender,至:msg.sender,
path: path1,路徑:路徑1,
pool: pool,泳池: 泳池,
deadline: block.timestamp截止日期:區塊.時間戳
););
amountOut = swapRouter.swapExactETHForTokens{value: msg.value}}
(1, params);
}}
}}
function swapExactTokensForKLAY(uint256 amountIn) external returns (uint256[] memory amountOut) {函數 swapExactTokensForKLAY(uint256 amountIn) 外部回傳 (uint256[] 記憶體 amountOut) {
SKAI_TOKEN.transferFrom(msg.sender, address(this), amountIn);SKAI_TOKEN.transferFrom(msg.sender, 位址(this), amountIn);
SKAI_TOKEN.approve(address(swapRouter), amountIn);SKAI_TOKEN.approve(地址(swapRouter), amountIn);
IKlaySwapRouterV2.SwapParams memory params = IKlaySwapRouterV2.SwapParams(IKlaySwapRouterV2.SwapParams 記憶體參數 = IKlaySwapRouterV2.SwapParams(
to: msg.sender,至:msg.sender,
path: path2,路徑:路徑2,
pool: pool,泳池: 泳池,
deadline: block.timestamp截止日期:區塊.時間戳
););
amountOut = swapRouter.swapExactTokensForETH(amountIn, 1, params);amountOut = swapRouter.swapExactTokensForETH(amountIn, 1, params);
}}
}}
}}
5. Deploying MiniKLAYswap.sol using thirdweb CLI:
Utilize the thirdweb CLI to deploy the MiniKLAYswap.sol contract. Provide a project name and select your desired framework (Hardhat or Foundry). For this example, choose the Empty base contract option.
使用thirdweb CLI部署MiniKLAYswap.sol合約。提供項目名稱並選擇所需的框架(Hardhat 或 Foundry)。對於本範例,選擇空基礎合約選項。
6. Performing the Swap Operation:
6. 執行交換操作:
To execute the swap operations, navigate to the explorer tab of your deployed MiniKLAYswap contract on the thirdweb dashboard. Select the appropriate swap method (e.g., swapExactKLAYForTokens) and provide the necessary input parameters. Click 'Execute' to perform the swap.
若要執行交換操作,請導覽至 ThirdWeb 儀表板上已部署的 MiniKLAYswap 合約的瀏覽器標籤。選擇適當的交換方法(例如 swapExactKLAYForTokens)並提供必要的輸入參數。按一下“執行”以執行交換。
7. Verifying the Swap Operation:
7. 驗證交換操作:
To verify the swap, paste the transaction hash into the Klaytnscope search bar. This will display the details of the transaction, including the swapped token amounts.
要驗證交換,請將交易哈希貼到 Klaytnscope 搜尋欄中。這將顯示交易的詳細信息,包括交換的代幣金額。
Conclusion
結論
By integrating KLAYswap V2 into your smart contracts, you empower users to seamlessly swap tokens within your dApp. This enhances the user experience and opens up new possibilities for token-related services.
透過將 KLAYswap V2 整合到您的智能合約中,您可以讓用戶在您的 dApp 中無縫交換代幣。這增強了用戶體驗,並為代幣相關服務開闢了新的可能性。
For further exploration and guidance, refer to the Klaytn Docs and KLAYswap Docs. If you encounter any queries, engage with the Klaytn Forum for assistance. Stay tuned for Part 2 of this tutorial, where we will delve into building a minimalistic frontend to interact with the MiniKLAYswap contract using thirdweb and Next.js.
如需進一步的探索和指導,請參閱 Klaytn 文件和 KLAYswap 文件。如果您遇到任何疑問,請聯絡 Klaytn 論壇尋求協助。請繼續關注本教學的第 2 部分,我們將深入建立一個簡約的前端,以使用thirdweb 和 Next.js 與 MiniKLAYswap 合約進行互動。
免責聲明:info@kdj.com
所提供的資訊並非交易建議。 kDJ.com對任何基於本文提供的資訊進行的投資不承擔任何責任。加密貨幣波動性較大,建議您充分研究後謹慎投資!
如果您認為本網站使用的內容侵犯了您的版權,請立即聯絡我們(info@kdj.com),我們將及時刪除。
-
- Hive Digital 揭露比特幣產出小幅成長
- 2024-11-09 10:25:02
- 比特幣礦商 Hive Digital 報告稱,10 月比特幣產量小幅增長,同時提高了挖礦能力和基礎設施。