DEV Community

lilian
lilian

Posted on

Tutorial: How to Build a DeFi App on a Bitcoin L2 Using Liquid Staked BTC

The arrival of Bitcoin L2s is exciting, but the real innovation comes from the new assets we can build with. Let's explore how to use a liquid restaking token from a protocol like Lorenzo in a hypothetical DeFi application.

Step 1: Understand the Asset: stBTC
The Lorenzo Protocol allows users to stake BTC and receive stBTC. This is a yield-bearing token. Its value continuously accrues the Staking Rewards from the underlying BTC being used to secure other networks. For a developer, stBTC is a form of interest-bearing collateral.

Step 2: Conceptual dApp: A Collateralized Lending Protocol
Imagine a lending dApp on a Bitcoin L2 that speaks EVM. You can create a money market that accepts stBTC as collateral.

Here's a simplified Solidity interface:

solidity
interface IStakedBTC {
// Returns the current value of 1 stBTC in BTC (sats)
// This value increases over time as it accrues rewards.
function exchangeRate() external view returns (uint256);

// Standard ERC20 functions
function balanceOf(address account) external view returns (uint256);
function transfer(address recipient, uint256 amount) external returns (bool);
Enter fullscreen mode Exit fullscreen mode

}

contract LendingProtocol {
IStakedBTC public stBTC;

mapping(address => uint256) public collateral_stBTC;

constructor(address _stBTC_address) {
    stBTC = IStakedBTC(_stBTC_address);
}

function depositCollateral(uint256 amount) external {
    // User deposits stBTC into our protocol
    stBTC.transferFrom(msg.sender, address(this), amount);
    collateral_stBTC[msg.sender] += amount;
}

function calculateCollateralValue(address user) public view returns (uint256) {
    uint256 userBalance = collateral_stBTC[user];
    uint256 rate = stBTC.exchangeRate();

    // Calculate the user's collateral value in BTC (sats)
    return userBalance * rate / 1e18; // Normalize
}
Enter fullscreen mode Exit fullscreen mode

}
By integrating stBTC, your lending protocol allows users to borrow stablecoins or other assets against their yield-earning Bitcoin, unlocking immense capital efficiency.

The system relies on the underlying Lorenzo Node network to manage the delegations and report the rewards, which is reflected in the exchangeRate. For detailed architecture and integration specs, the official community documentation is the definitive Guide.

Top comments (0)