Skip to content

Commit

Permalink
test: update gas test to be deterministic (#52)
Browse files Browse the repository at this point in the history
  • Loading branch information
ChefMist authored Jul 3, 2024
1 parent ce9b2a4 commit 139a66b
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1 +1 @@
53954
53942
Original file line number Diff line number Diff line change
@@ -1 +1 @@
51884
53680
2 changes: 1 addition & 1 deletion .forge-snapshots/BinFungibleTokenTest#testBurn.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
27030
26848
2 changes: 1 addition & 1 deletion .forge-snapshots/BinFungibleTokenTest#testMint.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
67297
73705
68 changes: 62 additions & 6 deletions test/pool-bin/BinFungibleToken.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,18 @@ contract BinFungibleTokenTest is Test, GasSnapshot {
assertEq(token.balanceOf(alice, id), 0);

// mint and verify bal
token.mint(alice, id, amt);
assertEq(token.balanceOf(alice, id), amt);
}

function testGas_Mint() public {
uint256 id = 5;
uint256 amt = 100;

// mint
snapStart("BinFungibleTokenTest#testMint");
token.mint(alice, id, amt);
snapEnd();
assertEq(token.balanceOf(alice, id), amt);
}

function testMintMultiple(uint256 id, uint256 amt) public {
Expand All @@ -61,10 +69,20 @@ contract BinFungibleTokenTest is Test, GasSnapshot {
assertEq(token.balanceOf(alice, id), amt);

// burn and verify bal
token.burn(alice, id, amt);
assertEq(token.balanceOf(alice, id), 0);
}

function testGas_Burn() public {
uint256 id = 5;
uint256 amt = 100;

// before: mint and verify bal
token.mint(alice, id, amt);

snapStart("BinFungibleTokenTest#testBurn");
token.burn(alice, id, amt);
snapEnd();
assertEq(token.balanceOf(alice, id), 0);
}

function testBurn_ExceedBalance(uint256 id, uint256 amt) public {
Expand Down Expand Up @@ -228,15 +246,32 @@ contract BinFungibleTokenTest is Test, GasSnapshot {
// transfer
vm.expectEmit();
emit TransferBatch(alice, alice, bob, ids, amounts);
snapStart("BinFungibleTokenTest#testBatchTransferFrom_FromOwner");
token.batchTransferFrom(alice, bob, ids, amounts);
snapEnd();

// verify
assertEq(token.balanceOf(alice, id), 0);
assertEq(token.balanceOf(bob, id), amt);
}

function testGas_BatchTransferFrom_FromOwner() public {
uint256 id = 5;
uint256 amt = 100;

// pre-req mint some nft to alice
token.mint(alice, id, amt);

vm.startPrank(alice);
uint256[] memory ids = new uint256[](1);
ids[0] = id;
uint256[] memory amounts = new uint256[](1);
amounts[0] = amt;

// transfer
snapStart("BinFungibleTokenTest#testBatchTransferFrom_FromOwner");
token.batchTransferFrom(alice, bob, ids, amounts);
snapEnd();
}

function testBatchTransferFrom_FromBob(uint256 id, uint256 amt) public {
// pre-req mint some nft to alice
token.mint(alice, id, amt);
Expand All @@ -256,12 +291,33 @@ contract BinFungibleTokenTest is Test, GasSnapshot {
// transfer
vm.expectEmit();
emit TransferBatch(bob, alice, bob, ids, amounts);
snapStart("BinFungibleTokenTest#testBatchTransferFrom_FromBob");
token.batchTransferFrom(alice, bob, ids, amounts);
snapEnd();

// verify
assertEq(token.balanceOf(alice, id), 0);
assertEq(token.balanceOf(bob, id), amt);
}

function testGas_BatchTransferFrom_FromBob() public {
uint256 id = 5;
uint256 amt = 100;

// pre-req mint some nft to alice
token.mint(alice, id, amt);

// alice give bob approval
vm.prank(alice);
token.approveForAll(bob, true);

// bob tries to transfer
vm.startPrank(bob);
uint256[] memory ids = new uint256[](1);
ids[0] = id;
uint256[] memory amounts = new uint256[](1);
amounts[0] = amt;

snapStart("BinFungibleTokenTest#testBatchTransferFrom_FromBob");
token.batchTransferFrom(alice, bob, ids, amounts);
snapEnd();
}
}

0 comments on commit 139a66b

Please sign in to comment.