WineGometa紅酒鏈遊系統開發丨紅酒鏈遊智慧合約開發dapp詳情

JT1769119發表於2023-03-17

WineGometa是一種基於區塊鏈技術的紅酒鏈遊,可以實現紅酒品牌的推廣和交易。在WineGometa上,使用者可以購買、

收藏、出售紅酒,同時也可以參加紅酒品鑑、交流活動。為了實現紅酒鏈遊的順暢運作,需要開發智慧合約和相關的編

程程式碼。


下面是一些可能需要的程式碼示例:


建立NFT代幣合約

typescript

Copy code

pragma solidity ^0.8.0;


import "@openzeppelin/contracts/token/ERC721/ERC721.sol";


contract WineGometaNFT is ERC721 {

    uint256 public tokenCounter;


    constructor() ERC721("WineGometaNFT", "WGNT") {

        tokenCounter = 0;

    }


    function createToken(address owner, string memory tokenURI) public returns (uint256) {

        uint256 newTokenId = tokenCounter;

        _safeMint(owner, newTokenId);

        _setTokenURI(newTokenId, tokenURI);

        tokenCounter++;

        return newTokenId;

    }

}

建立質押挖礦合約

typescript

Copy code

pragma solidity ^0.8.0;


import "@openzeppelin/contracts/token/ERC20/IERC20.sol";

import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";

import "@openzeppelin/contracts/token/ERC721/IERC721.sol";

import "@openzeppelin/contracts/token/ERC721/utils/ERC721Holder.sol";


contract WineGometaStaking is ERC721Holder {

    using SafeERC20 for IERC20;


    struct PoolInfo {

        uint256 allocPoint;

        uint256 lastRewardBlock;

        uint256 accRewardPerShare;

    }


    IERC20 public rewardToken;

    IERC721 public nftToken;

    uint256 public totalAllocPoint;

    PoolInfo[] public poolInfo;


    constructor(IERC20 _rewardToken, IERC721 _nftToken) {

        rewardToken = _rewardToken;

        nftToken = _nftToken;

    }


    function deposit(uint256 poolId, uint256 tokenId) external {

        PoolInfo storage pool = poolInfo[poolId];

        IERC20 lpToken = IERC20(pool.lpToken);

        require(lpToken.balanceOf(msg.sender) >= amount, "Insufficient balance");

        require(lpToken.allowance(msg.sender, address(this)) >= amount, "Insufficient allowance");

        require(nftToken.ownerOf(tokenId) == msg.sender, "Not the token owner");


        updatePool(poolId);


        uint256 pendingReward = user.amount * pool.accRewardPerShare / 1e18 - user.rewardDebt;

        if (pendingReward > 0) {

            rewardToken.safeTransfer(msg.sender, pendingReward);

        }


        lpToken.safeTransferFrom(msg.sender, address(this), amount);

        pool.lpToken.safeTransferFrom(msg.sender, address(this), tokenId);

        user.amount += amount;

        user.rewardDebt = user.amount * pool.accRewardPerShare / 1e18;

    }


    function withdraw(uint256 poolId, uint256 tokenId) external {

        PoolInfo storage pool = poolInfo[poolId];

        User storage user = userInfo[poolId][msg.sender];

        require(user.amount > 0, "No amount to withdraw");

        require


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/69982110/viewspace-2940233/,如需轉載,請註明出處,否則將追究法律責任。

相關文章