Skip to content

Instantly share code, notes, and snippets.

@m1guelpf
Created January 20, 2022 05:22
Show Gist options
  • Save m1guelpf/ffed281b6ff4c6023c82b2b44760cd3c to your computer and use it in GitHub Desktop.
Save m1guelpf/ffed281b6ff4c6023c82b2b44760cd3c to your computer and use it in GitHub Desktop.
An optimized version of nnnnicholas.eth's leaderboard contract
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.10;
import 'solmate/utils/SafeTransferLib.sol';
/**
* @title Leaderboard
* @author @nnnnicholas, tweaked by @m1guelpf
* @dev Receive ETH and associate contributions with contract addresses.
*/
contract Leaderboard {
error Unauthorized();
error ContractPaused();
bool public isPaused;
uint256 public totalAttention;
address public immutable owner;
mapping(address => uint256) public retrieveAttention;
event attentionReset(address indexed _contract);
event attentionDrawnTo(address indexed _contract, uint256 amount);
constructor() {
owner = msg.sender;
}
/**
* @dev Store cumulative value in attention mapping
* @param _contract to pay attention to
*/
function payAttention(address _contract) external payable {
if (isPaused) revert ContractPaused();
// cannot realistically overflow in human timescales
unchecked {
retrieveAttention[_contract] += msg.value;
totalAttention += msg.value;
}
emit attentionDrawnTo(_contract, msg.value);
}
function withdrawTo(address to, uint256 amount) public payable {
if (msg.sender != owner) revert Unauthorized();
SafeTransferLib.safeTransferETH(to, amount);
}
function withdraw() external payable {
withdrawTo(msg.sender, address(this).balance);
}
function resetAttention(address _contract) external payable {
if (msg.sender != owner) revert Unauthorized();
retrieveAttention[_contract] = 0;
emit attentionReset(_contract);
}
function pause() external payable {
if (msg.sender != owner) revert Unauthorized();
isPaused = true;
}
function unpause() external payable {
if (msg.sender != owner) revert Unauthorized();
isPaused = false;
}
receive() external payable {}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment