Example: Asset Trading Library

The purpose of this library is to facilitate the creation, management and trading of digital assets in blockchain gaming. This library ensures secure, decentralized asset ownership and trading, enabling players to fully control their assets and engage in economic activities within the game ecosystem.

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

import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
import "@openzeppelin/contracts/token/ERC721/extensions/ERC721Enumerable.sol";
import "@openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/utils/Counters.sol";

contract EvireAssetTradingLibrary is ERC721, ERC721Enumerable, ERC721URIStorage, Ownable {
    using Counters for Counters.Counter;
    Counters.Counter private _tokenIdCounter;

    struct Asset {
        uint256 id;
        string name;
        string metadataURI;
        uint256 price;
        bool forSale;
    }

    mapping(uint256 => Asset) private assets;

    event AssetCreated(uint256 indexed assetId, string name, string metadataURI, uint256 price);
    event AssetForSale(uint256 indexed assetId, uint256 price);
    event AssetSold(uint256 indexed assetId, address indexed buyer, uint256 price);
    event AssetRemovedFromSale(uint256 indexed assetId);

    constructor() ERC721("EvireAsset", "EVA") {}

    function createAsset(string memory name, string memory metadataURI, uint256 price) public onlyOwner {
        uint256 assetId = _tokenIdCounter.current();
        _tokenIdCounter.increment();
        
        assets[assetId] = Asset(assetId, name, metadataURI, price, false);
        
        _mint(msg.sender, assetId);
        _setTokenURI(assetId, metadataURI);

        emit AssetCreated(assetId, name, metadataURI, price);
    }

    function listAssetForSale(uint256 assetId, uint256 price) public onlyOwner {
        require(_exists(assetId), "Asset does not exist");
        require(ownerOf(assetId) == msg.sender, "You do not own this asset");

        assets[assetId].price = price;
        assets[assetId].forSale = true;

        emit AssetForSale(assetId, price);
    }

    function buyAsset(uint256 assetId) public payable {
        require(_exists(assetId), "Asset does not exist");
        require(assets[assetId].forSale, "Asset is not for sale");
        require(msg.value == assets[assetId].price, "Incorrect price");

        address owner = ownerOf(assetId);
        _transfer(owner, msg.sender, assetId);

        payable(owner).transfer(msg.value);
        
        assets[assetId].forSale = false;

        emit AssetSold(assetId, msg.sender, msg.value);
    }

    function removeAssetFromSale(uint256 assetId) public onlyOwner {
        require(_exists(assetId), "Asset does not exist");
        require(assets[assetId].forSale, "Asset is not for sale");

        assets[assetId].forSale = false;

        emit AssetRemovedFromSale(assetId);
    }

    function _beforeTokenTransfer(address from, address to, uint256 tokenId) internal override(ERC721, ERC721Enumerable) {
        super._beforeTokenTransfer(from, to, tokenId);
    }

    function _burn(uint256 tokenId) internal override(ERC721, ERC721URIStorage) {
        super._burn(tokenId);
    }

    function tokenURI(uint256 tokenId) public view override(ERC721, ERC721URIStorage) returns (string memory) {
        return super.tokenURI(tokenId);
    }

    function supportsInterface(bytes4 interfaceId) public view override(ERC721, ERC721Enumerable) returns (bool) {
        return super.supportsInterface(interfaceId);
    }
}

Example Usage

This dApp will manage a marketplace for unique digital assets where users can create, list for sale, buy and remove assets from sale.

Smart Contract

Web3.js Integration

Below is a JavaScript example using Web3.js to interact with the deployed EvireMarketplace smart contract. This will include functions to create an asset, list it for sale, buy it and remove it from sale.

Explanation

  1. Smart Contract:

    • The EvireMarketplace contract initializes with the address of the deployed EvireAssetTradingLibrary.

    • It exposes functions to create assets, list them for sale, buy them, and remove them from sale, while emitting corresponding events.

    • It provides a way to get asset details and the total number of assets.

  2. Web3.js Integration:

    • This script uses Web3.js to interact with the smart contract.

    • It includes functions to create assets, list them for sale, buy them, and remove them from sale.

    • It demonstrates the usage of these functions in an example asynchronous function.

Last updated