Skip to content

feat: UnionTransfer EVM library #323

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

Open
wants to merge 12 commits into
base: main
Choose a base branch
from

Conversation

keyleu
Copy link
Contributor

@keyleu keyleu commented Apr 18, 2025

First UnionTransfer library (EVM side). Next one will be the CosmWasm one.

This library is slightly more complicated than other bridges due to the amount of parameters required and how some of these are encoded. The docs try to provide examples of how all parameters must be passed and also include links to all relevant sections.

As usual, e2e tests against the real protocol and unit tests to check all validations.

Summary by CodeRabbit

  • New Features

    • Introduced the Union Transfer library and contract for EVM, enabling cross-chain token transfers using the Union UCS03-ZKGM protocol.
    • Added support for the Union Transfer variant in the encoder, including message encoding and configuration options.
    • Provided a new Solidity script and interface for interacting with Union Transfer operations.
  • Documentation

    • Added comprehensive documentation for the Union Transfer library, including usage, configuration, and protocol details.
  • Tests

    • Implemented extensive test suites for the Union Transfer contract and encoder, covering configuration, transfer execution, and data compatibility.

Copy link
Contributor

coderabbitai bot commented Apr 18, 2025

Walkthrough

This update introduces the new "UnionTransfer" EVM library variant across Rust encoder code, Solidity contracts, and documentation. It adds the union_transfer module with configuration structs, encoding logic, and message handling in Rust, and exposes corresponding Solidity contracts and interfaces to enable cross-chain token transfers using the Union UCS03-ZKGM protocol. The documentation is extended with a detailed guide and configuration reference. Supporting encoder utilities and Solidity type definitions are included. Comprehensive tests verify configuration validation, encoding correctness, and transfer execution. A Solidity script demonstrates deployment, configuration, and transfer scenarios on an Ethereum mainnet fork.

Changes

File(s) Change Summary
contracts/encoders/evm-encoder/src/lib.rs, contracts/encoders/evm-encoder/src/libraries/mod.rs Added UnionTransfer as a new EVM library variant and module; extended encoder logic to support it.
contracts/encoders/evm-encoder/src/libraries/union_transfer.rs Implemented encoding logic, configuration struct, and function message enum for UnionTransfer in Rust.
contracts/encoders/evm-encoder/src/tests.rs Added test verifying interoperability between CosmWasm and Alloy 256-bit unsigned integer types.
docs/src/SUMMARY.md, docs/src/libraries/evm/union_transfer.md Added documentation entry and detailed guide for the UnionTransfer library, including config struct and usage.
packages/encoder-utils/src/libraries/mod.rs, packages/encoder-utils/src/libraries/union_transfer/mod.rs Added and exposed the union_transfer module and its solidity_types submodule for encoder utilities.
packages/encoder-utils/src/libraries/union_transfer/solidity_types.rs Defined Solidity struct and external function interface for UnionTransfer configuration and transfer.
solidity/script/UnionTransfer.script.sol Added demonstration script for deploying, configuring, and testing UnionTransfer contracts on Ethereum fork.
solidity/src/libraries/UnionTransfer.sol Added main UnionTransfer Solidity contract with configuration validation, update, and transfer logic.
solidity/src/libraries/interfaces/union/IUnion.sol Defined interface for Union protocol interactions, including Instruction and FungibleAssetOrder structs.
solidity/test/libraries/UnionTransfer.t.sol Added comprehensive test suite covering configuration validation, transfer scenarios, and data decoding.
solidity/test/libraries/IBCEurekaTransfer.t.sol Modified sourceClient string value in test setup (minor update).

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant Processor
    participant UnionTransfer (Library/Contract)
    participant zkGM (IUnion)
    participant Recipient

    User->>Processor: Initiate transfer request
    Processor->>UnionTransfer: Call transfer(quoteAmount)
    UnionTransfer->>UnionTransfer: Validate config and amounts
    UnionTransfer->>zkGM: Approve tokens and send instruction (send)
    zkGM-->>Recipient: Transfer tokens on destination chain
    UnionTransfer-->>Processor: Return transfer result
Loading

Poem

🐇 In bytes and chains, a rabbit hops,
UnionTransfer now in our encoder crops!
Solidity and Rust, hand in paw,
Cross-chain tokens, without a flaw.
Docs and tests, all lined up neat—
This protocol hop is quite the feat!
✨🐰


🪧 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>, please review it.
    • 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 gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @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 using 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 generate sequence diagram to generate a sequence diagram of the changes in this 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.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

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.

@keyleu keyleu requested review from hxrts, vlopes11 and bekauz April 18, 2025 20:09
Copy link
Contributor

@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: 7

🧹 Nitpick comments (7)
packages/encoder-utils/src/libraries/union_transfer/mod.rs (1)

1-1: Expose solidity_types submodule
The file correctly exposes the solidity_types module for Union Transfer. Consider adding a documentation comment above the module declaration to briefly explain its purpose and maintain consistency with other library modules.

packages/encoder-utils/src/libraries/union_transfer/solidity_types.rs (1)

3-22: Well-structured Solidity type definitions for UnionTransfer.

The UnionTransferConfig struct is comprehensive, capturing all necessary parameters for union transfer operations including protocol details, token information, addresses, amounts, and metadata.

The external function definition for transfer is correctly declared with the appropriate parameter.

However, I would recommend adding documentation comments to the struct fields to make their purpose more clear, especially for complex fields like transferTokenUnwrappingPath.

 sol! {
     struct UnionTransferConfig {
+        /// Protocol version for the Union transfer
         uint8 protocolVersion;
+        /// Decimals of the token being transferred
         uint8 transferTokenDecimals;
+        /// Channel ID for the transfer
         uint32 channelId;
+        /// Timeout for the transfer operation in seconds
         uint64 timeout;
+        /// Account that provides input for the transfer
         address inputAccount;
+        /// Zero-knowledge Gateway Manager address
         address zkGM;
+        /// Amount of the transfer token to send
         uint256 amount;
+        /// Amount of the quote token requested in return
         uint256 quoteTokenAmount;
+        /// Path for unwrapping the transfer token (if needed)
         uint256 transferTokenUnwrappingPath;
+        /// Recipient address encoded as bytes
         bytes recipient;
+        /// Transfer token address encoded as bytes
         bytes transferToken;
+        /// Quote token address encoded as bytes
         bytes quoteToken;
+        /// Name of the transfer token
         string transferTokenName;
+        /// Symbol of the transfer token
         string transferTokenSymbol;
     }

     function transfer(uint256 _quoteAmount) external;
 }
solidity/script/UnionTransfer.script.sol (1)

99-103: Use Forge’s assertEq/vm.expectRevert helpers for clearer failures

assert gives no context on failure and is optimised out in some build modes.
Forge provides richer helpers:

-assert(ubbnBalanceBefore - ubbnBalanceAfter == tokenAmount);
+assertEq(
+    ubbnBalanceBefore - ubbnBalanceAfter,
+    tokenAmount,
+    "Incorrect amount transferred"
+);

The same applies to the full‑balance assertion below.
Clear messages speed up debugging when the script is run in CI.

docs/src/libraries/evm/union_transfer.md (1)

68-73: Minor wording & formatting nit

The sentence mixes singular/plural and has an unmatched parenthesis.

- * @param quoteToken The token requested in return on destination chain. Bytes conversion of the token.
- *      For example, the quote Token for WETH on Babylon would be `0x62626e31333030736530767775653737686e36733877706836346579366435357a616634386a72766567397761667371756e636e33653473637373677664`
- *      which bytes conversion of "bbn1300se0vwue77hn6s8wph64ey6d55zaf48jrveg9wafsquncn3e4scssgvd" because WETH is a CW20 token on Babylon.
+ * @param quoteToken Bytes representation of the token requested in return on the destination chain.
+ *      Example (WETH on Babylon):
+ *      `0x6262…73677664`
+ *      (bytes of `"bbn1300se0vwue77hn6s8wph64ey6d55zaf48jrveg9wafsquncn3e4scssgvd"` – WETH is a CW20 token on Babylon).
solidity/src/libraries/UnionTransfer.sol (1)

7-8: Remove unused console import to avoid extra byte‑code and silencing of compiler warnings
console.sol is only helpful while debugging; it is never referenced in this contract.
Removing unused imports slightly reduces deployment size and prevents potential unused‑import linter violations.

-import {console} from "forge-std/src/console.sol";
solidity/test/libraries/UnionTransfer.t.sol (2)

286-289: Variable shadowing: transferTokenDecimals loses the new prefix
The destructuring introduces a local transferTokenDecimals, yet the assertion comment still says “Transfer token decimals should be updated.”
Renaming the variable to newTransferTokenDecimals improves readability and consistency with the other names.


433-474: testDecodeFungibleAssetOrderTest cannot be pure because it emits cheat‑code logs
console.log* introduces external calls, making the function at best view.
Mark it view (or remove the modifier) to satisfy the compiler when optimizations or stricter settings are enabled.

-    function testDecodeFungibleAssetOrderTest() public pure {
+    function testDecodeFungibleAssetOrderTest() public view {
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 150a43a and 76928d2.

📒 Files selected for processing (14)
  • contracts/encoders/evm-encoder/src/lib.rs (3 hunks)
  • contracts/encoders/evm-encoder/src/libraries/mod.rs (1 hunks)
  • contracts/encoders/evm-encoder/src/libraries/union_transfer.rs (1 hunks)
  • contracts/encoders/evm-encoder/src/tests.rs (2 hunks)
  • docs/src/SUMMARY.md (1 hunks)
  • docs/src/libraries/evm/union_transfer.md (1 hunks)
  • packages/encoder-utils/src/libraries/mod.rs (1 hunks)
  • packages/encoder-utils/src/libraries/union_transfer/mod.rs (1 hunks)
  • packages/encoder-utils/src/libraries/union_transfer/solidity_types.rs (1 hunks)
  • solidity/script/UnionTransfer.script.sol (1 hunks)
  • solidity/src/libraries/UnionTransfer.sol (1 hunks)
  • solidity/src/libraries/interfaces/union/IUnion.sol (1 hunks)
  • solidity/test/libraries/IBCEurekaTransfer.t.sol (1 hunks)
  • solidity/test/libraries/UnionTransfer.t.sol (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (3)
  • GitHub Check: solidity contracts
  • GitHub Check: setup-local-ic
  • GitHub Check: build-and-deploy
🔇 Additional comments (13)
solidity/test/libraries/IBCEurekaTransfer.t.sol (1)

26-26: Confirm updated source client ID
The test now uses "cosmoshub-0" instead of the previous "cosmoshub-4". Please verify that this change aligns with the actual client identifier expected by IBCEurekaTransfer and update any related documentation or fixtures if necessary.

docs/src/SUMMARY.md (1)

52-52: Include Union Transfer in EVM libraries summary
The new entry for Union Transfer is correctly inserted under the EVM libraries section and follows the established indentation and ordering.

packages/encoder-utils/src/libraries/mod.rs (1)

12-12: Export union_transfer module
The new pub mod union_transfer; declaration correctly exposes the Union Transfer submodule in encoder-utils. Ensure the corresponding directory libraries/union_transfer and its contents follow the package conventions.

contracts/encoders/evm-encoder/src/libraries/mod.rs (1)

15-15: Register union_transfer in encoder libraries
The addition of pub mod union_transfer; integrates the Union Transfer encoding logic into the EVM encoder’s library registry. Ensure the corresponding union_transfer.rs file exists and adheres to the project's formatting and naming conventions.

contracts/encoders/evm-encoder/src/lib.rs (3)

8-8: Clean import addition for the new UnionTransfer module.

The import has been added in a consistent manner alongside other library imports.


34-34: Good implementation of the UnionTransfer enum variant.

The UnionTransfer variant is appropriately added to the EVMLibrary enum, maintaining the existing pattern.


68-68: Well-structured handler for the UnionTransfer variant.

The implementation follows the established pattern of calling the module's encode function when handling the UnionTransfer variant in the encode_message method.

contracts/encoders/evm-encoder/src/tests.rs (1)

536-571: Excellent test coverage for numeric type conversions.

The test thoroughly validates the interoperability between cosmwasm_std::Uint256 and alloy_primitives::U256, which is crucial for the UnionTransfer library. The test covers multiple important scenarios:

  1. Small integer values
  2. Maximum u128 values
  3. Values larger than u128::MAX
  4. Bidirectional conversion (CosmWasm → Alloy and Alloy → CosmWasm)

This ensures that numeric values are preserved correctly during type conversions used in the encoding process.

solidity/src/libraries/interfaces/union/IUnion.sol (2)

5-35: Well-defined interface for Union protocol integration.

The interface clearly defines the structures needed for interacting with the Union protocol:

  • The Instruction struct with version, opcode, and operand
  • The FungibleAssetOrder with comprehensive token transfer parameters
  • The send function with appropriate parameters

This interface provides a solid foundation for the UnionTransfer library implementation.


1-3:

❓ Verification inconclusive

Ensure proper attribution for external code.

The comment indicates this interface is taken from the Union Labs GitHub repository. While you've included the SPDX license identifier, it's important to ensure you're complying with the original license terms when incorporating external code.

Please verify that:

  1. The Apache-2.0 license is compatible with your project's license
  2. You're meeting all attribution requirements from the original repo
  3. Any modifications to the original code are clearly documented

🏁 Script executed:

#!/bin/bash
# Check if there's a LICENSE file in the repo and examine its contents
find . -name "LICENSE" -type f -exec echo "Found license file:" {} \; -exec cat {} \; -exec echo -e "\n---\n" \;

# Check the original Union repository license if it's public
echo "Checking original Union repository license:"
curl -s https://raw.githubusercontent.com/unionlabs/union/main/LICENSE || echo "Could not fetch license from original repo"

Length of output: 11684


Verify Original Union Labs License and Attribution

The project’s LICENSE is Apache‑2.0 (Timewave Inc.), which is compatible, but we couldn’t fetch Union Labs’ LICENSE at the provided URL. Please confirm and satisfy all Apache‑2.0 requirements for the external code:

  • Confirm Union Labs’ original repo is under Apache‑2.0 (or a compatible license) and fetch its LICENSE/NOTICE files.
  • Include any required NOTICE text from Union Labs in your distribution (e.g., in a NOTICE file or documentation).
  • In solidity/src/libraries/interfaces/union/IUnion.sol, retain the original copyright header
    and add a prominent “Modified by Timewave Inc.” notice if you’ve changed the code.
  • Document all modifications to the interface (either in-file comments or in your CHANGELOG).
solidity/script/UnionTransfer.script.sol (1)

48-54: ⚠️ Potential issue

abi.encodePacked loses type information – use bytes20 or bytes literal

abi.encodePacked(UBBN_ADDR) produces a variable‑length bytes whose runtime length must be inferred.
Because UnionTransferConfig.transferToken ultimately expects the raw 20‑byte address without extra ABI length‑prefixing, it is safer to cast directly:

-bytes memory transferToken = abi.encodePacked(UBBN_ADDR);
+bytes memory transferToken = abi.encode(bytes20(UBBN_ADDR));

(or expose bytes20 in the struct).
This avoids silent mis‑encoding if additional data is ever appended.

Likely an incorrect or invalid review comment.

contracts/encoders/evm-encoder/src/libraries/union_transfer.rs (1)

66-72: 🛠️ Refactor suggestion

unwrap_or_default() breaks the “use‑config‑value” contract

Spec dictates: “If quote_amount is not passed, fall back to the configured value”.
By defaulting to zero, the encoded call will always override the config with 0, silently changing semantics when the config’s quoteTokenAmount ≠ 0.

Proposed fix:

-    let transfer_call = transferCall {
-        _quoteAmount: alloy_primitives::U256::from_be_bytes(
-            quote_amount.unwrap_or_default().to_be_bytes(),
-        ),
-    };
+    let amount = quote_amount
+        .map(|v| v.to_be_bytes())
+        .unwrap_or([0u8; 32]); // leave config un‑altered (contract interprets 0 as “use‑config”)
+
+    let transfer_call = transferCall {
+        _quoteAmount: alloy_primitives::U256::from_be_bytes(amount),
+    };

Likely an incorrect or invalid review comment.

solidity/src/libraries/UnionTransfer.sol (1)

199-207: Use safeApprove pattern or reset allowance to 0 before re‑approving
Some ERC‑20 tokens (e.g. USDT, KNC) require the allowance to be set to 0 before it can be changed.
If _config.amount varies between transfers, the second call may revert on such tokens.
Either reset to 0 first, or use SafeERC20.forceApprove from OpenZeppelin.

Copy link
Contributor

@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: 2

♻️ Duplicate comments (1)
solidity/test/libraries/UnionTransfer.t.sol (1)

23-24: Mock zkGM still points to an address with no code – success-path tests will revert

zkGMAddress is set to address(3) and later cast to the IUnion interface, but no stub contract is deployed at that location. Any call that ultimately bubbles into IUnion.send (or similar) will fail when UnionTransfer.transfer() executes, breaking testTransferSucceeds* and related tests.

Re-use Foundry cheat codes to deploy / mock the call instead:

-    address zkGMAddress = address(3);
-    IUnion mockZkGM;
+    IUnion mockZkGM;
+
+    /// @dev Minimal stub that always returns true for `send()`
+    contract ZkGMStub {
+        function send(bytes calldata) external returns (bool) {
+            return true;
+        }
+    }
...
-        mockZkGM = IUnion(zkGMAddress);
+        mockZkGM = IUnion(address(new ZkGMStub()));

Tagging as a duplicate of the earlier review because the underlying issue remains.

Also applies to: 41-42

🧹 Nitpick comments (1)
solidity/test/libraries/UnionTransfer.t.sol (1)

71-262: Test suite contains heavy duplication – extract a helper to build configs

Every negative-path test recreates almost identical UnionTransferConfig structs, changing only one field. Extracting a private _baseConfig() factory and cloning it with modified fields will cut ~200 lines, improve readability and ease future updates when the struct changes.

Example:

function _baseConfig() internal view returns (UnionTransfer.UnionTransferConfig memory cfg) {
    cfg = UnionTransfer.UnionTransferConfig({
        amount: 1000,
        inputAccount: inputAccount,
        recipient: recipient,
        protocolVersion: 1,
        zkGM: mockZkGM,
        transferToken: transferToken,
        transferTokenName: "TEST",
        transferTokenSymbol: "TEST",
        transferTokenDecimals: 18,
        transferTokenUnwrappingPath: 1,
        quoteToken: quoteToken,
        quoteTokenAmount: 980,
        channelId: channelId,
        timeout: timeout
    });
}

Then each revert test becomes a three-liner:

UnionTransfer.UnionTransferConfig memory cfg = _baseConfig();
cfg.zkGM = IUnion(address(0));
vm.expectRevert("zkGM can't be zero address");
unionTransfer.updateConfig(abi.encode(cfg));
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Lite

📥 Commits

Reviewing files that changed from the base of the PR and between 01de34f and a3f6fe6.

📒 Files selected for processing (1)
  • solidity/test/libraries/UnionTransfer.t.sol (1 hunks)
🔇 Additional comments (1)
solidity/test/libraries/UnionTransfer.t.sol (1)

263-317: Nice coverage on the happy-path configuration update

The assertions comprehensively verify every field exposed by config(), ensuring that encoding/decoding round-trips correctly after mutation.

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.

1 participant