Hello everyone! This is my first post in this forum and I am excited to start participating in this forum and to interact with you all! For my first post I wanted to see if I could get some advice on testing my smart contract. I am currently using Foundry to test my smart contract for my NFT Marketplace project however since I am relatively new to testing, I am a bit confused on an error that I am getting and how to handle it. I am getting an assertion error however it is not telling me where it is failing specifically(only telling me the functi0n that is failing). How do you guys figure out what the specific error is? Any tips? Here is my code and my test. Thank you guys in advance! And sorry in advance if its a really silly mistake on my part haha. I welcome constructive criticism so be honest! Thank you:)
It tells you that it reverts with TestCreateListing()
, so you can go into that function of yours and find out exactly where that happens in the code.
Also, please post all the necessary details (and only the necessary details) in PLAINTEXT, which readers can easily copy-paste from.
Hello. I actually figured this all out haha. My testing is now running pretty smoothy as I am getting a good grip on how this works. And i will note that as well. Thank you!
Also is there anyway to test incrementation and decrementation in foundry? That is one thing I want to test in my fucntions however I am not too sure on how to code that?
Incrementation and decrementation of what?
You haven't posted any code whatsoever!
I believe I did? On my screen it shows that I posted my code(pictures) for both my test and contract itself. But here is my code for two functions as an example of what I mean.
function sell(
uint256 _tokenId,
uint256 _price,
address _seller,
address _owner,
address _buyer
)external payable returns(
address,
uint256,
uint256)
{
require(balanceOf[_buyer] <= _price, "Insufficient funds to purchase NFT");
balanceOf[_buyer] <= _price;
require(_seller == _owner, "Seller is not the current owner of the NFT");
_seller == _owner;
require(msg.value == _price, "Price must be the correct selling price of the NFT");
msg.value == _price;
require(_buyer == msg.sender, "Buyer not the caller of the transaction");
_buyer ==msg.sender;
itemsSold++;
tokenIds++;
_tokenSupply--;
uint256 newItemsSold = currentItemsSold;
owner = _owner;
return(_owner, _tokenId, newItemsSold);
}
function buyNft(
address _owner,
address _buyer
)external payable returns(uint256)
{
itemsSold++;
uint256 newItemsSold = currentItemsSold;
require (msg.sender != receiver, "You cannot send money to yourself");
if(msg.sender != receiver){
revert("Not authorized to buy this NFT");
}
require (balanceOf[_buyer] > 0, "Not enough funds to purchase NFT");
balanceOf[_buyer] = 0;
if(balanceOf[_buyer] <= 0){
revert("Insufficient funds to purchase NFT listing");
}
owner = _owner;
require(nftAmount[tokenId] >= 1, "Token does not exist");
balanceOf[owner] += balance;
balanceOf[receiver] -= balance;
return newItemsSold;
emit nftSold(
owner,
msg.sender,
tokenId,
msg.value
);
}
Here are my tests for these functions(they are not complete).
function testBuyListing() public payable{
address owner;
//the owner of the NFT
uint256 listingPriceOfNFT;
uint256 balanceOfBuyer = listingPriceOfNFT;
assertGe(balanceOfBuyer, listingPriceOfNFT, "Balance must be greater than or equal to NFT listing price");
address buyer = msg.sender;
assertEq(buyer, msg.sender, "Only a buyer can call this function");
address seller = owner;
assertEq(seller, owner, "Only the owner of this NFT is authorized to sell");
emit log_address(owner);
emit log_address(msg.sender);
emit log_uint(tokenIds);
emit log_uint(msg.value);
}
function testSellFunction() public payable{
address ownerOfNft;
uint256 priceOfNft;
uint256 balanceOfBuyer;
address seller = msg.sender;
assertEq(seller, msg.sender, "The address calling this function must be the seller of the NFT");
address sellerOfNft = ownerOfNft;
assertEq(sellerOfNft, ownerOfNft, "Seller must be the owner of this NFT");
assertGe(balanceOfBuyer, priceOfNft, "Balance must be higher in order to purchase NFT");
uint256 nftPrice = msg.value;
assertEq(nftPrice, msg.value,"Price must be the correct selling price of the NFT");
}
If you have any suggestions for my code feel free to say! Thank you
And what did the first response to your question tell you about that?
Hint: PLAINTEXT.
Anyways, now that you have posted your code in a proper manner, please explain your question in this context (i.e., with regards to that code).
Here is your previous question
Please explain what "incrementation and decrementation" has to do with your code, and what it is exactly that you are trying to ask.
An example would be in this function ;
function sell(
uint256 _tokenId,
uint256 _price,
address _seller,
address _owner,
address _buyer
)external payable returns(
address,
uint256,
uint256)
{
require(balanceOf[_buyer] <= _price, "Insufficient funds to purchase NFT");
balanceOf[_buyer] <= _price;
require(_seller == _owner, "Seller is not the current owner of the NFT");
_seller == _owner;
require(msg.value == _price, "Price must be the correct selling price of the NFT");
msg.value == _price;
require(_buyer == msg.sender, "Buyer not the caller of the transaction");
_buyer ==msg.sender;
itemsSold++;
tokenIds++;
_tokenSupply--;
uint256 newItemsSold = currentItemsSold;
owner = _owner;
return(_owner, _tokenId, newItemsSold);
}
The incrementation and decrementation I am speaking about is this part of my code,
itemsSold++;
tokenIds++;
_tokenSupply--;
I basically want to test if some variables and values within my functions are incrementing and decrementing like they should.