Cryo Explorer Ethereum Mainnet

Address Contract Verified

Address 0xD9ED413bCF58c266F95fE6BA63B13cf79299CE31
Balance 0 ETH
Nonce 1
Code Size 6547 bytes
Indexed Transactions 0
External Etherscan · Sourcify

Contract Bytecode

6547 bytes
0x608060405234801561001057600080fd5b50600436106101375760003560e01c806379f171b2116100b8578063c4d66de81161007c578063c4d66de81461026e578063cbcbb50714610281578063cc69afec14610249578063dde43cba14610289578063f11b818814610291578063f5cf673b146102b357610137565b806379f171b2146102235780638b599f2614610236578063919cd40f1461024957806399248ea714610251578063aaf5eb681461025957610137565b80633373ee4c116100ff5780633373ee4c146101c457806339ccbdd3146101d757806341485304146101ea5780636d34b96e146101fd57806374d945ec1461021057610137565b80631652e7b71461013c578063198fa81e146101675780631c39b672146101875780633111e7b31461019c57806331873e2e146101af575b600080fd5b61014f61014a3660046114ec565b6102c6565b60405161015e9392919061190b565b60405180910390f35b61017a6101753660046114ec565b610306565b60405161015e9190611902565b61018f610325565b60405161015e9190611789565b61017a6101aa36600461166f565b610349565b6101c26101bd366004611538565b610393565b005b61017a6101d2366004611506565b610428565b6101c26101e5366004611736565b610459565b61017a6101f8366004611625565b6104e1565b61017a61020b3660046116ca565b6104fb565b61018f61021e3660046114ec565b6105a0565b6101c26102313660046115bc565b6105be565b61017a61024436600461156a565b610823565b61017a6109da565b61018f6109e0565b610261610a04565b60405161015e9190611921565b6101c261027c3660046114ec565b610a09565b61018f610a5b565b61017a610a7f565b6102a461029f3660046114ec565b610a84565b60405161015e939291906118d8565b6101c26102c1366004611506565b610abb565b6001600160a01b0316600090815260336020526040902054600160681b81046001600160681b039081169290821691600160d01b900464ffffffffff1690565b6001600160a01b0381166000908152603560205260409020545b919050565b7f0000000000000000000000004da27a545c0c5b758a6ba100e3a049001de870f581565b60006001600160a01b03821661037a5760405162461bcd60e51b8152600401610371906117e4565b60405180910390fd5b610388858585333387610b5a565b90505b949350505050565b60006103a184338486610e82565b90508015610422576001600160a01b0384166000908152603560205260409020546103cc9082610f41565b6001600160a01b038516600081815260356020526040908190209290925590517f2468f9268c60ad90e2d49edb0032c8a001e733ae888b3ab8e982edf535be1a7690610419908490611902565b60405180910390a25b50505050565b6001600160a01b03808216600090815260336020908152604080832093861683526001909301905220545b92915050565b336001600160a01b037f000000000000000000000000ee56e2b3d491590b5b31738cc34d5232f378a8d516146104a15760405162461bcd60e51b815260040161037190611867565b60348190556040517f1cc1849a6602c3e91f2088cadea4381cc5717f2f28584197060ed2ebb434c16f906104d6908390611902565b60405180910390a150565b60006104f1848484333333610b5a565b90505b9392505050565b6001600160a01b0380831660009081526036602052604081205490913391859116821461053a5760405162461bcd60e51b815260040161037190611896565b6001600160a01b0385166105605760405162461bcd60e51b8152600401610371906117b6565b6001600160a01b0384166105865760405162461bcd60e51b8152600401610371906117e4565b610594888888338989610b5a565b98975050505050505050565b6001600160a01b039081166000908152603660205260409020541690565b336001600160a01b037f000000000000000000000000ee56e2b3d491590b5b31738cc34d5232f378a8d516146106065760405162461bcd60e51b815260040161037190611867565b8281146106255760405162461bcd60e51b815260040161037190611810565b60608367ffffffffffffffff8111801561063e57600080fd5b5060405190808252806020026020018201604052801561067857816020015b610665611442565b81526020019060019003908161065d5790505b50905060005b848110156108125785858281811061069257fe5b90506020020160208101906106a791906114ec565b8282815181106106b357fe5b6020026020010151604001906001600160a01b031690816001600160a01b0316815250508383828181106106e357fe5b905060200201358282815181106106f657fe5b60209081029190910101516001600160681b03909116905283838281811061071a57fe5b9050602002013582828151811061072d57fe5b6020026020010151600001516001600160681b03161461075f5760405162461bcd60e51b815260040161037190611810565b85858281811061076b57fe5b905060200201602081019061078091906114ec565b6001600160a01b031663b1bf962d6040518163ffffffff1660e01b815260040160206040518083038186803b1580156107b857600080fd5b505afa1580156107cc573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906107f0919061174e565b8282815181106107fc57fe5b602090810291909101810151015260010161067e565b5061081c81610f51565b5050505050565b6001600160a01b03811660009081526035602052604081205460608467ffffffffffffffff8111801561085557600080fd5b5060405190808252806020026020018201604052801561088f57816020015b61087c611462565b8152602001906001900390816108745790505b50905060005b858110156109bb578686828181106108a957fe5b90506020020160208101906108be91906114ec565b8282815181106108ca57fe5b60209081029190910101516001600160a01b0390911690528686828181106108ee57fe5b905060200201602081019061090391906114ec565b6001600160a01b0316630afbcdc9866040518263ffffffff1660e01b815260040161092e9190611789565b604080518083038186803b15801561094557600080fd5b505afa158015610959573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525081019061097d9190611766565b83838151811061098957fe5b60200260200101516020018484815181106109a057fe5b60209081029190910101516040019190915252600101610895565b506109d06109c9858361108d565b8390610f41565b9695505050505050565b60345490565b7f0000000000000000000000004da27a545c0c5b758a6ba100e3a049001de870f590565b601281565b6000610a1361118b565b90506000548111610a555760405162461bcd60e51b815260040180806020018281038252602e815260200180611930602e913960400191505060405180910390fd5b60005550565b7f000000000000000000000000ee56e2b3d491590b5b31738cc34d5232f378a8d581565b600281565b6033602052600090815260409020546001600160681b0380821691600160681b810490911690600160d01b900464ffffffffff1683565b336001600160a01b037f000000000000000000000000ee56e2b3d491590b5b31738cc34d5232f378a8d51614610b035760405162461bcd60e51b815260040161037190611867565b6001600160a01b0382811660008181526036602052604080822080546001600160a01b0319169486169485179055517f4925eafc82d0c4d67889898eeed64b18488ab19811e61620f387026dec126a289190a35050565b600084610b69575060006109d0565b6001600160a01b03831660009081526035602052604090205460608767ffffffffffffffff81118015610b9b57600080fd5b50604051908082528060200260200182016040528015610bd557816020015b610bc2611462565b815260200190600190039081610bba5790505b50905060005b88811015610d0157898982818110610bef57fe5b9050602002016020810190610c0491906114ec565b828281518110610c1057fe5b60209081029190910101516001600160a01b039091169052898982818110610c3457fe5b9050602002016020810190610c4991906114ec565b6001600160a01b0316630afbcdc9876040518263ffffffff1660e01b8152600401610c749190611789565b604080518083038186803b158015610c8b57600080fd5b505afa158015610c9f573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190610cc39190611766565b838381518110610ccf57fe5b6020026020010151602001848481518110610ce657fe5b60209081029190910101516040019190915252600101610bdb565b506000610d0e8683611190565b90508015610d6457610d208382610f41565b9250856001600160a01b03167f2468f9268c60ad90e2d49edb0032c8a001e733ae888b3ab8e982edf535be1a7682604051610d5b9190611902565b60405180910390a25b82610d7557600093505050506109d0565b6000838911610d845788610d86565b835b6001600160a01b03808916600090815260356020526040908190208388039055516356e4bb9760e11b81529192507f0000000000000000000000004da27a545c0c5b758a6ba100e3a049001de870f5169063adc9772e90610ded908990859060040161179d565b600060405180830381600087803b158015610e0757600080fd5b505af1158015610e1b573d6000803e3d6000fd5b50505050876001600160a01b0316866001600160a01b0316886001600160a01b03167f5637d7f962248a7f05a7ab69eec6446e31f3d0a299d997f135a65c62806e789184604051610e6c9190611902565b60405180910390a49a9950505050505050505050565b6001600160a01b0380841660009081526033602090815260408083209388168352600184019091528120549091908280610ebd8885886111fc565b9050808314610f35578615610eda57610ed787828561132c565b91505b6001600160a01b03808a1660008181526001870160205260409081902084905551918a16917fbb123b5c06d5408bbea3c4fef481578175cfb432e3b482c6186f02ed9086585b90610f2c908590611902565b60405180910390a35b50979650505050505050565b8082018281101561045357600080fd5b60005b815181101561108957600060336000848481518110610f6f57fe5b6020026020010151604001516001600160a01b03166001600160a01b031681526020019081526020016000209050610fd6838381518110610fac57fe5b60200260200101516040015182858581518110610fc557fe5b6020026020010151602001516111fc565b50828281518110610fe357fe5b60209081029190910101515181546cffffffffffffffffffffffffff19166001600160681b03909116178155825183908390811061101d57fe5b6020026020010151604001516001600160a01b03167f87fa03892a0556cb6b8f97e6d533a150d4d55fcbf275fff5fa003fa636bcc7fa84848151811061105f57fe5b60200260200101516000015160405161107891906118c4565b60405180910390a250600101610f54565b5050565b600080805b8351811015611183576000603360008684815181106110ad57fe5b602090810291909101810151516001600160a01b0316825281019190915260400160009081208054875191935061112291600160681b82046001600160681b039081169290811691600160d01b90910464ffffffffff16908a908890811061111157fe5b60200260200101516040015161135b565b905061117761117087858151811061113657fe5b602002602001015160200151838560010160008c6001600160a01b03166001600160a01b031681526020019081526020016000205461132c565b8590610f41565b93505050600101611092565b509392505050565b600290565b600080805b8351811015611183576111f26109c9868684815181106111b157fe5b6020026020010151600001518785815181106111c957fe5b6020026020010151602001518886815181106111e157fe5b602002602001015160400151610e82565b9150600101611195565b81546000906001600160681b03600160681b82048116919081169064ffffffffff600160d01b9091041642811415611239578293505050506104f4565b60006112478484848961135b565b90508381146113025780816001600160681b0316146112785760405162461bcd60e51b81526004016103719061183f565b86546cffffffffffffffffffffffffff60681b1916600160681b6001600160681b038316021764ffffffffff60d01b1916600160d01b4264ffffffffff16021787556040516001600160a01b038916907f5777ca300dfe5bead41006fbce4389794dbc0ed8d6cccebfaf94630aa04184bc906112f5908490611902565b60405180910390a2611321565b865464ffffffffff60d01b1916600160d01b4264ffffffffff16021787555b979650505050505050565b6000670de0b6b3a764000061134b61134485856113fb565b869061140b565b8161135257fe5b04949350505050565b60345460009084158061136c575082155b8061137f575042846001600160801b0316145b80611393575080846001600160801b031610155b156113a1578591505061038b565b60008142116113b057426113b2565b815b905060006113c9826001600160801b0388166113fb565b9050610594886113f5876113ef670de0b6b3a76400006113e98d8861140b565b9061140b565b9061142f565b90610f41565b8082038281111561045357600080fd5b60008215806114265750508181028183828161142357fe5b04145b61045357600080fd5b600081838161143a57fe5b049392505050565b604080516060810182526000808252602082018190529181019190915290565b604051806060016040528060006001600160a01b0316815260200160008152602001600081525090565b80356001600160a01b038116811461032057600080fd5b60008083601f8401126114b4578182fd5b50813567ffffffffffffffff8111156114cb578182fd5b60208301915083602080830285010111156114e557600080fd5b9250929050565b6000602082840312156114fd578081fd5b6104f48261148c565b60008060408385031215611518578081fd5b6115218361148c565b915061152f6020840161148c565b90509250929050565b60008060006060848603121561154c578081fd5b6115558461148c565b95602085013595506040909401359392505050565b60008060006040848603121561157e578283fd5b833567ffffffffffffffff811115611594578384fd5b6115a0868287016114a3565b90945092506115b390506020850161148c565b90509250925092565b600080600080604085870312156115d1578081fd5b843567ffffffffffffffff808211156115e8578283fd5b6115f4888389016114a3565b9096509450602087013591508082111561160c578283fd5b50611619878288016114a3565b95989497509550505050565b600080600060408486031215611639578283fd5b833567ffffffffffffffff81111561164f578384fd5b61165b868287016114a3565b909790965060209590950135949350505050565b60008060008060608587031215611684578384fd5b843567ffffffffffffffff81111561169a578485fd5b6116a6878288016114a3565b909550935050602085013591506116bf6040860161148c565b905092959194509250565b6000806000806000608086880312156116e1578081fd5b853567ffffffffffffffff8111156116f7578182fd5b611703888289016114a3565b9096509450506020860135925061171c6040870161148c565b915061172a6060870161148c565b90509295509295909350565b600060208284031215611747578081fd5b5035919050565b60006020828403121561175f578081fd5b5051919050565b60008060408385031215611778578182fd5b505080516020909101519092909150565b6001600160a01b0391909116815260200190565b6001600160a01b03929092168252602082015260400190565b602080825260149082015273494e56414c49445f555345525f4144445245535360601b604082015260600190565b602080825260129082015271494e56414c49445f544f5f4144445245535360701b604082015260600190565b60208082526015908201527424a72b20a624a22fa1a7a72324a3aaa920aa24a7a760591b604082015260600190565b6020808252600e908201526d496e646578206f766572666c6f7760901b604082015260600190565b60208082526015908201527427a7262cafa2a6a4a9a9a4a7a72fa6a0a720a3a2a960591b604082015260600190565b60208082526014908201527310d3105253515497d5539055551213d49256915160621b604082015260600190565b6001600160681b0391909116815260200190565b6001600160681b03938416815291909216602082015264ffffffffff909116604082015260600190565b90815260200190565b9283526020830191909152604082015260600190565b60ff9190911681526020019056fe436f6e747261637420696e7374616e63652068617320616c7265616479206265656e20696e697469616c697a6564a26469706673582212204e55ba81be283f95a8fbd5cd467a70e07e6bbf42411d1890d365397d6a348b5c64736f6c63430007050033

Verified Source Code Full Match

Compiler: v0.7.5+commit.eb77ed08 EVM: istanbul Optimization: Yes (200 runs)
SafeMath.sol 37 lines
// SPDX-License-Identifier: agpl-3.0
pragma solidity ^0.7.5;

/// @title Optimized overflow and underflow safe math operations
/// @notice Contains methods for doing math operations that revert on overflow or underflow for minimal gas cost
/// inspired by uniswap V3
library SafeMath {
    /// @notice Returns x + y, reverts if sum overflows uint256
    /// @param x The augend
    /// @param y The addend
    /// @return z The sum of x and y
    function add(uint256 x, uint256 y) internal pure returns (uint256 z) {
        require((z = x + y) >= x);
    }

    /// @notice Returns x - y, reverts if underflows
    /// @param x The minuend
    /// @param y The subtrahend
    /// @return z The difference of x and y
    function sub(uint256 x, uint256 y) internal pure returns (uint256 z) {
        require((z = x - y) <= x);
    }

    /// @notice Returns x * y, reverts if overflows
    /// @param x The multiplicand
    /// @param y The multiplier
    /// @return z The product of x and y
    function mul(uint256 x, uint256 y) internal pure returns (uint256 z) {
        require(x == 0 || (z = x * y) / x == y);
    }

    function div(uint256 x, uint256 y) internal pure returns(uint256) {
        // no need to check for division by zero - solidity already reverts
        return x / y;
    }
}
DistributionTypes.sol 17 lines
// SPDX-License-Identifier: agpl-3.0
pragma solidity 0.7.5;
pragma experimental ABIEncoderV2;

library DistributionTypes {
  struct AssetConfigInput {
    uint104 emissionPerSecond;
    uint256 totalStaked;
    address underlyingAsset;
  }

  struct UserStakeInput {
    address underlyingAsset;
    uint256 stakedByUser;
    uint256 totalStaked;
  }
}
Address.sol 62 lines
// SPDX-License-Identifier: MIT
pragma solidity 0.7.5;

/**
 * @dev Collection of functions related to the address type
 * From https://github.com/OpenZeppelin/openzeppelin-contracts
 */
library Address {
  /**
   * @dev Returns true if `account` is a contract.
   *
   * [IMPORTANT]
   * ====
   * It is unsafe to assume that an address for which this function returns
   * false is an externally-owned account (EOA) and not a contract.
   *
   * Among others, `isContract` will return false for the following
   * types of addresses:
   *
   *  - an externally-owned account
   *  - a contract in construction
   *  - an address where a contract will be created
   *  - an address where a contract lived, but was destroyed
   * ====
   */
  function isContract(address account) internal view returns (bool) {
    // According to EIP-1052, 0x0 is the value returned for not-yet created accounts
    // and 0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470 is returned
    // for accounts without code, i.e. `keccak256('')`
    bytes32 codehash;
    bytes32 accountHash = 0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470;
    // solhint-disable-next-line no-inline-assembly
    assembly {
      codehash := extcodehash(account)
    }
    return (codehash != accountHash && codehash != 0x0);
  }

  /**
   * @dev Replacement for Solidity's `transfer`: sends `amount` wei to
   * `recipient`, forwarding all available gas and reverting on errors.
   *
   * https://eips.ethereum.org/EIPS/eip-1884[EIP1884] increases the gas cost
   * of certain opcodes, possibly making contracts go over the 2300 gas limit
   * imposed by `transfer`, making them unable to receive funds via
   * `transfer`. {sendValue} removes this limitation.
   *
   * https://diligence.consensys.net/posts/2019/09/stop-using-soliditys-transfer-now/[Learn more].
   *
   * IMPORTANT: because control is transferred to `recipient`, care must be
   * taken to not create reentrancy vulnerabilities. Consider using
   * {ReentrancyGuard} or the
   * https://solidity.readthedocs.io/en/v0.5.11/security-considerations.html#use-the-checks-effects-interactions-pattern[checks-effects-interactions pattern].
   */
  function sendValue(address payable recipient, uint256 amount) internal {
    require(address(this).balance >= amount, 'Address: insufficient balance');

    // solhint-disable-next-line avoid-low-level-calls, avoid-call-value
    (bool success, ) = recipient.call{value: amount}('');
    require(success, 'Address: unable to send value, recipient may have reverted');
  }
}
SafeMath.sol 164 lines
// SPDX-License-Identifier: agpl-3.0
pragma solidity 0.7.5;

/**
 * @dev From https://github.com/OpenZeppelin/openzeppelin-contracts
 * Wrappers over Solidity's arithmetic operations with added overflow
 * checks.
 *
 * Arithmetic operations in Solidity wrap on overflow. This can easily result
 * in bugs, because programmers usually assume that an overflow raises an
 * error, which is the standard behavior in high level programming languages.
 * `SafeMath` restores this intuition by reverting the transaction when an
 * operation overflows.
 *
 * Using this library instead of the unchecked operations eliminates an entire
 * class of bugs, so it's recommended to use it always.
 */
library SafeMath {
  /**
   * @dev Returns the addition of two unsigned integers, reverting on
   * overflow.
   *
   * Counterpart to Solidity's `+` operator.
   *
   * Requirements:
   * - Addition cannot overflow.
   */
  function add(uint256 a, uint256 b) internal pure returns (uint256) {
    uint256 c = a + b;
    require(c >= a, 'SafeMath: addition overflow');

    return c;
  }

  /**
   * @dev Returns the subtraction of two unsigned integers, reverting on
   * overflow (when the result is negative).
   *
   * Counterpart to Solidity's `-` operator.
   *
   * Requirements:
   * - Subtraction cannot overflow.
   */
  function sub(uint256 a, uint256 b) internal pure returns (uint256) {
    return sub(a, b, 'SafeMath: subtraction overflow');
  }

  /**
   * @dev Returns the subtraction of two unsigned integers, reverting with custom message on
   * overflow (when the result is negative).
   *
   * Counterpart to Solidity's `-` operator.
   *
   * Requirements:
   * - Subtraction cannot overflow.
   */
  function sub(
    uint256 a,
    uint256 b,
    string memory errorMessage
  ) internal pure returns (uint256) {
    require(b <= a, errorMessage);
    uint256 c = a - b;

    return c;
  }

  /**
   * @dev Returns the multiplication of two unsigned integers, reverting on
   * overflow.
   *
   * Counterpart to Solidity's `*` operator.
   *
   * Requirements:
   * - Multiplication cannot overflow.
   */
  function mul(uint256 a, uint256 b) internal pure returns (uint256) {
    // Gas optimization: this is cheaper than requiring 'a' not being zero, but the
    // benefit is lost if 'b' is also tested.
    // See: https://github.com/OpenZeppelin/openzeppelin-contracts/pull/522
    if (a == 0) {
      return 0;
    }

    uint256 c = a * b;
    require(c / a == b, 'SafeMath: multiplication overflow');

    return c;
  }

  /**
   * @dev Returns the integer division of two unsigned integers. Reverts on
   * division by zero. The result is rounded towards zero.
   *
   * Counterpart to Solidity's `/` operator. Note: this function uses a
   * `revert` opcode (which leaves remaining gas untouched) while Solidity
   * uses an invalid opcode to revert (consuming all remaining gas).
   *
   * Requirements:
   * - The divisor cannot be zero.
   */
  function div(uint256 a, uint256 b) internal pure returns (uint256) {
    return div(a, b, 'SafeMath: division by zero');
  }

  /**
   * @dev Returns the integer division of two unsigned integers. Reverts with custom message on
   * division by zero. The result is rounded towards zero.
   *
   * Counterpart to Solidity's `/` operator. Note: this function uses a
   * `revert` opcode (which leaves remaining gas untouched) while Solidity
   * uses an invalid opcode to revert (consuming all remaining gas).
   *
   * Requirements:
   * - The divisor cannot be zero.
   */
  function div(
    uint256 a,
    uint256 b,
    string memory errorMessage
  ) internal pure returns (uint256) {
    // Solidity only automatically asserts when dividing by 0
    require(b > 0, errorMessage);
    uint256 c = a / b;
    // assert(a == b * c + a % b); // There is no case in which this doesn't hold

    return c;
  }

  /**
   * @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo),
   * Reverts when dividing by zero.
   *
   * Counterpart to Solidity's `%` operator. This function uses a `revert`
   * opcode (which leaves remaining gas untouched) while Solidity uses an
   * invalid opcode to revert (consuming all remaining gas).
   *
   * Requirements:
   * - The divisor cannot be zero.
   */
  function mod(uint256 a, uint256 b) internal pure returns (uint256) {
    return mod(a, b, 'SafeMath: modulo by zero');
  }

  /**
   * @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo),
   * Reverts with custom message when dividing by zero.
   *
   * Counterpart to Solidity's `%` operator. This function uses a `revert`
   * opcode (which leaves remaining gas untouched) while Solidity uses an
   * invalid opcode to revert (consuming all remaining gas).
   *
   * Requirements:
   * - The divisor cannot be zero.
   */
  function mod(
    uint256 a,
    uint256 b,
    string memory errorMessage
  ) internal pure returns (uint256) {
    require(b != 0, errorMessage);
    return a % b;
  }
}
SafeERC20.sol 65 lines
// SPDX-License-Identifier: MIT

pragma solidity 0.7.5;

import {IERC20} from '../interfaces/IERC20.sol';
import {SafeMath} from './SafeMath.sol';
import {Address} from './Address.sol';

/**
 * @title SafeERC20
 * @dev From https://github.com/OpenZeppelin/openzeppelin-contracts
 * Wrappers around ERC20 operations that throw on failure (when the token
 * contract returns false). Tokens that return no value (and instead revert or
 * throw on failure) are also supported, non-reverting calls are assumed to be
 * successful.
 * To use this library you can add a `using SafeERC20 for IERC20;` statement to your contract,
 * which allows you to call the safe operations as `token.safeTransfer(...)`, etc.
 */
library SafeERC20 {
  using SafeMath for uint256;
  using Address for address;

  function safeTransfer(
    IERC20 token,
    address to,
    uint256 value
  ) internal {
    callOptionalReturn(token, abi.encodeWithSelector(token.transfer.selector, to, value));
  }

  function safeTransferFrom(
    IERC20 token,
    address from,
    address to,
    uint256 value
  ) internal {
    callOptionalReturn(token, abi.encodeWithSelector(token.transferFrom.selector, from, to, value));
  }

  function safeApprove(
    IERC20 token,
    address spender,
    uint256 value
  ) internal {
    require(
      (value == 0) || (token.allowance(address(this), spender) == 0),
      'SafeERC20: approve from non-zero to non-zero allowance'
    );
    callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, value));
  }

  function callOptionalReturn(IERC20 token, bytes memory data) private {
    require(address(token).isContract(), 'SafeERC20: call to non-contract');

    // solhint-disable-next-line avoid-low-level-calls
    (bool success, bytes memory returndata) = address(token).call(data);
    require(success, 'SafeERC20: low-level call failed');

    if (returndata.length > 0) {
      // Return data is optional
      // solhint-disable-next-line max-line-length
      require(abi.decode(returndata, (bool)), 'SafeERC20: ERC20 operation did not succeed');
    }
  }
}
DistributionManager.sol 263 lines
// SPDX-License-Identifier: agpl-3.0
pragma solidity 0.7.5;
pragma experimental ABIEncoderV2;

import {IAaveDistributionManager} from '../interfaces/IAaveDistributionManager.sol';
import {SafeMath} from '../lib/SafeMath.sol';
import {DistributionTypes} from '../lib/DistributionTypes.sol';

/**
 * @title DistributionManager
 * @notice Accounting contract to manage multiple staking distributions
 * @author Aave
 **/
contract DistributionManager is IAaveDistributionManager {
  using SafeMath for uint256;

  struct AssetData {
    uint104 emissionPerSecond;
    uint104 index;
    uint40 lastUpdateTimestamp;
    mapping(address => uint256) users;
  }

  address public immutable EMISSION_MANAGER;

  uint8 public constant PRECISION = 18;

  mapping(address => AssetData) public assets;

  uint256 internal _distributionEnd;

  modifier onlyEmissionManager() {
    require(msg.sender == EMISSION_MANAGER, 'ONLY_EMISSION_MANAGER');
    _;
  }

  constructor(address emissionManager) {
    EMISSION_MANAGER = emissionManager;
  }

  /// @inheritdoc IAaveDistributionManager
  function setDistributionEnd(uint256 distributionEnd) external override onlyEmissionManager {
    _distributionEnd = distributionEnd;
    emit DistributionEndUpdated(distributionEnd);
  }

  /// @inheritdoc IAaveDistributionManager
  function getDistributionEnd() external view override returns (uint256) {
    return _distributionEnd;
  }

  /// @inheritdoc IAaveDistributionManager
  function DISTRIBUTION_END() external view override returns (uint256) {
    return _distributionEnd;
  }

  /// @inheritdoc IAaveDistributionManager
  function getUserAssetData(address user, address asset) public view override returns (uint256) {
    return assets[asset].users[user];
  }

  /// @inheritdoc IAaveDistributionManager
  function getAssetData(address asset) public view override returns (uint256, uint256, uint256) {
    return (assets[asset].index, assets[asset].emissionPerSecond, assets[asset].lastUpdateTimestamp);
  }

  /**
   * @dev Configure the assets for a specific emission
   * @param assetsConfigInput The array of each asset configuration
   **/
  function _configureAssets(DistributionTypes.AssetConfigInput[] memory assetsConfigInput)
    internal
  {
    for (uint256 i = 0; i < assetsConfigInput.length; i++) {
      AssetData storage assetConfig = assets[assetsConfigInput[i].underlyingAsset];

      _updateAssetStateInternal(
        assetsConfigInput[i].underlyingAsset,
        assetConfig,
        assetsConfigInput[i].totalStaked
      );

      assetConfig.emissionPerSecond = assetsConfigInput[i].emissionPerSecond;

      emit AssetConfigUpdated(
        assetsConfigInput[i].underlyingAsset,
        assetsConfigInput[i].emissionPerSecond
      );
    }
  }

  /**
   * @dev Updates the state of one distribution, mainly rewards index and timestamp
   * @param asset The address of the asset being updated
   * @param assetConfig Storage pointer to the distribution's config
   * @param totalStaked Current total of staked assets for this distribution
   * @return The new distribution index
   **/
  function _updateAssetStateInternal(
    address asset,
    AssetData storage assetConfig,
    uint256 totalStaked
  ) internal returns (uint256) {
    uint256 oldIndex = assetConfig.index;
    uint256 emissionPerSecond = assetConfig.emissionPerSecond;
    uint128 lastUpdateTimestamp = assetConfig.lastUpdateTimestamp;

    if (block.timestamp == lastUpdateTimestamp) {
      return oldIndex;
    }

    uint256 newIndex =
      _getAssetIndex(oldIndex, emissionPerSecond, lastUpdateTimestamp, totalStaked);

    if (newIndex != oldIndex) {
      require(uint104(newIndex) == newIndex, 'Index overflow');
      //optimization: storing one after another saves one SSTORE
      assetConfig.index = uint104(newIndex);
      assetConfig.lastUpdateTimestamp = uint40(block.timestamp);
      emit AssetIndexUpdated(asset, newIndex);
    } else {
      assetConfig.lastUpdateTimestamp = uint40(block.timestamp);
    }

    return newIndex;
  }

  /**
   * @dev Updates the state of an user in a distribution
   * @param user The user's address
   * @param asset The address of the reference asset of the distribution
   * @param stakedByUser Amount of tokens staked by the user in the distribution at the moment
   * @param totalStaked Total tokens staked in the distribution
   * @return The accrued rewards for the user until the moment
   **/
  function _updateUserAssetInternal(
    address user,
    address asset,
    uint256 stakedByUser,
    uint256 totalStaked
  ) internal returns (uint256) {
    AssetData storage assetData = assets[asset];
    uint256 userIndex = assetData.users[user];
    uint256 accruedRewards = 0;

    uint256 newIndex = _updateAssetStateInternal(asset, assetData, totalStaked);

    if (userIndex != newIndex) {
      if (stakedByUser != 0) {
        accruedRewards = _getRewards(stakedByUser, newIndex, userIndex);
      }

      assetData.users[user] = newIndex;
      emit UserIndexUpdated(user, asset, newIndex);
    }

    return accruedRewards;
  }

  /**
   * @dev Used by "frontend" stake contracts to update the data of an user when claiming rewards from there
   * @param user The address of the user
   * @param stakes List of structs of the user data related with his stake
   * @return The accrued rewards for the user until the moment
   **/
  function _claimRewards(address user, DistributionTypes.UserStakeInput[] memory stakes)
    internal
    returns (uint256)
  {
    uint256 accruedRewards = 0;

    for (uint256 i = 0; i < stakes.length; i++) {
      accruedRewards = accruedRewards.add(
        _updateUserAssetInternal(
          user,
          stakes[i].underlyingAsset,
          stakes[i].stakedByUser,
          stakes[i].totalStaked
        )
      );
    }

    return accruedRewards;
  }

  /**
   * @dev Return the accrued rewards for an user over a list of distribution
   * @param user The address of the user
   * @param stakes List of structs of the user data related with his stake
   * @return The accrued rewards for the user until the moment
   **/
  function _getUnclaimedRewards(address user, DistributionTypes.UserStakeInput[] memory stakes)
    internal
    view
    returns (uint256)
  {
    uint256 accruedRewards = 0;

    for (uint256 i = 0; i < stakes.length; i++) {
      AssetData storage assetConfig = assets[stakes[i].underlyingAsset];
      uint256 assetIndex =
        _getAssetIndex(
          assetConfig.index,
          assetConfig.emissionPerSecond,
          assetConfig.lastUpdateTimestamp,
          stakes[i].totalStaked
        );

      accruedRewards = accruedRewards.add(
        _getRewards(stakes[i].stakedByUser, assetIndex, assetConfig.users[user])
      );
    }
    return accruedRewards;
  }

  /**
   * @dev Internal function for the calculation of user's rewards on a distribution
   * @param principalUserBalance Amount staked by the user on a distribution
   * @param reserveIndex Current index of the distribution
   * @param userIndex Index stored for the user, representation his staking moment
   * @return The rewards
   **/
  function _getRewards(
    uint256 principalUserBalance,
    uint256 reserveIndex,
    uint256 userIndex
  ) internal pure returns (uint256) {
    return principalUserBalance.mul(reserveIndex.sub(userIndex)) / 10**uint256(PRECISION);
  }

  /**
   * @dev Calculates the next value of an specific distribution index, with validations
   * @param currentIndex Current index of the distribution
   * @param emissionPerSecond Representing the total rewards distributed per second per asset unit, on the distribution
   * @param lastUpdateTimestamp Last moment this distribution was updated
   * @param totalBalance of tokens considered for the distribution
   * @return The new index.
   **/
  function _getAssetIndex(
    uint256 currentIndex,
    uint256 emissionPerSecond,
    uint128 lastUpdateTimestamp,
    uint256 totalBalance
  ) internal view returns (uint256) {
    uint256 distributionEnd = _distributionEnd;
    if (
      emissionPerSecond == 0 ||
      totalBalance == 0 ||
      lastUpdateTimestamp == block.timestamp ||
      lastUpdateTimestamp >= distributionEnd
    ) {
      return currentIndex;
    }

    uint256 currentTimestamp =
      block.timestamp > distributionEnd ? distributionEnd : block.timestamp;
    uint256 timeDelta = currentTimestamp.sub(lastUpdateTimestamp);
    return
      emissionPerSecond.mul(timeDelta).mul(10**uint256(PRECISION)).div(totalBalance).add(
        currentIndex
      );
  }
}
IScaledBalanceToken.sol 26 lines
// SPDX-License-Identifier: agpl-3.0
pragma solidity 0.7.5;

interface IScaledBalanceToken {
  /**
   * @dev Returns the scaled balance of the user. The scaled balance is the sum of all the
   * updated stored balance divided by the reserve's liquidity index at the moment of the update
   * @param user The user whose balance is calculated
   * @return The scaled balance of the user
   **/
  function scaledBalanceOf(address user) external view returns (uint256);

  /**
   * @dev Returns the scaled balance of the user and the scaled total supply.
   * @param user The address of the user
   * @return The scaled balance of the user
   * @return The scaled balance and the scaled total supply
   **/
  function getScaledUserBalanceAndSupply(address user) external view returns (uint256, uint256);

  /**
   * @dev Returns the scaled total supply of the token. Represents sum(debt/index)
   * @return The scaled total supply
   **/
  function scaledTotalSupply() external view returns (uint256);
}
IStakedTokenWithConfig.sol 8 lines
// SPDX-License-Identifier: AGPL-3.0
pragma solidity 0.7.5;

import {IStakedToken} from '@aave/aave-stake/contracts/interfaces/IStakedToken.sol';

interface IStakedTokenWithConfig is IStakedToken {
  function STAKED_TOKEN() external view returns(address);
}
IERC20.sol 81 lines
// SPDX-License-Identifier: MIT
pragma solidity 0.7.5;

/**
 * @dev Interface of the ERC20 standard as defined in the EIP.
 * From https://github.com/OpenZeppelin/openzeppelin-contracts
 */
interface IERC20 {
  /**
   * @dev Returns the amount of tokens in existence.
   */
  function totalSupply() external view returns (uint256);

  /**
   * @dev Returns the amount of tokens owned by `account`.
   */
  function balanceOf(address account) external view returns (uint256);

  /**
   * @dev Moves `amount` tokens from the caller's account to `recipient`.
   *
   * Returns a boolean value indicating whether the operation succeeded.
   *
   * Emits a {Transfer} event.
   */
  function transfer(address recipient, uint256 amount) external returns (bool);

  /**
   * @dev Returns the remaining number of tokens that `spender` will be
   * allowed to spend on behalf of `owner` through {transferFrom}. This is
   * zero by default.
   *
   * This value changes when {approve} or {transferFrom} are called.
   */
  function allowance(address owner, address spender) external view returns (uint256);

  /**
   * @dev Sets `amount` as the allowance of `spender` over the caller's tokens.
   *
   * Returns a boolean value indicating whether the operation succeeded.
   *
   * IMPORTANT: Beware that changing an allowance with this method brings the risk
   * that someone may use both the old and the new allowance by unfortunate
   * transaction ordering. One possible solution to mitigate this race
   * condition is to first reduce the spender's allowance to 0 and set the
   * desired value afterwards:
   * https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729
   *
   * Emits an {Approval} event.
   */
  function approve(address spender, uint256 amount) external returns (bool);

  /**
   * @dev Moves `amount` tokens from `sender` to `recipient` using the
   * allowance mechanism. `amount` is then deducted from the caller's
   * allowance.
   *
   * Returns a boolean value indicating whether the operation succeeded.
   *
   * Emits a {Transfer} event.
   */
  function transferFrom(
    address sender,
    address recipient,
    uint256 amount
  ) external returns (bool);

  /**
   * @dev Emitted when `value` tokens are moved from one account (`from`) to
   * another (`to`).
   *
   * Note that `value` may be zero.
   */
  event Transfer(address indexed from, address indexed to, uint256 value);

  /**
   * @dev Emitted when the allowance of a `spender` for an `owner` is set by
   * a call to {approve}. `value` is the new allowance.
   */
  event Approval(address indexed owner, address indexed spender, uint256 value);
}
IAaveDistributionManager.sol 46 lines
// SPDX-License-Identifier: agpl-3.0
pragma solidity 0.7.5;
pragma experimental ABIEncoderV2;

import {DistributionTypes} from '../lib/DistributionTypes.sol';

interface IAaveDistributionManager {
  
  event AssetConfigUpdated(address indexed asset, uint256 emission);
  event AssetIndexUpdated(address indexed asset, uint256 index);
  event UserIndexUpdated(address indexed user, address indexed asset, uint256 index);
  event DistributionEndUpdated(uint256 newDistributionEnd);

  /**
  * @dev Sets the end date for the distribution
  * @param distributionEnd The end date timestamp
  **/
  function setDistributionEnd(uint256 distributionEnd) external;

  /**
  * @dev Gets the end date for the distribution
  * @return The end of the distribution
  **/
  function getDistributionEnd() external view returns (uint256);

  /**
  * @dev for backwards compatibility with the previous DistributionManager used
  * @return The end of the distribution
  **/
  function DISTRIBUTION_END() external view returns(uint256);

   /**
   * @dev Returns the data of an user on a distribution
   * @param user Address of the user
   * @param asset The address of the reference asset of the distribution
   * @return The new index
   **/
   function getUserAssetData(address user, address asset) external view returns (uint256);

   /**
   * @dev Returns the configuration of the distribution for a certain asset
   * @param asset The address of the reference asset of the distribution
   * @return The asset index, the emission per second and the last updated timestamp
   **/
   function getAssetData(address asset) external view returns (uint256, uint256, uint256);
}
IAaveIncentivesController.sol 112 lines
// SPDX-License-Identifier: agpl-3.0
pragma solidity 0.7.5;

pragma experimental ABIEncoderV2;

import {IAaveDistributionManager} from '../interfaces/IAaveDistributionManager.sol';

interface IAaveIncentivesController is IAaveDistributionManager {
  
  event RewardsAccrued(address indexed user, uint256 amount);
  
  event RewardsClaimed(
    address indexed user,
    address indexed to,
    address indexed claimer,
    uint256 amount
  );

  event ClaimerSet(address indexed user, address indexed claimer);

  /**
   * @dev Whitelists an address to claim the rewards on behalf of another address
   * @param user The address of the user
   * @param claimer The address of the claimer
   */
  function setClaimer(address user, address claimer) external;

  /**
   * @dev Returns the whitelisted claimer for a certain address (0x0 if not set)
   * @param user The address of the user
   * @return The claimer address
   */
  function getClaimer(address user) external view returns (address);

  /**
   * @dev Configure assets for a certain rewards emission
   * @param assets The assets to incentivize
   * @param emissionsPerSecond The emission for each asset
   */
  function configureAssets(address[] calldata assets, uint256[] calldata emissionsPerSecond)
    external;


  /**
   * @dev Called by the corresponding asset on any update that affects the rewards distribution
   * @param asset The address of the user
   * @param userBalance The balance of the user of the asset in the lending pool
   * @param totalSupply The total supply of the asset in the lending pool
   **/
  function handleAction(
    address asset,
    uint256 userBalance,
    uint256 totalSupply
  ) external;

  /**
   * @dev Returns the total of rewards of an user, already accrued + not yet accrued
   * @param user The address of the user
   * @return The rewards
   **/
  function getRewardsBalance(address[] calldata assets, address user)
    external
    view
    returns (uint256);

  /**
   * @dev Claims reward for an user, on all the assets of the lending pool, accumulating the pending rewards
   * @param amount Amount of rewards to claim
   * @param to Address that will be receiving the rewards
   * @return Rewards claimed
   **/
  function claimRewards(
    address[] calldata assets,
    uint256 amount,
    address to
  ) external returns (uint256);

  /**
   * @dev Claims reward for an user on behalf, on all the assets of the lending pool, accumulating the pending rewards. The caller must
   * be whitelisted via "allowClaimOnBehalf" function by the RewardsAdmin role manager
   * @param amount Amount of rewards to claim
   * @param user Address to check and claim rewards
   * @param to Address that will be receiving the rewards
   * @return Rewards claimed
   **/
  function claimRewardsOnBehalf(
    address[] calldata assets,
    uint256 amount,
    address user,
    address to
  ) external returns (uint256);

  /**
   * @dev Claims rewards for a user, on the specified assets of the lending pool, distributing the pending rewards to self
   * @param assets Incentivized assets on which to claim rewards
   * @param amount Amount of rewards to claim
   * @return Rewards claimed
   **/
  function claimRewardsToSelf(address[] calldata assets, uint256 amount) external returns (uint256);

  /**
   * @dev returns the unclaimed rewards of the user
   * @param user the address of the user
   * @return the unclaimed user rewards
   */
  function getUserUnclaimedRewards(address user) external view returns (uint256);

  /**
  * @dev for backward compatibility with previous implementation of the Incentives controller
  */
  function REWARD_TOKEN() external view returns (address);
}
IStakedToken.sol 13 lines
// SPDX-License-Identifier: agpl-3.0
pragma solidity 0.7.5;

interface IStakedToken {
  
  function stake(address to, uint256 amount) external;

  function redeem(address to, uint256 amount) external;

  function cooldown() external;

  function claimRewards(address to, uint256 amount) external;
}
StakedTokenIncentivesController.sol 223 lines
// SPDX-License-Identifier: agpl-3.0
pragma solidity 0.7.5;
pragma experimental ABIEncoderV2;

import {SafeERC20} from '@aave/aave-stake/contracts/lib/SafeERC20.sol';
import {SafeMath} from '../lib/SafeMath.sol';
import {DistributionTypes} from '../lib/DistributionTypes.sol';
import {VersionedInitializable} from '@aave/aave-stake/contracts/utils/VersionedInitializable.sol';
import {DistributionManager} from './DistributionManager.sol';
import {IStakedTokenWithConfig} from '../interfaces/IStakedTokenWithConfig.sol';
import {IERC20} from '@aave/aave-stake/contracts/interfaces/IERC20.sol';
import {IScaledBalanceToken} from '../interfaces/IScaledBalanceToken.sol';
import {IAaveIncentivesController} from '../interfaces/IAaveIncentivesController.sol';

/**
 * @title StakedTokenIncentivesController
 * @notice Distributor contract for rewards to the Aave protocol, using a staked token as rewards asset.
 * The contract stakes the rewards before redistributing them to the Aave protocol participants.
 * The reference staked token implementation is at https://github.com/aave/aave-stake-v2
 * @author Aave
 **/
contract StakedTokenIncentivesController is
  IAaveIncentivesController,
  VersionedInitializable,
  DistributionManager
{
  using SafeMath for uint256;
  using SafeERC20 for IERC20;

  uint256 public constant REVISION = 2;

  IStakedTokenWithConfig public immutable STAKE_TOKEN;

  mapping(address => uint256) internal _usersUnclaimedRewards;

  // this mapping allows whitelisted addresses to claim on behalf of others
  // useful for contracts that hold tokens to be rewarded but don't have any native logic to claim Liquidity Mining rewards
  mapping(address => address) internal _authorizedClaimers;

  modifier onlyAuthorizedClaimers(address claimer, address user) {
    require(_authorizedClaimers[user] == claimer, 'CLAIMER_UNAUTHORIZED');
    _;
  }

  constructor(IStakedTokenWithConfig stakeToken, address emissionManager)
    DistributionManager(emissionManager)
  {
    STAKE_TOKEN = stakeToken;
  }

  /**
   * @dev Initialize IStakedTokenIncentivesController. Empty after REVISION 1, but maintains the expected interface.
   **/
  function initialize(address) external initializer {}

  /// @inheritdoc IAaveIncentivesController
  function configureAssets(address[] calldata assets, uint256[] calldata emissionsPerSecond)
    external
    override
    onlyEmissionManager
  {
    require(assets.length == emissionsPerSecond.length, 'INVALID_CONFIGURATION');

    DistributionTypes.AssetConfigInput[] memory assetsConfig =
      new DistributionTypes.AssetConfigInput[](assets.length);

    for (uint256 i = 0; i < assets.length; i++) {
      assetsConfig[i].underlyingAsset = assets[i];
      assetsConfig[i].emissionPerSecond = uint104(emissionsPerSecond[i]);

      require(assetsConfig[i].emissionPerSecond == emissionsPerSecond[i], 'INVALID_CONFIGURATION');

      assetsConfig[i].totalStaked = IScaledBalanceToken(assets[i]).scaledTotalSupply();
    }
    _configureAssets(assetsConfig);
  }

  /// @inheritdoc IAaveIncentivesController
  function handleAction(
    address user,
    uint256 totalSupply,
    uint256 userBalance
  ) external override {
    uint256 accruedRewards = _updateUserAssetInternal(user, msg.sender, userBalance, totalSupply);
    if (accruedRewards != 0) {
      _usersUnclaimedRewards[user] = _usersUnclaimedRewards[user].add(accruedRewards);
      emit RewardsAccrued(user, accruedRewards);
    }
  }

  /// @inheritdoc IAaveIncentivesController
  function getRewardsBalance(address[] calldata assets, address user)
    external
    view
    override
    returns (uint256)
  {
    uint256 unclaimedRewards = _usersUnclaimedRewards[user];

    DistributionTypes.UserStakeInput[] memory userState =
      new DistributionTypes.UserStakeInput[](assets.length);
    for (uint256 i = 0; i < assets.length; i++) {
      userState[i].underlyingAsset = assets[i];
      (userState[i].stakedByUser, userState[i].totalStaked) = IScaledBalanceToken(assets[i])
        .getScaledUserBalanceAndSupply(user);
    }
    unclaimedRewards = unclaimedRewards.add(_getUnclaimedRewards(user, userState));
    return unclaimedRewards;
  }

  /// @inheritdoc IAaveIncentivesController
  function claimRewards(
    address[] calldata assets,
    uint256 amount,
    address to
  ) external override returns (uint256) {
    require(to != address(0), 'INVALID_TO_ADDRESS');
    return _claimRewards(assets, amount, msg.sender, msg.sender, to);
  }

  /// @inheritdoc IAaveIncentivesController
  function claimRewardsOnBehalf(
    address[] calldata assets,
    uint256 amount,
    address user,
    address to
  ) external override onlyAuthorizedClaimers(msg.sender, user) returns (uint256) {
    require(user != address(0), 'INVALID_USER_ADDRESS');
    require(to != address(0), 'INVALID_TO_ADDRESS');
    return _claimRewards(assets, amount, msg.sender, user, to);
  }

  /// @inheritdoc IAaveIncentivesController
  function claimRewardsToSelf(address[] calldata assets, uint256 amount)
    external
    override
    returns (uint256)
  {
    return _claimRewards(assets, amount, msg.sender, msg.sender, msg.sender);
  }

  /**
   * @dev Claims reward for an user on behalf, on all the assets of the lending pool, accumulating the pending rewards.
   * @param amount Amount of rewards to claim
   * @param user Address to check and claim rewards
   * @param to Address that will be receiving the rewards
   * @return Rewards claimed
   **/

  /// @inheritdoc IAaveIncentivesController
  function setClaimer(address user, address caller) external override onlyEmissionManager {
    _authorizedClaimers[user] = caller;
    emit ClaimerSet(user, caller);
  }

  /// @inheritdoc IAaveIncentivesController
  function getClaimer(address user) external view override returns (address) {
    return _authorizedClaimers[user];
  }

  /// @inheritdoc IAaveIncentivesController
  function getUserUnclaimedRewards(address _user) external view override returns (uint256) {
    return _usersUnclaimedRewards[_user];
  }

  /// @inheritdoc IAaveIncentivesController
  function REWARD_TOKEN() external view override returns (address) {
    return address(STAKE_TOKEN);
  }

  /**
   * @dev returns the revision of the implementation contract
   */
  function getRevision() internal pure override returns (uint256) {
    return REVISION;
  }

  /**
   * @dev Claims reward for an user on behalf, on all the assets of the lending pool, accumulating the pending rewards.
   * @param amount Amount of rewards to claim
   * @param user Address to check and claim rewards
   * @param to Address that will be receiving the rewards
   * @return Rewards claimed
   **/
  function _claimRewards(
    address[] calldata assets,
    uint256 amount,
    address claimer,
    address user,
    address to
  ) internal returns (uint256) {
    if (amount == 0) {
      return 0;
    }
    uint256 unclaimedRewards = _usersUnclaimedRewards[user];

    DistributionTypes.UserStakeInput[] memory userState =
      new DistributionTypes.UserStakeInput[](assets.length);
    for (uint256 i = 0; i < assets.length; i++) {
      userState[i].underlyingAsset = assets[i];
      (userState[i].stakedByUser, userState[i].totalStaked) = IScaledBalanceToken(assets[i])
        .getScaledUserBalanceAndSupply(user);
    }

    uint256 accruedRewards = _claimRewards(user, userState);
    if (accruedRewards != 0) {
      unclaimedRewards = unclaimedRewards.add(accruedRewards);
      emit RewardsAccrued(user, accruedRewards);
    }

    if (unclaimedRewards == 0) {
      return 0;
    }

    uint256 amountToClaim = amount > unclaimedRewards ? unclaimedRewards : amount;
    _usersUnclaimedRewards[user] = unclaimedRewards - amountToClaim; // Safe due to the previous line

    STAKE_TOKEN.stake(to, amountToClaim);
    emit RewardsClaimed(user, to, claimer, amountToClaim);

    return amountToClaim;
  }
}
VersionedInitializable.sol 42 lines
// SPDX-License-Identifier: agpl-3.0
pragma solidity 0.7.5;

/**
 * @title VersionedInitializable
 *
 * @dev Helper contract to support initializer functions. To use it, replace
 * the constructor with a function that has the `initializer` modifier.
 * WARNING: Unlike constructors, initializer functions must be manually
 * invoked. This applies both to deploying an Initializable contract, as well
 * as extending an Initializable contract via inheritance.
 * WARNING: When used with inheritance, manual care must be taken to not invoke
 * a parent initializer twice, or ensure that all initializers are idempotent,
 * because this is not dealt with automatically as with constructors.
 *
 * @author Aave, inspired by the OpenZeppelin Initializable contract
 */
abstract contract VersionedInitializable {
  /**
   * @dev Indicates that the contract has been initialized.
   */
  uint256 internal lastInitializedRevision = 0;

  /**
   * @dev Modifier to use in the initializer function of a contract.
   */
  modifier initializer() {
    uint256 revision = getRevision();
    require(revision > lastInitializedRevision, 'Contract instance has already been initialized');

    lastInitializedRevision = revision;

    _;
  }

  /// @dev returns the revision number of the contract.
  /// Needs to be defined in the inherited class as a constant.
  function getRevision() internal pure virtual returns (uint256);

  // Reserved storage space to allow for layout changes in the future.
  uint256[50] private ______gap;
}

Read Contract

DISTRIBUTION_END 0x919cd40f → uint256
EMISSION_MANAGER 0xcbcbb507 → address
PRECISION 0xaaf5eb68 → uint8
REVISION 0xdde43cba → uint256
REWARD_TOKEN 0x99248ea7 → address
STAKE_TOKEN 0x1c39b672 → address
assets 0xf11b8188 → uint104, uint104, uint40
getAssetData 0x1652e7b7 → uint256, uint256, uint256
getClaimer 0x74d945ec → address
getDistributionEnd 0xcc69afec → uint256
getRewardsBalance 0x8b599f26 → uint256
getUserAssetData 0x3373ee4c → uint256
getUserUnclaimedRewards 0x198fa81e → uint256

Write Contract 8 functions

These functions modify contract state and require a wallet transaction to execute.

claimRewards 0x3111e7b3
address[] assets
uint256 amount
address to
returns: uint256
claimRewardsOnBehalf 0x6d34b96e
address[] assets
uint256 amount
address user
address to
returns: uint256
claimRewardsToSelf 0x41485304
address[] assets
uint256 amount
returns: uint256
configureAssets 0x79f171b2
address[] assets
uint256[] emissionsPerSecond
handleAction 0x31873e2e
address user
uint256 totalSupply
uint256 userBalance
initialize 0xc4d66de8
address
setClaimer 0xf5cf673b
address user
address caller
setDistributionEnd 0x39ccbdd3
uint256 distributionEnd

Recent Transactions

No transactions found for this address