|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
以编程方式将 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:将精确数量的令牌交换为令牌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
The information provided is not trading advice. kdj.com does not assume any responsibility for any investments made based on the information provided in this article. Cryptocurrencies are highly volatile and it is highly recommended that you invest with caution after thorough research!
If you believe that the content used on this website infringes your copyright, please contact us immediately (info@kdj.com) and we will delete it promptly.
-
- 尽管较广泛的加密货币市场撤退,但Dogecoin(加密货币:Doge)在星期二上涨了星期二
- 2025-02-02 08:50:56
- Dogecoin投资者在新的政府效率部门网站上感到惊喜。
-
- 本赛季顶级加密货币奖励:BDAG,ADA,XRP,TRX
- 2025-02-02 08:50:56
- 随着Altcoin季节的增长,许多人正在寻找增长潜力最高的数字资产。比特币经常领导市场
-
- 吨接近6个月的低点,供应分配和Netflows的迹象冲突
- 2025-02-02 08:50:56
- Toncoin [ton]在分散和集中式交流(DEX和CEX)以及衍生品中,其总价值锁定(TVL)稳步下降
-
- 比特币可以飙升吗?关于当今市场动态的见解!
- 2025-02-02 08:50:56
- 加密货币市场目前正经历着重要的上升,这在很大程度上受到稳定驱动的全新流动性的影响。
-
- 您不会相信Dogecoin接下来会发生什么!
- 2025-02-02 08:50:56
- 令人兴奋的Dogecoin的新发展