LoginSignup
0
0

More than 1 year has passed since last update.

Orthoverse∅のコントラクトを解説する

Posted at

コントラクトの場所

リポジトリのorthoverse-contractsを参照してください。
以下が、今回作成したコントラクトです。

// SPDX-License-Identifier: MIT
// SPDX-FileCopyrightText: 2022- Suguru Oho <oho.sugu@gmail.com>

pragma solidity >=0.4.22 <0.9.0;

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

contract PNSRegistry is ERC721Enumerable, Ownable {
  using SafeMath for uint256;
  uint256 initialPrice = 0.01 ether;

  mapping(uint256 => string) private records;

  constructor() ERC721("OrthoverseZero", "ORTZ") {
  }

  function mint(address to, uint256 _spatialCode, string memory _url) public payable virtual {
    require(msg.value == initialPrice, "Not enough ehter");
    // 22bit 
    require(_spatialCode <= 0xFFFFFFFFFFF, "Spatial Code Range exceed");

    records[_spatialCode] = _url;
    _mint(to, _spatialCode);
  }

  function getRecord(uint256 _spatialCode) public view returns(string memory) {
    return records[_spatialCode];
  }

  function changeUrl(uint256 _spatialCode, string memory _url) public virtual {
    require(_exists(_spatialCode));
    require(ownerOf(_spatialCode) == msg.sender);

    records[_spatialCode] = _url;
  }

  function setInitialPrice(uint256 _initialPrice) external onlyOwner {
    initialPrice = _initialPrice;
  }

  function getInitialPrice() external view returns(uint256){
    return initialPrice;
  }

  function withdraw() external onlyOwner {
    require(address(this).balance > 0, "Not enough ether");
    address payable _owner = payable(owner());
    _owner.transfer(address(this).balance);
  }

  function _baseURI() internal view virtual override returns (string memory) {
    return "https://ortv.tech/meta/";
  }
}

空間IDとURLの紐付けのために、recordsというマッピングを追加しています。
recordsには、誰でも空間IDを指定して紐付くURLが取得できるgetRecordメソッドと、
オーナーのみURLを変更できるchangeUrlメソッドを追加しました。

また、コントラクトのオーナーがミント価格を変更したりできるようになっています。

_baseURIは、NFTのメタデータのためにベースURLを返します。
このURLで、メタデータのJSONを返すWebアプリが動作しています。

もともと、CryptoZombiesを参考にもっと量の多いコントラクトコードを書いていたのですが、
改めていろいろ調べたら同じ機能がこれだけで済みました。
OpenZeppelinで用意されている親コントラクトがとてもよくできているのだと思います。

感想

実際にEthereumブロックチェーンで動くコントラクトを書いてみて、
一番理解するのが難しかった概念が、ブロックチェーンへの書き込みの性質です。
同時に一番面白いと感じたのもここで、巨大なブロックチェーンのデータベースに誰でもガス代さえ払えば実質永久的にデータを記録できるわけです。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0