Skip to content

Instantly share code, notes, and snippets.

@earthchie
Last active January 2, 2023 10:28
Show Gist options
  • Save earthchie/7dcee22e4d7ce8a98278b6517955edc1 to your computer and use it in GitHub Desktop.
Save earthchie/7dcee22e4d7ce8a98278b6517955edc1 to your computer and use it in GitHub Desktop.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;
import "@openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol";
import "@openzeppelin/contracts/utils/Counters.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
contract ThaiChainSBT is ERC721URIStorage, Ownable {
using Counters for Counters.Counter;
Counters.Counter private _tokenIds;
constructor() ERC721("ThaiChain SBT Certificate", "Cert") {}
event Memo(string memo);
event Attest(address indexed to, uint256 indexed tokenId);
event Revoke(address indexed to, uint256 indexed tokenId);
function safeMint(address to, string memory uri, string memory memo) public onlyOwner returns (uint256) {
uint256 tokenId = _tokenIds.current();
_tokenIds.increment();
_safeMint(to, tokenId);
_setTokenURI(tokenId, uri);
if (bytes(memo).length > 0) {
emit Memo(memo);
}
return tokenId;
}
function burn(uint256 tokenId) external {
require(ownerOf(tokenId) == msg.sender, "Only owner can burn it");
_burn(tokenId);
}
function revoke(uint256 tokenId) external onlyOwner {
_burn(tokenId);
}
function _beforeTokenTransfer(address from, address to, uint256) pure override internal {
require(from == address(0) || to == address(0), "Not allowed to transfer it");
}
function _afterTokenTransfer(address from, address to, uint256 tokenId) override internal {
if (from == address(0)) {
emit Attest(to, tokenId);
} else if (to == address(0)) {
emit Revoke(to, tokenId);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment