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