Browse Source

Split Contract

develop
Ezerous 6 years ago
parent
commit
8f0dd9e78a
  1. 6
      app/src/config/drizzleOptions.js
  2. 2
      app/src/containers/BoardContainer.js
  3. 85
      contracts/Forum.sol
  4. 85
      contracts/Posting.sol
  5. 5
      migrations/2_deploy_contracts.js

6
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,

2
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 {

85
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 {
@ -155,86 +163,41 @@ contract Forum {
);
}
//----------------------------------------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<numTopics); // Only allow posting to a topic that exists
uint postID = numPosts++;
posts[postID] = Post(postID, msg.sender, block.timestamp, topicID);
topics[topicID].postIDs.push(postID);
uint postID = posting.createPost(topicID, msg.sender);
users[msg.sender].postIDs.push(postID);
emit PostCreated(postID, topicID);
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) {
//require(hasUserSignedUp(msg.sender)); needed?
require(topicID<numTopics);
return (getOrbitTopicsDB(topics[topicID].author),
topics[topicID].author,
users[topics[topicID].author].username,
topics[topicID].timestamp,
topics[topicID].postIDs
(address author, uint timestamp, uint[] memory postIDs) = posting.getTopicInfo(topicID);
return (getOrbitTopicsDB(author),
author,
users[author].username,
timestamp,
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) {
//require(hasUserSignedUp(msg.sender)); needed?
require(postID<numPosts);
return (getOrbitPostsDB(posts[postID].author),
posts[postID].author,
users[posts[postID].author].username,
posts[postID].timestamp,
posts[postID].topicID
(address author, uint timestamp, uint topicID) = posting.getPostInfo(postID);
return (getOrbitPostsDB(author),
author,
users[author].username,
timestamp,
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 Posting = artifacts.require("Posting");
module.exports = function(deployer) {
deployer.deploy(Forum);
deployer.deploy(Posting).then(function() {
return deployer.deploy(Forum, Posting.address)
});
};
Loading…
Cancel
Save