///File: @aragon/os/contracts/acl/IACL.sol pragma solidity ^0.4.18; interface IACL { function initialize(address permissionsCreator) public; function hasPermission(address who, address where, bytes32 what, bytes how) public view returns (bool); } ///File: @aragon/os/contracts/common/IVaultRecoverable.sol pragma solidity ^0.4.18; interface IVaultRecoverable { function transferToVault(address token) external; function allowRecoverability(address token) public view returns (bool); function getRecoveryVault() public view returns (address); } ///File: @aragon/os/contracts/kernel/IKernel.sol pragma solidity ^0.4.18; // This should be an interface, but interfaces can't inherit yet :( contract IKernel is IVaultRecoverable { event SetApp(bytes32 indexed namespace, bytes32 indexed name, bytes32 indexed id, address app); function acl() public view returns (IACL); function hasPermission(address who, address where, bytes32 what, bytes how) public view returns (bool); function setApp(bytes32 namespace, bytes32 name, address app) public returns (bytes32 id); function getApp(bytes32 id) public view returns (address); } ///File: @aragon/os/contracts/apps/AppStorage.sol pragma solidity ^0.4.18; contract AppStorage { IKernel public kernel; bytes32 public appId; address internal pinnedCode; // used by Proxy Pinned uint256 internal initializationBlock; // used by Initializable uint256[95] private storageOffset; // forces App storage to start at after 100 slots uint256 private offset; } ///File: @aragon/os/contracts/common/Initializable.sol pragma solidity ^0.4.18; contract Initializable is AppStorage { modifier onlyInit { require(initializationBlock == 0); _; } modifier isInitialized { require(initializationBlock > 0); _; } /** * @return Block number in which the contract was initialized */ function getInitializationBlock() public view returns (uint256) { return initializationBlock; } /** * @dev Function to be called by top level contract after initialization has finished. */ function initialized() internal onlyInit { initializationBlock = getBlockNumber(); } /** * @dev Returns the current block number. * Using a function rather than `block.number` allows us to easily mock the block number in * tests. */ function getBlockNumber() internal view returns (uint256) { return block.number; } } ///File: @aragon/os/contracts/common/EtherTokenConstant.sol pragma solidity ^0.4.18; // aragonOS and aragon-apps rely on address(0) to denote native ETH, in // contracts where both tokens and ETH are accepted contract EtherTokenConstant { address constant public ETH = address(0); } ///File: @aragon/os/contracts/common/IsContract.sol pragma solidity ^0.4.18; contract IsContract { function isContract(address _target) internal view returns (bool) { if (_target == address(0)) { return false; } uint256 size; assembly { size := extcodesize(_target) } return size > 0; } } ///File: @aragon/os/contracts/lib/zeppelin/token/ERC20Basic.sol pragma solidity ^0.4.11; /** * @title ERC20Basic * @dev Simpler version of ERC20 interface * @dev see https://github.com/ethereum/EIPs/issues/179 */ contract ERC20Basic { function totalSupply() public view returns (uint256); function balanceOf(address who) public constant returns (uint256); function transfer(address to, uint256 value) public returns (bool); event Transfer(address indexed from, address indexed to, uint256 value); } ///File: @aragon/os/contracts/lib/zeppelin/token/ERC20.sol pragma solidity ^0.4.11; import './ERC20Basic.sol'; /** * @title ERC20 interface * @dev see https://github.com/ethereum/EIPs/issues/20 */ contract ERC20 is ERC20Basic { function allowance(address owner, address spender) public constant returns (uint256); function transferFrom(address from, address to, uint256 value) public returns (bool); function approve(address spender, uint256 value) public returns (bool); event Approval(address indexed owner, address indexed spender, uint256 value); } ///File: @aragon/os/contracts/common/VaultRecoverable.sol pragma solidity ^0.4.18; contract VaultRecoverable is IVaultRecoverable, EtherTokenConstant, IsContract { /** * @notice Send funds to recovery Vault. This contract should never receive funds, * but in case it does, this function allows one to recover them. * @param _token Token balance to be sent to recovery vault. */ function transferToVault(address _token) external { require(allowRecoverability(_token)); address vault = getRecoveryVault(); require(isContract(vault)); if (_token == ETH) { vault.transfer(this.balance); } else { uint256 amount = ERC20(_token).balanceOf(this); ERC20(_token).transfer(vault, amount); } } /** * @dev By default deriving from AragonApp makes it recoverable * @param token Token address that would be recovered * @return bool whether the app allows the recovery */ function allowRecoverability(address token) public view returns (bool) { return true; } } ///File: @aragon/os/contracts/evmscript/ScriptHelpers.sol pragma solidity ^0.4.18; library ScriptHelpers { // To test with JS and compare with actual encoder. Maintaining for reference. // t = function() { return IEVMScriptExecutor.at('0x4bcdd59d6c77774ee7317fc1095f69ec84421e49').contract.execScript.getData(...[].slice.call(arguments)).slice(10).match(/.{1,64}/g) } // run = function() { return ScriptHelpers.new().then(sh => { sh.abiEncode.call(...[].slice.call(arguments)).then(a => console.log(a.slice(2).match(/.{1,64}/g)) ) }) } // This is truly not beautiful but lets no daydream to the day solidity gets reflection features function abiEncode(bytes _a, bytes _b, address[] _c) public pure returns (bytes d) { return encode(_a, _b, _c); } function encode(bytes memory _a, bytes memory _b, address[] memory _c) internal pure returns (bytes memory d) { // A is positioned after the 3 position words uint256 aPosition = 0x60; uint256 bPosition = aPosition + 32 * abiLength(_a); uint256 cPosition = bPosition + 32 * abiLength(_b); uint256 length = cPosition + 32 * abiLength(_c); d = new bytes(length); assembly { // Store positions mstore(add(d, 0x20), aPosition) mstore(add(d, 0x40), bPosition) mstore(add(d, 0x60), cPosition) } // Copy memory to correct position copy(d, getPtr(_a), aPosition, _a.length); copy(d, getPtr(_b), bPosition, _b.length); copy(d, getPtr(_c), cPosition, _c.length * 32); // 1 word per address } function abiLength(bytes memory _a) internal pure returns (uint256) { // 1 for length + // memory words + 1 if not divisible for 32 to offset word return 1 + (_a.length / 32) + (_a.length % 32 > 0 ? 1 : 0); } function abiLength(address[] _a) internal pure returns (uint256) { // 1 for length + 1 per item return 1 + _a.length; } function copy(bytes _d, uint256 _src, uint256 _pos, uint256 _length) internal pure { uint dest; assembly { dest := add(add(_d, 0x20), _pos) } memcpy(dest, _src, _length + 32); } function getPtr(bytes memory _x) internal pure returns (uint256 ptr) { assembly { ptr := _x } } function getPtr(address[] memory _x) internal pure returns (uint256 ptr) { assembly { ptr := _x } } function getSpecId(bytes _script) internal pure returns (uint32) { return uint32At(_script, 0); } function uint256At(bytes _data, uint256 _location) internal pure returns (uint256 result) { assembly { result := mload(add(_data, add(0x20, _location))) } } function addressAt(bytes _data, uint256 _location) internal pure returns (address result) { uint256 word = uint256At(_data, _location); assembly { result := div(and(word, 0xffffffffffffffffffffffffffffffffffffffff000000000000000000000000), 0x1000000000000000000000000) } } function uint32At(bytes _data, uint256 _location) internal pure returns (uint32 result) { uint256 word = uint256At(_data, _location); assembly { result := div(and(word, 0xffffffff00000000000000000000000000000000000000000000000000000000), 0x100000000000000000000000000000000000000000000000000000000) } } function locationOf(bytes _data, uint256 _location) internal pure returns (uint256 result) { assembly { result := add(_data, add(0x20, _location)) } } function toBytes(bytes4 _sig) internal pure returns (bytes) { bytes memory payload = new bytes(4); assembly { mstore(add(payload, 0x20), _sig) } return payload; } function memcpy(uint _dest, uint _src, uint _len) internal pure { uint256 src = _src; uint256 dest = _dest; uint256 len = _len; // Copy word-length chunks while possible for (; len >= 32; len -= 32) { assembly { mstore(dest, mload(src)) } dest += 32; src += 32; } // Copy remaining bytes uint mask = 256 ** (32 - len) - 1; assembly { let srcpart := and(mload(src), not(mask)) let destpart := and(mload(dest), mask) mstore(dest, or(destpart, srcpart)) } } } ///File: @aragon/os/contracts/evmscript/IEVMScriptExecutor.sol pragma solidity ^0.4.18; interface IEVMScriptExecutor { function execScript(bytes script, bytes input, address[] blacklist) external returns (bytes); } ///File: @aragon/os/contracts/evmscript/IEVMScriptRegistry.sol pragma solidity ^0.4.18; contract EVMScriptRegistryConstants { /* Hardcoded constants to save gas // repeated definitions from KernelStorage, to avoid out of gas issues bytes32 constant public APP_ADDR_NAMESPACE = keccak256("app"); bytes32 constant public EVMSCRIPT_REGISTRY_APP_ID = apmNamehash("evmreg"); bytes32 constant public EVMSCRIPT_REGISTRY_APP = keccak256(APP_ADDR_NAMESPACE, EVMSCRIPT_REGISTRY_APP_ID); */ bytes32 constant public APP_ADDR_NAMESPACE = 0xd6f028ca0e8edb4a8c9757ca4fdccab25fa1e0317da1188108f7d2dee14902fb; bytes32 constant public EVMSCRIPT_REGISTRY_APP_ID = 0xddbcfd564f642ab5627cf68b9b7d374fb4f8a36e941a75d89c87998cef03bd61; bytes32 constant public EVMSCRIPT_REGISTRY_APP = 0x34f01c17e9be6ddbf2c61f37b5b1fb9f1a090a975006581ad19bda1c4d382871; } interface IEVMScriptRegistry { function addScriptExecutor(address executor) external returns (uint id); function disableScriptExecutor(uint256 executorId) external; function getScriptExecutor(bytes script) public view returns (address); } ///File: @aragon/os/contracts/evmscript/EVMScriptRunner.sol pragma solidity ^0.4.18; contract EVMScriptRunner is AppStorage, EVMScriptRegistryConstants { using ScriptHelpers for bytes; function runScript(bytes _script, bytes _input, address[] _blacklist) protectState internal returns (bytes output) { // TODO: Too much data flying around, maybe extracting spec id here is cheaper address executorAddr = getExecutor(_script); require(executorAddr != address(0)); bytes memory calldataArgs = _script.encode(_input, _blacklist); bytes4 sig = IEVMScriptExecutor(0).execScript.selector; require(executorAddr.delegatecall(sig, calldataArgs)); bytes memory ret = returnedDataDecoded(); require(ret.length > 0); return ret; } function getExecutor(bytes _script) public view returns (IEVMScriptExecutor) { return IEVMScriptExecutor(getExecutorRegistry().getScriptExecutor(_script)); } // TODO: Internal function getExecutorRegistry() internal view returns (IEVMScriptRegistry) { address registryAddr = kernel.getApp(EVMSCRIPT_REGISTRY_APP); return IEVMScriptRegistry(registryAddr); } /** * @dev copies and returns last's call data. Needs to ABI decode first */ function returnedDataDecoded() internal pure returns (bytes ret) { assembly { let size := returndatasize switch size case 0 {} default { ret := mload(0x40) // free mem ptr get mstore(0x40, add(ret, add(size, 0x20))) // free mem ptr set returndatacopy(ret, 0x20, sub(size, 0x20)) // copy return data } } return ret; } modifier protectState { address preKernel = kernel; bytes32 preAppId = appId; _; // exec require(kernel == preKernel); require(appId == preAppId); } } ///File: @aragon/os/contracts/acl/ACLSyntaxSugar.sol pragma solidity ^0.4.18; contract ACLSyntaxSugar { function arr() internal pure returns (uint256[] r) {} function arr(bytes32 _a) internal pure returns (uint256[] r) { return arr(uint256(_a)); } function arr(bytes32 _a, bytes32 _b) internal pure returns (uint256[] r) { return arr(uint256(_a), uint256(_b)); } function arr(address _a) internal pure returns (uint256[] r) { return arr(uint256(_a)); } function arr(address _a, address _b) internal pure returns (uint256[] r) { return arr(uint256(_a), uint256(_b)); } function arr(address _a, uint256 _b, uint256 _c) internal pure returns (uint256[] r) { return arr(uint256(_a), _b, _c); } function arr(address _a, uint256 _b) internal pure returns (uint256[] r) { return arr(uint256(_a), uint256(_b)); } function arr(address _a, address _b, uint256 _c, uint256 _d, uint256 _e) internal pure returns (uint256[] r) { return arr(uint256(_a), uint256(_b), _c, _d, _e); } function arr(address _a, address _b, address _c) internal pure returns (uint256[] r) { return arr(uint256(_a), uint256(_b), uint256(_c)); } function arr(address _a, address _b, uint256 _c) internal pure returns (uint256[] r) { return arr(uint256(_a), uint256(_b), uint256(_c)); } function arr(uint256 _a) internal pure returns (uint256[] r) { r = new uint256[](1); r[0] = _a; } function arr(uint256 _a, uint256 _b) internal pure returns (uint256[] r) { r = new uint256[](2); r[0] = _a; r[1] = _b; } function arr(uint256 _a, uint256 _b, uint256 _c) internal pure returns (uint256[] r) { r = new uint256[](3); r[0] = _a; r[1] = _b; r[2] = _c; } function arr(uint256 _a, uint256 _b, uint256 _c, uint256 _d) internal pure returns (uint256[] r) { r = new uint256[](4); r[0] = _a; r[1] = _b; r[2] = _c; r[3] = _d; } function arr(uint256 _a, uint256 _b, uint256 _c, uint256 _d, uint256 _e) internal pure returns (uint256[] r) { r = new uint256[](5); r[0] = _a; r[1] = _b; r[2] = _c; r[3] = _d; r[4] = _e; } } contract ACLHelpers { function decodeParamOp(uint256 _x) internal pure returns (uint8 b) { return uint8(_x >> (8 * 30)); } function decodeParamId(uint256 _x) internal pure returns (uint8 b) { return uint8(_x >> (8 * 31)); } function decodeParamsList(uint256 _x) internal pure returns (uint32 a, uint32 b, uint32 c) { a = uint32(_x); b = uint32(_x >> (8 * 4)); c = uint32(_x >> (8 * 8)); } } ///File: @aragon/os/contracts/apps/AragonApp.sol pragma solidity ^0.4.18; // ACLSyntaxSugar and EVMScriptRunner are not directly used by this contract, but are included so // that they are automatically usable by subclassing contracts contract AragonApp is AppStorage, Initializable, ACLSyntaxSugar, VaultRecoverable, EVMScriptRunner { modifier auth(bytes32 _role) { require(canPerform(msg.sender, _role, new uint256[](0))); _; } modifier authP(bytes32 _role, uint256[] params) { require(canPerform(msg.sender, _role, params)); _; } function canPerform(address _sender, bytes32 _role, uint256[] params) public view returns (bool) { bytes memory how; // no need to init memory as it is never used if (params.length > 0) { uint256 byteLength = params.length * 32; assembly { how := params // forced casting mstore(how, byteLength) } } return address(kernel) == 0 || kernel.hasPermission(_sender, address(this), _role, how); } function getRecoveryVault() public view returns (address) { // Funds recovery via a vault is only available when used with a kernel require(address(kernel) != 0); return kernel.getRecoveryVault(); } } ///File: ./contracts/ILiquidPledgingPlugin.sol pragma solidity ^0.4.0; /* Copyright 2018, Jordi Baylina Contributors: AdriĆ  Massanet , RJ Ewing, Griff Green, Arthur Lunn This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ /// @dev `ILiquidPledgingPlugin` is the basic interface for any /// liquid pledging plugin contract ILiquidPledgingPlugin { /// @notice Plugins are used (much like web hooks) to initiate an action /// upon any donation, delegation, or transfer; this is an optional feature /// and allows for extreme customization of the contract. This function /// implements any action that should be initiated before a transfer. /// @param pledgeManager The admin or current manager of the pledge /// @param pledgeFrom This is the Id from which value will be transfered. /// @param pledgeTo This is the Id that value will be transfered to. /// @param context The situation that is triggering the plugin: /// 0 -> Plugin for the owner transferring pledge to another party /// 1 -> Plugin for the first delegate transferring pledge to another party /// 2 -> Plugin for the second delegate transferring pledge to another party /// ... /// 255 -> Plugin for the intendedProject transferring pledge to another party /// /// 256 -> Plugin for the owner receiving pledge to another party /// 257 -> Plugin for the first delegate receiving pledge to another party /// 258 -> Plugin for the second delegate receiving pledge to another party /// ... /// 511 -> Plugin for the intendedProject receiving pledge to another party /// @param amount The amount of value that will be transfered. function beforeTransfer( uint64 pledgeManager, uint64 pledgeFrom, uint64 pledgeTo, uint64 context, address token, uint amount ) public returns (uint maxAllowed); /// @notice Plugins are used (much like web hooks) to initiate an action /// upon any donation, delegation, or transfer; this is an optional feature /// and allows for extreme customization of the contract. This function /// implements any action that should be initiated after a transfer. /// @param pledgeManager The admin or current manager of the pledge /// @param pledgeFrom This is the Id from which value will be transfered. /// @param pledgeTo This is the Id that value will be transfered to. /// @param context The situation that is triggering the plugin: /// 0 -> Plugin for the owner transferring pledge to another party /// 1 -> Plugin for the first delegate transferring pledge to another party /// 2 -> Plugin for the second delegate transferring pledge to another party /// ... /// 255 -> Plugin for the intendedProject transferring pledge to another party /// /// 256 -> Plugin for the owner receiving pledge to another party /// 257 -> Plugin for the first delegate receiving pledge to another party /// 258 -> Plugin for the second delegate receiving pledge to another party /// ... /// 511 -> Plugin for the intendedProject receiving pledge to another party /// @param amount The amount of value that will be transfered. function afterTransfer( uint64 pledgeManager, uint64 pledgeFrom, uint64 pledgeTo, uint64 context, address token, uint amount ) public; } ///File: ./contracts/LiquidPledgingStorage.sol pragma solidity ^0.4.18; /// @dev This is an interface for `LPVault` which serves as a secure storage for /// the ETH that backs the Pledges, only after `LiquidPledging` authorizes /// payments can Pledges be converted for ETH interface ILPVault { function authorizePayment(bytes32 _ref, address _dest, address _token, uint _amount) public; } /// This contract contains all state variables used in LiquidPledging contracts /// This is done to have everything in 1 location, b/c state variable layout /// is MUST have be the same when performing an upgrade. contract LiquidPledgingStorage { enum PledgeAdminType { Giver, Delegate, Project } enum PledgeState { Pledged, Paying, Paid } /// @dev This struct defines the details of a `PledgeAdmin` which are /// commonly referenced by their index in the `admins` array /// and can own pledges and act as delegates struct PledgeAdmin { PledgeAdminType adminType; // Giver, Delegate or Project address addr; // Account or contract address for admin uint64 commitTime; // In seconds, used for time Givers' & Delegates' have to veto uint64 parentProject; // Only for projects bool canceled; //Always false except for canceled projects /// @dev if the plugin is 0x0 then nothing happens, if its an address // than that smart contract is called when appropriate ILiquidPledgingPlugin plugin; string name; string url; // Can be IPFS hash } struct Pledge { uint amount; uint64[] delegationChain; // List of delegates in order of authority uint64 owner; // PledgeAdmin uint64 intendedProject; // Used when delegates are sending to projects uint64 commitTime; // When the intendedProject will become the owner uint64 oldPledge; // Points to the id that this Pledge was derived from address token; PledgeState pledgeState; // Pledged, Paying, Paid } PledgeAdmin[] admins; //The list of pledgeAdmins 0 means there is no admin Pledge[] pledges; /// @dev this mapping allows you to search for a specific pledge's /// index number by the hash of that pledge mapping (bytes32 => uint64) hPledge2idx; // this whitelist is for non-proxied plugins mapping (bytes32 => bool) pluginContractWhitelist; // this whitelist is for proxied plugins mapping (address => bool) pluginInstanceWhitelist; bool public whitelistDisabled = false; ILPVault public vault; // reserve 50 slots for future upgrades. uint[50] private storageOffset; } ///File: ./contracts/Pledges.sol pragma solidity ^0.4.18; /* Copyright 2017, Jordi Baylina, RJ Ewing Contributors: AdriĆ  Massanet , Griff Green, Arthur Lunn This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ contract Pledges is AragonApp, LiquidPledgingStorage { // Limits inserted to prevent large loops that could prevent canceling uint constant MAX_DELEGATES = 10; // a constant for when a delegate is requested that is not in the system uint64 constant NOTFOUND = 0xFFFFFFFFFFFFFFFF; ///////////////////////////// // Public constant functions //////////////////////////// /// @notice A constant getter that returns the total number of pledges /// @return The total number of Pledges in the system function numberOfPledges() external view returns (uint) { return pledges.length - 1; } /// @notice A getter that returns the details of the specified pledge /// @param idPledge the id number of the pledge being queried /// @return the amount, owner, the number of delegates (but not the actual /// delegates, the intendedProject (if any), the current commit time and /// the previous pledge this pledge was derived from function getPledge(uint64 idPledge) external view returns( uint amount, uint64 owner, uint64 nDelegates, uint64 intendedProject, uint64 commitTime, uint64 oldPledge, address token, PledgeState pledgeState ) { Pledge memory p = _findPledge(idPledge); amount = p.amount; owner = p.owner; nDelegates = uint64(p.delegationChain.length); intendedProject = p.intendedProject; commitTime = p.commitTime; oldPledge = p.oldPledge; token = p.token; pledgeState = p.pledgeState; } //////////////////// // Internal methods //////////////////// /// @notice This creates a Pledge with an initial amount of 0 if one is not /// created already; otherwise it finds the pledge with the specified /// attributes; all pledges technically exist, if the pledge hasn't been /// created in this system yet it simply isn't in the hash array /// hPledge2idx[] yet /// @param owner The owner of the pledge being looked up /// @param delegationChain The list of delegates in order of authority /// @param intendedProject The project this pledge will Fund after the /// commitTime has passed /// @param commitTime The length of time in seconds the Giver has to /// veto when the Giver's delegates Pledge funds to a project /// @param oldPledge This value is used to store the pledge the current /// pledge was came from, and in the case a Project is canceled, the Pledge /// will revert back to it's previous state /// @param state The pledge state: Pledged, Paying, or state /// @return The hPledge2idx index number function _findOrCreatePledge( uint64 owner, uint64[] delegationChain, uint64 intendedProject, uint64 commitTime, uint64 oldPledge, address token, PledgeState state ) internal returns (uint64) { bytes32 hPledge = keccak256(delegationChain, owner, intendedProject, commitTime, oldPledge, token, state); uint64 id = hPledge2idx[hPledge]; if (id > 0) { return id; } id = uint64(pledges.length); hPledge2idx[hPledge] = id; pledges.push( Pledge( 0, delegationChain, owner, intendedProject, commitTime, oldPledge, token, state ) ); return id; } /// @param idPledge the id of the pledge to load from storage /// @return The Pledge function _findPledge(uint64 idPledge) internal view returns(Pledge storage) { require(idPledge < pledges.length); return pledges[idPledge]; } /// @notice A getter that searches the delegationChain for the level of /// authority a specific delegate has within a Pledge /// @param p The Pledge that will be searched /// @param idDelegate The specified delegate that's searched for /// @return If the delegate chain contains the delegate with the /// `admins` array index `idDelegate` this returns that delegates /// corresponding index in the delegationChain. Otherwise it returns /// the NOTFOUND constant function _getDelegateIdx(Pledge p, uint64 idDelegate) internal pure returns(uint64) { for (uint i = 0; i < p.delegationChain.length; i++) { if (p.delegationChain[i] == idDelegate) { return uint64(i); } } return NOTFOUND; } /// @notice A getter to find how many old "parent" pledges a specific Pledge /// had using a self-referential loop /// @param p The Pledge being queried /// @return The number of old "parent" pledges a specific Pledge had function _getPledgeLevel(Pledge p) internal view returns(uint) { if (p.oldPledge == 0) { return 0; } Pledge storage oldP = _findPledge(p.oldPledge); return _getPledgeLevel(oldP) + 1; // a loop lookup } }