-
Notifications
You must be signed in to change notification settings - Fork 62
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update Swap tutorial with better localnet support #490
Conversation
📝 Walkthrough📝 WalkthroughWalkthroughThe pull request introduces significant enhancements to the Changes
Possibly related PRs
Suggested reviewers
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 2
🧹 Outside diff range and nitpick comments (7)
src/pages/developers/tutorials/swap-any.mdx (4)
Line range hint
1-38
: Enhance clarity of the testnet compatibility alert.The alert message about testnet compatibility is informative but could be more specific. Consider adding an estimated timeframe or a way for users to check when the gateway will be deployed on testnet.
Suggested improvement:
<Alert> {" "} This tutorial depends on the gateway, which is available on localnet but not yet deployed on testnet. It will be compatible - with testnet after the gateway is deployed. In other words, you cannot deploy this tutorial on testnet yet.{" "} + with testnet after the gateway is deployed. You cannot deploy this tutorial on testnet yet. Check our official + announcements or documentation for updates on the gateway deployment to testnet.{" "} </Alert>
Line range hint
39-186
: Enhance contract code readability and gas efficiency.The contract implementation is correct, but there are opportunities for improvement in terms of readability and gas efficiency.
Consider the following enhancements:
- Use named return variables for better readability:
- function swapAndWithdraw( + function swapAndWithdraw( address inputToken, uint256 amount, address targetToken, bytes memory recipient, bool withdraw - ) internal { + ) internal returns (uint256 outputAmount) { // ... existing code ... - uint256 outputAmount = SwapHelperLib.swapExactTokensForTokens( + outputAmount = SwapHelperLib.swapExactTokensForTokens( systemContract, inputToken, swapAmount, targetToken, 0 ); // ... rest of the function ... }
- Optimize gas usage by avoiding unnecessary storage reads:
function swap( address inputToken, uint256 amount, address targetToken, bytes memory recipient, bool withdraw ) public { + IZRC20 inputZRC20 = IZRC20(inputToken); - IZRC20(inputToken).transferFrom(msg.sender, address(this), amount); + inputZRC20.transferFrom(msg.sender, address(this), amount); swapAndWithdraw(inputToken, amount, targetToken, recipient, withdraw); }
- Consider adding input validation to prevent potential issues:
function swap( address inputToken, uint256 amount, address targetToken, bytes memory recipient, bool withdraw ) public { + require(inputToken != address(0) && targetToken != address(0), "Invalid token addresses"); + require(amount > 0, "Amount must be greater than zero"); IZRC20(inputToken).transferFrom(msg.sender, address(this), amount); swapAndWithdraw(inputToken, amount, targetToken, recipient, withdraw); }These changes will improve the contract's readability, gas efficiency, and robustness.
Line range hint
266-283
: Provide more context for system contract and gateway addresses.The deployment instructions are clear, but additional information about the system contract and gateway addresses would be beneficial for users.
Consider adding the following explanation after the deployment instructions:
Note: The `systemContractAddress` and `gatewayAddress` are required for contract deployment. On localnet, these addresses are pre-configured: - System Contract Address: 0x... - Gateway Address: 0x... For other networks, you'll need to provide the correct addresses for that specific network.Replace the ellipsis (...) with the actual addresses used in the localnet environment. This addition will help users understand the importance of these addresses and prepare them for deploying to other networks in the future.
🧰 Tools
🪛 LanguageTool
[uncategorized] ~300-~300: Possible missing comma found.
Context: ...ZRC-20 to the recipient In the command above thewithdraw
istrue
, so the target...(AI_HYDRA_LEO_MISSING_COMMA)
[uncategorized] ~312-~312: Possible missing comma found.
Context: ...66 --withdraw false ``` In the command above thewithdraw
is `true`, so the target...(AI_HYDRA_LEO_MISSING_COMMA)
Line range hint
332-348
: Enhance conclusion with next steps or related resources.The conclusion effectively summarizes the tutorial's achievements. However, it could be improved by providing readers with guidance on next steps or related resources.
Consider adding a paragraph at the end of the conclusion:
## Next Steps Now that you've learned how to create and interact with the SwapToAnyToken contract, consider exploring the following: 1. Implement error handling and event emission in the contract for better monitoring and debugging. 2. Explore integrating this swap functionality into a decentralized application (dApp). 3. Learn about other ZetaChain features and how they can be combined with this swap functionality. For more advanced topics and tutorials, visit our [Developer Documentation](https://www.zetachain.com/docs/developers/).This addition provides readers with direction for further learning and application of the concepts covered in the tutorial.
🧰 Tools
🪛 LanguageTool
[uncategorized] ~300-~300: Possible missing comma found.
Context: ...ZRC-20 to the recipient In the command above thewithdraw
istrue
, so the target...(AI_HYDRA_LEO_MISSING_COMMA)
[uncategorized] ~312-~312: Possible missing comma found.
Context: ...66 --withdraw false ``` In the command above thewithdraw
is `true`, so the target...(AI_HYDRA_LEO_MISSING_COMMA)
src/pages/developers/tutorials/swap.mdx (3)
269-271
: Deployment commands updated appropriately.The change to
npx hardhat
commands is consistent with modern Node.js project practices. The--force
flag for compilation ensures a clean build, which is a good practice.Consider adding a brief comment explaining the purpose of the
--force
flag for clarity:-npx hardhat compile --force +# Ensure a clean build by forcing recompilation +npx hardhat compile --force
294-297
: Swap command updated with new receiver address.The updated command reflects changes in the contract deployment or structure, which is necessary for the tutorial's accuracy.
To improve flexibility and reduce the need for updates, consider using a placeholder or variable for the
--receiver
address:-npx hardhat swap-from-evm --network localhost --receiver 0xE6E340D132b5f46d1e472DebcD681B2aBc16e57E --amount 1 --target 0x9fd96203f7b22bCF72d9DCb40ff98302376cE09c --recipient 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266 +npx hardhat swap-from-evm --network localhost --receiver <SWAP_CONTRACT_ADDRESS> --amount 1 --target 0x9fd96203f7b22bCF72d9DCb40ff98302376cE09c --recipient 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266Then, add a note explaining that
<SWAP_CONTRACT_ADDRESS>
should be replaced with the actual deployed contract address.
Line range hint
328-337
: Updated command and explanation for ERC-20 to gas token swap.The updated command and expanded explanation provide more details about the swap process, enhancing the tutorial's educational value.
To improve flexibility and reduce the need for updates, consider using placeholders or variables for the addresses:
-npx hardhat swap-from-evm --network localhost --receiver 0xE6E340D132b5f46d1e472DebcD681B2aBc16e57E --amount 1 --target 0x2ca7d64A7EFE2D62A725E2B35Cf7230D6677FfEe --recipient 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266 --erc20 0x0DCd1Bf9A1b36cE34237eEaFef220932846BCD82 +npx hardhat swap-from-evm --network localhost --receiver <SWAP_CONTRACT_ADDRESS> --amount 1 --target <TARGET_TOKEN_ADDRESS> --recipient <RECIPIENT_ADDRESS> --erc20 <SOURCE_ERC20_ADDRESS>Then, add a note explaining that these placeholders should be replaced with actual addresses when running the command.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
📒 Files selected for processing (2)
- src/pages/developers/tutorials/swap-any.mdx (2 hunks)
- src/pages/developers/tutorials/swap.mdx (4 hunks)
🧰 Additional context used
🪛 LanguageTool
src/pages/developers/tutorials/swap-any.mdx
[uncategorized] ~300-~300: Possible missing comma found.
Context: ...ZRC-20 to the recipient In the command above thewithdraw
istrue
, so the target...(AI_HYDRA_LEO_MISSING_COMMA)
[uncategorized] ~312-~312: Possible missing comma found.
Context: ...66 --withdraw false ``` In the command above thewithdraw
is `true`, so the target...(AI_HYDRA_LEO_MISSING_COMMA)
🔇 Additional comments (3)
src/pages/developers/tutorials/swap.mdx (3)
280-280
: Contract address updated in example output.The updated contract address reflects a new deployment, which is appropriate for maintaining the accuracy of the tutorial.
311-317
: Improved explanation of contract execution flow.The updated explanation provides a clearer and more concise description of how ZetaChain handles the cross-chain call and executes the
onCrossChainCall
function. This enhancement improves the overall clarity of the tutorial.
343-346
: Improved explanation of swap execution and withdrawal process.The updated explanation provides a clearer and more concise description of how the Swap contract handles the cross-chain call, decodes the message, and initiates the swap logic. The withdrawal process is also accurately described. These improvements enhance the overall clarity and accuracy of the tutorial.
@brewmaster012 @bbbeeeee @zeta-chain/fullstack please, review. |
Updated example commands to match the updated Swap example.
Depends on zeta-chain/example-contracts#203
Summary by CodeRabbit
New Features
SwapToAnyToken
and cross-chain swap contracts, allowing users to swap tokens with options for withdrawal or retention on ZetaChain.swap
function for direct interactions on ZetaChain.Documentation