can you add time based staking elements to the score of this smart contract?…
- User
can you add time based staking elements to the score of this smart contract?
//SPDX-License-Identifier: MIT pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
contract LP_LEADERBOARD {
mapping(address => mapping(address => uint256)) public userLockedBalances; mapping(uint256 => address) public userIdToAddress; mapping(address => uint256) public addressToUserId; uint256 public userIdCounter; address[] public LPTokens; struct userScore { address user; uint256 score; } constructor(address[] memory _LPTokens){ LPTokens = _LPTokens; } function isLPToken(address _token) public view returns(bool isToken){ address[] memory LPTokens_ = LPTokens; uint256 arrLength = LPTokens_.length; for(uint i; i < arrLength; i++){ if(LPTokens_[i] == _token){ isToken = true; } } } // Lock LP function depositLP(address _token, uint256 _amount) public { require(isLPToken(_token), "Not an approved token"); require(_amount > 0, "deposit amount must be more than 0"); if(addressToUserId[msg.sender] != 0){ require(IERC20(_token).transferFrom(msg.sender, address(this), _amount), "tx failed"); userLockedBalances[msg.sender][_token] += _amount; } else { require(IERC20(_token).transferFrom(msg.sender, address(this), _amount), "tx failed"); uint256 userIdCounter_ = ++userIdCounter; userLockedBalances[msg.sender][_token]+=_amount; addressToUserId[msg.sender] = userIdCounter_; userIdToAddress[userIdCounter_] = msg.sender; } } // Remove LP function withdrawLP(address _token, uint256 _amount) public { require(_amount > 0, "deposit amount must be more than 0"); require(_amount <= userLockedBalances[msg.sender][_token], "user does not have that many locked up"); require(IERC20(_token).transfer(msg.sender, _amount),"transfer failed"); userLockedBalances[msg.sender][_token] -= _amount; } function getIndividualScore() public view returns (uint256){ uint256 totalScore; for(uint i; i < LPTokens.length; i++){ address token = LPTokens[i]; totalScore += userLockedBalances[msg.sender][token]; } return totalScore; } function getLeaderboard() public view returns (userScore[] memory) { userScore[] memory scores = new userScore[](userIdCounter); for (uint256 i = 1; i <= userIdCounter; i++) { address user = userIdToAddress[i]; uint256 totalScore; for (uint256 j = 0; j < LPTokens.length; j++) { address token = LPTokens[j]; totalScore += userLockedBalances[user][token]; } scores[i - 1] = userScore(user, totalScore); } // Sort scores in descending order for (uint256 i = 0; i < userIdCounter - 1; i++) { for (uint256 j = 0; j < userIdCounter - i - 1; j++) { if (scores[j].score < scores[j + 1].score) { userScore memory temp = scores[j]; scores[j] = scores[j + 1]; scores[j + 1] = temp; } } } return scores; } - Assistant
- User
so if someone removes their stake, the score would be 0
- Assistant
Yes, you're correct. In the current implementation, as soon as someone withdraws their stake, the user's locked balance becomes zero and that would result in a score of zero. If you want to maintain the user's historical score even after they withdraw their stake, you might consider creating a mapping to store the historical scores for users.