我正处于学习坚固性和安全帽的初级阶段。我找到了一个似乎相当不错的教程,我能够从下面的示例中获得代码:
https://dev.to/dabit3/building-scalable-full-stack-apps-on-ethereum-with-polygon-2cfb
.假设我想扩展这个示例,并在其中添加工厂模式功能。如果一个方法是通过工厂方法调用的,我如何确保事件通过工厂方法传播出去?在这种情况下,我将如何处理示例中的事件。js文件?
NFT市场工厂。索尔:
// 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);
}
}
NFT市场。索尔
/* 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
);
}
测试/样本。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})
.
.
.