-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStakingVault.sol
More file actions
49 lines (42 loc) · 1.52 KB
/
StakingVault.sol
File metadata and controls
49 lines (42 loc) · 1.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// File: StakingVault.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
contract StakingVault is Ownable {
IERC20 public token;
mapping(address => uint256) public stakes;
address[] public stakers;
event Staked(address indexed user, uint256 amount);
event Withdrawn(address indexed user, uint256 amount);
constructor(IERC20 _token) {
token = _token;
}
function stake(uint256 amount) external {
require(amount > 0, "Zero stake");
if (stakes[msg.sender] == 0) stakers.push(msg.sender);
stakes[msg.sender] += amount;
token.transferFrom(msg.sender, address(this), amount);
emit Staked(msg.sender, amount);
}
function withdraw(uint256 amount) external {
require(stakes[msg.sender] >= amount, "Insufficient");
stakes[msg.sender] -= amount;
token.transfer(msg.sender, amount);
emit Withdrawn(msg.sender, amount);
}
function getTopStakers() public view returns (address[5] memory top) {
uint256 len = stakers.length;
address[] memory arr = stakers;
for (uint i = 0; i < len; i++) {
for (uint j = i + 1; j < len; j++) {
if (stakes[arr[j]] > stakes[arr[i]]) {
(arr[i], arr[j]) = (arr[j], arr[i]);
}
}
}
for (uint i = 0; i < 5 && i < len; i++) {
top[i] = arr[i];
}
}
}