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 a deposit-then-delegate function #24

Merged
merged 14 commits into from
Jun 16, 2024

Conversation

MaxMustermann2
Copy link
Collaborator

@MaxMustermann2 MaxMustermann2 commented Jun 10, 2024

Fixes: #4

  • Add a new function in ILSTRestakingController called depositThenDelegateTo.
  • Implement said function in Bootstrap
  • Implement said function in LSTRestakingController, which is inherited by ClientChainGateway
  • Process the LZ request generated from said function in ExocoreGateway
  • Handle the response from the processing in ClientChainGateway
  • Add a unit test (one each for the bootstrap and the gateway) for the call, making the generateUID function common in tests inheriting from ExocoreDeployer. With unit tests, we verify that (1) deposit is received, (2) delegate is received, and (3) the assets are withdrawn from the user's balance and increased in the vault.

Based on previous discussions, it is assumed that the deposit part does not fail and any failure returned by ExocoreGateway to ClientChainGateway refers to the delegation. This is correctly reflected in the event definition as provided below. Note that the delegatedAmount will be, by definition, equal to the amount that is being deposited since both the steps happen atomically.

    event DepositThenDelegateResult(
        bool indexed delegateSuccess,
        address indexed delegator,
        string indexed delegatee,
        address token,
        uint256 delegatedAmount
    );

Summary by CodeRabbit

  • New Features

    • Introduced the ability to deposit tokens and delegate them to another address in a single transaction.
  • Enhancements

    • Improved the flow for token deposit and delegation to streamline user interactions.
  • Bug Fixes

    • Enhanced security checks during withdrawals to ensure ether balance requirements are met.
  • Tests

    • Added comprehensive tests for the new deposit-then-delegate functionality, ensuring robustness and reliability.

Copy link
Contributor

coderabbitai bot commented Jun 10, 2024

Walkthrough

The update introduces a streamlined way for stakers to deposit and delegate tokens in a single transaction, enhancing user experience by reducing cross-chain communication costs. Changes span several files, incorporating new functions, renaming actions, and updating enums, primarily to support this combined deposit-and-delegate functionality.

Changes

Files Change Summary
src/core/Bootstrap.sol Added depositThenDelegateTo method and updated methods to align with ILSTRestakingController.
src/core/ExocoreGateway.sol Introduced requestDepositThenDelegateTo function, updated function selectors.
src/storage/GatewayStorage.sol Added new actions to the Action enum.
test/foundry/Bootstrap.t.sol Replaced occurrences of MARK_BOOTSTRAP with REQUEST_MARK_BOOTSTRAP.
src/core/ClientChainGateway.sol Added action to _registeredResponseHooks and updated whitelist selectors.
src/core/BaseRestakingController.sol Enhanced _executeAction to handle REQUEST_DEPOSIT_THEN_DELEGATE_TO.
test/mocks/ExocoreGatewayMock.sol Updated Action enum and function calls.
test/mocks/DepositMock.sol Made principleBalances public and added return statement to withdrawFrom.
test/foundry/DepositThenDelegateTo.t.sol Introduced contract and functions to test depositThenDelegateTo functionality.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant ClientChainGateway
    participant ExocoreGateway
    participant Bootstrap
    
    User->>ClientChainGateway: depositThenDelegateTo(token, amount, operator)
    ClientChainGateway->>ExocoreGateway: requestDepositThenDelegateTo()
    ExocoreGateway->>Bootstrap: depositThenDelegateTo(token, amount, operator)
    Bootstrap-->>ExocoreGateway: success
    ExocoreGateway-->>ClientChainGateway: response
    ClientChainGateway-->>User: confirmation
Loading

Assessment against linked issues

Objective Addressed Explanation
Add functions for deposit and delegation in a single call (#4)

Poem

In fields where bunnies leap and play,
Code is refined in a seamless way.
Tokens deposited and delegated in a single go,
Making transactions smooth and user flow.
Efficient changes—swift and bright,
Simplifying tasks with joyous delight! 🌟🐰🚀


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

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 8c0aef0 and 2431f18.

Files selected for processing (14)
  • src/core/Bootstrap.sol (8 hunks)
  • src/core/ClientChainGateway.sol (1 hunks)
  • src/core/ClientGatewayLzReceiver.sol (1 hunks)
  • src/core/ExocoreGateway.sol (2 hunks)
  • src/core/LSTRestakingController.sol (1 hunks)
  • src/interfaces/ILSTRestakingController.sol (1 hunks)
  • src/storage/BootstrapStorage.sol (1 hunks)
  • src/storage/ExocoreGatewayStorage.sol (1 hunks)
  • src/storage/GatewayStorage.sol (1 hunks)
  • test/foundry/Delegation.t.sol (1 hunks)
  • test/foundry/DepositThenDelegateTo.t.sol (1 hunks)
  • test/foundry/DepositWithdrawPrinciple.t.sol (1 hunks)
  • test/foundry/ExocoreDeployer.t.sol (1 hunks)
  • test/foundry/WithdrawReward.t.sol (1 hunks)
Files skipped from review due to trivial changes (4)
  • src/storage/ExocoreGatewayStorage.sol
  • test/foundry/Delegation.t.sol
  • test/foundry/DepositWithdrawPrinciple.t.sol
  • test/foundry/WithdrawReward.t.sol
Additional comments not posted (8)
src/core/LSTRestakingController.sol (1)

40-51: Implementation of depositThenDelegateTo looks solid and well-integrated.

The use of multiple modifiers for validation and the reuse of the _processRequest method are good practices that enhance maintainability and security.

src/interfaces/ILSTRestakingController.sol (1)

50-62: Well-documented addition of depositThenDelegateTo in the interface.

The detailed documentation provides clear expectations for the implementation, which is crucial for maintaining the integrity of the interface contract.

test/foundry/DepositThenDelegateTo.t.sol (1)

44-154: Comprehensive tests for DepositThenDelegateTo functionality.

The tests are well-structured and cover both request and response scenarios comprehensively. Consider verifying the integration with the actual contract to ensure that the tests reflect the real-world behavior accurately.

src/core/ClientGatewayLzReceiver.sol (1)

171-190: The implementation of afterReceiveDepositThenDelegateToResponse correctly handles the response for the combined deposit and delegate action. Ensure that edge cases and error scenarios are thoroughly tested.

src/core/ExocoreGateway.sol (1)

226-252: The implementation of requestDepositThenDelegateTo correctly handles the combined deposit and delegate request. Ensure that error handling and integration with other system components are thoroughly tested.

test/foundry/ExocoreDeployer.t.sol (1)

279-289: The implementation of generateUID correctly generates unique identifiers for transactions. Ensure that this function is used consistently across the system for tracking and auditing purposes.

Verification successful

The generateUID function is used consistently across the system, as evidenced by multiple instances found in the codebase.

Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify consistent usage of generateUID across the system.

# Test: Search for usages of generateUID. Expect: Consistent usage across the system.
rg --type solidity $'generateUID'

Length of output: 65



Script:

#!/bin/bash
# Description: Verify consistent usage of generateUID across the system.

# Find all Solidity files and search for usages of generateUID within them.
fd -e sol -x rg 'generateUID' {}

Length of output: 1081

src/storage/BootstrapStorage.sol (1)

320-335: The new event DepositThenDelegateResult is well-defined and aligns with the PR objectives to handle combined deposit and delegation actions.

src/core/Bootstrap.sol (1)

308-308: The implementation of depositThenDelegateTo in Bootstrap.sol correctly integrates the deposit and delegation processes into a single transaction, which aligns with the PR objectives to improve user experience by reducing transaction costs.

Also applies to: 441-454

src/storage/GatewayStorage.sol Outdated Show resolved Hide resolved
src/core/ClientChainGateway.sol Show resolved Hide resolved
- rename MARK_BOOTSTRAP to REQUEST_MARK_BOOTSTRAP
- move REQUEST_DEPOSIT_THEN_DELEGATE_TO upwards
@ExocoreNetwork ExocoreNetwork deleted a comment from github-actions bot Jun 11, 2024
@ExocoreNetwork ExocoreNetwork deleted a comment from github-actions bot Jun 11, 2024
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: 3

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 2431f18 and c03c7aa.

Files selected for processing (6)
  • src/core/Bootstrap.sol (9 hunks)
  • src/core/ClientChainGateway.sol (2 hunks)
  • src/core/ExocoreGateway.sol (3 hunks)
  • src/storage/GatewayStorage.sol (1 hunks)
  • test/foundry/Bootstrap.t.sol (3 hunks)
  • test/mocks/ExocoreGatewayMock.sol (2 hunks)
Additional comments not posted (5)
src/core/ExocoreGateway.sol (1)

70-71: The addition of the REQUEST_DEPOSIT_THEN_DELEGATE_TO action to _whiteListFunctionSelectors is correctly implemented. This ensures that the new function requestDepositThenDelegateTo can be called internally following the LayerZero messaging pattern.

test/mocks/ExocoreGatewayMock.sol (1)

39-40: The addition of REQUEST_DEPOSIT_THEN_DELEGATE_TO to the Action enum in the mock contract is correctly implemented. This ensures that the mock environment aligns with the changes in the main contract, facilitating accurate testing.

test/foundry/Bootstrap.t.sol (3)

817-817: Ensure the correct encoding of the action in the lzReceive function.

Verification successful

Ensure the correct encoding of the action in the lzReceive function.

The encoding of REQUEST_MARK_BOOTSTRAP appears consistent across the codebase.

  • test/foundry/Bootstrap.t.sol: Multiple instances of abi.encodePacked(GatewayStorage.Action.REQUEST_MARK_BOOTSTRAP, "")
  • src/core/ExocoreGateway.sol: _sendInterchainMsg(uint32(clientChainId), Action.REQUEST_MARK_BOOTSTRAP, "")
  • src/core/Bootstrap.sol: _whiteListFunctionSelectors[Action.REQUEST_MARK_BOOTSTRAP] = this.markBootstrapped.selector
  • src/storage/GatewayStorage.sol: REQUEST_MARK_BOOTSTRAP
  • src/core/ClientChainGateway.sol: delete _whiteListFunctionSelectors[Action.REQUEST_MARK_BOOTSTRAP]
Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify that the encoding of the action REQUEST_MARK_BOOTSTRAP is consistent across all contracts.

# Test: Search for the encoding usage. Expect: Consistent usage across the codebase.
rg --type solidity $'REQUEST_MARK_BOOTSTRAP'

Length of output: 76



Script:

#!/bin/bash
# Description: Verify that the encoding of the action REQUEST_MARK_BOOTSTRAP is consistent across all contracts.

# Test: Search for the encoding usage. Expect: Consistent usage across the codebase.
rg 'REQUEST_MARK_BOOTSTRAP' --glob '*.sol'

Length of output: 1133


847-852: Ensure proper handling of unsupported requests.

Verification successful

The search results indicate that the UnsupportedRequest error is defined and used in multiple places across the codebase, including in the Bootstrap.t.sol file. This suggests that unsupported requests are indeed being handled, typically by reverting the transaction.

  • test/mocks/ExocoreGatewayMock.sol: Defines and reverts UnsupportedRequest.
  • src/storage/GatewayStorage.sol: Defines UnsupportedRequest.
  • src/core/ExocoreGateway.sol: Reverts UnsupportedRequest.
  • src/core/BootstrapLzReceiver.sol: Reverts UnsupportedRequest.
  • src/core/ClientGatewayLzReceiver.sol: Reverts UnsupportedRequest.

These findings confirm that unsupported requests are being handled properly by reverting the transaction with the UnsupportedRequest error.

Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify that unsupported requests are handled correctly across all contracts.

# Test: Search for the handling of unsupported requests. Expect: Proper error handling and reverts.
rg --type solidity $'UnsupportedRequest'

Length of output: 72



Script:

#!/bin/bash
# Description: Verify that unsupported requests are handled correctly across all contracts.

# Test: Search for the handling of unsupported requests. Expect: Proper error handling and reverts.
rg --type sol $'UnsupportedRequest'

Length of output: 62



Script:

#!/bin/bash
# Description: Verify that unsupported requests are handled correctly across all contracts.

# Test: Search for the handling of unsupported requests. Expect: Proper error handling and reverts.
fd -e sol | xargs rg 'UnsupportedRequest'

Length of output: 656


836-836: Check the timing conditions for marking bootstrap.

src/storage/GatewayStorage.sol Show resolved Hide resolved
src/core/ExocoreGateway.sol Outdated Show resolved Hide resolved
src/core/Bootstrap.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: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between c03c7aa and 267b46b.

Files selected for processing (3)
  • src/storage/GatewayStorage.sol (1 hunks)
  • test/foundry/Bootstrap.t.sol (3 hunks)
  • test/mocks/ExocoreGatewayMock.sol (2 hunks)
Files skipped from review as they are similar to previous changes (3)
  • src/storage/GatewayStorage.sol
  • test/foundry/Bootstrap.t.sol
  • test/mocks/ExocoreGatewayMock.sol

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

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 267b46b and b9409a5.

Files selected for processing (3)
  • src/core/Bootstrap.sol (9 hunks)
  • src/core/ExocoreGateway.sol (3 hunks)
  • test/foundry/Bootstrap.t.sol (4 hunks)
Files skipped from review as they are similar to previous changes (3)
  • src/core/Bootstrap.sol
  • src/core/ExocoreGateway.sol
  • test/foundry/Bootstrap.t.sol

@cloud8little
Copy link
Contributor

_processRequest is unchanged that calling depositThenDelegateTo method does not make the vault charge tokens from users.

   function _processRequest(
        address token,
        address sender,
        uint256 amount,
        Action action,
        string memory operator // Optional parameter, empty string when not needed.
    ) internal {
        if (token != VIRTUAL_STAKED_ETH_ADDRESS) {
            IVault vault = _getVault(token);
            if (action == Action.REQUEST_DEPOSIT) {
                vault.deposit(sender, amount); // Logic specific to the REQUEST_DEPOSIT action.
            }
        }
        outboundNonce++;
        bool hasOperator = bytes(operator).length > 0;

        // Use a single abi.encode call via ternary operators to handle both cases.
        _registeredRequests[outboundNonce] =
            hasOperator ? abi.encode(token, operator, sender, amount) : abi.encode(token, sender, amount);

        _registeredRequestActions[outboundNonce] = action;

        // Use a single abi.encodePacked call via ternary operators to handle both cases.
        bytes memory actionArgs = hasOperator
            ? abi.encodePacked(bytes32(bytes20(token)), bytes32(bytes20(sender)), bytes(operator), amount)
            : abi.encodePacked(bytes32(bytes20(token)), bytes32(bytes20(sender)), amount);

        _sendMsgToExocore(action, actionArgs);
    }

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

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between b9409a5 and 2786ef2.

Files selected for processing (4)
  • src/core/BaseRestakingController.sol (1 hunks)
  • test/foundry/Bootstrap.t.sol (4 hunks)
  • test/foundry/DepositThenDelegateTo.t.sol (1 hunks)
  • test/mocks/DepositMock.sol (2 hunks)
Files skipped from review as they are similar to previous changes (1)
  • test/foundry/Bootstrap.t.sol
Additional comments not posted (3)
test/mocks/DepositMock.sol (2)

9-9: Visibility change of principleBalances to public is appropriate for a mock contract used in testing.


32-32: The updated return statement in withdrawPrinciple improves function clarity and usability.

test/foundry/DepositThenDelegateTo.t.sol (1)

46-185: The test implementation in DepositThenDelegateToTest is thorough, covering various important aspects of the deposit-then-delegate functionality, including balance checks and event emissions.

src/core/BaseRestakingController.sol 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: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 2786ef2 and 5190514.

Files selected for processing (1)
  • test/foundry/DepositThenDelegateTo.t.sol (1 hunks)
Files skipped from review as they are similar to previous changes (1)
  • test/foundry/DepositThenDelegateTo.t.sol

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

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 5190514 and 90680cd.

Files selected for processing (1)
  • test/foundry/DepositThenDelegateTo.t.sol (1 hunks)
Files skipped from review as they are similar to previous changes (1)
  • test/foundry/DepositThenDelegateTo.t.sol

@cloud8little
Copy link
Contributor

Test passed with following user scenarioes.
Test object: 90680cd

  1. When the depositor has enough tokens and delegate to one of the operators, tokens are deposited to the vault contract, delegation and assets both update correctly.
  2. When the depositor has enough tokens and delegate to a non-existing operator, tokens are deposited to the vault contract, assets update correctly, delegation of the operator remain the same.
  3. When the depositor has not enought tokens, transaction fail, no tokens deposited.

@MaxMustermann2 MaxMustermann2 merged commit 3bba16b into main Jun 16, 2024
5 checks passed
@cloud8little cloud8little deleted the fix/deposit-then-delegate branch June 17, 2024 07:59
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.

[LST][Feature]: add functions so that staker could deposit and delegate in one time
4 participants