Skip to content

Commit

Permalink
Changed craft function to allow for batch crafting
Browse files Browse the repository at this point in the history
Also added some tests for emitted events in crafting, rolling,
accepting.
  • Loading branch information
zomglings committed May 22, 2024
1 parent 616feca commit d4aa4c2
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 6 deletions.
8 changes: 4 additions & 4 deletions src/JackpotJunction.sol
Original file line number Diff line number Diff line change
Expand Up @@ -244,15 +244,15 @@ contract JackpotJunction is ERC1155, ReentrancyGuard {
return (entropy, _outcome, value);
}

function craft(uint256 poolID) external nonReentrant returns (uint256 newPoolID) {
if (balanceOf(msg.sender, poolID) < 2) {
function craft(uint256 poolID, uint256 numOutputs) external nonReentrant returns (uint256 newPoolID) {
if (balanceOf(msg.sender, poolID) < 2 * numOutputs) {
revert InsufficientItems(poolID);
}

newPoolID = poolID + 28;

_burn(msg.sender, poolID, 2);
_mint(msg.sender, newPoolID, 1, "");
_burn(msg.sender, poolID, 2 * numOutputs);
_mint(msg.sender, newPoolID, numOutputs, "");

(uint256 itemType, uint256 terrainType, uint256 tier) = genera(newPoolID);
if (CurrentTier[itemType][terrainType] < tier) {
Expand Down
70 changes: 68 additions & 2 deletions test/JackpotJunction.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ contract TestableJackpotJunction is JackpotJunction {
}

contract JackpotJunctionTest is Test {
event Roll(address indexed player);

JackpotJunction game;

uint256 deployerPrivateKey = 0x42;
Expand Down Expand Up @@ -136,6 +138,8 @@ contract JackpotJunctionTest is Test {
vm.expectRevert(JackpotJunction.InsufficientValue.selector);
game.roll();

vm.expectEmit();
emit Roll(player1);
game.roll{value: costToRoll}();
assertEq(player1.balance, 0);
assertEq(address(game).balance, costToRoll);
Expand Down Expand Up @@ -217,6 +221,10 @@ contract JackpotJunctionTest is Test {
}

contract JackpotJunctionPlayTest is Test {
event Roll(address indexed player);
event TierUnlocked(uint256 indexed itemType, uint256 indexed terrainType, uint256 indexed tier, uint256 poolID);
event Award(address indexed player, uint256 indexed outcome, uint256 value);

TestableJackpotJunction game;

uint256 deployerPrivateKey = 0x42;
Expand Down Expand Up @@ -254,6 +262,8 @@ contract JackpotJunctionPlayTest is Test {
vm.deal(player1, 1000*costToRoll);
vm.deal(address(game), 1000000 ether);

vm.expectEmit();
emit Roll(player1);
game.roll{value: costToRoll}();

vm.roll(block.number + 1);
Expand All @@ -263,6 +273,8 @@ contract JackpotJunctionPlayTest is Test {
assertEq(actualOutcome, 0);
assertEq(actualValue, 0);

vm.expectEmit();
emit Roll(player1);
game.roll{value: costToReroll}();

vm.roll(block.number + 1);
Expand All @@ -276,6 +288,8 @@ contract JackpotJunctionPlayTest is Test {

assertEq(game.balanceOf(player1, 4*terrainType + itemType), 0);

vm.expectEmit();
emit Award(player1, 1, 4*terrainType + itemType);
(actualEntropy, actualOutcome, actualValue) = game.accept();
assertEq(actualEntropy, game.Entropy());
assertEq(actualOutcome, 1);
Expand Down Expand Up @@ -826,7 +840,9 @@ contract JackpotJunctionPlayTest is Test {
uint256 initialInputBalance = game.balanceOf(player2, inputPoolID);
uint256 initialOutputBalance = game.balanceOf(player2, outputPoolID);
assertEq(game.CurrentTier(i, j), 0);
game.craft(inputPoolID);
vm.expectEmit();
emit TierUnlocked(i, j, 1, outputPoolID);
game.craft(inputPoolID, 1);
uint256 terminalInputBalance = game.balanceOf(player2, inputPoolID);
uint256 terminalOutputBalance = game.balanceOf(player2, outputPoolID);
assertEq(terminalInputBalance, initialInputBalance - 2);
Expand All @@ -836,6 +852,29 @@ contract JackpotJunctionPlayTest is Test {
}
}

function test_crafting_tier_0_to_tier_1_193284_outputs() public {
vm.startPrank(player2);
for (uint256 i = 0; i < 4; i++) {
for (uint256 j = 0; j < 7; j++) {
uint256 inputPoolID = 4 * j + i;
uint256 outputPoolID = 28 + inputPoolID;
uint256 numOutputs = 193284;
game.mint(player2, inputPoolID, 2 * numOutputs);
uint256 initialInputBalance = game.balanceOf(player2, inputPoolID);
uint256 initialOutputBalance = game.balanceOf(player2, outputPoolID);
assertEq(game.CurrentTier(i, j), 0);
vm.expectEmit();
emit TierUnlocked(i, j, 1, outputPoolID);
game.craft(inputPoolID, numOutputs);
uint256 terminalInputBalance = game.balanceOf(player2, inputPoolID);
uint256 terminalOutputBalance = game.balanceOf(player2, outputPoolID);
assertEq(terminalInputBalance, initialInputBalance - 2 * numOutputs);
assertEq(terminalOutputBalance, initialOutputBalance + numOutputs);
assertEq(game.CurrentTier(i, j), 1);
}
}
}

function test_crafting_tier_92384_to_tier_92385() public {
vm.startPrank(player2);
for (uint256 i = 0; i < 4; i++) {
Expand All @@ -846,7 +885,9 @@ contract JackpotJunctionPlayTest is Test {
uint256 initialInputBalance = game.balanceOf(player2, inputPoolID);
uint256 initialOutputBalance = game.balanceOf(player2, outputPoolID);
assertEq(game.CurrentTier(i, j), 92384);
game.craft(inputPoolID);
vm.expectEmit();
emit TierUnlocked(i, j, 92385, outputPoolID);
game.craft(inputPoolID, 1);
uint256 terminalInputBalance = game.balanceOf(player2, inputPoolID);
uint256 terminalOutputBalance = game.balanceOf(player2, outputPoolID);
assertEq(terminalInputBalance, initialInputBalance - 2);
Expand All @@ -856,4 +897,29 @@ contract JackpotJunctionPlayTest is Test {
}
}

function test_crafting_fails_when_insufficient_zero_balance() public {
vm.startPrank(player2);
for (uint256 i = 0; i < 4; i++) {
for (uint256 j = 0; j < 7; j++) {
uint256 inputPoolID = 92384 * 28 + 4 * j + i;
game.burn(inputPoolID, game.balanceOf(player2, inputPoolID));
vm.expectRevert(abi.encodeWithSelector(JackpotJunction.InsufficientItems.selector, inputPoolID));
game.craft(inputPoolID, 1);
}
}
}

function test_crafting_fails_when_insufficient_positive_balance() public {
vm.startPrank(player2);
for (uint256 i = 0; i < 4; i++) {
for (uint256 j = 0; j < 7; j++) {
uint256 inputPoolID = 92384 * 28 + 4 * j + i;
uint256 numOutputs = 19283498;
game.burn(inputPoolID, game.balanceOf(player2, inputPoolID));
game.mint(player2, inputPoolID, 2*numOutputs - 1);
vm.expectRevert(abi.encodeWithSelector(JackpotJunction.InsufficientItems.selector, inputPoolID));
game.craft(inputPoolID, numOutputs);
}
}
}
}

0 comments on commit d4aa4c2

Please sign in to comment.