Skip to content

Instantly share code, notes, and snippets.

@Magicking
Last active November 15, 2022 18:07
Show Gist options
  • Save Magicking/eaaaae36dcb40de19951df3802bb46d0 to your computer and use it in GitHub Desktop.
Save Magicking/eaaaae36dcb40de19951df3802bb46d0 to your computer and use it in GitHub Desktop.
// SPDX-License-Identifier: WTFPL
// Author: [email protected]
pragma solidity >=0.7.0 <0.9.0;
/**
* @title TxIndexRoll
* @dev Roll your place amongst the TxIndex top of your block
*/
contract TxIndexRoll {
uint256 public number;
uint256 public lastBlock;
address payable public winner;
event LOSER(address, uint256);
event TOPPLAYER(address, uint256);
event WINNER(address, uint256);
/**
* @dev Check if your tx index is the top player of your block
*/
function roll() public payable {
uint256 txIndex;
require(msg.value > 0, "You got to pay to participate");
assembly {
txIndex := verbatim_0i_1o(hex"49")
}
// New block pay the previous winner!
if (lastBlock != block.timestamp) { // https://www.youtube.com/watch?v=T_WSXXPQYeY
lastBlock = block.timestamp; // UUID
number = 0; // You got to revert if you are last
if (winner != address(0x0)) { // First time contract is called?
emit WINNER(winner, address(this).balance);
winner.transfer(address(this).balance); // Do the reentrancy dance https://www.youtube.com/watch?v=I1188GO4p1E
}
if (txIndex == 0) { // First transaction of the block has the highest probability of loosing
winner = payable(msg.sender);
emit TOPPLAYER(winner, address(this).balance);
return;
}
}
// You are the the top player https://youtu.be/vzA7kCCBiRw?t=176
if (txIndex > number) {
winner = payable(msg.sender);
number = txIndex;
emit TOPPLAYER(winner, address(this).balance);
return;
}
emit LOSER(msg.sender, address(this).balance);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment