lint and interface abstraction

This commit is contained in:
Ricardo Guilherme Schmidt 2018-03-17 12:55:25 -03:00
parent 834e9ff98d
commit 338025223e
4 changed files with 353 additions and 22 deletions

View File

@ -1,15 +1,16 @@
pragma solidity ^0.4.17;
import "../token/MiniMeToken.sol";
import "../token/MiniMeTokenInterface.sol";
import "./DelegateProxyInterface.sol";
/**
* @title DelegationProxy
* @author Ricardo Guilherme Schmidt (Status Research & Development GmbH)
* @dev Creates a delegation proxy layer for MiniMeToken.
* @dev Creates a delegation proxy layer for MiniMeTokenInterface.
*/
contract DelegationProxy {
event Delegate(address who, address to);
contract DelegationProxy is DelegateProxyInterface {
//default delegation proxy, being used when user didn't set any delegation at this level.
address public parentProxy;
//snapshots of changes, allow delegation changes be done at any time without compromising vote results.
@ -54,7 +55,11 @@ contract DelegationProxy {
* @param _block From what block.
* @return True when found final delegate.
*/
function findFinalDelegate(address _delegator, uint256 _block, uint256 loopLimit)
function findFinalDelegate(
address _delegator,
uint256 _block,
uint256 loopLimit
)
external
returns (bool)
{
@ -87,7 +92,11 @@ contract DelegationProxy {
* @param _stackLimit how much deep explore to build the indexes
* @return address of delegate when found, or the last top delegate found if stacklimit reached without getting into FinalDelegate.
*/
function buildFinalDelegateChain(address _delegator, uint256 _block, uint256 _stackLimit)
function buildFinalDelegateChain(
address _delegator,
uint256 _block,
uint256 _stackLimit
)
public
returns (address delegate, bool found)
{
@ -121,7 +130,11 @@ contract DelegationProxy {
* @param _who What address to lookup.
* @return The address `_who` choosen delegate to.
*/
function delegatedTo(address _who) public constant returns (address) {
function delegatedTo(address _who)
public
constant
returns (address)
{
return delegatedToAt(_who, block.number);
}
@ -130,34 +143,52 @@ contract DelegationProxy {
* @param _who Address to lookup.
* @return Final delegate address.
*/
function delegationOf(address _who) public constant returns(address) {
function delegationOf(address _who)
public
constant
returns(address)
{
return delegationOfAt(_who, block.number);
}
/**
* @notice Reads the sum of votes a `_who' have at block number `_block`.
* @param _who From what address.
* @param _token Address of source MiniMeToken.
* @param _token Address of source MiniMeTokenInterface.
* @return Amount of influence of `who` have.
*/
function influenceOf(address _who, MiniMeToken _token) public constant returns(uint256 _total) {
function influenceOf(
address _who,
MiniMeTokenInterface _token
)
public
constant
returns(uint256 _total)
{
return influenceOfAt(_who, _token, block.number);
}
/**
* @notice Reads amount delegated influence `_who` received from other addresses.
* @param _who What address to lookup.
* @param _token Source MiniMeToken.
* @param _token Source MiniMeTokenInterface.
* @return Sum of delegated influence received by `_who` in block `_block` from other addresses.
*/
function delegatedInfluenceFrom(address _who, address _token) public constant returns(uint256 _total) {
function delegatedInfluenceFrom(
address _who,
address _token
)
public
constant
returns(uint256 _total)
{
return delegatedInfluenceFromAt(_who, _token, block.number, address(this));
}
/**
* @notice Reads amount delegated influence `_who` received from other addresses at block number `_block`.
* @param _who What address to lookup.
* @param _token Source MiniMeToken.
* @param _token Source MiniMeTokenInterface.
* @param _block Position in history to lookup.
* @return Sum of delegated influence received by `_who` in block `_block` from other addresses
*/
@ -180,7 +211,14 @@ contract DelegationProxy {
* @param _block Block number of what height in history.
* @return The address `_who` choosen delegate to.
*/
function delegatedToAt(address _who, uint _block) public constant returns (address addr) {
function delegatedToAt(
address _who,
uint _block
)
public
constant
returns (address addr)
{
Delegation[] storage checkpoints = delegations[_who];
//In case there is no registry
@ -205,7 +243,14 @@ contract DelegationProxy {
* @param _block From what block.
* @return Final delegate address.
*/
function delegationOfAt(address _who, uint _block) public constant returns(address delegate) {
function delegationOfAt(
address _who,
uint _block
)
public
constant
returns(address delegate)
{
bytes32 searchIndex = keccak256(_who, _block);
FinalDelegate memory search = delegationView[searchIndex];
if (search.found) {
@ -225,7 +270,7 @@ contract DelegationProxy {
/**
* @dev Reads amount delegated influence received from other addresses.
* @param _who What address to lookup.
* @param _token Source MiniMeToken.
* @param _token Source MiniMeTokenInterface.
* @param _block Position in history to lookup.
* @param _childProxy The child DelegationProxy requesting the call to parent.
* @return Sum of delegated influence received by `_who` in block `_block` from other addresses.
@ -269,7 +314,7 @@ contract DelegationProxy {
uint _len = d.from.length;
for (uint256 i = 0; _len > i; i++) {
address _from = d.from[i];
_total += MiniMeToken(_token).balanceOfAt(_from, _block); // source of _who votes
_total += MiniMeTokenInterface(_token).balanceOfAt(_from, _block); // source of _who votes
_total += DelegationProxy(_childProxy).delegatedInfluenceFromAt(
_from,
_token,
@ -283,13 +328,21 @@ contract DelegationProxy {
/**
* @notice Reads the sum of votes a `_who' have at block number `_block`.
* @param _who From what address.
* @param _token Address of source MiniMeToken.
* @param _token Address of source MiniMeTokenInterface.
* @param _block From what block
* @return Amount of influence of `who` have.
*/
function influenceOfAt(address _who, MiniMeToken _token, uint _block) public constant returns(uint256 _total) {
function influenceOfAt(
address _who,
MiniMeTokenInterface _token,
uint _block
)
public
constant
returns(uint256 _total)
{
if (delegationOfAt(_who, _block) == _who) { //is endpoint of delegation?
_total = MiniMeToken(_token).balanceOfAt(_who, _block); // source of _who votes
_total = MiniMeTokenInterface(_token).balanceOfAt(_who, _block); // source of _who votes
_total += delegatedInfluenceFromAt(_who, _token, _block, address(this)); //votes delegated to `_who`
} else {
_total = 0; //no influence because were delegated

View File

@ -0,0 +1,179 @@
pragma solidity ^0.4.17;
import "../token/MiniMeTokenInterface.sol";
contract DelegationProxyInterface {
event Delegate(address who, address to);
/**
* @notice Changes the delegation of `msg.sender` to `_to`. if _to 0x00: delegate to self.
* In case of having a parent proxy, if never defined, fall back to parent proxy.
* If once defined and want to delegate to parent proxy, set `_to` as parent address.
* @param _to To what address the caller address will delegate to.
*/
function delegate(address _to) external;
/**
* @notice Dig into delegate chain to find final delegate, makes delegationOfAt cheaper to call;
* Should be used when you want to track an isolated long delegation chain FinalDelegate
* @param _delegator Address to lookup final delegate.
* @param _block From what block.
* @return True when found final delegate.
*/
function findFinalDelegate(
address _delegator,
uint256 _block,
uint256 loopLimit
)
external
returns (bool);
/**
* @notice Explore the chain from `_delegator`, saving FinalDelegate indexes for all delegates, makes delegationOfAt cheaper to call.
* Should be used to track a common FinalDelegates in a small delegation chain, saving gas on repetitive lookups;
* @param _delegator Address to lookup final delegate.
* @param _block From what block.
* @param _stackLimit how much deep explore to build the indexes
* @return address of delegate when found, or the last top delegate found if stacklimit reached without getting into FinalDelegate.
*/
function buildFinalDelegateChain(
address _delegator,
uint256 _block,
uint256 _stackLimit
)
public
returns (address delegate, bool found);
/**
* @notice Reads `_who` configured delegation in this level,
* or from parent level if `_who` never defined/defined to parent address.
* @param _who What address to lookup.
* @return The address `_who` choosen delegate to.
*/
function delegatedTo(address _who)
public
constant
returns (address);
/**
* @notice Reads the final delegate of `_who` at block number `_block`.
* @param _who Address to lookup.
* @return Final delegate address.
*/
function delegationOf(address _who)
public
constant
returns(address);
/**
* @notice Reads the sum of votes a `_who' have at block number `_block`.
* @param _who From what address.
* @param _token Address of source MiniMeTokenInterface.
* @return Amount of influence of `who` have.
*/
function influenceOf(
address _who,
MiniMeTokenInterface _token
)
public
constant
returns(uint256 _total);
/**
* @notice Reads amount delegated influence `_who` received from other addresses.
* @param _who What address to lookup.
* @param _token Source MiniMeTokenInterface.
* @return Sum of delegated influence received by `_who` in block `_block` from other addresses.
*/
function delegatedInfluenceFrom(
address _who,
address _token
)
public
constant
returns(uint256 _total);
/**
* @notice Reads amount delegated influence `_who` received from other addresses at block number `_block`.
* @param _who What address to lookup.
* @param _token Source MiniMeTokenInterface.
* @param _block Position in history to lookup.
* @return Sum of delegated influence received by `_who` in block `_block` from other addresses
*/
function delegatedInfluenceFromAt(
address _who,
address _token,
uint _block
)
public
constant
returns(uint256 _total);
/**
* @notice Reads `_who` configured delegation at block number `_block` in this level,
* or from parent level if `_who` never defined/defined to parent address.
* @param _who What address to lookup.
* @param _block Block number of what height in history.
* @return The address `_who` choosen delegate to.
*/
function delegatedToAt(
address _who,
uint _block
)
public
constant
returns (address addr);
/**
* @notice Reads the final delegate of `_who` at block number `_block`.
* @param _who Address to lookup.
* @param _block From what block.
* @return Final delegate address.
*/
function delegationOfAt(
address _who,
uint _block
)
public
constant
returns(address delegate);
/**
* @dev Reads amount delegated influence received from other addresses.
* @param _who What address to lookup.
* @param _token Source MiniMeTokenInterface.
* @param _block Position in history to lookup.
* @param _childProxy The child DelegationProxy requesting the call to parent.
* @return Sum of delegated influence received by `_who` in block `_block` from other addresses.
*/
function delegatedInfluenceFromAt(
address _who,
address _token,
uint _block,
address _childProxy
)
public
constant
returns(uint256 _total);
/**
* @notice Reads the sum of votes a `_who' have at block number `_block`.
* @param _who From what address.
* @param _token Address of source MiniMeTokenInterface.
* @param _block From what block
* @return Amount of influence of `who` have.
*/
function influenceOfAt(
address _who,
MiniMeTokenInterface _token,
uint _block
)
public
constant
returns(uint256 _total);
}

View File

@ -28,11 +28,13 @@ pragma solidity ^0.4.6;
import "../common/Controlled.sol";
import "./TokenController.sol";
import "./ApproveAndCallFallBack.sol";
import "./MiniMeTokenInterface.sol";
import "./MiniMeTokenFactory.sol";
/// @dev The actual token contract, the default controller is the msg.sender
/// that deploys the contract, so usually this token will be deployed by a
/// token controller contract, which Giveth will call a "Campaign"
contract MiniMeToken is Controlled {
contract MiniMeToken is MiniMeTokenInterface, Controlled {
string public name; //The Token's name: e.g. DigixDAO Tokens
uint8 public decimals; //Number of decimals of the smallest unit
@ -43,7 +45,7 @@ contract MiniMeToken is Controlled {
/// @dev `Checkpoint` is the structure that attaches a block number to a
/// given value, the block number attached is the one that last changed the
/// value
struct Checkpoint {
struct Checkpoint {
// `fromBlock` is the block number that the value was generated from
uint128 fromBlock;

View File

@ -0,0 +1,97 @@
pragma solidity ^0.4.17;
import "./ERC20Token.sol";
contract MiniMeTokenInterface is ERC20Token {
/// @notice `msg.sender` approves `_spender` to send `_amount` tokens on
/// its behalf, and then a function is triggered in the contract that is
/// being approved, `_spender`. This allows users to use their tokens to
/// interact with contracts in one function call instead of two
/// @param _spender The address of the contract able to transfer the tokens
/// @param _amount The amount of tokens to be approved for transfer
/// @return True if the function call was successful
function approveAndCall(
address _spender,
uint256 _amount,
bytes _extraData
)
public
returns (bool success);
/// @notice Creates a new clone token with the initial distribution being
/// this token at `_snapshotBlock`
/// @param _cloneTokenName Name of the clone token
/// @param _cloneDecimalUnits Number of decimals of the smallest unit
/// @param _cloneTokenSymbol Symbol of the clone token
/// @param _snapshotBlock Block when the distribution of the parent token is
/// copied to set the initial distribution of the new clone token;
/// if the block is zero than the actual block, the current block is used
/// @param _transfersEnabled True if transfers are allowed in the clone
/// @return The address of the new MiniMeToken Contract
function createCloneToken(
string _cloneTokenName,
uint8 _cloneDecimalUnits,
string _cloneTokenSymbol,
uint _snapshotBlock,
bool _transfersEnabled
)
public
returns(address);
/// @notice Generates `_amount` tokens that are assigned to `_owner`
/// @param _owner The address that will be assigned the new tokens
/// @param _amount The quantity of tokens generated
/// @return True if the tokens are generated correctly
function generateTokens(
address _owner,
uint _amount
)
public
returns (bool);
/// @notice Burns `_amount` tokens from `_owner`
/// @param _owner The address that will lose the tokens
/// @param _amount The quantity of tokens to burn
/// @return True if the tokens are burned correctly
function destroyTokens(
address _owner,
uint _amount
)
public
returns (bool);
/// @notice Enables token holders to transfer their tokens freely if true
/// @param _transfersEnabled True if transfers are allowed in the clone
function enableTransfers(bool _transfersEnabled) public;
/// @notice This method can be used by the controller to extract mistakenly
/// sent tokens to this contract.
/// @param _token The address of the token contract that you want to recover
/// set to 0 in case you want to extract ether.
function claimTokens(address _token) public;
/// @dev Queries the balance of `_owner` at a specific `_blockNumber`
/// @param _owner The address from which the balance will be retrieved
/// @param _blockNumber The block number when the balance is queried
/// @return The balance at `_blockNumber`
function balanceOfAt(
address _owner,
uint _blockNumber
)
public
constant
returns (uint);
/// @notice Total amount of tokens at a specific `_blockNumber`.
/// @param _blockNumber The block number when the totalSupply is queried
/// @return The total amount of tokens at `_blockNumber`
function totalSupplyAt(uint _blockNumber) public constant returns(uint);
}