Skip to content

Commit eb22101

Browse files
Axel ValavaaraAxel Valavaara
authored andcommitted
started on fuzz testing
1 parent a1fda76 commit eb22101

5 files changed

Lines changed: 103 additions & 3 deletions

File tree

foundry.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ remappings = ["@openzeppelin/contracts=lib/openzeppelin-contracts/contracts"]
77
[invariant]
88
runs = 128
99
depth = 20
10-
fail_on_revert = false
10+
fail_on_revert = true
1111

1212
# See more config options https://github.com/foundry-rs/foundry/blob/master/crates/config/README.md#all-options

src/MerchantNft.sol

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,13 @@ contract MerchantNft is ERC721 {
8282
return "ipfs://bafkreidn7tkkov6iykgq5kb7zbfb77tg7lza5exutuufoozebeg7gwz22u";
8383
}
8484

85+
function ownerBurn(uint256 tokenId) external onlyOwner {
86+
if (_ownerOf(tokenId) == address(0)) {
87+
revert MerchantNft__TokenDoesNotExist();
88+
}
89+
_burn(tokenId);
90+
}
91+
8592
/*//////////////////////////////////////////////////////////////
8693
GETTER FUNCTIONS
8794
//////////////////////////////////////////////////////////////*/

test/fuzz/Handler.t.sol

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
//SPDX-License-Identifier: MIT
2+
3+
import {OrderContract} from "../../src/OrderContract.sol";
4+
import {Test, console} from "forge-std/Test.sol";
5+
import {A3AToken} from "../../src/A3Atoken.sol";
6+
7+
8+
pragma solidity ^0.8.18;
9+
10+
11+
contract FuzzHandler is Test {
12+
13+
A3AToken public tokenContract;
14+
OrderContract public orderContract;
15+
16+
uint64 public orderIdCounter = 0;
17+
uint64[] public orderIdArray;
18+
19+
constructor(address orderContractAddress, address tokenAddress) {
20+
orderContract = OrderContract(orderContractAddress);
21+
tokenContract = A3AToken(tokenAddress);
22+
23+
}
24+
25+
function proposeOrder(bytes32 promptHash, address userWalletAddress) external {
26+
if (userWalletAddress == address(0)) {
27+
return;
28+
}
29+
orderIdCounter++;
30+
vm.prank(address(orderContract));
31+
tokenContract.mint(userWalletAddress, 1000 ether);
32+
vm.prank(userWalletAddress);
33+
tokenContract.approve(address(orderContract), 500 ether);
34+
vm.startPrank(address(5));
35+
orderContract.proposeOrder(promptHash, userWalletAddress);
36+
vm.stopPrank();
37+
orderIdCounter++;
38+
orderIdArray.push(orderIdCounter);
39+
40+
41+
42+
}
43+
44+
function proposeOrderAnswer(bytes32 answerHash, uint64 offerId, uint256 priceForOffer, address seller) external {
45+
if (seller == address(0)) {
46+
return;
47+
}
48+
if (orderIdArray.length == 0) {
49+
return;
50+
}
51+
uint64 offerID = orderIdArray[offerId % orderIdArray.length];
52+
53+
if (offerID == 0) {
54+
return;
55+
}
56+
if (orderContract.getOfferStatus(offerID) != OrderContract.OrderStatus.InProgress) {
57+
return;
58+
}
59+
vm.prank(address(5));
60+
orderContract.proposeOrderAnswer(answerHash, offerID, priceForOffer, seller);
61+
}
62+
63+
}

test/fuzz/InvariantsTest.t.sol

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,20 @@ import {HelperConfig} from "script/HelperConfig.s.sol";
88
import {DeployOrderContract} from "script/DeployOrderContract.s.sol";
99
import {ERC20Mock} from "@openzeppelin/contracts/mocks/token/ERC20Mock.sol";
1010
import {StdInvariant} from "forge-std/StdInvariant.sol";
11+
import {FuzzHandler} from "./Handler.t.sol";
12+
import {A3AToken} from "../../src/A3Atoken.sol";
1113

1214
contract InvariantsTest is StdInvariant, Test {
1315
OrderContract orderContract;
1416
HelperConfig helperConfig;
17+
FuzzHandler handler;
18+
A3AToken token;
1519

1620
function setUp() public {
1721
DeployOrderContract deployer = new DeployOrderContract();
18-
(orderContract, helperConfig,,) = deployer.run();
19-
targetContract(address(orderContract));
22+
(orderContract, helperConfig,token,) = deployer.run();
23+
handler = new FuzzHandler(address(orderContract), address(token));
24+
targetContract(address(handler));
2025
}
2126

2227

test/unit/OrderContractTest.t.sol

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,31 @@ contract OrderContractTest is Test {
403403
merchantNft.mintNft(merchantId, USER);
404404
}
405405

406+
function testMerchantNftOwnerBurn() public {
407+
// Arrange
408+
uint256 merchantId = 2;
409+
vm.prank(owner);
410+
merchantNft.mintNft(merchantId, SELLER);
411+
// Act
412+
vm.prank(owner);
413+
merchantNft.ownerBurn(merchantId);
414+
// Assert
415+
vm.prank(owner);
416+
vm.expectRevert(MerchantNft.MerchantNft__TokenDoesNotExist.selector);
417+
merchantNft.ownerBurn(merchantId);
418+
}
419+
420+
function testMerchantNftOnlyOwnerCanBurn() public {
421+
// Arrange
422+
uint256 merchantId = 2;
423+
vm.prank(owner);
424+
merchantNft.mintNft(merchantId, SELLER);
425+
// Act / Assert
426+
vm.prank(USER);
427+
vm.expectRevert(MerchantNft.MerchantNft__OnlyOwnerCanCall.selector);
428+
merchantNft.ownerBurn(merchantId);
429+
}
430+
406431
/*//////////////////////////////////////////////////////////////
407432
CANCEL ORDER TESTS
408433
//////////////////////////////////////////////////////////////*/

0 commit comments

Comments
 (0)