Skip to content

Conversation

@Supa-mega
Copy link

Cross-Shard Liquidity Borrowing Implementation

Overview

This PR implements cross-shard liquidity borrowing functionality in the lending protocol example, allowing users to borrow tokens from lending pools across different shards when their local shard has insufficient liquidity.

Key Changes

  • Added cross-shard lending pool registration system
  • Implemented liquidity checking across all registered lending pools
  • Added cross-shard token transfer capabilities
  • Enhanced borrow flow to support cross-shard operations

Technical Implementation Details

Lending Pool Registration

  • Added registerLendingPool() function to register lending pools from different shards
  • Implemented mappings and arrays to track lending pools across shards
  • Added validation to prevent duplicate pool registrations

Cross-Shard Liquidity Management

  • Implemented checkCrossShardLiquidity() to query available liquidity across shards
  • Added handleLiquidityCheck() to process responses from liquidity queries
  • Created transferCrossShardLiquidity() to handle token transfers between shards
  • Added getTokenBalance() utility function for querying pool balances

Enhanced Borrowing Flow

  • Modified borrow() function to check local liquidity first, then query other shards
  • Implemented request ID system to prevent duplicate loan processing
  • Updated loan processing to handle cross-shard scenarios
  • Enhanced finalization process to support cross-shard token transfers

Testing

  • Tested cross-shard liquidity queries
  • Verified correct token transfers between shards
  • Validated loan recording in GlobalLedger
  • Confirmed proper handling of insufficient liquidity scenarios

Security Considerations

  • Added validation checks for pool addresses
  • Implemented request tracking to prevent duplicate processing
  • Added proper error handling for cross-shard communication failures
  • Ensured accurate accounting across shards

Documentation

  • Added inline documentation for new functions
  • Updated function documentation to reflect cross-shard capabilities

Related Issues

Closes #611

@0xAleksaOpacic 0xAleksaOpacic added the ODHack12 Tag for issues which are suitable for ODHack week label Mar 26, 2025
Copy link
Contributor

@gitshreevatsa gitshreevatsa left a comment

Choose a reason for hiding this comment

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

Thanks for opening the PR, there are some changes that have to be addressed.

Copy link
Contributor

@gitshreevatsa gitshreevatsa left a comment

Choose a reason for hiding this comment

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

Please use rebasing, instead of merges. You can check how to do it here

@Supa-mega Supa-mega force-pushed the feat/cross-shard-lending branch from b0e8a84 to e21535a Compare March 28, 2025 08:10
@Supa-mega Supa-mega requested a review from gitshreevatsa March 28, 2025 19:47
@gitshreevatsa
Copy link
Contributor

Hey! Your implementation is very good. I tried to test the code. During borrowing, I don't see a change in the balance of the account 1. It stays the same before and after the borrow. Can you please check that once?

@Supa-mega
Copy link
Author

Logging the transaction receipt for borrow shows that it fails in CrossShardTransaction: tokenBalance

Screenshot 2025-04-13 at 5 17 11 AM Screenshot 2025-04-13 at 5 18 06 AM

Any idea why this fails?

@ikemHood
Copy link
Contributor

Refactor: Centralize Lending Logic and Liquidity in GlobalLedger

  • Centralized Hub: The GlobalLedger contract now holds all protocol liquidity (deposits/collateral) and manages the core state for loans and collateral balances. It executes the primary logic for deposits, borrows, and repayments.

  • Shard Entry Points: LendingPool contracts are now simplified, stateless entry points on each shard. They forward user requests and orchestrate necessary calls to GlobalLedger, Oracle, and InterestManager.

  • Deployment: GlobalLedger can now deploy and register LendingPool contracts across shards.

Fixes: Addresses issues with token balance updates in the previous distributed model by ensuring all fund movements originate from the central GlobalLedger.

Screenshot 2025-04-13 at 5 54 48 PM

Copy link
Contributor

@gitshreevatsa gitshreevatsa left a comment

Choose a reason for hiding this comment

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

This implementation has the GlobalLedger as the owner/storage of funds and lending pool acting as proxies for interacting. The previous implementation was more accurate to the issue. I understahnd you had an issue of Cross shard transfer failure due to token balance. As discussed in the offfice hours, please change it to previous implementation and update it in the PR. I will take a look at why it is failing.

@ukorvl
Copy link
Contributor

ukorvl commented May 24, 2025

@gitshreevatsa @Supa-mega What is the status? Can we proceed with this patch or should close?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ODHack12 Tag for issues which are suitable for ODHack week

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[academy] Implement a cross shard liquidity borrowing in the lending protocol example

5 participants