Web3 & DApp Development Fundamentals · Lekcja

Marketplace'y

Wystawianie i transfery

Lekcja 4 z 413 kroki

Marketplace'y to bezpłatna lekcja Web3 & DApp Development Fundamentals na CoddyKit. To lekcja 4 z 4. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej Web3 & DApp Development Fundamentals, a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs Web3 & DApp Development Fundamentals zawiera 4 lekcji w sumie.

Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.

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
Bezpłatny start

Ucz się Web3 & DApp Development Fundamentals dzięki korepetycjom AI — za darmo

Pisz i uruchamiaj kod w przeglądarce, otrzymuj natychmiastową pomoc od korepetytora AI dostępnego 24/7 i kontynuuj naukę w sieci lub w aplikacji.

Kursy
29
Lekcje
105

Często zadawane pytania

Czy lekcja „Marketplace'y” jest bezpłatna?

Tak — pełny tekst „Marketplace'y” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu Web3 & DApp Development Fundamentals, przejdź na CoddyKit PRO. Kurs Web3 & DApp Development Fundamentals zawiera 4 lekcji w sumie.

Co nauczysz się w „Marketplace'y”?

Wystawianie i transfery Ćwiczysz Web3 & DApp Development Fundamentals z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.

Czy potrzebuję doświadczenia, aby zacząć Web3 & DApp Development Fundamentals?

Nie wymagamy żadnego doświadczenia. Web3 & DApp Development Fundamentals w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 4 z 4.

Ile czasu zajmuje lekcja „Marketplace'y”?

Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.

Czy mogę pisać i uruchamiać kod w tej lekcji Web3 & DApp Development Fundamentals?

Tak. Każda lekcja Web3 & DApp Development Fundamentals zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.

Wszystkie lekcje w tym kursie

  1. Standard ERC-721
  2. Tworzenie NFT
  3. Metadane tokenów
  4. Marketplace'y
← Powrót do Web3 & DApp Development Fundamentals