Contract Source Code:
pragma solidity ^0.5.10;
import "./StorageUnit.sol";
import "./IsContract.sol";
library DistributedStorage {
function contractSlot(bytes32 _struct) private view returns (address) {
return address(
uint256(
keccak256(
abi.encodePacked(
byte(0xff),
address(this),
_struct,
keccak256(type(StorageUnit).creationCode)
)
)
)
);
}
function deploy(bytes32 _struct) private {
bytes memory slotcode = type(StorageUnit).creationCode;
/* solium-disable-next-line */
assembly{ pop(create2(0, add(slotcode, 0x20), mload(slotcode), _struct)) }
}
function write(
bytes32 _struct,
bytes32 _key,
bytes32 _value
) internal {
StorageUnit store = StorageUnit(contractSlot(_struct));
if (!IsContract.isContract(address(store))) {
deploy(_struct);
}
/* solium-disable-next-line */
(bool success, ) = address(store).call(
abi.encodeWithSelector(
store.write.selector,
_key,
_value
)
);
require(success, "error writing storage");
}
function read(
bytes32 _struct,
bytes32 _key
) internal view returns (bytes32) {
StorageUnit store = StorageUnit(contractSlot(_struct));
if (!IsContract.isContract(address(store))) {
return bytes32(0);
}
/* solium-disable-next-line */
(bool success, bytes memory data) = address(store).staticcall(
abi.encodeWithSelector(
store.read.selector,
_key
)
);
require(success, "error reading storage");
return abi.decode(data, (bytes32));
}
}
pragma solidity ^0.5.10;
/*
*/
library Farm {
using Farm for Farm.Pigpen;
struct Pigpen {
uint256[] entries;
mapping(address => uint256) index;
}
function initialize(Pigpen storage _pigpen) internal {
require(_pigpen.entries.length == 0, "already initialized");
_pigpen.entries.push(0);
}
function encode(address _addr, uint256 _value) internal pure returns (uint256 _entry) {
/* solium-disable-next-line */
assembly {
_entry := not(or(and(0xffffffffffffffffffffffffffffffffffffffff, _addr), shl(160, _value)))
}
}
function decode(uint256 _entry) internal pure returns (address _addr, uint256 _value) {
/* solium-disable-next-line */
assembly {
let entry := not(_entry)
_addr := and(entry, 0xffffffffffffffffffffffffffffffffffffffff)
_value := shr(160, entry)
}
}
function decodeAddress(uint256 _entry) internal pure returns (address _addr) {
/* solium-disable-next-line */
assembly {
_addr := and(not(_entry), 0xffffffffffffffffffffffffffffffffffffffff)
}
}
function top(Pigpen storage _pigpen) internal view returns(address, uint256) {
if (_pigpen.entries.length < 2) {
return (address(0), 0);
}
return decode(_pigpen.entries[1]);
}
function has(Pigpen storage _pigpen, address _addr) internal view returns (bool) {
return _pigpen.index[_addr] != 0;
}
function size(Pigpen storage _pigpen) internal view returns (uint256) {
return _pigpen.entries.length - 1;
}
function entry(Pigpen storage _pigpen, uint256 _i) internal view returns (address, uint256) {
return decode(_pigpen.entries[_i + 1]);
}
// RemoveMax pops off the root element of the pigpen (the highest value here) and rebalances the pigpen
function popTop(Pigpen storage _pigpen) internal returns(address _addr, uint256 _value) {
// Ensure the pigpen exists
uint256 pigpenLength = _pigpen.entries.length;
require(pigpenLength > 1, "The pigpen does not exists");
// take the root value of the pigpen
(_addr, _value) = decode(_pigpen.entries[1]);
_pigpen.index[_addr] = 0;
if (pigpenLength == 2) {
_pigpen.entries.length = 1;
} else {
// Takes the last element of the array and put it at the root
uint256 val = _pigpen.entries[pigpenLength - 1];
_pigpen.entries[1] = val;
// Delete the last element from the array
_pigpen.entries.length = pigpenLength - 1;
// Start at the top
uint256 ind = 1;
// Bubble down
ind = _pigpen.bubbleDown(ind, val);
// Update index
_pigpen.index[decodeAddress(val)] = ind;
}
}
// Inserts adds in a value to our pigpen.
function insert(Pigpen storage _pigpen, address _addr, uint256 _value) internal {
require(_pigpen.index[_addr] == 0, "The entry already exists");
// Add the value to the end of our array
uint256 encoded = encode(_addr, _value);
_pigpen.entries.push(encoded);
// Start at the end of the array
uint256 currentIndex = _pigpen.entries.length - 1;
// Bubble Up
currentIndex = _pigpen.bubbleUp(currentIndex, encoded);
// Update index
_pigpen.index[_addr] = currentIndex;
}
function update(Pigpen storage _pigpen, address _addr, uint256 _value) internal {
uint256 ind = _pigpen.index[_addr];
require(ind != 0, "The entry does not exists");
uint256 can = encode(_addr, _value);
uint256 val = _pigpen.entries[ind];
uint256 newInd;
if (can < val) {
// Bubble down
newInd = _pigpen.bubbleDown(ind, can);
} else if (can > val) {
// Bubble up
newInd = _pigpen.bubbleUp(ind, can);
} else {
// no changes needed
return;
}
// Update entry
_pigpen.entries[newInd] = can;
// Update index
if (newInd != ind) {
_pigpen.index[_addr] = newInd;
}
}
function bubbleUp(Pigpen storage _pigpen, uint256 _ind, uint256 _val) internal returns (uint256 ind) {
// Bubble up
ind = _ind;
if (ind != 1) {
uint256 pen = _pigpen.entries[ind / 2];
while (pen < _val) {
// If the pen value is lower than our current value, we swap them
(_pigpen.entries[ind / 2], _pigpen.entries[ind]) = (_val, pen);
// Update moved Index
_pigpen.index[decodeAddress(pen)] = ind;
// change our current Index to go up to the pen
ind = ind / 2;
if (ind == 1) {
break;
}
// Update pen
pen = _pigpen.entries[ind / 2];
}
}
}
function bubbleDown(Pigpen storage _pigpen, uint256 _ind, uint256 _val) internal returns (uint256 ind) {
// Bubble down
ind = _ind;
uint256 lenght = _pigpen.entries.length;
uint256 target = lenght - 1;
while (ind * 2 < lenght) {
// get the current index of the pigs
uint256 j = ind * 2;
// left pig value
uint256 leftPig = _pigpen.entries[j];
// Store the value of the pigs
uint256 pigValue;
if (target > j) {
// The pen has two pigs
// Load right pig value
uint256 rightPig = _pigpen.entries[j + 1];
// Compare the left and right pigs
// if the rightPig is greater, then point j to it's index
// and save the value
if (leftPig < rightPig) {
pigValue = rightPig;
j = j + 1;
} else {
// The left pig is greater
pigValue = leftPig;
}
} else {
// The pen has a single pig
pigValue = leftPig;
}
// Check if the pig has a lower value
if (_val > pigValue) {
break;
}
// else swap the value
(_pigpen.entries[ind], _pigpen.entries[j]) = (pigValue, _val);
// Update moved Index
_pigpen.index[decodeAddress(pigValue)] = ind;
// and let's keep going down the pigpen
ind = j;
}
}
}
pragma solidity ^0.5.10;
contract GasPump {
bytes32 private stub;
modifier requestGas(uint256 _factor) {
if (tx.gasprice == 0 || gasleft() > block.gaslimit) {
uint256 startgas = gasleft();
_;
uint256 delta = startgas - gasleft();
uint256 target = (delta * _factor) / 100;
startgas = gasleft();
while (startgas - gasleft() < target) {
// Burn gas
stub = keccak256(abi.encodePacked(stub));
}
} else {
_;
}
}
}
pragma solidity ^0.5.10;
interface IERC20 {
event Transfer(address indexed _from, address indexed _to, uint256 _value);
event Approval(address indexed _owner, address indexed _spender, uint256 _value);
function transfer(address _to, uint _value) external returns (bool success);
function transferFrom(address _from, address _to, uint256 _value) external returns (bool success);
function allowance(address _owner, address _spender) external view returns (uint256 remaining);
function approve(address _spender, uint256 _value) external returns (bool success);
function balanceOf(address _owner) external view returns (uint256 balance);
}
pragma solidity ^0.5.10;
library IsContract {
function isContract(address _addr) internal view returns (bool) {
bytes32 codehash;
/* solium-disable-next-line */
assembly { codehash := extcodehash(_addr) }
return codehash != bytes32(0) && codehash != bytes32(0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470);
}
}
pragma solidity ^0.5.10;
library Math {
function orderOfMagnitude(uint256 input) internal pure returns (uint256){
uint256 counter = uint(-1);
uint256 temp = input;
do {
temp /= 10;
counter++;
} while (temp != 0);
return counter;
}
function min(uint256 _a, uint256 _b) internal pure returns (uint256) {
if (_a < _b) {
return _a;
} else {
return _b;
}
}
function max(uint256 _a, uint256 _b) internal pure returns (uint256) {
if (_a > _b) {
return _a;
} else {
return _b;
}
}
}
pragma solidity ^0.5.10;
contract Ownable {
address public owner;
event TransferOwnership(address _from, address _to);
constructor() public {
owner = msg.sender;
emit TransferOwnership(address(0), msg.sender);
}
modifier onlyOwner() {
require(msg.sender == owner, "only owner");
_;
}
function setOwner(address _owner) external onlyOwner {
emit TransferOwnership(owner, _owner);
owner = _owner;
}
}
pragma solidity ^0.5.10;
import "./Ownable.sol";
import "./Farm.sol";
contract Pigpen is Ownable {
using Farm for Farm.Pigpen;
// pigpen
Farm.Pigpen private pigpen;
// Pigpen events
event JoinPigpen(address indexed _address, uint256 _balance, uint256 _prevSize);
event LeavePigpen(address indexed _address, uint256 _balance, uint256 _prevSize);
uint256 public constant TOP_SIZE = 100;
constructor() public {
pigpen.initialize();
}
function topSize() external pure returns (uint256) {
return TOP_SIZE;
}
function addressAt(uint256 _i) external view returns (address addr) {
(addr, ) = pigpen.entry(_i);
}
function indexOf(address _addr) external view returns (uint256) {
return pigpen.index[_addr];
}
function entry(uint256 _i) external view returns (address, uint256) {
return pigpen.entry(_i);
}
function top() external view returns (address, uint256) {
return pigpen.top();
}
function size() external view returns (uint256) {
return pigpen.size();
}
function update(address _addr, uint256 _new) external onlyOwner {
uint256 _size = pigpen.size();
// If the pigpen is empty
// join the _addr
if (_size == 0) {
emit JoinPigpen(_addr, _new, 0);
pigpen.insert(_addr, _new);
return;
}
// Load top value of the pigpen
(, uint256 lastBal) = pigpen.top();
// If our target address already is in the pigpen
if (pigpen.has(_addr)) {
// Update the target address value
pigpen.update(_addr, _new);
// If the new value is 0
// always pop the pigpen
// we updated the pigpen, so our address should be on top
if (_new == 0) {
pigpen.popTop();
emit LeavePigpen(_addr, 0, _size);
}
} else {
// IF pigpen is full or new balance is higher than pop pigpen
if (_new != 0 && (_size < TOP_SIZE || lastBal < _new)) {
// If pigpen is full pop pigpen
if (_size >= TOP_SIZE) {
(address _poped, uint256 _balance) = pigpen.popTop();
emit LeavePigpen(_poped, _balance, _size);
}
// Insert new value
pigpen.insert(_addr, _new);
emit JoinPigpen(_addr, _new, _size);
}
}
}
}
pragma solidity ^0.5.10;
import "./Ownable.sol";
import "./DistributedStorage.sol";
import "./SafeMath.sol";
import "./Math.sol";
import "./GasPump.sol";
import "./IERC20.sol";
import "./Pigpen.sol";
contract Porkchop is Ownable, GasPump, IERC20 {
using DistributedStorage for bytes32;
using SafeMath for uint256;
// Pork events
event Winner(address indexed _addr, uint256 _value);
// Managment events
event SetName(string _prev, string _new);
event SetExtraGas(uint256 _prev, uint256 _new);
event SetPigpen(address _prev, address _new);
event WhitelistFrom(address _addr, bool _whitelisted);
event WhitelistTo(address _addr, bool _whitelisted);
uint256 public totalSupply;
bytes32 private constant BALANCE_KEY = keccak256("balance");
// game
uint256 public constant FEE = 100;
// metadata
string public name = "Porkchop";
string public constant symbol = "CHOP";
uint8 public constant decimals = 18;
// fee whitelist
mapping(address => bool) public whitelistFrom;
mapping(address => bool) public whitelistTo;
// pigpen
Pigpen public pigpen;
// internal
uint256 public extraGas;
bool inited;
function init(
address _to,
uint256 _amount
) external {
// Only init once
assert(!inited);
inited = true;
// Sanity checks
assert(totalSupply == 0);
assert(address(pigpen) == address(0));
// Create Pigpen
pigpen = new Pigpen();
emit SetPigpen(address(0), address(pigpen));
// Init contract variables and mint
// entire token balance
extraGas = 15;
emit SetExtraGas(0, extraGas);
emit Transfer(address(0), _to, _amount);
_setBalance(_to, _amount);
totalSupply = _amount;
}
///
// Storage access functions
///
// Getters
function _toKey(address a) internal pure returns (bytes32) {
return bytes32(uint256(a));
}
function _balanceOf(address _addr) internal view returns (uint256) {
return uint256(_toKey(_addr).read(BALANCE_KEY));
}
function _allowance(address _addr, address _spender) internal view returns (uint256) {
return uint256(_toKey(_addr).read(keccak256(abi.encodePacked("allowance", _spender))));
}
function _nonce(address _addr, uint256 _cat) internal view returns (uint256) {
return uint256(_toKey(_addr).read(keccak256(abi.encodePacked("nonce", _cat))));
}
// Setters
function _setAllowance(address _addr, address _spender, uint256 _value) internal {
_toKey(_addr).write(keccak256(abi.encodePacked("allowance", _spender)), bytes32(_value));
}
function _setNonce(address _addr, uint256 _cat, uint256 _value) internal {
_toKey(_addr).write(keccak256(abi.encodePacked("nonce", _cat)), bytes32(_value));
}
function _setBalance(address _addr, uint256 _balance) internal {
_toKey(_addr).write(BALANCE_KEY, bytes32(_balance));
pigpen.update(_addr, _balance);
}
///
// Internal methods
///
function _isWhitelisted(address _from, address _to) internal view returns (bool) {
return whitelistFrom[_from]||whitelistTo[_to];
}
function _random(address _s1, uint256 _s2, uint256 _s3, uint256 _max) internal pure returns (uint256) {
uint256 rand = uint256(keccak256(abi.encodePacked(_s1, _s2, _s3)));
return rand % (_max + 1);
}
function _pickWinner(address _from, uint256 _value) internal returns (address winner) {
// Get order of magnitude of the tx
uint256 magnitude = Math.orderOfMagnitude(_value);
// Pull nonce for a given order of magnitude
uint256 nonce = _nonce(_from, magnitude);
_setNonce(_from, magnitude, nonce + 1);
// pick entry from pigpen
winner = pigpen.addressAt(_random(_from, nonce, magnitude, pigpen.size() - 1));
}
function _transferFrom(address _operator, address _from, address _to, uint256 _value, bool _payFee) internal {
// If transfer amount is zero
// emit event and stop execution
if (_value == 0) {
emit Transfer(_from, _to, 0);
return;
}
// Load sender balance
uint256 balanceFrom = _balanceOf(_from);
require(balanceFrom >= _value, "balance not enough");
// Check if operator is sender
if (_from != _operator) {
// If not, validate allowance
uint256 allowanceFrom = _allowance(_from, _operator);
// If allowance is not 2 ** 256 - 1, consume allowance
if (allowanceFrom != uint(-1)) {
// Check allowance and save new one
require(allowanceFrom >= _value, "allowance not enough");
_setAllowance(_from, _operator, allowanceFrom.sub(_value));
}
}
// Calculate receiver balance
// initial receive is full value
uint256 receive = _value;
uint256 burn = 0;
uint256 chop = 0;
// Change sender balance
_setBalance(_from, balanceFrom.sub(_value));
// If the transaction is not whitelisted
// or if sender requested to pay the fee
// calculate fees
if (_payFee || !_isWhitelisted(_from, _to)) {
// Fee is the same for BURN and CHOP
// If we are sending value one
// give priority to BURN
burn = _value.divRound(FEE);
chop = _value == 1 ? 0 : burn;
// Subtract fees from receiver amount
receive = receive.sub(burn.add(chop));
// Burn tokens
totalSupply = totalSupply.sub(burn);
emit Transfer(_from, address(0), burn);
// Porkchop tokens
// Pick winner pseudo-randomly
address winner = _pickWinner(_from, _value);
// Transfer balance to winner
_setBalance(winner, _balanceOf(winner).add(chop));
emit Winner(winner, chop);
emit Transfer(_from, winner, chop);
}
// Sanity checks
// no tokens where created
assert(burn.add(chop).add(receive) == _value);
// Add tokens to receiver
_setBalance(_to, _balanceOf(_to).add(receive));
emit Transfer(_from, _to, receive);
}
/*
MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMmy/-```:hMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
MMMMMMMMMMMMMMMMMMMMMMMmysydMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMmy-`-:::::/ dMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
MMMMMMMMMMMMMMMMMMMMMMo`.::.-odMMMMMMMMMMMMMmmmmmmmmNMMMNo`.:/--::..o /MMMMMMMMMMMMMMMMMMMMMMMMMMMMM
MMMMMMMMMMMMMMMMMMMMN:`::.-::-.:ymMMNdys+:.............+.`::.-/+/o..+ /MMMMMMMMMMMMMMMMMMMMMMMMMMMMM
MMMMMMMMMMMMMMMMMMMMs ::.+/:.-::..+:----:::::::::::::::::/-.:+:::o..o /MMMMMMMMMMMMMMMMMMMMMMMMMMMMM
MMMMMMMMMMMMMMMMMMMM-`+.-o:/+-.-/::::--.....-----------.....////:+../.-MMMMMMMMMMMMMMMMMMMMMMMMMMMMM
MMMMMMMMMMMMMMMMMMMM `+.:+::++...-.-:://+ooooossoooosooooo+///:/+:..:: MMMMMMMMMMMMMMMMMMMMMMMMMMMMM
MMMMMMMMMMMMMMMMMMMM- +.-o//:--:/+ooossssoooooooooooooooooosssooo+/:/- mMMMMMMMMMMMMMMMMMMMMMMMMMMMM
MMMMMMMMMMMMMMMMMMMMs ::.:::+oosssoooooossssssssssssssssssooooooosssso..hMMMMMMMMMMMMMMMMMMMMMMMMMMM
MMMMMMMMMMMMMMMMMMMMs .o/+ossooooosssoo++//:::------:::://+ooosssooooss-`hMMMMMMMMMMMMMMMMMMMMMMMMMM
MMMMMMMMMMMMMMMMMMMo`-ssssooossoo+/:--....................-:---:/+oossos:`yMMMMMMMMMMMMMMMMMMMMMMMMM
MMMMMMMMMMMMMMMMMMh`-ssooosso+:-......................-::/:-......--:+oss-`mMMMMMMMMMMMMMMMMMMMMMMMM
MMMMMMMMMMMMMMMMMd.-soosoo/-..........................----:::-.........-/o..hMMMMMMMMMMMMMMMMMMMMMMM
MMMMMMMMMMMMMMMMN-`ssso/-:::----......-:://////:-......./yhdhyo-.........-+-./ymMMMMMMMMMMMMMMMMMMMM
MMMMMMMMMMMMMMMMo +o/-....---::-....-///:::::/:://-....-hddddddy-.........:ys+:-/sdNMMMMMMMMMMMMMMMM
MMMMMMMMMMMMMMMm -+-....:+so+-.....-o:+s:::::yy::/+....-hddddddy-..........ssssso/-:+symMMMMMMMMMMMM
MMMMMMMMMMMMMMMo +-....ohddddh/....+/:ys:::::/o/::o.....:oyhhyo:.........../ysssssyso+.`yMMMMMMMMMMM
MMMMMMMNMMMMMMM/`+....:dddddddy....:+:::::::::::/+-........--..............-yssssssssy.`NMMMMMMMMMMM
MMMMMMMMMMMMMMM../....-ydddddh/.....-////:::////-...........................ssssssssso +MMMMMMMMMMMM
MMMMMMMMMMMMMMM`-/.....-/ooo/-.........--:::-..............................-+`:sssssy- NMMMMMMMMMMMM
MMMMMMMMMMMMMMM/./........................................................./y- `/ssss +MMMMMMMMMMMMM
MMMMMMMMMMMMMMM/`+........................................................-ysy: `/s: mMMMMMMMMMMMMM
MMMMMMMMMMMMMMMh /:......................................................:ssssy+``.``/MMMMMMMMMMMMMM
MMMMMMMMMMMMMMMM:`+-...................................................-oysssssyo..yMMMMMMMMMMMMMMMM
MMMMMMMMMMMMMMMMN-`/-................................................://yssssssssy: /mMMMMMMMMMMMMMM
MMMMMMMMMMMMMMMMMN+ :/-...........................................:::.` +yssssssssy+`.dMMMMMMMMMMMMM
MMMMMMMMMMMMMMMMMMN: `/+:....................................-:::-``/sM.`ssssssssssss- sMMMMMMMMMMMM
MMMMMMMMMMMMMMMMMMo`/::::////:--....................--::::-:o:.----`-+md.-ysssssyso+:`.yMMMMMMMMMMMM
MMMMMMMMMMMMMMMMMh /:......:/`.-::::::+::/:::+::::/:/+/.`.:-` `-:.`sy /yso+:..:/shNMMMMMMMMMMMMM
MMMMMMMMMMMMMMMMMN:`/-.....:- ./-``::` -/-.-::-` .:-` `....` -:`:s/:-:+sdNMMMMMMMMMMMMMMMMMM
MMMMMMMMMMMMMMMMMMm+`-::--:/ `::` ./` `/+-` .--. `:::---+. ./ oMNNMMMMMMMMMMMMMMMMMMMMMMM
MMMMMMMMMMMMMMMMMMMMmo-`.-/---...:yho:. `:- -::-.` /-.....-/ :..dMMMMMMMMMMMMMMMMMMMMMMMMM
MMMMMMMMMMMMMMMMMMMMMMNmhho:``-/-`./shhy+:.` .:. `::` /-......+ /`.dMMMMMMMMMMMMMMMMMMMMMMMMM
MMMMMMMMMMMMMMMMMMMMMMMMdo..-:::` .:+yhhys+///` `:-` `::--..:/`.-:.`yMMMMMMMMMMMMMMMMMMMMMMMMMM
MMMMMMMMMMMMMMMMMMMMMmy-..--` `-:. `.-/oyhhdhysooy+::::/+osshyo..:odMMMMMMMMMMMMMMMMMMMMMMMMMMM
MMMMMMMMMMMMMMMMMMNh+..--` `.:-` `/ohdhdo+yysyyyhhyyyyyyys`:NMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
MMMMMMMMMMMMMMMMMm:.-/+. `--..` `+hs:sdy.`-:-``-/:-`````:.`MMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
MMMMMMMMMMMMMMMMMy +-.-/- `.--..-` /++- .--.``--.` -- MMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
MMMMMMMMMMMMMMMMMh ::.../- `--``.---...`` `.--.`.-:-:: MMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
MMMMMMMMMMMMMMMMMN/`/:..-+ ````..---:---.```....-------:/:---::: MMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
MMMMMMMMMMMMMMMMMMm-`--::/------...---:/oyo+:--.----. `/ MMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
MMMMMMMMMMMMMMMMMMMNho++//////+osyyddNMMMMMMMmhyo+:`:- .: MMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMo -: :- MMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMh /: /`:MMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMN`-//-` :- yMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMd -:.-/-`` .:.`yMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM:`-/-.-:::/::.`/mMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMNy-`.:::::-`-sNMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMNs+/-/ohNMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
*/
///
// Managment
///
function setWhitelistedTo(address _addr, bool _whitelisted) external onlyOwner {
emit WhitelistTo(_addr, _whitelisted);
whitelistTo[_addr] = _whitelisted;
}
function setWhitelistedFrom(address _addr, bool _whitelisted) external onlyOwner {
emit WhitelistFrom(_addr, _whitelisted);
whitelistFrom[_addr] = _whitelisted;
}
function setName(string calldata _name) external onlyOwner {
emit SetName(name, _name);
name = _name;
}
function setExtraGas(uint256 _gas) external onlyOwner {
emit SetExtraGas(extraGas, _gas);
extraGas = _gas;
}
function setPigpen(Pigpen _pigpen) external onlyOwner {
emit SetPigpen(address(pigpen), address(_pigpen));
pigpen = _pigpen;
}
/////
// Pigpen methods
/////
function topSize() external view returns (uint256) {
return pigpen.topSize();
}
function pigpenSize() external view returns (uint256) {
return pigpen.size();
}
function pigpenEntry(uint256 _i) external view returns (address, uint256) {
return pigpen.entry(_i);
}
function pigpenTop() external view returns (address, uint256) {
return pigpen.top();
}
function pigpenIndex(address _addr) external view returns (uint256) {
return pigpen.indexOf(_addr);
}
function getNonce(address _addr, uint256 _cat) external view returns (uint256) {
return _nonce(_addr, _cat);
}
/////
// ERC20
/////
function balanceOf(address _addr) external view returns (uint256) {
return _balanceOf(_addr);
}
function allowance(address _addr, address _spender) external view returns (uint256) {
return _allowance(_addr, _spender);
}
function approve(address _spender, uint256 _value) external returns (bool) {
emit Approval(msg.sender, _spender, _value);
_setAllowance(msg.sender, _spender, _value);
return true;
}
function transfer(address _to, uint256 _value) external requestGas(extraGas) returns (bool) {
_transferFrom(msg.sender, msg.sender, _to, _value, false);
return true;
}
function transferWithFee(address _to, uint256 _value) external requestGas(extraGas) returns (bool) {
_transferFrom(msg.sender, msg.sender, _to, _value, true);
return true;
}
function transferFrom(address _from, address _to, uint256 _value) external requestGas(extraGas) returns (bool) {
_transferFrom(msg.sender, _from, _to, _value, false);
return true;
}
function transferFromWithFee(address _from, address _to, uint256 _value) external requestGas(extraGas) returns (bool) {
_transferFrom(msg.sender, _from, _to, _value, true);
return true;
}
}
pragma solidity ^0.5.10;
library SafeMath {
function add(uint256 x, uint256 y) internal pure returns (uint256) {
uint256 z = x + y;
require(z >= x, "Add overflow");
return z;
}
function sub(uint256 x, uint256 y) internal pure returns (uint256) {
require(x >= y, "Sub underflow");
return x - y;
}
function mult(uint256 x, uint256 y) internal pure returns (uint256) {
if (x == 0) {
return 0;
}
uint256 z = x * y;
require(z / x == y, "Mult overflow");
return z;
}
function div(uint256 x, uint256 y) internal pure returns (uint256) {
require(y != 0, "Div by zero");
return x / y;
}
function divRound(uint256 x, uint256 y) internal pure returns (uint256) {
require(y != 0, "Div by zero");
uint256 r = x / y;
if (x % y != 0) {
r = r + 1;
}
return r;
}
}
pragma solidity ^0.5.10;
contract StorageUnit {
address private owner;
mapping(bytes32 => bytes32) private store;
constructor() public {
owner = msg.sender;
}
function write(bytes32 _key, bytes32 _value) external {
/* solium-disable-next-line */
require(msg.sender == owner);
store[_key] = _value;
}
function read(bytes32 _key) external view returns (bytes32) {
return store[_key];
}
}