_distributeToReservedTokenSplitsOf
- Step by step
- Only code
- Events
- Bug bounty
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 theJBFundingCycle
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β
-
Keep a reference to the token store.
// Keep a reference to the token store.
IJBTokenStore _tokenStore = tokenStore;Internal references:
-
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; -
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:
-
Loop through each split.
//Transfer between all splits.
for (uint256 _i; _i < _splits.length; ) { ... }-
Get a reference to the current split being iterated on.
// Get a reference to the split being iterated on.
JBSplit memory _split = _splits[_i]; -
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:
PRBMath
.mulDiv(...)
JBConstants
.SPLITS_TOTAL_PERCENT
-
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:
-
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:
-
Increment the loop counter.
unchecked {
++_i;
}
-
/**
@notice
Distribute tokens to the splits according to the specified funding cycle configuration.
@param _projectId The ID of the project for which reserved token splits are being distributed.
@param _domain The domain of the splits to distribute the reserved tokens between.
@param _group The group of the splits to distribute the reserved tokens between.
@param _amount The total amount of tokens to mint.
@return leftoverAmount If the splits percents dont add up to 100%, the leftover amount is returned.
*/
function _distributeToReservedTokenSplitsOf(
uint256 _projectId,
uint256 _domain,
uint256 _group,
uint256 _amount
) internal returns (uint256 leftoverAmount) {
// Keep a reference to the token store.
IJBTokenStore _tokenStore = tokenStore;
// Set the leftover amount to the initial amount.
leftoverAmount = _amount;
// Get a reference to the project's reserved token splits.
JBSplit[] memory _splits = splitsStore.splitsOf(_projectId, _domain, _group);
//Transfer between all splits.
for (uint256 _i; _i < _splits.length; ) {
// Get a reference to the split being iterated on.
JBSplit memory _split = _splits[_i];
// The amount to send towards the split.
uint256 _tokenCount = PRBMath.mulDiv(
_amount,
_split.percent,
JBConstants.SPLITS_TOTAL_PERCENT
);
// 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;
}
emit DistributeToReservedTokenSplit(
_projectId,
_domain,
_group,
_split,
_tokenCount,
msg.sender
);
unchecked {
++_i;
}
}
}
Name | Data |
---|---|
DistributeToReservedTokenSplit |
|
Category | Description | Reward |
---|---|---|
Optimization | Help make this operation more efficient. | 0.5ETH |
Low severity | Identify a vulnerability in this operation that could lead to an inconvenience for a user of the protocol or for a protocol developer. | 1ETH |
High severity | Identify a vulnerability in this operation that could lead to data corruption or loss of funds. | 5+ETH |