Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New function implementation to prevent the remaining balance allocation to protocols with 0% weight #36

Open
samster91 opened this issue May 9, 2023 · 0 comments

Comments

@samster91
Copy link

/**
 * Calculate amounts from percentage allocations (100000 => 100%)
 *
 * @param allocations : array of protocol allocations in percentage
 * @param total : total amount
 * @return : array with amounts
 */
function _amountsFromAllocations(uint256[] memory allocations, uint256 total)
  internal view returns (uint256[] memory newAmounts) {
  newAmounts = new uint256[](allocations.length);
  uint256 currBalance;
  uint256 allocatedBalance;
  uint256 firstProtocolIndexWithAllocation;

  for (uint256 i = 0; i < allocations.length; i++) {
    // Take last protocol with valid allocations
    if (allocations[i] > 0){
      firstProtocolIndexWithAllocation = i;
    }
    currBalance = total.mul(allocations[i]).div(FULL_ALLOC);
    allocatedBalance = allocatedBalance.add(currBalance);
    newAmounts[i] = currBalance;
  }

  // Allocate remaining amount to last protocol
  if (total.sub(allocatedBalance) > 0){
    newAmounts[firstProtocolIndexWithAllocation] = newAmounts[firstProtocolIndexWithAllocation].add(total.sub(allocatedBalance));
  }

  return newAmounts;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant