'Solidity and Etheruem: Handling events while using the factory pattern
I am in the beginning stages on learning Solidity and hardhat. I found a tutorial that seems pretty good and I was able to get the code to work from the example here: https://dev.to/dabit3/building-scalable-full-stack-apps-on-ethereum-with-polygon-2cfb. Let's say I wanted to expand this example and add factory pattern functionality to it. If a method is getting called through a factory method, how do I ensure events are propogated out past the factory method? In this case, how would I handle the event(s) in the sample.js file?
NFTMarketFactory.sol:
// contracts/Market.sol
// SPDX-License-Identifier: MIT OR Apache-2.0
pragma solidity ^0.8.3;
import "./NFTMarket.sol";
import "@openzeppelin/contracts/security/ReentrancyGuard.sol";
import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
contract NFTMarketFactory is ReentrancyGuard {
address address;
address owner;
constructor() {
owner = payable(msg.sender);
}
.
.
.
function createMarketItem(
address nftContract,
uint256 tokenId,
uint256 price
) public payable nonReentrant {
NFTMarket market = NFTMarket(address);
return market.createMarketItem{value: msg.value}(address, tokenId, price, msg.sender);
}
}
NFTMarket.sol
/* Places an item for sale on the marketplace */
function createMarketItem(
address nftContract,
uint256 tokenId,
uint256 price,
address sender
) public payable nonReentrant {
require(price > 0, "Price must be at least 1 wei");
require(msg.value == listingPrice, "Price must be equal to listing price");
_itemIds.increment();
uint256 itemId = _itemIds.current();
idToMarketItem[itemId] = MarketItem(
itemId,
nftContract,
tokenId,
payable(sender),
payable(address(0)),
price,
false
);
IERC721(nftContract).transferFrom(sender, address(this), tokenId);
emit MarketItemCreated(
itemId,
nftContract,
tokenId,
sender,
address(0),
price,
false
);
}
test/sample.js
.
.
.
/* create two tokens */
let token1 = await nft.createToken("https://www.mytokenlocation.com");
let token2 = await nft.createToken("https://www.mytokenlocation2.com");
/* put both tokens for sale */
await nftMarketFactory.createMerchandise(nftContractAddress, 1, auctionPrice, { value: listingPrice })
await nftMarketFactory.createMerchandise(nftContractAddress, 2, auctionPrice, { value: listingPrice })
const [_, buyerAddress] = await ethers.getSigners()
/* execute sale of token to another user */
await nftMarketFactory.connect(buyerAddress).createMarketSale(nftContractAddress, 1, { value: auctionPrice})
.
.
.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
