Web3XTLab / DAppStore

0 stars 1 forks source link

[RFC] Core Contract #39

Open nikogu opened 2 years ago

nikogu commented 2 years ago

Goals

Technical Analysis

The contract is divided into two parts:

The Code (beta)

Relying on the ERC721 specification library provided by openzeppelin to generate our own AppNFT.

AppNFT.sol

// contracts/AppNFT.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;

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

contract AppNFT is ERC721URIStorage {
    using Counters for Counters.Counter;
    Counters.Counter private _tokenIds;

    constructor() ERC721("BitForest", "BFT") {}

    function awardItem(address player, string memory tokenURI) public returns (uint256) {
        uint256 newItemId = _tokenIds.current();
        _mint(player, newItemId);
        _setTokenURI(newItemId, tokenURI);

        _tokenIds.increment();
        return newItemId;
    }
}

AppStore.sol

Encapsulate the three information of the app's price, seller, and buyer into a struct structure called AppProperty.

The information(such as image, name and so on) of the App is stored in the tokenURI following NFT.

Then implement the three methods of sell, buy, verify to do the series connection of the process.

The OpenAPI needs to use verify to verify whether the user has purchased the software.

All purchased information is on the blockchain of Ethereum, transparent and freedom.

// contracts/AppStore.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;

import "./AppNFT.sol";

contract AppStore {
    // debug log
    event Log(bool value, string message);

    AppNFT public appNFT;

    /**
     * App Property
     * @price: The price of app / wei
     * @seller: Who sell this app
     * @buyers: Who buy this app
     */
    struct AppProperty {
        uint price;
        address payable seller;
        mapping(address => bool) buyers;
    }

    /**
     * AppNFT `TokenId(uint256)` => `AppProperty`
     */
    mapping(uint256 => AppProperty) public appMap;

    constructor() {
        appNFT = new AppNFT();
    }

    /**
     * The method developer use to publish app
     */
    function sell(uint amount, string memory tokenURI) public returns (uint256)  {
        // create NFT token
        uint NFTToken = appNFT.awardItem(msg.sender, tokenURI);

        // record app property: the price
        appMap[NFTToken].seller = payable(msg.sender);
        appMap[NFTToken].price = amount;

        return NFTToken;
    }

    /**
     * The method consumers use to buy app
     */
    function buy(uint256 tokenId) public payable returns (bool)  {
        AppProperty storage buyApp = appMap[tokenId];

        require(buyApp.seller != address(0), "Error: There is no app pointed to by this tokenId");
        require(buyApp.price == msg.value, "Error: The purchase price for this app is incorrect");
        require(buyApp.buyers[msg.sender] != true, "Error: This user has already purchased");

        // transform eth to seller
        buyApp.seller.transfer(msg.value);

        // add buyers to buyApp.buyers 
        buyApp.buyers[msg.sender] = true;

        return true;
    }

    /**
     * The method developer use to verify consumer permissions
     *
     */
    function verify(uint256 tokenId) public view returns (bool) {
        require(appMap[tokenId].seller != address(0), "Error: There is no app pointed to by this tokenId");

        return appMap[tokenId].buyers[msg.sender] == true;
    }
}
novac42 commented 2 years ago

用区块链给软件授权的,之前搜出来过两个开源项目,供参考:

Valist,帮开发者做web3原生软件分发的开源项目Web3-native Software Distribution - Valist Documentation 今年3月5号的宣讲视频:Valist Distributing and Monetizing your Game with Valist - YouTube

另一个叫Dotta License的: GitHub - cryppadotta/dotta-license: ERC721-based Software Licensing Framework