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: add faucet contract #110

Merged
merged 10 commits into from
Oct 15, 2024

Conversation

MaxMustermann2
Copy link
Collaborator

@MaxMustermann2 MaxMustermann2 commented Oct 14, 2024

The CombinedFaucet contract supports two types of faucets

  • Faucet for native token (address = 0x0000000000000000000000000000000000000000). This needs a backend service running to provide funds to users who may not have any funds, including funds to pay for gas to claim from the faucet.
  • Faucet for ERC20 token (any other address). This faucet does not need a backend service, since users dealing in ERC20 tokens should have enough native tokens to pay for gas.

The former is deployed on Exocore while the latter is deployed on the client chain. The same script was used to deploy both, depending on an environment variable value. However, the addresses aren't yet made public because faucets must be protected with a frontend captcha to avoid Sybil attacks.

Lastly, a script to simulate LZ receive is added for easy testing of messages that may be stuck.

Summary by CodeRabbit

  • New Features

    • Introduced a SimulateReceive contract to simulate message reception in a LayerZero context.
    • Added a DeployScript contract for deploying a faucet contract based on specific conditions.
    • Launched the CombinedFaucet contract for controlled distribution of ERC20 tokens or native Ether.
  • Bug Fixes

    • Enhanced validation processes for transaction data in the SimulateReceive contract.
  • Tests

    • Implemented a comprehensive testing framework for the CombinedFaucet contract, covering both ERC20 and native token functionalities.

Copy link
Contributor

coderabbitai bot commented Oct 14, 2024

Walkthrough

This pull request introduces three new Solidity smart contracts: SimulateReceive, DeployScript, and CombinedFaucet. The SimulateReceive contract simulates message reception in a LayerZero context, deploying necessary mock contracts and processing transaction data from JSON. The DeployScript contract manages the deployment of a faucet contract based on specific conditions, while the CombinedFaucet contract facilitates controlled token distribution. Additionally, unit tests for both ERC20 and native token functionalities are provided to ensure the correctness of the CombinedFaucet contract.

Changes

File Change Summary
script/18_SimulateReceive.s.sol Added SimulateReceive contract with run and addressToBytes32 functions.
script/19_DeployFaucet.s.sol Added DeployScript contract with overridden setUp and run functions for faucet deployment.
src/core/CombinedFaucet.sol Added CombinedFaucet contract with various functions for token distribution and management.
test/foundry/unit/CombinedFaucet.t.sol Added BaseFaucetTest, ERC20FaucetTest, and NativeTokenFaucetTest contracts for testing CombinedFaucet functionality.

Possibly related PRs

Suggested reviewers

  • magj2006
  • cloud8little
  • TimmyExogenous
  • bwhour

Poem

In the meadow where the code does play,
A rabbit hops with joy today.
New contracts bloom, like flowers in spring,
Simulating messages, oh what a thing!
With faucets flowing and tests that gleam,
We code our dreams, a developer's dream! 🐇✨


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between d58883e and c583763.

📒 Files selected for processing (1)
  • script/18_SimulateReceive.s.sol (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • script/18_SimulateReceive.s.sol

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>, 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 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 or @coderabbitai title 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.

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

🧹 Outside diff range and nitpick comments (3)
script/18_SimulateReceive.s.sol (2)

50-54: Clarify the use of the encoded variable

The variable encoded is created using abi.encodeWithSelector but is only used in a debug console.logBytes statement.

If the encoded data is for debugging purposes, consider adding a comment to explain its purpose. If it's no longer needed, you might remove it to clean up the code.

bytes memory encoded = abi.encodeWithSelector(
    IOAppCore(receiver).endpoint().lzReceive.selector, origin, receiver, guid, payload, extraData
);
console.logBytes(encoded);
+// Encoded data for debugging purposes

Or, if unnecessary:

-bytes memory encoded = abi.encodeWithSelector(
-    IOAppCore(receiver).endpoint().lzReceive.selector, origin, receiver, guid, payload, extraData
-);
-console.logBytes(encoded);

57-59: Simplify the addressToBytes32 function

The current implementation correctly converts an address to bytes32, but it can be simplified for readability.

Consider using built-in Solidity functions for clarity:

function addressToBytes32(address _addr) internal pure returns (bytes32) {
-    return bytes32(uint256(uint160(_addr)));
+    return bytes32(uint256(uint160(address(_addr))));
}

Or, since an address occupies the lower 20 bytes of a bytes32, you can shift the address to the correct position:

function addressToBytes32(address _addr) internal pure returns (bytes32) {
    return bytes32(uint256(uint160(_addr)) << 96);
}

This makes the intention of the code clearer to readers.

test/foundry/unit/CombinedFaucet.t.sol (1)

291-296: Relevance of ERC721 rejection test in NativeTokenFaucetTest

While it's useful to test that the faucet rejects ERC721 tokens, consider whether this test is necessary in the context of a native token faucet. If the onERC721Received function is not applicable, it might be more appropriate to test for incorrect calls or misuse relevant to native tokens.

Evaluate if this test should be adjusted or potentially removed to better align with the faucet's functionality.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 25ba9e0 and 6d470f3.

📒 Files selected for processing (4)
  • script/18_SimulateReceive.s.sol (1 hunks)
  • script/19_DeployFaucet.s.sol (1 hunks)
  • src/core/CombinedFaucet.sol (1 hunks)
  • test/foundry/unit/CombinedFaucet.t.sol (1 hunks)
🧰 Additional context used
🔇 Additional comments (3)
test/foundry/unit/CombinedFaucet.t.sol (3)

90-100: Test ownership restrictions more thoroughly

In testOnlyOwnerCanSetTokenAddress, after setting the token address to address(0xdead), it's good practice to verify that the faucet still functions correctly or behaves as expected with the new token address.

You can add assertions or additional test cases to confirm that the faucet's behavior aligns with expectations after the token address is changed.


149-153: Ensure onERC721Received function is implemented in the contract

The test testRejectERC721 is checking the behavior when receiving ERC721 tokens:

faucet.onERC721Received(user1, user2, 1, "");

Verify that the CombinedFaucet contract properly implements the onERC721Received function and handles rejections as intended.

Check the CombinedFaucet contract to ensure the onERC721Received function is present and reverts with the correct message.


32-34: Ensure correct token distribution in tests

In the setUp function, tokens are minted to user1:

token.mint(user1, tokenAmount);

Consider whether it's necessary to mint tokens to user1 for these tests, especially since the faucet is supposed to distribute tokens to users who may not have any. This can help ensure that the tests accurately reflect the intended use case.

To verify if user1 requires an initial balance, you can temporarily remove the minting line and run the tests to see if they pass without it.

script/18_SimulateReceive.s.sol Outdated Show resolved Hide resolved
script/18_SimulateReceive.s.sol Outdated Show resolved Hide resolved
script/19_DeployFaucet.s.sol Outdated Show resolved Hide resolved
script/19_DeployFaucet.s.sol Outdated Show resolved Hide resolved
script/19_DeployFaucet.s.sol Show resolved Hide resolved
test/foundry/unit/CombinedFaucet.t.sol Outdated Show resolved Hide resolved
test/foundry/unit/CombinedFaucet.t.sol Outdated Show resolved Hide resolved
test/foundry/unit/CombinedFaucet.t.sol Outdated Show resolved Hide resolved
test/foundry/unit/CombinedFaucet.t.sol Outdated Show resolved Hide resolved
test/foundry/unit/CombinedFaucet.t.sol Outdated Show resolved Hide resolved
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: 4

🧹 Outside diff range and nitpick comments (6)
test/foundry/unit/CombinedFaucet.t.sol (6)

295-295: Double-check initial balance in testRecoverTokens

The comment states:

// Initially, owner has 10 * `tokenAmount` native tokens

However, since vm.deal(owner, tokenAmount * 10); is called in the setUp function, it's worth verifying that the owner's balance is indeed tokenAmount * 10 before proceeding. Additionally, consider adding an assertion to confirm this initial balance within the test for clarity.

You can add the following assertion:

assertEq(owner.balance, tokenAmount * 10);

This ensures the test explicitly confirms the initial state.


181-181: Confirm the faucet's balance setup

In the setUp function of NativeTokenFaucetTest, you deal tokenAmount * 5 to the faucet:

vm.deal(address(faucet), tokenAmount * 5);

Ensure that this setup correctly aligns with your testing scenarios, especially if subsequent tests depend on the faucet's balance. Consider adding comments to clarify the purpose of this step for future maintainability.


308-312: Update revert message in testRejectERC721

The revert message in testRejectERC721 is:

vm.expectRevert("Faucet: ERC721 tokens not accepted");

For consistency with other error messages in the CombinedFaucet contract, consider updating the message to include "CombinedFaucet" as a prefix.

Apply this diff:

-vm.expectRevert("Faucet: ERC721 tokens not accepted");
+vm.expectRevert("CombinedFaucet: ERC721 tokens not accepted");

Ensure that the CombinedFaucet contract's revert message is updated accordingly.


154-155: Clarify the recovered token amount in the comment

The comment states:

// Owner should recover `tokenAmount` TST tokens

For clarity, specify the exact amount to make it explicit in the comment, enhancing readability.

Update the comment:

-// Owner should recover `tokenAmount` TST tokens
+// Owner should recover 1 TST token (`tokenAmount`)

46-46: Remove redundant comment

The comment:

// Deploy a mock ERC20 token

Is somewhat redundant as the code directly below is self-explanatory. Consider removing or enhancing the comment to provide additional value.

Optionally, you could modify it to:

// Initialize mock ERC20 token for testing

11-37: Consider documenting the BaseFaucetTest contract

Adding documentation comments to the BaseFaucetTest contract can help other developers understand its purpose and how it facilitates the shared setup for both faucet tests.

Consider adding a contract-level comment:

/// @title BaseFaucetTest
/// @notice Provides common setup and utilities for faucet tests
contract BaseFaucetTest is Test {
    // ...
}
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 6d470f3 and 897a085.

📒 Files selected for processing (3)
  • script/19_DeployFaucet.s.sol (1 hunks)
  • src/core/CombinedFaucet.sol (1 hunks)
  • test/foundry/unit/CombinedFaucet.t.sol (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (2)
  • script/19_DeployFaucet.s.sol
  • src/core/CombinedFaucet.sol
🧰 Additional context used

test/foundry/unit/CombinedFaucet.t.sol Show resolved Hide resolved
test/foundry/unit/CombinedFaucet.t.sol Outdated Show resolved Hide resolved
test/foundry/unit/CombinedFaucet.t.sol Show resolved Hide resolved
test/foundry/unit/CombinedFaucet.t.sol Outdated Show resolved Hide resolved
@MaxMustermann2
Copy link
Collaborator Author

See also https://github.com/ExocoreNetwork/exo-faucet which is the backend deployed on our URL.

script/18_SimulateReceive.s.sol Show resolved Hide resolved
Copy link
Collaborator

@bwhour bwhour left a comment

Choose a reason for hiding this comment

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

looks good.

@MaxMustermann2 MaxMustermann2 added this pull request to the merge queue Oct 15, 2024
Merged via the queue into ExocoreNetwork:main with commit 09197a8 Oct 15, 2024
7 checks passed
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