Skip to main content

_distributeToReservedTokenSplitsOf

Distribute tokens to the splits according to the specified funding cycle configuration.

Definition​

function _distributeToReservedTokenSplitsOf(
uint256 _projectId,
uint256 _domain,
uint256 _group,
uint256 _amount
) internal returns (uint256 leftoverAmount) { ... }
  • Arguments:
    • _projectId is the ID of the project for which reserved token splits are being distributed.
    • _fundingCycle is the JBFundingCycle to base the token distribution on.
    • _domain is the domain of the splits to distribute the reserved tokens between.
    • _group is the group of the splits to distribute the reserved tokens between.
    • _amount is the total amount of tokens to mint.
  • The resulting function is internal to this contract and its inheriters.
  • The function returns the leftover amount after all splits have been distributed.

Body​

  1. Keep a reference to the token store.

    // Keep a reference to the token store.
    IJBTokenStore _tokenStore = tokenStore;

    Internal references:

  2. Save the passed in amount as the leftover amount that will be returned. The subsequent routine will decrement the leftover amount as splits are settled.

    // Set the leftover amount to the initial amount.
    leftoverAmount = _amount;
  3. Get a reference to reserved token splits for the current funding cycle configuration of the project.

    // Get a reference to the project's reserved token splits.
    JBSplit[] memory _splits = splitsStore.splitsOf(_projectId, _domain, _group);

    Internal references:

    External references:

  4. Loop through each split.

    //Transfer between all splits.
    for (uint256 _i; _i < _splits.length; ) { ... }
    1. Get a reference to the current split being iterated on.

      // Get a reference to the split being iterated on.
      JBSplit memory _split = _splits[_i];
    2. Get a reference to the amount of tokens to distribute to the current split. This amount is the total amount multiplied by the percentage of the split, which is a number out of the max value.

      // The amount to send towards the split.
      uint256 _tokenCount = PRBMath.mulDiv(
      _amount,
      _split.percent,
      JBConstants.SPLITS_TOTAL_PERCENT
      );

      Library references:

    3. If there are tokens to mint for the given split, do so. If the split has an allocator specified, the tokens should go to that address. Otherwise if the split has a project ID specified, the tokens should be directed to the project's owner. Otherwise, the tokens should be directed at the beneficiary address of the split if it has one, or to the message sender if not. Afterwards, if there's an allocator specified, let it know that tokens have been sent. Reduce the leftover amount by the tokens that were sent to the split.

      // Mints tokens for the split if needed.
      if (_tokenCount > 0) {
      tokenStore.mintFor(
      // If an allocator is set in the splits, set it as the beneficiary.
      // Otherwise if a projectId is set in the split, set the project's owner as the beneficiary.
      // If the split has a beneficiary send to the split's beneficiary. Otherwise send to the msg.sender.
      _split.allocator != IJBSplitAllocator(address(0))
      ? address(_split.allocator)
      : _split.projectId != 0
      ? projects.ownerOf(_split.projectId)
      : _split.beneficiary != address(0)
      ? _split.beneficiary
      : msg.sender,
      _projectId,
      _tokenCount,
      _split.preferClaimed
      );

      // If there's an allocator set, trigger its `allocate` function.
      if (_split.allocator != IJBSplitAllocator(address(0)))
      _split.allocator.allocate(
      JBSplitAllocationData(
      address(_tokenStore.tokenOf(_projectId)),
      _tokenCount,
      18, // 18 decimals.
      _projectId,
      _group,
      _split
      )
      );

      // Subtract from the amount to be sent to the beneficiary.
      leftoverAmount = leftoverAmount - _tokenCount;
      }

      External references:

    4. Emit a DistributeToReservedTokenSplit event for the split being iterated on with the relevant parameters.

      emit DistributeToReservedTokenSplit(
      _projectId,
      _domain,
      _group,
      _split,
      _tokenCount,
      msg.sender
      );

      Event references:

    5. Increment the loop counter.

      unchecked {
      ++_i;
      }