In this lesson, we’ll explore the core functionality and design of the CrossChainStaking smart contract, which manages both staking and borrowing within our cross-chain DeFi system.
1. What is CrossChainStaking?
CrossChainStaking is a simple staking and lending smart contract. It allows users to deposit one token as collateral (stake) and borrow another token in return.
📌 Basic flow:
1.The user stakes a supported token (e.g., Wrapped SOL).
2.The contract mints LoanTokens based on a predefined ratio.
3.The user can later repay the LoanTokens to unlock and withdraw their staked assets.
💡 Think of it like a crypto mortgage—you “lock” your assets (e.g., Wrapped SOL) and receive LoanTokens as a loan.
📌 2. Key Features
The CrossChainStaking contract offers two primary functions:
✅ stake() – Stake assets and receive LoanTokens
✅ unstake() – Repay LoanTokens and unlock staked assets
📌 Example:
●Alice stakes 10 SOL
●At a fixed ratio of 1 SOL = 10 LoanTokens, she receives 100 LoanTokens
●Later, when Alice repays 100 LoanTokens, her 10 SOL is returned to her
Note: This is a simplified example for learning purposes. A real-world implementation would involve price oracles and more accurate risk controls.
📌 3. Contract Structure Overview
CrossChainStaking.sol:
1
💡 Users must repay their LoanTokens to unlock and retrieve their original staked collateral.
📌 4. Core Logic
stake() – Stake and Mint LoanTokens
●Users call stake(amount) to deposit tokens
●The contract calculates how many LoanTokens to mint using loanRatio
●LoanTokens are minted and issued to the user as a representation of the loan
unstake() – Repay LoanTokens and Withdraw Collateral
●Users call unstake(amount) to repay LoanTokens
●The contract verifies the repayment and burns the tokens
●Once verified, the user’s original collateral is released
Example
1