Skip to content
Verified contractPartialMIT

WBTCPool

Address0xa4f02fa811a7a08850f2e6a00a04a4890cbea40b
Balance

0 ETH

 

Transactions

54

 

Compiler

solc 0.8.27

 

Same code

Only this contract

 

Details

Compiler
solc 0.8.27+commit.40a35a09
Language
solidity
Optimization
Yes, 200 runs
EVM version
shanghai
License
MIT
Match
Partialcreation code: no, runtime code: yes
Verified on Sourcify
Created in block
21,752,762
Creation transaction
0x33c2fb1e1f…d7ad369b67
Libraries
src/utils/Time.sol:Time0x5dcd…d133d3

99 files

The source code is large: one file is shown at a time. Choose a file in the list.

src/pools/WBTCPool.sol

// SPDX-License-Identifier: MIT
pragma solidity 0.8.27;

import "@const/Constants.sol";
import {Time} from "@utils/Time.sol";
import {Errors} from "@utils/Errors.sol";
import {wdiv, wmul} from "@utils/Math.sol";
import {ShaolinMining} from "@core/Mining.sol";
import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import {SafeERC20} from "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";

struct CycleState {
    uint256 totalShares; //< Total shares in the cycle.
    uint256 available; //< Rewards per share for the cycle.
    mapping(address user => uint256 shares) sharesOf; //< Shares of a user for a cycle
}

/**
 * @title WBTCPool
 * @dev Manages rewards distribution for WBTC staking cycles.
 */
contract WBTCPool is Errors {
    using SafeERC20 for IERC20;

    /// @notice Duration of each rewards cycle in days.
    uint32 constant DURATION_PER_CYCLE = 28;

    /// @notice Reference to the ShaolinMining contract.
    ShaolinMining immutable mining;

    /// @notice Reference to the WBTC ERC20 token contract.
    IERC20 public immutable wbtc;

    /// @notice The start timestamp of the contract.
    uint32 public immutable startTimestamp;

    /// @dev Mapping of cycle numbers to their respective cycle states.
    mapping(uint32 cycle => CycleState) cycles;

    /// @dev Tracks pending shares for users in specific cycles.
    mapping(uint32 cycle => mapping(address _user => uint256)) public pendingShares;

    /// @notice Emitted when shares are added for a user.
    /// @param _user The address of the user.
    /// @param _cycle The cycle for which the user deposited
    /// @param additionalAmount The amount of shares added.
    event SharesAdded(address indexed _user, uint32 indexed _cycle, uint256 indexed additionalAmount);

    /// @notice Emitted when shares are added for a user.
    /// @param _user The address of the user.
    /// @param _cycle The cycle for which the user deposited
    /// @param _claimed The amount of WBTC claimed.
    event RewardClaimed(address indexed _user, uint32 indexed _cycle, uint256 indexed _claimed);

    /// @notice Thrown when a function is called by an address other than the mining contract.
    error WBTCPool__OnlyMining();

    /// @notice Thrown when there are no rewards available to claim.
    error WBTCPool__NothingToClaim();

    error WBTCPool__CycleNotOver();

    /**
     * @dev Initializes the contract with the given parameters.
     * @param _startTimestamp The timestamp when the contract starts.
     * @param _wbtc The address of the WBTC token.
     * @param _mining The address of the ShaolinMining contract.
     */
    constructor(uint32 _startTimestamp, address _wbtc, address _mining) {
        mining = ShaolinMining(payable(_mining));
        wbtc = IERC20(_wbtc);
        startTimestamp = _startTimestamp;
    }

    /// @dev Modifier to restrict access to the mining contract.
    modifier onlyMining() {
        _onlyMining();
        _;
    }

    /**
     * @notice Adds shares to a user for the current cycle.
     * @param _user The address of the user.
     * @param _amount The amount of shares to add.
     */
    function addToShares(address _user, uint256 _amount) public onlyMining {
        uint32 _currCycle = getCurrentCycle();

        CycleState storage _cycle = cycles[_currCycle];
        uint256 shares = _cycle.sharesOf[_user];

        if (shares > 0) {
            emit SharesAdded(_user, _currCycle, _amount);

            _cycle.totalShares += _amount;
            _cycle.sharesOf[_user] += _amount;
        } else {
            pendingShares[_currCycle][_user] += _amount;

            uint256 total = pendingShares[_currCycle][_user];

            if (total < MIN_SHAO_FOR_WBTC_POOL) return;

            emit SharesAdded(_user, _currCycle, _amount);

            _cycle.totalShares += total;
            _cycle.sharesOf[_user] += total;

            pendingShares[_currCycle][_user] = 0;
        }
    }

    /**
     * @notice Calculates the claimable rewards for a user in a specific cycle.
     * @param _user The address of the user.
     * @param _cycle The cycle number.
     * @return The claimable rewards amount.
     */
    function claimableAmount(address _user, uint32 _cycle) public view returns (uint256) {
        CycleState storage _cycleState = cycles[_cycle];
        uint256 shares = _cycleState.sharesOf[_user];

        uint256 available;

        if (_cycleState.totalShares == 0) return 0;

        if (_cycle < getCurrentCycle()) available = _cycleState.available;

        return (((shares * available) * WAD) / _cycleState.totalShares) / WAD;
    }

    /**
     * @notice Calculates the total claimable rewards for a user across multiple cycles.
     * @param _user The address of the user.
     * @param _cycles The array of cycle numbers.
     * @return total The total claimable rewards.
     */
    function batchClaimableAmount(address _user, uint32[] calldata _cycles) external view returns (uint256 total) {
        for (uint32 i; i < _cycles.length; i++) {
            total += claimableAmount(_user, _cycles[i]);
        }
    }

    /**
     * @notice Claims rewards for a user across multiple cycles.
     * @param _receiver The address to send the rewards to.
     * @param _cycles The array of cycle numbers.
     */
    function batchClaim(address _receiver, uint32[] calldata _cycles) external {
        for (uint32 i; i < _cycles.length; i++) {
            claim(_receiver, _cycles[i]);
        }
    }

    /**
     * @notice Claims rewards for a specific cycle and sends them to the receiver.
     * @param _receiver The address to send the rewards to.
     * @param _cycle The cycle number for which to claim rewards.
     */
    function claim(address _receiver, uint32 _cycle) public notAddress0(_receiver) {
        require(_cycle < getCurrentCycle(), WBTCPool__CycleNotOver());

        CycleState storage _cycleState = cycles[_cycle];

        uint256 amountToClaim = claimableAmount(msg.sender, _cycle);

        require(amountToClaim > 0, WBTCPool__NothingToClaim());

        _cycleState.sharesOf[msg.sender] = 0;

        emit RewardClaimed(msg.sender, _cycle, amountToClaim);

        wbtc.transfer(_receiver, amountToClaim);
    }

    /**
     * @notice Distributes rewards to the current cycle.
     * @param amount The amount of rewards to distribute.
     */
    function distribute(uint256 amount) external notAmount0(amount) {
        cycles[getCurrentCycle()].available += amount;

        wbtc.safeTransferFrom(msg.sender, address(this), amount);
    }

    /**
     * @dev Returns the current cycle number based on the current day.
     * @return cycle The current cycle number.
     */
    function getCurrentCycle() public view returns (uint32 cycle) {
        uint32 currentDay = _getCurrentDay();

        cycle = (currentDay / DURATION_PER_CYCLE) + 1;
    }

    function getAvailableForCycle(uint32 _cycle) external view returns (uint256 available) {
        return cycles[_cycle].available;
    }

    function getShareOfUserForCycle(uint32 _cycle, address _user) public view returns (uint256 shares) {
        return cycles[_cycle].sharesOf[_user];
    }

    /**
     * @dev Returns the current day since the contract started.
     * @return currentDay The current day.
     */
    function _getCurrentDay() internal view returns (uint32 currentDay) {
        if (startTimestamp > Time.blockTs()) return 1;
        currentDay = Time.dayGap(startTimestamp, Time.blockTs()) + 1;
    }

    /**
     * @dev Ensures that the caller is the mining contract.
     */
    function _onlyMining() internal view {
        require(msg.sender == address(mining), WBTCPool__OnlyMining());
    }
}
Compiler settingsShow
{
  "metadata": {
    "bytecodeHash": "ipfs"
  },
  "libraries": {
    "src/utils/Time.sol": {
      "Time": "0x5dcd2315a462f0046641b2118e106fbb47d133d3"
    }
  },
  "optimizer": {
    "runs": 200,
    "enabled": true
  },
  "evmVersion": "shanghai",
  "remappings": [
    ":@actions/=src/actions/",
    ":@chainlink/=lib/chainlink/",
    ":@const/=src/const/",
    ":@core/=src/",
    ":@interfaces/=src/interfaces/",
    ":@layerzerolabs/lz-evm-protocol-v2/=lib/layerzero-v2/packages/layerzero-v2/evm/protocol/",
    ":@layerzerolabs/oapp-evm/=lib/devtools/packages/oapp-evm/",
    ":@layerzerolabs/oft-evm/=lib/devtools/packages/oft-evm/",
    ":@libs/=src/libs/",
    ":@murky/=lib/murky/src/",
    ":@openzeppelin/contracts/=lib/openzeppelin-contracts/contracts/",
    ":@script/=script/",
    ":@uniswap/v2-core/=lib/v2-core/",
    ":@uniswap/v2-periphery/=lib/v2-periphery/",
    ":@uniswap/v3-core/=lib/v3-core/",
    ":@uniswap/v3-periphery/=lib/v3-periphery/",
    ":@utils/=src/utils/",
    ":chainlink/=lib/chainlink/",
    ":devtools/=lib/devtools/packages/toolbox-foundry/src/",
    ":ds-test/=lib/openzeppelin-contracts/lib/forge-std/lib/ds-test/src/",
    ":erc4626-tests/=lib/openzeppelin-contracts/lib/erc4626-tests/",
    ":forge-std/=lib/forge-std/src/",
    ":halmos-cheatcodes/=lib/openzeppelin-contracts/lib/halmos-cheatcodes/src/",
    ":layerzero-v2/=lib/layerzero-v2/",
    ":openzeppelin-contracts/=lib/openzeppelin-contracts/",
    ":v3-core/=lib/v3-core/contracts/",
    ":v3-periphery/=lib/v3-periphery/contracts/"
  ]
}

Source code and metadata: Sourcify.

The source code belongs to the authors of the contract.