Avatar
1
tvd12 Enlightened
tvd12 Enlightened
Ý tưởng về trò chơi lời hứa - 001: định nghĩa contract
Mình có ý tưởng về 1 game blockchain: giữ lời hứa, và đây là Contract sơ bộ:

// contracts/KeepPromise.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import "./ERC721.sol";
import "./../../utils/Counters.sol";

struct Promise {
    uint256 _id;
    string _promise;
    string _deadline;
    string _punishment;
    address _punisher;
}

contract KeepPromise is ERC721 {
    using Counters for Counters.Counter;
    Counters.Counter private _promiseIds;

    mapping(uint256 => Promise) private _promises;
    mapping(address => string) private _playerNickNames;

    constructor() public ERC721("Promise", "PRM") {}

    function join(string memory pNickName) public returns(string memory) {
        address player = msg.sender;
        require(_notJoined(player), "KeepPromise: you've already joined game");
        _playerNickNames[player] = pNickName;
        return string(abi.encodePacked("SUCCESS: ", pNickName));
    }

    function submitPromise( 
        string memory pPromise, 
        string memory pDeadline, 
        string memory pPunishment,
        address pPunisher
    ) public returns (uint256)
    {
        address player = msg.sender;
        require(_joined(player), "KeepPromise: you've not joined game yet");
        require(_joined(pPunisher), "KeepPromise: the Punisher's not joined game yet");
        _promiseIds.increment();

        uint256 newPromiseId = _promiseIds.current();
        _mint(player, newPromiseId);
        Promise memory prom = Promise(newPromiseId, pPromise, pDeadline, pPunishment, pPunisher);
        _addPromise(newPromiseId, prom);

        return newPromiseId;
    }

    function getPromise(uint256 pPromiseId) public view returns(Promise memory) {
        address player = msg.sender;
        require(_joined(player), "KeepPromise: you've not joined game yet");
        return _promises[pPromiseId];
    }

    function getNickName() public view returns(string memory) {
        address player = msg.sender;
        return _playerNickNames[player];
    }

    function getPlayerNickName(address pPlayer) public view returns(string memory) {
        return _playerNickNames[pPlayer];
    }

    function _addPromise(uint256 pPromiseId, Promise memory pPromise) internal virtual {
        _promises[pPromiseId] = pPromise;
    }

    function _joined(address pPlayer) internal view virtual returns (bool) {
        return bytes(_playerNickNames[pPlayer]).length != 0;
    }

    function _notJoined(address pPlayer) internal view virtual returns (bool) {
        return bytes(_playerNickNames[pPlayer]).length == 0;
    }
}

Chạy lệnh sẽ thế này:

let contract = await KeepPromise.deployed()
contract.join("Dzung")
contract.getNickName();
contract.submitPromise("Love myself", "2021-12-12", "Love Her", "0x3c31b4b9d6c24bad29498f99aaa6914231362c7f")
contract.getPromise(1);
  • Answer
blockchain keep promise
Remain: 5
1 Answer
Avatar
monkey Enlightened
monkey Enlightened
Sharing
  • 0
  • Reply