Setting Up the Project
Building a Complete DApp
Chapter 2: Setting Up the Project
Initialize the Anchor project and frontend.
Steps:
anchor init marketplace
Add React and Web3.js dependencies.
Configure Anchor.toml for localnet.
Interactivity: Copy setup commands from a terminal emulator.
Chapter 3: Implementing the Backend with Anchor
Create the marketplace program with listing and buying logic.
Key Instructions:
list_item: Stores item data in a PDA.
buy_item: Transfers SOL and updates the PDA.
Code Example:
pub fn list_item(ctx: Context<ListItem>, price: u64) -> Result<()> {
let item = &mut ctx.accounts.item;
item.seller = *ctx.accounts.seller.key;
item.price = price;
item.is_sold = false;
Ok(())
}
pub fn buy_item(ctx: Context<BuyItem>) -> Result<()> {
let item = &mut ctx.accounts.item;
require!(!item.is_sold, MarketplaceError::ItemSold);
// Transfer SOL logic
item.is_sold = true;
Ok(())
}
Interactivity: Simulate listing and buying items, seeing PDA updates.
Last updated