From 184fe4a1dbc2799cb1d7967b55bb22eece207f64 Mon Sep 17 00:00:00 2001 From: Ezerous Date: Mon, 18 Mar 2019 21:52:46 +0200 Subject: [PATCH] Init --- app/src/components/PostList.js | 4 +- app/src/components/TopicList.js | 4 +- app/src/config/drizzleOptions.js | 6 +- app/src/containers/BoardContainer.js | 2 +- contracts/Forum.sol | 101 +++++++++------------------ contracts/Posting.sol | 85 ++++++++++++++++++++++ migrations/2_deploy_contracts.js | 7 +- 7 files changed, 131 insertions(+), 78 deletions(-) create mode 100644 contracts/Posting.sol diff --git a/app/src/components/PostList.js b/app/src/components/PostList.js index 22a1024..085b900 100644 --- a/app/src/components/PostList.js +++ b/app/src/components/PostList.js @@ -5,8 +5,8 @@ import { drizzle } from '../index'; import Post from './Post'; -const contract = 'Forum'; -const getPostMethod = 'getPost'; +const contract = 'Posting'; +const getPostMethod = 'getPostInfo'; class PostList extends Component { constructor(props) { diff --git a/app/src/components/TopicList.js b/app/src/components/TopicList.js index dd00318..0c24fcc 100644 --- a/app/src/components/TopicList.js +++ b/app/src/components/TopicList.js @@ -5,8 +5,8 @@ import { drizzle } from '../index'; import Topic from './Topic'; -const contract = 'Forum'; -const getTopicMethod = 'getTopic'; +const contract = 'Posting'; +const getTopicMethod = 'getTopicInfo'; class TopicList extends Component { constructor(props) { diff --git a/app/src/config/drizzleOptions.js b/app/src/config/drizzleOptions.js index ce31144..fa0b853 100644 --- a/app/src/config/drizzleOptions.js +++ b/app/src/config/drizzleOptions.js @@ -1,4 +1,5 @@ import Forum from '../contracts/Forum.json'; +import Posting from '../contracts/Posting.json'; const drizzleOptions = { web3: { @@ -7,9 +8,10 @@ const drizzleOptions = { url: 'ws://127.0.0.1:9545' } }, - contracts: [Forum], + contracts: [Forum, Posting], events: { - Forum: ['UserSignedUp', 'UsernameUpdated', 'TopicCreated', 'PostCreated'] + Forum: ['UserSignedUp', 'UsernameUpdated'], + Posting: ['TopicCreated', 'PostCreated'] }, polls: { accounts: 2000, diff --git a/app/src/containers/BoardContainer.js b/app/src/containers/BoardContainer.js index 49b1395..8486ce0 100644 --- a/app/src/containers/BoardContainer.js +++ b/app/src/containers/BoardContainer.js @@ -11,7 +11,7 @@ import FloatingButton from '../components/FloatingButton'; /* import { showProgressBar, hideProgressBar } from '../redux/actions/userInterfaceActions'; */ -const contract = 'Forum'; +const contract = 'Posting'; const getNumberOfTopicsMethod = 'getNumberOfTopics'; class BoardContainer extends Component { diff --git a/contracts/Forum.sol b/contracts/Forum.sol index add5f3c..b8b75b7 100644 --- a/contracts/Forum.sol +++ b/contracts/Forum.sol @@ -1,6 +1,14 @@ pragma solidity >=0.5.6 <0.6.0; +import "./Posting.sol"; + contract Forum { + Posting posting; + + constructor(address addr) public { + posting = Posting(addr); + posting.setForumContractAddress(); + } //----------------------------------------USER---------------------------------------- struct User { @@ -137,9 +145,9 @@ contract Forum { function getOrbitIdentityInfo(address userAddress) public view returns (string memory, string memory, string memory) { require (hasUserSignedUp(userAddress), "User hasn't signed up."); return ( - users[userAddress].orbitdb.identityId, - users[userAddress].orbitdb.identityPublicKey, - users[userAddress].orbitdb.identityPrivateKey + users[userAddress].orbitdb.identityId, + users[userAddress].orbitdb.identityPublicKey, + users[userAddress].orbitdb.identityPrivateKey ); } @@ -147,94 +155,49 @@ contract Forum { string memory, string memory, string memory) { require (hasUserSignedUp(userAddress), "User hasn't signed up."); return ( - users[userAddress].orbitdb.orbitId, - users[userAddress].orbitdb.orbitPublicKey, - users[userAddress].orbitdb.orbitPrivateKey, - users[userAddress].orbitdb.topicsDB, - users[userAddress].orbitdb.postsDB + users[userAddress].orbitdb.orbitId, + users[userAddress].orbitdb.orbitPublicKey, + users[userAddress].orbitdb.orbitPrivateKey, + users[userAddress].orbitdb.topicsDB, + users[userAddress].orbitdb.postsDB ); } - //----------------------------------------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); + //----POSTING----- function createTopic() public returns (uint, uint) { require(hasUserSignedUp(msg.sender)); // Only registered users can create topics - //Creates topic - uint topicID = numTopics++; - topics[topicID] = Topic(topicID, msg.sender, block.timestamp, new uint[](0)); + (uint topicID, uint postID) = posting.createTopic(msg.sender); 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); - - emit TopicCreated(topicID, postID); return (topicID, postID); } function createPost(uint topicID) public returns (uint) { require(hasUserSignedUp(msg.sender)); // Only registered users can create posts - require(topicID=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