Skip to content

Instantly share code, notes, and snippets.

@mboyle
Last active August 25, 2022 12:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mboyle/041856af0ac8b7cb4389adac325f18fe to your computer and use it in GitHub Desktop.
Save mboyle/041856af0ac8b7cb4389adac325f18fe to your computer and use it in GitHub Desktop.
Comments.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.12;
import { Ownable } from "@openzeppelin/contracts/access/Ownable.sol";
import { Strings } from "@openzeppelin/contracts/utils/Strings.sol";
import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
import "@openzeppelin/contracts/token/ERC721/IERC721Receiver.sol";
import "./ITablelandTables.sol";
import "./ITablelandController.sol";
contract Comments is Ownable,IERC721Receiver {
ITablelandTables private _tableland;
uint256 _registryTableId;
string public _registryTable;
uint256 _commentsTableId;
string public _commentsTable;
mapping(address => bool) authorizedAddresses;
string private _baseURIString = "https://testnet.tableland.network/query?s=";
constructor(address registry) {
_tableland = ITablelandTables(registry);
_createRegistryTable();
_createCommentsTable();
}
function setAddressAuthorization(address newAddress, bool isAuthorized) public onlyOwner {
authorizedAddresses[newAddress] = isAuthorized;
}
function addComment(uint256 id, uint256 created_at, string memory authorAddress, string memory body, uint256 contract_id, uint256 token_id, uint256 parent_id) public {
if(msg.sender != owner() && authorizedAddresses[msg.sender] != true) revert("Unauthorized");
_tableland.runSQL(
address(this),
_commentsTableId,
string.concat(
"INSERT INTO ",
_commentsTable,
" (id, created_at, address) VALUES (",
Strings.toString(id), ', ',
Strings.toString(created_at), ', ',
"\"", authorAddress, "\", ",
"\"", body, "\", ",
Strings.toString(contract_id), ', ',
Strings.toString(token_id), ', ',
Strings.toString(parent_id),
");"
)
);
}
function addCollection(uint256 id, string memory contractAddress, uint256 chain_id, string memory collectionName, string memory collectionSymbol) public {
if(msg.sender != owner() && authorizedAddresses[msg.sender] != true) revert("Unauthorized");
_tableland.runSQL(
address(this),
_registryTableId,
string.concat(
"INSERT INTO ",
_registryTable,
" (id, address, chain_id, name, symbol) VALUES (",
Strings.toString(id), ', ',
"\"", contractAddress, "\", ",
Strings.toString(chain_id), ', ',
"\"", collectionName, "\", ",
"\"", collectionSymbol, "\"",
");"
)
);
}
function getCommentsForToken(uint256 contractId, uint256 tokenId) public view returns (string memory) {
return string.concat(
_baseURIString,
"SELECT%20*%20FROM%20",
_commentsTable,
"%20WHERE%20contract_id%20=%20",
Strings.toString(contractId),
"%20AND%20token_id%20=",
Strings.toString(tokenId)
);
}
function _createRegistryTable() internal {
_registryTableId = _tableland.createTable(
address(this),
string.concat(
"CREATE TABLE ",
"nfcregistry_",
Strings.toString(block.chainid),
" (id INT, address TEXT, chain_id INT, name TEXT, symbol TEXT);"
)
);
_registryTable = string.concat(
"nfcregistry_",
Strings.toString(block.chainid),
"_",
Strings.toString(_registryTableId)
);
}
function _createCommentsTable() internal {
_commentsTableId = _tableland.createTable(
address(this),
string.concat(
"CREATE TABLE ",
"nfccomments_",
Strings.toString(block.chainid),
" (id INT, created_at INT, address TEXT, body TEXT, contract_id INT, token_id INT, parent_id INT)"
)
);
_commentsTable = string.concat(
"nfccomments_",
Strings.toString(block.chainid),
"_",
Strings.toString(_commentsTableId)
);
}
function onERC721Received(address, address, uint256, bytes calldata) external pure returns (bytes4) {
return IERC721Receiver.onERC721Received.selector;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment