Tutorial: Liquid Stake Your Assets with the Magma Protocol SDK
Source: Dev.to
Step 1: Understanding the Magma Liquid Staking Flow
The core concept is simple:
- User deposits a PoS asset (e.g., ETH) into the Magma smart contract.
- The protocol stakes the asset with its network of validator nodes.
- The user receives a liquid token (e.g.,
mETH) back in their wallet. - The
mETHtoken automatically accrues staking rewards in its value. - The user can redeem
mETHat any time for the original ETH plus accrued rewards.
Step 2: Integrating the SDK
Below is a high‑level example for a JavaScript environment using Ethers.js and the (hypothetical) Magma SDK.
import { ethers } from "ethers";
import { magmaSDK } from "@magmaprotocol/sdk"; // Hypothetical SDK
// Connect to the user's wallet
const provider = new ethers.providers.Web3Provider(window.ethereum);
const signer = provider.getSigner();
// Initialize the Magma SDK
const magma = new magmaSDK(signer);
async function stakeAndReceiveLiquidToken(amount) {
// Convert the amount of ETH to wei
const stakeAmount = ethers.utils.parseEther(amount);
try {
console.log(`Staking ${amount} ETH...`);
// The core 'stake' function handles the transaction
const tx = await magma.stake({ value: stakeAmount });
await tx.wait();
const mTokenBalance = await magma.getLiquidTokenBalance();
console.log(`Success! You now have ${mTokenBalance} mETH.`);
console.log(
"This token represents your staked position and earns rewards."
);
} catch (err) {
console.error("Staking failed:", err);
}
}
// Example usage
stakeAndReceiveLiquidToken("1.0");
The SDK abstracts away the complexity of interacting directly with the staking pool contracts and the underlying staking logic, allowing your dApp to offer staking as a feature without forcing users to lock up their capital.
For detailed API references, contract addresses, and advanced features, consult the official Magma Protocol documentation.