Skip to content
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

feat: added ability to swap token on and from ZetaChain #186

Merged
merged 5 commits into from
Aug 12, 2024

Conversation

fadeev
Copy link
Member

@fadeev fadeev commented Jun 27, 2024

Extracting the swap logic into a separate function makes it easy to add a set of additional swap features:

  • swapping between tokens on ZetaChain (by making a call to the contract on ZetaChain directly).
  • swap a token on ZetaChain and withdraw to a connected chain. Let's say you have ZRC-20 ETH already on ZetaChain and you want to withdraw BNB.

Summary by CodeRabbit

  • New Features

    • Introduced new functions to facilitate token swapping operations and external payable transactions.
    • Added error handling for failed transfers.
  • Refactor

    • Refactored internal logic for better code organization.
  • Improvements

    • Enhanced token swap operations with new parameters for better flexibility and control.

@fadeev fadeev marked this pull request as ready for review June 28, 2024 05:17
@fadeev
Copy link
Member Author

fadeev commented Jun 28, 2024

@andresaiello please, review.

@fadeev
Copy link
Member Author

fadeev commented Jul 4, 2024

@andresaiello please, review.

Copy link

coderabbitai bot commented Jul 17, 2024

Walkthrough

The recent updates enhance token swapping functionality in the Swap and SwapToAnyToken smart contracts. Key changes include parameter updates for swapAndWithdraw, new internal and public swap functions, and a receive() function to handle payable transactions. The new Hardhat task script swap.ts facilitates token swaps by interacting with the Swap contract on ZetaChain, validating parameters, and managing token approvals and transfers.

Changes

File Change Summary
.../Swap.sol Updated swapAndWithdraw function to use targetToken parameter consistently and accept new parameters.
.../SwapToAnyToken.sol Added error TransferFailed() declaration; receive() function; refactored onCrossChainCall; introduced swap functions.
.../tasks/swap.ts Introduced a task for interacting with the Swap contract: validates parameters, manages token approvals, and facilitates swaps.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant Hardhat
    participant SwapContract
    participant SwapToAnyTokenContract
    participant ERC20TokenContract

    User->>Hardhat: Initiate swap task
    Hardhat->>SwapContract: Validate parameters
    Hardhat->>ERC20TokenContract: Approve tokens
    ERC20TokenContract-->>Hardhat: Approval complete
    Hardhat->>SwapContract: Execute swapAndWithdraw
    SwapContract->>SwapToAnyTokenContract: Call swapAndWithdraw
    SwapToAnyTokenContract->>ERC20TokenContract: Perform swap
    ERC20TokenContract-->>SwapToAnyTokenContract: Swap complete
    SwapToAnyTokenContract-->>SwapContract: SwapAndWithdraw complete
    SwapContract-->>Hardhat: Swap task complete
    Hardhat-->>User: Swap successful
Loading

Poem

Across the chains, the tokens fly,
In contracts' code, they now comply.
With swaps anew and flows refined,
Transactions swift, no fees confined.
In Rabbit's world of tokens fair,
A dance of bytes, a seamless pair.
Swap and trade, with ease we share.


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?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

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 as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Comment on lines 117 to 127
function swap(
address inputToken,
uint256 amount,
address targetToken,
bytes memory recipient,
bool withdraw
) public {
IZRC20(inputToken).transferFrom(msg.sender, address(this), amount);

swapAndWithdraw(inputToken, amount, targetToken, recipient, withdraw);
}

Check failure

Code scanning / Slither

Unchecked transfer High

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 3

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between e1a2ae4 and 2682ea8.

Files selected for processing (3)
  • omnichain/swap/contracts/Swap.sol (1 hunks)
  • omnichain/swap/contracts/SwapToAnyToken.sol (3 hunks)
  • omnichain/swap/tasks/swap.ts (1 hunks)
Additional context used
GitHub Check: Slither
omnichain/swap/contracts/SwapToAnyToken.sol

[failure] 117-127: Unchecked transfer
SwapToAnyToken.swap(address,uint256,address,bytes,bool) (contracts/SwapToAnyToken.sol#117-127) ignores return value by IZRC20(inputToken).transferFrom(msg.sender,address(this),amount) (contracts/SwapToAnyToken.sol#124)

Additional comments not posted (1)
omnichain/swap/contracts/Swap.sol (1)

48-77: Ensure consistent token handling and gas optimization in swapAndWithdraw.

The swapAndWithdraw function has been updated to use targetToken consistently and introduces a new parameter recipient. The logic for handling gas fees and token swaps appears sound, but ensure that the gas fee calculations and token approvals are optimized to prevent unnecessary costs or vulnerabilities.

Consider verifying the gas fee logic and token approval steps to ensure they align with the expected behavior and security standards. Additionally, ensure that the recipient parameter is validated to prevent potential security issues.

Tools
GitHub Check: Slither

[warning] 48-78: Unused return
Swap.swapAndWithdraw(address,uint256,address,bytes) (contracts/Swap.sol#48-78) ignores return value by IZRC20(targetToken).withdraw(recipient,outputAmount) (contracts/Swap.sol#77)


[warning] 48-78: Unused return
Swap.swapAndWithdraw(address,uint256,address,bytes) (contracts/Swap.sol#48-78) ignores return value by IZRC20(gasZRC20).approve(targetToken,gasFee) (contracts/Swap.sol#76)

omnichain/swap/tasks/swap.ts Show resolved Hide resolved
omnichain/swap/tasks/swap.ts Show resolved Hide resolved
omnichain/swap/contracts/SwapToAnyToken.sol Outdated Show resolved Hide resolved
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 2682ea8 and b922d4c.

Files selected for processing (1)
  • omnichain/swap/contracts/SwapToAnyToken.sol (4 hunks)
Files skipped from review as they are similar to previous changes (1)
  • omnichain/swap/contracts/SwapToAnyToken.sol

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between b922d4c and 0fdec54.

Files selected for processing (1)
  • omnichain/swap/contracts/SwapToAnyToken.sol (4 hunks)
Files skipped from review as they are similar to previous changes (1)
  • omnichain/swap/contracts/SwapToAnyToken.sol

Copy link

@Drowzilla Drowzilla left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So I had zetachain (mainnet) on Atomic's wallet and I wasn't aware that I couldn't send directly to my Coinbase account which is. Er20 or something like that so I never recived it can anyone help me please

@fadeev fadeev merged commit dfbf923 into main Aug 12, 2024
12 checks passed
@fadeev fadeev deleted the swap-from-zetachain branch August 12, 2024 18:44
@coderabbitai coderabbitai bot mentioned this pull request Oct 14, 2024
@Drowzilla

This comment was marked as spam.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants