Wormhole
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

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