Skip to content

Commit

Permalink
mitigation: get only percentage (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
arr00 authored Aug 12, 2024
1 parent a12a2c2 commit cd465c0
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
12 changes: 12 additions & 0 deletions src/MintERC1155.sol
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,18 @@ contract MintERC1155 is ERC1155Upgradeable, OwnableUpgradeable, ERC2981Upgradeab
return allEditions;
}

/**
* @notice Get the percent chance of each edition. Used for filling orders.
* @return An ordered array of the percent chance of each edition.
*/
function getPercentChances() external view returns (uint256[] memory) {
uint256[] memory percentChances = new uint256[](editions.length);
for (uint256 i = 0; i < editions.length; i++) {
percentChances[i] = editions[i].percentChance;
}
return percentChances;
}

function uri(uint256 tokenId) public view override returns (string memory) {
Edition memory edition = editions[tokenId - 1];

Expand Down
14 changes: 7 additions & 7 deletions src/NFTMint.sol
Original file line number Diff line number Diff line change
Expand Up @@ -247,21 +247,21 @@ contract NFTMint is Ownable {
// Don't fill orders in the same block to ensure there is randomness
break;
}
MintERC1155.Edition[] memory editions = currentOrder.mint.getAllEditions();
uint256[] memory percentChances = currentOrder.mint.getPercentChances();

uint256[] memory ids = new uint256[](editions.length);
uint256[] memory amounts = new uint256[](editions.length);
uint256[] memory ids = new uint256[](percentChances.length);
uint256[] memory amounts = new uint256[](percentChances.length);

for (uint256 i = 0; i < editions.length; i++) {
for (uint256 i = 0; i < percentChances.length; i++) {
ids[i] = i + 1;
}

for (uint256 i = 0; i < currentOrder.amount; i++) {
uint256 roll = uint256(keccak256(abi.encodePacked(nonce++, blockhash(block.number - 1)))) % 100;

uint256 cumulativeChance = 0;
for (uint256 j = 0; j < editions.length; j++) {
cumulativeChance += editions[j].percentChance;
for (uint256 j = 0; j < percentChances.length; j++) {
cumulativeChance += percentChances[j];
if (roll < cumulativeChance) {
amounts[j]++;
break;
Expand All @@ -272,7 +272,7 @@ contract NFTMint is Ownable {
emit OrderFilled(currentOrder.mint, nextOrderIdToFill_, currentOrder.to, currentOrder.amount, amounts);

uint256 numNonZero = 0;
for (uint256 i = 0; i < editions.length; i++) {
for (uint256 i = 0; i < percentChances.length; i++) {
if (amounts[i] != 0) {
if (numNonZero < i) {
ids[numNonZero] = ids[i];
Expand Down

0 comments on commit cd465c0

Please sign in to comment.