⚛️Cross-chain Logic

Explains how the Cross-Chain Lending Gateway (CLG) uses Stargate to securely execute lending operations across different blockchains.

What is CLG?

The Cross-Chain Lending Gateway (CLG) is a smart contract designed to bridge lending operations between chains using Stargate. It acts as a router and dispatcher, allowing users to interact with Compound-style markets on remote chains.

How CLG Works

When a user wants to perform a cross-chain action (e.g. supply on Ethereum and borrow on Hyperliquid), CLG packages the request and sends it through Stargate.

Stargate handles the bridging of tokens and execution messages, which are then processed on the destination chain by the corresponding CLG instance.

CLG Functions

1. Cross-chain Supply and Borrow

This function allows a user to supply collateral on a remote chain and simultaneously borrow an asset from that chain.

function supplyThenBorrowCrosschain(
    uint16 dstChainId,
    address dstCTokenCollateral,
    address dstCTokenBorrow,
    uint256 mintAmount,
    uint256 borrowAmount,
    address borrower,
    address receiver,
    uint256 lzFee
) external payable

2. Cross-chain Repay

This function enables repayment of a loan on another chain. The caller sends the repayment amount and metadata, and the remote CLG performs the actual repayment.

3. Cross-chain Redeem

This function lets users redeem their collateral from another chain and withdraw the underlying asset locally.

Stargate Integration

Stargate is responsible for two things:

  1. Token bridging: The underlying ERC20 tokens are securely transferred between chains using Stargate’s liquidity network.

  2. Message execution: A payload is encoded and sent with lzSend, received by the destination chain’s lzReceive, and interpreted for execution.

Stargate Send Example

Stargate Receive Logic

The payload includes encoded parameters and the operation type (enum), which the receiving CLG parses and routes to the appropriate internal function.

User Flow

Here is a complete user flow for a cross-chain supplyThenBorrowCrosschain operation:

  1. User calls supplyThenBorrowCrosschain(...) on source chain CLG.

  2. CLG pulls tokens from the user and transfers them to the Stargate Router.

  3. Stargate bridges the tokens and an encoded payload to the destination chain.

  4. Destination CLG receives the payload in lzReceive, decodes it.

  5. CLG supplies collateral to the destination cToken market on behalf of the user.

  6. CLG borrows the requested amount from the borrow market.

  7. The borrowed tokens are transferred to the user’s receiver address on the destination chain.

  8. Events are emitted to reflect the full transaction lifecycle.


Cross-chain Repay (from Origin Chain)

  1. User calls repayBorrowCrosschain(...) on source chain CLG.

  2. CLG pulls the repayment tokens from the user and transfers them to Stargate.

  3. Stargate bridges tokens and an encoded REPAY payload to the destination chain.

  4. Destination CLG receives the payload, decodes it, and performs a repayment on the borrow market for the borrower.

  5. Repayment event is emitted.


Cross-chain Repay (from Destination Chain)

  1. User directly calls the native Compound repayBorrow(...) on the local cToken.

  2. Repayment is processed within the destination chain; CLG is not involved.

Last updated