Skip to content

Instantly share code, notes, and snippets.

@SilentCicero
Last active May 11, 2017 21:27
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 SilentCicero/347a7f84fa41de1e62f2b320925dd49b to your computer and use it in GitHub Desktop.
Save SilentCicero/347a7f84fa41de1e62f2b320925dd49b to your computer and use it in GitHub Desktop.
/*
You should inherit from StandardToken or, for a token like you would want to
deploy in something like Mist, see HumanStandardToken.sol.
(This implements ONLY the standard functions and NOTHING else.
If you deploy this, you won't have anything useful.)
Implements ERC 20 Token standard: https://github.com/ethereum/EIPs/issues/20
.*/
pragma solidity ^0.4.8;
import "./Token.sol";
contract MiniMeStandardToken is Token {
function MiniMeStandardToken(address _parent, uint256 _snapShotBlock) {
parent = RecordStandardToken(_parent);
snapShotBlock = _snapShotBlock;
}
function transfer(address _to, uint256 _value) returns (bool success) {
//Default assumes totalSupply can't be over max (2^256 - 1).
//If your token leaves out totalSupply and can issue more tokens as time goes on, you need to check if it doesn't wrap.
//Replace the if with this one instead.
//if (balances[msg.sender] >= _value && balances[_to] + _value > balances[_to]) {
if (balances[msg.sender] >= _value && _value > 0) {
recordTransfer(msg.sender, _to, _value);
balances[msg.sender] -= _value;
balances[_to] += _value;
Transfer(msg.sender, _to, _value);
return true;
} else { return false; }
}
function transferFrom(address _from, address _to, uint256 _value) returns (bool success) {
//same as above. Replace this line with the following if you want to protect against wrapping uints.
//if (balances[_from] >= _value && allowed[_from][msg.sender] >= _value && balances[_to] + _value > balances[_to]) {
if (balances[_from] >= _value && allowed[_from][msg.sender] >= _value && _value > 0) {
recordTransfer(_from, _to, _value);
balances[_to] += _value;
balances[_from] -= _value;
allowed[_from][msg.sender] -= _value;
Transfer(_from, _to, _value);
return true;
} else { return false; }
}
function balanceOf(address _owner) constant returns (uint256 balance) {
return balanceOfAtBlock(_owner, block.number);
}
function approve(address _spender, uint256 _value) returns (bool success) {
allowed[msg.sender][_spender] = _value;
Approval(msg.sender, _spender, _value);
return true;
}
function allowance(address _owner, address _spender) constant returns (uint256 remaining) {
return allowed[_owner][_spender];
}
function recordTransfer(address _from, address _to, uint256 _value) internal {
balanceAtTime[_from][now] = balanceOf(_from) - _value;
balanceAtTime[_to][now] = balanceOf(_to) + _value;
balanceAtTimeRecords[_to].push(now);
balanceAtBlock[_from][block.number] = balanceOf(_from) - _value;
balanceAtBlock[_to][block.number] = balanceOf(_to) + _value;
balanceAtBlockRecords[_to].push(block.number);
}
function closest(uint256 _targetValue, uint256[] _targetArray) constant returns (uint256) {
uint256 current = _targetArray[0];
if (_targetValue == 0 || _targetArray.length == 0) { return 0; } // overflow prevention on zero value/empty arr
for (uint8 i = 0; i < _targetArray.length; i++) {
if ((_targetValue - _targetArray[i]) < (_targetValue - current)) {
current = _targetArray[i];
}
}
return current;
}
function hasTransfers(address _owner) constant returns (bool) {
return (balanceAtTimeRecords[_owner].length > 0);
}
function genesisTransferTime(address _owner) constant returns (uint256) {
return balanceAtTime[_owner][balanceAtTimeRecords[_owner][0]];
}
function genesisTransferBlock(address _owner) constant returns (uint256) {
return balanceAtBlock[_owner][balanceAtBlockRecords[_owner][0]];
}
function balanceOfAtTime(address _owner, uint256 _time) constant returns (uint256) {
if ((!hasTransfers(_owner) || genesisTransferTime(_owner) > _time) && address(parent) != address(0)) {
return parent.balanceOfAtBlock(_owner, snapShotBlock);
}
uint256 closestTime = closest(_time, balanceAtTimeRecords[_owner]);
return balanceAtTime[_owner][closestTime];
}
function balanceOfAtBlock(address _owner, uint256 _blockNumber) constant returns (uint256) {
if ((!hasTransfers(_owner) || genesisTransferBlock(_owner) > _blockNumber) && address(parent) != address(0)) {
return parent.balanceOfAtBlock(_owner, snapShotBlock);
}
uint256 closestBlockNumber = closest(_blockNumber, balanceAtBlockRecords[_owner]);
return balanceAtBlockRecords[_owner][closestBlockNumber];
}
RecordStandardToken public parent;
uint256 public snapShotBlock;
mapping (address => uint256) balances;
mapping (address => mapping (address => uint256)) allowed;
mapping (address => mapping(uint256 => uint256)) balanceAtTime;
mapping (address => uint256[]) balanceAtTimeRecords;
mapping (address => mapping(uint256 => uint256)) balanceAtBlock;
mapping (address => uint256[]) balanceAtBlockRecords;
}
@SilentCicero
Copy link
Author

I just want to note the overflow risk here with the closest function. Still working that out.

@SilentCicero
Copy link
Author

Should return zero if target value is zero.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment