CryptoSpiel.com
No Result
View All Result
  • Home
  • Live Crypto Prices
  • Live ICO
  • Exchange
  • Crypto News
  • Bitcoin
  • Altcoins
  • Blockchain
  • Regulations
  • Trading
  • Scams
  • Home
  • Live Crypto Prices
  • Live ICO
  • Exchange
  • Crypto News
  • Bitcoin
  • Altcoins
  • Blockchain
  • Regulations
  • Trading
  • Scams
No Result
View All Result
CryptoSpiel.com
No Result
View All Result

Creating a Web3 Marketplace Dapp on Linea: A Comprehensive Guide

September 18, 2024
in Blockchain
Reading Time: 4 mins read
A A
0
Understanding Ambiguity: Causes and Effects
0
SHARES
2
VIEWS
ShareShareShareShareShare


Ted Hisokawa
Sep 18, 2024 05:59

Learn how to build a Web3 marketplace dapp on Linea, covering front-end and smart contract development for a decentralized marketplace.





In the burgeoning world of Web3, decentralized applications (dapps) have emerged as a cornerstone, providing secure, transparent, and trustless services. Among these, decentralized marketplaces stand out, allowing users to buy and sell items directly on the blockchain, effectively eliminating intermediaries. According to linea.mirror.xyz, this guide delves into building a simple marketplace dapp on Linea, a Layer 2 zkEVM technology that offers scalability and cost efficiency while maintaining Ethereum’s security.

Understanding the Marketplace Dapp Architecture

The architecture of the marketplace dapp incorporates smart contracts, frontend, and blockchain integration. The smart contract will handle features like listing items, buying items, and transferring ownership. The frontend will serve as the user interface enabling buyers and sellers to interact with the smart contract. The interaction with the Linea blockchain will utilize the Metamask SDK along with Wagmi and Infura RPC providers.

Setting Up the Environment

First, initialize a monorepo to manage the project:

mkdir web3-marketplace-linea
cd web3-marketplace-linea
pnpm init

Create a pnpm-workspace.yaml file in the root:

packages:
  - 'packages/*'

The workspace structure will include:

packages
├── site         # Frontend built with Next.js, Tailwind CSS, and Shadcn UI
└── blockchain   # Smart contracts using Hardhat

Next, initialize a Hardhat project in the blockchain directory:

cd blockchain
npx hardhat init

Choose the TypeScript project option when prompted.

Writing the Smart Contract

The smart contract will manage the marketplace’s items, their ownership, and transactions:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract Marketplace {
    struct Item {
        uint id;
        string name;
        uint price;
        address payable seller;
        address owner;
        bool isSold;
    }
    uint public itemCount = 0;
    mapping(uint => Item) public items;
    mapping(address => uint[]) public ownedItems;

    function listItem(string memory _name, uint _price) public {
        require(_price > 0, "Price must be greater than zero");
        itemCount++;
        items[itemCount] = Item(itemCount, _name, _price, payable(msg.sender), msg.sender, false);
        ownedItems[msg.sender].push(itemCount);
    }

    function purchaseItem(uint _id) public payable {
        Item storage item = items[_id];
        require(_id > 0 && _id <= itemCount, "Item does not exist");
        require(msg.value == item.price, "Incorrect price");
        require(!item.isSold, "Item already sold");
        require(msg.sender != item.seller, "Seller cannot buy their own item");
        item.isSold = true;
        item.seller.transfer(msg.value);
        _transferOwnership(_id, item.seller, msg.sender);
    }

    function _transferOwnership(uint _id, address _from, address _to) internal {
        Item storage item = items[_id];
        item.owner = _to;
        uint[] storage fromItems = ownedItems[_from];
        for (uint i = 0; i < fromItems.length; i++) {
            if (fromItems[i] == _id) {
                fromItems[i] = fromItems[fromItems.length - 1];
                fromItems.pop();
                break;
            }
        }
        ownedItems[_to].push(_id);
    }

    function transferItem(uint _id, address _to) public {
        Item storage item = items[_id];
        require(_id > 0 && _id <= itemCount, "Item does not exist");
        require(msg.sender == item.owner, "You do not own this item");
        _transferOwnership(_id, msg.sender, _to);
    }

    function getItemsByOwner(address _owner) public view returns (uint[] memory) {
        return ownedItems[_owner];
    }
}

This contract facilitates a decentralized marketplace where users can list items, purchase them, and transfer ownership, with all transactions recorded on the blockchain.

Deploying the Smart Contract

Create a deployment module in the ignition folder:

import { buildModule } from "@nomicfoundation/hardhat-ignition/modules";
const MarketplaceModule = buildModule("MarketplaceModule", (m) => {
  const marketplace = m.contract("Marketplace");
  return { marketplace };
});
export default MarketplaceModule;

Update the .env file with your Infura API key and account private key:

INFURA_API_KEY=your_infura_api_key_here
ACCOUNT_PRIVATE_KEY=your_account_private_key_here

Deploy the contract to the Linea testnet:

npx hardhat ignition deploy ignition/modules/Marketplace.ts --network linea-testnet

Frontend Development with Next.js and Shadcn UI

Initialize the Next.js project in the site directory:

mkdir site
cd site
npx create-next-app@latest .

Select TypeScript, ESLint, Tailwind CSS, and App Router options when prompted. Install Shadcn UI CLI:

npx shadcn-ui@latest init

Create a wagmi.config.ts file for blockchain connectivity:

import { http, createConfig } from "wagmi";
import { lineaSepolia } from "wagmi/chains";
import { metaMask } from "wagmi/connectors";

export const config = createConfig({
  chains: [lineaSepolia],
  connectors: [metaMask()],
  transports: {
    [lineaSepolia.id]: http(),
  },
});

Create a constants.ts file with the contract address and ABI:

export const CONTRACT_ADDRESS = "your_contract_address_here";
export const ABI = [/* ABI array here */];

Develop the frontend components and hook them up to the smart contract using Wagmi and Metamask SDK. Detailed instructions and code examples can be found in the full guide on linea.mirror.xyz.

Run the development server:

npm run dev

Your Next.js application with Shadcn UI should now be running at http://localhost:3000.

In this guide, a simple decentralized marketplace dapp was built on Linea, leveraging zkEVM technology for scalability and cost efficiency. The process covered everything from setting up the environment to writing and deploying smart contracts, and integrating the frontend with Next.js and Shadcn UI. This foundational knowledge opens doors for further enhancements, such as integrating additional features or optimizing the user experience.

Image source: Shutterstock


Credit: Source link

RELATED POSTS

Anthropic Reveals Claude Code Tool Design Philosophy Behind AI Agent Development

Riot Platforms Sells $289M in Bitcoin as Mining Output Drops 4% in Q1

Exploring Chainlink’s Role Beyond Price Feeds in the Blockchain Ecosystem

Buy JNews
ADVERTISEMENT
ShareTweetSendPinShare
Previous Post

Circle Partners With Sony to Introduce USDC on Soneium

Next Post

Dogecoin Hits 1.93 Million Transactions, Highest Since July

Related Posts

Bitcoin Addresses Holding Between 100 and 10,000 BTC Hit a 7-Week High
Blockchain

Anthropic Reveals Claude Code Tool Design Philosophy Behind AI Agent Development

April 10, 2026
Riot Blockchain Yearly Bitcoin Production Increases by 236%, Accumulates $194M in BTC
Blockchain

Riot Platforms Sells $289M in Bitcoin as Mining Output Drops 4% in Q1

April 2, 2026
Galaxy Digital: Ethereum Developers Discuss Key Upgrades During Latest Consensus Call
Blockchain

Exploring Chainlink’s Role Beyond Price Feeds in the Blockchain Ecosystem

December 9, 2025
Next Post
Dogecoin Hits 1.93 Million Transactions, Highest Since July

Dogecoin Hits 1.93 Million Transactions, Highest Since July

Gala Music Unveils NxWorries Mystery Box Featuring Exclusive Content

Gala Games to Showcase Innovations at TOKEN2049 Singapore 2024

Recommended Stories

No Content Available

Popular Stories

  • Hong Kong’s LEAP toward digital asset dominance

    Hong Kong’s LEAP toward digital asset dominance

    0 shares
    Share 0 Tweet 0
  • Trader Says DeFi Altcoin Aave Witnessing Clear Trend Switch, Updates Forecast on Two Low-Cap Coins

    0 shares
    Share 0 Tweet 0
  • NVIDIA’s AI Platform Enhances ASL Learning Experience

    0 shares
    Share 0 Tweet 0
  • Terra Virtua Joins Williams Racing as Official Metaverse Partner

    0 shares
    Share 0 Tweet 0
  • Cronos (CRO) Labs Expands Partnership with Google Cloud to Boost Blockchain Ecosystem

    0 shares
    Share 0 Tweet 0
CryptoSpiel.com

This is an online news portal that aims to provide the latest crypto news, blockchain, regulations and much more stuff like that around the world. Feel free to get in touch with us!

What’s New Here!

  • Ripple CEO Says CLARITY Act Talks Near Breakthrough as Senate Standoff Eases
  • SEC Opens Proceedings on NYSE Proposal to List Grayscale Crypto ETF Options – Regulation Bitcoin News
  • Anthropic Reveals Claude Code Tool Design Philosophy Behind AI Agent Development

Subscribe Now

Loading
  • Live Crypto Prices
  • Contact Us
  • Privacy Policy
  • Terms of Use
  • DMCA

© 2021 - cryptospiel.com - All rights reserved!

No Result
View All Result
  • Home
  • Live Crypto Prices
  • Live ICO
  • Exchange
  • Crypto News
  • Bitcoin
  • Altcoins
  • Blockchain
  • Regulations
  • Trading
  • Scams

© 2021 - cryptospiel.com - All rights reserved!

Please enter CoinGecko Free Api Key to get this plugin works.