Skip to content

Instantly share code, notes, and snippets.

@grepruby
Created July 12, 2018 11:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save grepruby/22ec5f2a133c90523592bdbc6a1ac6ac to your computer and use it in GitHub Desktop.
Save grepruby/22ec5f2a133c90523592bdbc6a1ac6ac to your computer and use it in GitHub Desktop.
pragma solidity ^0.4.21;
/**
* @title SafeMath
* @dev Math operations with safety checks that throw on error
*/
library SafeMath {
function mul(uint256 a, uint256 b) internal constant returns (uint256) {
uint256 c = a * b;
require(a == 0 || c / a == b);
return c;
}
function div(uint256 a, uint256 b) internal constant returns (uint256) {
// require(b > 0); // Solidity automatically throws when dividing by 0
uint256 c = a / b;
// require(a == b * c + a % b); // There is no case in which this doesn't hold
return c;
}
function sub(uint256 a, uint256 b) internal constant returns (uint256) {
require(b <= a);
return a - b;
}
function add(uint256 a, uint256 b) internal constant returns (uint256) {
uint256 c = a + b;
require(c >= a);
return c;
}
}
/**
* @title Ownable
* @dev The Ownable contract has an owner address, and provides basic authorization control
* functions, this simplifies the implementation of "user permissions".
*/
contract Ownable {
address public owner;
event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);
/**
* @dev The Ownable constructor sets the original `owner` of the contract to the sender
* account.
*/
function Ownable() public {
owner = msg.sender;
}
/**
* @dev Throws if called by any account other than the owner.
*/
modifier onlyOwner() {
require(msg.sender == owner);
_;
}
/**
* @dev Allows the current owner to transfer control of the contract to a newOwner.
* @param newOwner The address to transfer ownership to.
*/
function transferOwnership(address newOwner) public onlyOwner {
require(newOwner != address(0));
OwnershipTransferred(owner, newOwner);
owner = newOwner;
}
}
// ERC20 Token Standard Interface
// https://github.com/ethereum/EIPs/issues/20
interface IERC20 {
function balanceOf(address _owner) public constant returns (uint256);
function totalSupply() public constant returns (uint256);
function transfer(address _spender, uint256 _value) public returns (bool);
event Transfer(address indexed from, address indexed to, uint value);
}
contract LogicBank is IERC20, Ownable {
using SafeMath for uint;
address public pseudoToken;
uint256 internal total_supply;
mapping(address => uint256) internal balances;
mapping(address => mapping (address => uint)) internal allowances;
function LogicBank(address _pseudoToken) public {
pseudoToken = _pseudoToken;
}
function initialize(uint256 _value) {
require(total_supply == 0);
balances[msg.sender] += _value;
total_supply += _value;
}
function balanceOf(address _owner) public constant returns (uint256) {
return balances[_owner];
}
function totalSupply() public constant returns (uint256) {
return total_supply;
}
function transfer(address _spender, uint256 _value) public returns (bool) {
balances[msg.sender] = balances[msg.sender].sub(_value);
balances[_spender] = balances[_spender].add(_value);
Transfer(msg.sender, _spender, _value);
return true;
}
function kill(address _newLogicBank) public onlyOwner {
if(pseudoToken != address(0)) {
Ownable(pseudoToken).transferOwnership(msg.sender);
}
selfdestruct(_newLogicBank);
}
}
contract PseudoToken is Ownable {
/**
* @dev Performs a delegatecall and returns whatever the delegatecall returned (entire context execution will return!)
* @param _dst Destination address to perform the delegatecall
* @param _calldata Calldata for the delegatecall
*/
function delegatedFwd(address _dst, bytes _calldata) internal {
assembly {
let result := delegatecall(sub(gas, 10000), _dst, add(_calldata, 0x20), mload(_calldata), 0, 0)
let size := returndatasize
let ptr := mload(0x40)
returndatacopy(ptr, 0, size)
// revert instead of invalid() bc if the underlying call failed with invalid() it already wasted gas.
// if the call returned error data, forward it
switch result case 0 { revert(ptr, size) }
default { return(ptr, size) }
}
}
// fallback function
function () public payable {
delegatedFwd(owner, msg.data);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment