Created
May 5, 2023 22:57
-
-
Save maheshmurthy/44ed42fdf26fc3475101f085c07167f7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SPDX-License-Identifier: MIT | |
pragma solidity ^0.8.19; | |
contract KarmaStats { | |
struct User { | |
uint256 data1; | |
uint256 data2; | |
} | |
mapping(address => mapping(bytes32 => User)) public users; | |
function getUserKey(address tokenAddress, uint256 tokenChainId, uint8 period) public pure returns (bytes32) { | |
return keccak256(abi.encodePacked(tokenAddress, tokenChainId, period)); | |
} | |
function getUserData(address userAddress, address tokenAddress, uint256 tokenChainId) public view returns ( | |
uint8 period, | |
uint16 score, | |
uint16 forumscore, | |
uint16 discordscore, | |
uint16 offchainvotepct, | |
uint16 onchainvotepct, | |
uint16 proposalsCreated, | |
uint32 delegatedvotes | |
) { | |
bytes32 userKey = getUserKey(tokenAddress, tokenChainId, period); | |
User storage user = users[userAddress][userKey]; | |
uint256 data1 = user.data1; | |
score = uint16(data1); | |
forumscore = uint16(data1 >> 16); | |
discordscore = uint16(data1 >> 32); | |
offchainvotepct = uint16(data1 >> 48); | |
onchainvotepct = uint16(data1 >> 64); | |
proposalsCreated = uint16(data1 >> 80); | |
delegatedvotes = uint32(user.data2); | |
} | |
function batchUpdateUsers( | |
address[] memory userAddresses, | |
address[] memory tokenAddresses, | |
uint256[] memory tokenChainIds, | |
uint8[] memory periods, | |
uint16[] memory scores, | |
uint16[] memory forumscores, | |
uint16[] memory discordscores, | |
uint16[] memory offchainvotepcts, | |
uint16[] memory onchainvotepcts, | |
uint32[] memory delegatedVotesList, | |
uint16[] memory proposalsCreatedList | |
) public { | |
uint256 usersLength = userAddresses.length; | |
{ | |
require( | |
usersLength == tokenAddresses.length && | |
usersLength == tokenChainIds.length && | |
usersLength == periods.length && | |
usersLength == scores.length && | |
usersLength == forumscores.length && | |
usersLength == discordscores.length && | |
usersLength == offchainvotepcts.length && | |
usersLength == onchainvotepcts.length && | |
usersLength == delegatedVotesList.length && | |
usersLength == proposalsCreatedList.length, | |
"Input arrays must have the same length" | |
); | |
} | |
for (uint256 i = 0; i < usersLength; i++) { | |
uint256 data1 = ( | |
uint256(scores[i]) | | |
(uint256(forumscores[i]) << 16) | | |
(uint256(discordscores[i]) << 32) | | |
(uint256(offchainvotepcts[i]) << 48) | | |
(uint256(onchainvotepcts[i]) << 64) | | |
(uint256(proposalsCreatedList[i]) << 80) | |
); | |
bytes32 userKey = getUserKey(tokenAddresses[i], tokenChainIds[i], periods[i]); | |
users[userAddresses[i]][userKey] = User({data1: data1, data2: delegatedVotesList[i]}); | |
} | |
} | |
function _getUser(address userAddress, bytes32 userKey) private view returns (User storage) { | |
return users[userAddress][userKey]; | |
} | |
function getScore(address userAddress, address tokenAddress, uint256 tokenChainId, uint8 period) public view returns (uint16) { | |
bytes32 userKey = getUserKey(tokenAddress, tokenChainId, period); | |
return uint16(_getUser(userAddress, userKey).data1); | |
} | |
function getForumScore(address userAddress, address tokenAddress, uint256 tokenChainId, uint8 period) public view returns (uint16) { | |
bytes32 userKey = getUserKey(tokenAddress, tokenChainId, period); | |
return uint16(_getUser(userAddress, userKey).data1 >> 16); | |
} | |
function getDiscordScore(address userAddress, address tokenAddress, uint256 tokenChainId, uint8 period) public view returns (uint16) { | |
bytes32 userKey = getUserKey(tokenAddress, tokenChainId, period); | |
return uint16(_getUser(userAddress, userKey).data1 >> 32); | |
} | |
function getOffchainVotePct(address userAddress, address tokenAddress, uint256 tokenChainId, uint8 period) public view returns (uint16) { | |
bytes32 userKey = getUserKey(tokenAddress, tokenChainId, period); | |
return uint16(_getUser(userAddress, userKey).data1 >> 48); | |
} | |
function getOnchainVotePct(address userAddress, address tokenAddress, uint256 tokenChainId, uint8 period) public view returns (uint16) { | |
bytes32 userKey = getUserKey(tokenAddress, tokenChainId, period); | |
return uint16(_getUser(userAddress, userKey).data1 >> 64); | |
} | |
function getProposalsCreated(address userAddress, address tokenAddress, uint256 tokenChainId, uint8 period) public view returns (uint16) { | |
bytes32 userKey = getUserKey(tokenAddress, tokenChainId, period); | |
return uint16(_getUser(userAddress, userKey).data1 >> 80); | |
} | |
function getDelegatedVotes(address userAddress, address tokenAddress, uint256 tokenChainId, uint8 period) public view returns (uint32) { | |
bytes32 userKey = getUserKey(tokenAddress, tokenChainId, period); | |
return uint32(_getUser(userAddress, userKey).data2); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment