Browse Source

Init

develop
Ezerous 6 years ago
parent
commit
184fe4a1db
  1. 4
      app/src/components/PostList.js
  2. 4
      app/src/components/TopicList.js
  3. 6
      app/src/config/drizzleOptions.js
  4. 2
      app/src/containers/BoardContainer.js
  5. 101
      contracts/Forum.sol
  6. 85
      contracts/Posting.sol
  7. 5
      migrations/2_deploy_contracts.js

4
app/src/components/PostList.js

@ -5,8 +5,8 @@ import { drizzle } from '../index';
import Post from './Post'; import Post from './Post';
const contract = 'Forum'; const contract = 'Posting';
const getPostMethod = 'getPost'; const getPostMethod = 'getPostInfo';
class PostList extends Component { class PostList extends Component {
constructor(props) { constructor(props) {

4
app/src/components/TopicList.js

@ -5,8 +5,8 @@ import { drizzle } from '../index';
import Topic from './Topic'; import Topic from './Topic';
const contract = 'Forum'; const contract = 'Posting';
const getTopicMethod = 'getTopic'; const getTopicMethod = 'getTopicInfo';
class TopicList extends Component { class TopicList extends Component {
constructor(props) { constructor(props) {

6
app/src/config/drizzleOptions.js

@ -1,4 +1,5 @@
import Forum from '../contracts/Forum.json'; import Forum from '../contracts/Forum.json';
import Posting from '../contracts/Posting.json';
const drizzleOptions = { const drizzleOptions = {
web3: { web3: {
@ -7,9 +8,10 @@ const drizzleOptions = {
url: 'ws://127.0.0.1:9545' url: 'ws://127.0.0.1:9545'
} }
}, },
contracts: [Forum], contracts: [Forum, Posting],
events: { events: {
Forum: ['UserSignedUp', 'UsernameUpdated', 'TopicCreated', 'PostCreated'] Forum: ['UserSignedUp', 'UsernameUpdated'],
Posting: ['TopicCreated', 'PostCreated']
}, },
polls: { polls: {
accounts: 2000, accounts: 2000,

2
app/src/containers/BoardContainer.js

@ -11,7 +11,7 @@ import FloatingButton from '../components/FloatingButton';
/* import { showProgressBar, hideProgressBar } from '../redux/actions/userInterfaceActions'; */ /* import { showProgressBar, hideProgressBar } from '../redux/actions/userInterfaceActions'; */
const contract = 'Forum'; const contract = 'Posting';
const getNumberOfTopicsMethod = 'getNumberOfTopics'; const getNumberOfTopicsMethod = 'getNumberOfTopics';
class BoardContainer extends Component { class BoardContainer extends Component {

101
contracts/Forum.sol

@ -1,6 +1,14 @@
pragma solidity >=0.5.6 <0.6.0; pragma solidity >=0.5.6 <0.6.0;
import "./Posting.sol";
contract Forum { contract Forum {
Posting posting;
constructor(address addr) public {
posting = Posting(addr);
posting.setForumContractAddress();
}
//----------------------------------------USER---------------------------------------- //----------------------------------------USER----------------------------------------
struct User { struct User {
@ -137,9 +145,9 @@ contract Forum {
function getOrbitIdentityInfo(address userAddress) public view returns (string memory, string memory, string memory) { function getOrbitIdentityInfo(address userAddress) public view returns (string memory, string memory, string memory) {
require (hasUserSignedUp(userAddress), "User hasn't signed up."); require (hasUserSignedUp(userAddress), "User hasn't signed up.");
return ( return (
users[userAddress].orbitdb.identityId, users[userAddress].orbitdb.identityId,
users[userAddress].orbitdb.identityPublicKey, users[userAddress].orbitdb.identityPublicKey,
users[userAddress].orbitdb.identityPrivateKey users[userAddress].orbitdb.identityPrivateKey
); );
} }
@ -147,94 +155,49 @@ contract Forum {
string memory, string memory, string memory) { string memory, string memory, string memory) {
require (hasUserSignedUp(userAddress), "User hasn't signed up."); require (hasUserSignedUp(userAddress), "User hasn't signed up.");
return ( return (
users[userAddress].orbitdb.orbitId, users[userAddress].orbitdb.orbitId,
users[userAddress].orbitdb.orbitPublicKey, users[userAddress].orbitdb.orbitPublicKey,
users[userAddress].orbitdb.orbitPrivateKey, users[userAddress].orbitdb.orbitPrivateKey,
users[userAddress].orbitdb.topicsDB, users[userAddress].orbitdb.topicsDB,
users[userAddress].orbitdb.postsDB users[userAddress].orbitdb.postsDB
); );
} }
//----------------------------------------POSTING---------------------------------------- //----POSTING-----
struct Topic {
uint topicID;
address author;
uint timestamp;
uint[] postIDs;
}
struct Post {
uint postID;
address author;
uint timestamp;
uint topicID;
}
uint numTopics; // Total number of topics
uint numPosts; // Total number of posts
mapping (uint => Topic) topics;
mapping (uint => Post) posts;
event TopicCreated(uint topicID, uint postID);
event PostCreated(uint postID, uint topicID);
function createTopic() public returns (uint, uint) { function createTopic() public returns (uint, uint) {
require(hasUserSignedUp(msg.sender)); // Only registered users can create topics require(hasUserSignedUp(msg.sender)); // Only registered users can create topics
//Creates topic (uint topicID, uint postID) = posting.createTopic(msg.sender);
uint topicID = numTopics++;
topics[topicID] = Topic(topicID, msg.sender, block.timestamp, new uint[](0));
users[msg.sender].topicIDs.push(topicID); users[msg.sender].topicIDs.push(topicID);
//Adds first post to topic
uint postID = numPosts++;
posts[postID] = Post(postID, msg.sender, block.timestamp, topicID);
topics[topicID].postIDs.push(postID);
users[msg.sender].postIDs.push(postID); users[msg.sender].postIDs.push(postID);
emit TopicCreated(topicID, postID);
return (topicID, postID); return (topicID, postID);
} }
function createPost(uint topicID) public returns (uint) { function createPost(uint topicID) public returns (uint) {
require(hasUserSignedUp(msg.sender)); // Only registered users can create posts require(hasUserSignedUp(msg.sender)); // Only registered users can create posts
require(topicID<numTopics); // Only allow posting to a topic that exists uint postID = posting.createPost(topicID, msg.sender);
uint postID = numPosts++;
posts[postID] = Post(postID, msg.sender, block.timestamp, topicID);
topics[topicID].postIDs.push(postID);
users[msg.sender].postIDs.push(postID); users[msg.sender].postIDs.push(postID);
emit PostCreated(postID, topicID);
return postID; return postID;
} }
function getNumberOfTopics() public view returns (uint) {
return numTopics;
}
function getTopic(uint topicID) public view returns (string memory, address, string memory, uint, uint[] memory) { function getTopic(uint topicID) public view returns (string memory, address, string memory, uint, uint[] memory) {
//require(hasUserSignedUp(msg.sender)); needed? (address author, uint timestamp, uint[] memory postIDs) = posting.getTopicInfo(topicID);
require(topicID<numTopics); return (getOrbitTopicsDB(author),
return (getOrbitTopicsDB(topics[topicID].author), author,
topics[topicID].author, users[author].username,
users[topics[topicID].author].username, timestamp,
topics[topicID].timestamp, postIDs
topics[topicID].postIDs
); );
} }
function getTopicPosts(uint topicID) public view returns (uint[] memory) {
require(topicID<numTopics); // Topic should exist
return topics[topicID].postIDs;
}
function getPost(uint postID) public view returns (string memory, address, string memory, uint, uint) { function getPost(uint postID) public view returns (string memory, address, string memory, uint, uint) {
//require(hasUserSignedUp(msg.sender)); needed? (address author, uint timestamp, uint topicID) = posting.getPostInfo(postID);
require(postID<numPosts); return (getOrbitPostsDB(author),
return (getOrbitPostsDB(posts[postID].author), author,
posts[postID].author, users[author].username,
users[posts[postID].author].username, timestamp,
posts[postID].timestamp, topicID
posts[postID].topicID
); );
} }
} }

85
contracts/Posting.sol

@ -0,0 +1,85 @@
pragma solidity >=0.5.6 <0.6.0;
contract Posting {
address forumContractAddress;
function setForumContractAddress() public{
require(forumContractAddress==address(0));
forumContractAddress = msg.sender;
}
struct Topic {
uint topicID;
address author;
uint timestamp;
uint[] postIDs;
}
struct Post {
uint postID;
address author;
uint timestamp;
uint topicID;
}
uint numTopics; // Total number of topics
uint numPosts; // Total number of posts
mapping (uint => Topic) topics;
mapping (uint => Post) posts;
event TopicCreated(uint topicID, uint postID);
event PostCreated(uint postID, uint topicID);
function createTopic(address author) public returns (uint, uint) {
require(msg.sender==forumContractAddress);
//Creates topic
uint topicID = numTopics++;
topics[topicID] = Topic(topicID, author, block.timestamp, new uint[](0));
//Adds first post to topic
uint postID = numPosts++;
posts[postID] = Post(postID, author, block.timestamp, topicID);
topics[topicID].postIDs.push(postID);
emit TopicCreated(topicID, postID);
return (topicID, postID);
}
function createPost(uint topicID, address author) public returns (uint) {
require(msg.sender==forumContractAddress);
require(topicID<numTopics); // Only allow posting to a topic that exists
uint postID = numPosts++;
posts[postID] = Post(postID, author, block.timestamp, topicID);
topics[topicID].postIDs.push(postID);
emit PostCreated(postID, topicID);
return postID;
}
function getNumberOfTopics() public view returns (uint) {
return numTopics;
}
function getTopicInfo(uint topicID) public view returns (address, uint, uint[] memory) {
require(topicID<numTopics);
return (topics[topicID].author,
topics[topicID].timestamp,
topics[topicID].postIDs
);
}
function getTopicPosts(uint topicID) public view returns (uint[] memory) {
require(topicID<numTopics); // Topic should exist
return topics[topicID].postIDs;
}
function getPostInfo(uint postID) public view returns (address, uint, uint) {
require(postID<numPosts);
return (
posts[postID].author,
posts[postID].timestamp,
posts[postID].topicID
);
}
}

5
migrations/2_deploy_contracts.js

@ -1,5 +1,8 @@
const Forum = artifacts.require("Forum"); const Forum = artifacts.require("Forum");
const Posting = artifacts.require("Posting");
module.exports = function(deployer) { module.exports = function(deployer) {
deployer.deploy(Forum); deployer.deploy(Posting).then(function() {
return deployer.deploy(Forum, Posting.address)
});
}; };
Loading…
Cancel
Save