Skip to content

Step 1 - Writing the contract

// SPDX-License-Identifier: Unlicense
pragma solidity ^0.8.13;
contract StakingContract {
uint256 public totalStaked;
mapping(address => uint256) public stakedBalances;
function stake(uint256 amount) public payable {
require(amount > 0, "Amount must be greater than 0");
require(msg.value == amount, "Amount must be equal to msg.value");
totalStaked += amount;
stakedBalances[msg.sender] += amount;
}
function unstake(uint256 amount) public payable {
require(amount <= stakedBalances[msg.sender], "Not enough balance");
totalStaked -= amount;
stakedBalances[msg.sender] -= amount;
payable(msg.sender).transfer(amount);
}
}

https://sepolia.etherscan.io/tx/0x897ada77f33c0b76a16cf996b21c74f28d2702f8c8141ca8607ce4a53e7d53aa

ABI

[
{
"inputs": [
{
"internalType": "uint256",
"name": "amount",
"type": "uint256"
}
],
"name": "stake",
"outputs": [],
"stateMutability": "payable",
"type": "function"
},
{
"inputs": [
{
"internalType": "uint256",
"name": "amount",
"type": "uint256"
}
],
"name": "unstake",
"outputs": [],
"stateMutability": "payable",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "",
"type": "address"
}
],
"name": "stakedBalances",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "totalStaked",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
}
]