Skip to content

Commit 106739b

Browse files
authored
Correctly update pack asset after giving out reward (#484)
Correctly update total amount for each asset after giving out reward
1 parent c840fd5 commit 106739b

File tree

2 files changed

+111
-1
lines changed

2 files changed

+111
-1
lines changed

contracts/prebuilts/pack/Pack.sol

+3-1
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,9 @@ contract Pack is
354354
if (target < step + totalRewardUnitsOfKind) {
355355
_token[j].totalAmount -= perUnitAmount;
356356
_isUpdated[j] = true;
357-
rewardUnits[i] = _token[j];
357+
rewardUnits[i].assetContract = _token[j].assetContract;
358+
rewardUnits[i].tokenType = _token[j].tokenType;
359+
rewardUnits[i].tokenId = _token[j].tokenId;
358360
rewardUnits[i].totalAmount = perUnitAmount;
359361
totalRewardUnits -= 1;
360362
break;

src/test/pack/Pack.t.sol

+108
Original file line numberDiff line numberDiff line change
@@ -811,6 +811,114 @@ contract PackTest is BaseTest {
811811
assertEq(packed.length, packContents.length);
812812
}
813813

814+
/**
815+
* note: Total amount should get updated correctly -- reduce perUnitAmount from totalAmount of the token content, for each reward
816+
*/
817+
function test_state_openPack_totalAmounts_ERC721() public {
818+
vm.warp(1000);
819+
uint256 packId = pack.nextTokenIdToMint();
820+
uint256 packsToOpen = 1;
821+
address recipient = address(1);
822+
823+
erc721.mint(address(tokenOwner), 6);
824+
825+
ITokenBundle.Token[] memory tempContents = new ITokenBundle.Token[](1);
826+
uint256[] memory tempNumRewardUnits = new uint256[](1);
827+
828+
tempContents[0] = ITokenBundle.Token({
829+
assetContract: address(erc721),
830+
tokenType: ITokenBundle.TokenType.ERC721,
831+
tokenId: 0,
832+
totalAmount: 1
833+
});
834+
tempNumRewardUnits[0] = 1;
835+
836+
vm.prank(address(tokenOwner));
837+
(, uint256 totalSupply) = pack.createPack(tempContents, tempNumRewardUnits, packUri, 0, 1, recipient);
838+
839+
vm.prank(recipient, recipient);
840+
ITokenBundle.Token[] memory rewardUnits = pack.openPack(packId, packsToOpen);
841+
842+
assertEq(packUri, pack.uri(packId));
843+
assertEq(pack.totalSupply(packId), totalSupply - packsToOpen);
844+
845+
(ITokenBundle.Token[] memory packed, ) = pack.getPackContents(packId);
846+
assertEq(packed.length, tempContents.length);
847+
assertEq(packed[0].totalAmount, tempContents[0].totalAmount - rewardUnits[0].totalAmount);
848+
}
849+
850+
/**
851+
* note: Total amount should get updated correctly -- reduce perUnitAmount from totalAmount of the token content, for each reward
852+
*/
853+
function test_state_openPack_totalAmounts_ERC1155() public {
854+
vm.warp(1000);
855+
uint256 packId = pack.nextTokenIdToMint();
856+
uint256 packsToOpen = 1;
857+
address recipient = address(1);
858+
859+
erc1155.mint(address(tokenOwner), 0, 100);
860+
861+
ITokenBundle.Token[] memory tempContents = new ITokenBundle.Token[](1);
862+
uint256[] memory tempNumRewardUnits = new uint256[](1);
863+
864+
tempContents[0] = ITokenBundle.Token({
865+
assetContract: address(erc1155),
866+
tokenType: ITokenBundle.TokenType.ERC1155,
867+
tokenId: 0,
868+
totalAmount: 100
869+
});
870+
tempNumRewardUnits[0] = 10;
871+
872+
vm.prank(address(tokenOwner));
873+
(, uint256 totalSupply) = pack.createPack(tempContents, tempNumRewardUnits, packUri, 0, 1, recipient);
874+
875+
vm.prank(recipient, recipient);
876+
ITokenBundle.Token[] memory rewardUnits = pack.openPack(packId, packsToOpen);
877+
878+
assertEq(packUri, pack.uri(packId));
879+
assertEq(pack.totalSupply(packId), totalSupply - packsToOpen);
880+
881+
(ITokenBundle.Token[] memory packed, ) = pack.getPackContents(packId);
882+
assertEq(packed.length, tempContents.length);
883+
assertEq(packed[0].totalAmount, tempContents[0].totalAmount - rewardUnits[0].totalAmount);
884+
}
885+
886+
/**
887+
* note: Total amount should get updated correctly -- reduce perUnitAmount from totalAmount of the token content, for each reward
888+
*/
889+
function test_state_openPack_totalAmounts_ERC20() public {
890+
vm.warp(1000);
891+
uint256 packId = pack.nextTokenIdToMint();
892+
uint256 packsToOpen = 1;
893+
address recipient = address(1);
894+
895+
erc20.mint(address(tokenOwner), 2000 ether);
896+
897+
ITokenBundle.Token[] memory tempContents = new ITokenBundle.Token[](1);
898+
uint256[] memory tempNumRewardUnits = new uint256[](1);
899+
900+
tempContents[0] = ITokenBundle.Token({
901+
assetContract: address(erc20),
902+
tokenType: ITokenBundle.TokenType.ERC20,
903+
tokenId: 0,
904+
totalAmount: 1000 ether
905+
});
906+
tempNumRewardUnits[0] = 50;
907+
908+
vm.prank(address(tokenOwner));
909+
(, uint256 totalSupply) = pack.createPack(tempContents, tempNumRewardUnits, packUri, 0, 1, recipient);
910+
911+
vm.prank(recipient, recipient);
912+
ITokenBundle.Token[] memory rewardUnits = pack.openPack(packId, packsToOpen);
913+
914+
assertEq(packUri, pack.uri(packId));
915+
assertEq(pack.totalSupply(packId), totalSupply - packsToOpen);
916+
917+
(ITokenBundle.Token[] memory packed, ) = pack.getPackContents(packId);
918+
assertEq(packed.length, tempContents.length);
919+
assertEq(packed[0].totalAmount, tempContents[0].totalAmount - rewardUnits[0].totalAmount);
920+
}
921+
814922
/**
815923
* note: Testing event emission; pack owner calls `openPack` to open owned packs.
816924
*/

0 commit comments

Comments
 (0)