Last active
June 21, 2022 20:23
-
-
Save gigamesh/6f6c1a78fd886b2d76b06930a6f6d49b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
contract ArtistV6 { | |
struct Shareholder { | |
address account; | |
uint32 percentAllocation; | |
} | |
// Mapping of edition id to descriptive data. | |
mapping(uint256 => Edition) public editions; | |
// Balances for collectors captable | |
mapping(uint256 => mapping(address => uint32)) collectorBalances; | |
// etc... | |
function getShareholdersCapTable(uint256 _editionId) public returns (Shareholder[] memory shareholders) { | |
uint32 numSold = editions[_editionId].numSold; | |
uint32 royaltyRemainder = uint32(PERCENTAGE_SCALE) % numSold; | |
uint32 royaltyPerToken = uint32(PERCENTAGE_SCALE) / numSold; | |
address[] memory shareholderAddresses = new address[](numSold); | |
// build shareholder addresses array (with duplicates if owner owns more than 1) | |
uint256 shiftedEditionId = (_editionId << 128); | |
for (uint256 i = 0; i < numSold; i++) { | |
uint256 tokenId = shiftedEditionId | (i + 1); | |
address shareholder = ownerOf(tokenId); | |
shareholderAddresses[i] = shareholder; | |
collectorBalances[_editionId][shareholder] += 1; | |
} | |
// Loop over shareholderAddresses | |
uint32 shareholderIndex = 0; | |
Shareholder[] memory shareholdersWithNulls = new Shareholder[](numSold); | |
for (uint32 j = 0; j < shareholderAddresses.length; j++) { | |
// Create shareholder struct if it doesn't exist | |
if (collectorBalances[_editionId][shareholderAddresses[j]] != 0) { | |
shareholdersWithNulls[shareholderIndex] = Shareholder({ | |
account: shareholderAddresses[j], | |
percentAllocation: royaltyPerToken * collectorBalances[_editionId][shareholderAddresses[j]] | |
}); | |
// Zero out balance | |
collectorBalances[_editionId][shareholderAddresses[j]] = 0; | |
shareholderIndex++; | |
} | |
} | |
// add remainder to the first shareholder (if zero, it's more efficient to just add it anyway) | |
shareholdersWithNulls[0].percentAllocation += royaltyRemainder; | |
// Remove any null items left over from the last step | |
Shareholder[] memory unsortedShareholders = new Shareholder[](shareholderIndex); | |
for (uint32 k = 0; k < shareholderIndex; k++) { | |
// For every one that isn't null, add to final array | |
if (shareholdersWithNulls[k].account != address(0)) { | |
unsortedShareholders[k] = shareholdersWithNulls[k]; | |
} | |
} | |
// Sort & return shareholders | |
return sortSharedholders(unsortedShareholders); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment