市场
上架与转移
市场 是 CoddyKit 上的免费 Web3 & DApp Development Fundamentals 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Web3 & DApp Development Fundamentals 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Web3 & DApp Development Fundamentals 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
How NFT Marketplaces Work
An NFT marketplace lets users list tokens for sale and lets buyers purchase them. Behind the scenes it relies on the ERC-721 approval and transfer mechanics you have already learned.
The marketplace contract never owns your NFT - it just moves it when a sale executes.
The Approval Step
Before listing, the seller must approve the marketplace to move the token. Sellers usually call setApprovalForAll so one approval covers all their NFTs in a collection.
<code>// seller authorizes the marketplace
nft.setApprovalForAll(marketplaceAddress, true);</code>Storing a Listing
The marketplace records each listing with the seller, the NFT contract, the token id, and the price.
<code>struct Listing {
address seller;
address nft;
uint256 tokenId;
uint256 price;
}
mapping(bytes32 => Listing) public listings;</code>Creating a Listing
To list, the seller calls a function that verifies they own the token and that the marketplace is approved, then stores the listing.
<code>function list(address nft, uint256 tokenId, uint256 price) public {
require(IERC721(nft).ownerOf(tokenId) == msg.sender, 'not owner');
require(IERC721(nft).isApprovedForAll(msg.sender, address(this)), 'not approved');
bytes32 id = keccak256(abi.encodePacked(nft, tokenId));
listings[id] = Listing(msg.sender, nft, tokenId, price);
}</code>Buying an NFT
A buyer sends the price as Ether. The marketplace transfers the NFT from seller to buyer using safeTransferFrom and forwards the payment.
<code>function buy(bytes32 id) public payable {
Listing memory l = listings[id];
require(msg.value >= l.price, 'insufficient payment');
delete listings[id];
IERC721(l.nft).safeTransferFrom(l.seller, msg.sender, l.tokenId);
payable(l.seller).transfer(l.price);
}</code>Delete Before Transfer
Notice the listing is deleted before external calls. This follows the checks-effects-interactions pattern and prevents reentrancy from buying the same NFT twice.
<code>delete listings[id]; // effect first
// then interactions (transfers)</code>Marketplace Fees
Marketplaces typically take a percentage fee. The contract splits the payment between the seller and a fee recipient.
<code>uint256 public feeBps = 250; // 2.5%
uint256 fee = (l.price * feeBps) / 10000;
payable(feeRecipient).transfer(fee);
payable(l.seller).transfer(l.price - fee);</code>Royalties (ERC-2981)
The ERC-2981 standard lets creators earn a royalty on secondary sales. A marketplace queries royaltyInfo and pays the creator their share.
<code>function royaltyInfo(uint256 tokenId, uint256 salePrice)
external view returns (address receiver, uint256 royaltyAmount);</code>Cancelling a Listing
Sellers should be able to remove their listing. Only the original seller may cancel.
<code>function cancel(bytes32 id) public {
require(listings[id].seller == msg.sender, 'not seller');
delete listings[id];
}</code>Off-Chain Order Books
Large marketplaces like OpenSea avoid storing every listing on-chain. Instead sellers sign orders off-chain, and the contract only runs the transfer when a buyer submits a matching signed order. This saves enormous gas.
Security Considerations
Marketplaces are high-value targets. Guard against reentrancy, validate ownership and approvals at execution time, and never trust prices or addresses from untrusted input without checks.
Quick Check
Test your understanding of NFT marketplaces.
Recap
You learned how NFT marketplaces operate:
- Sellers approve the marketplace, which then transfers on sale (no escrow needed)
- Listings store seller, NFT, tokenId, and price
- Buying transfers the NFT and forwards payment, deleting the listing first (checks-effects-interactions)
- Fees and ERC-2981 royalties split the proceeds
- Big marketplaces use off-chain signed orders to save gas
用 AI 导师学习 Web3 & DApp Development Fundamentals — 免费
在浏览器中编写并运行真实代码,获得全天候 AI 导师的即时帮助,并在网页或应用中继续学习。
- 课程
- 29
- 课程
- 105
常见问题解答
「市场」课时是免费的吗?
是的 — 「市场」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Web3 & DApp Development Fundamentals 课程的其余内容,请升级到 CoddyKit PRO。 Web3 & DApp Development Fundamentals 课程共包含 4 节课。
「市场」这节课中我会学到什么?
上架与转移 你通过在浏览器中直接运行的动手代码来练习 Web3 & DApp Development Fundamentals,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Web3 & DApp Development Fundamentals 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Web3 & DApp Development Fundamentals 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。
「市场」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Web3 & DApp Development Fundamentals 课中编写并运行代码吗?
能。每节 Web3 & DApp Development Fundamentals 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。