add global forum contract

This commit is contained in:
Ladd Hoffman 2024-06-28 10:46:21 -05:00
parent ef19b9bd66
commit 282d9478df
1 changed files with 90 additions and 0 deletions

View File

@ -0,0 +1,90 @@
// SPDX-License-Identifier: Unlicense
pragma solidity ^0.8.24;
struct Reference {
int weightPPM;
string targetPostId;
}
struct Author {
uint weightPPM;
address authorAddress;
}
struct Post {
string id;
address sender;
Author[] authors;
Reference[] references;
string content;
}
contract GlobalForum {
mapping(string => Post) public posts;
string[] public postIds;
uint public postCount;
event PostAdded(string id);
function addPost(
string calldata postId,
Author[] calldata authors,
Reference[] calldata references,
string calldata content
) external {
require(authors.length > 0, "Post must include at least one author");
postCount++;
postIds.push(postId);
Post storage post = posts[postId];
require(
post.authors.length == 0,
"A post with this postId already exists"
);
post.sender = msg.sender;
post.id = postId;
post.content = content;
uint authorTotalWeightPPM;
for (uint i = 0; i < authors.length; i++) {
authorTotalWeightPPM += authors[i].weightPPM;
post.authors.push(authors[i]);
}
require(
authorTotalWeightPPM == 1000000,
"Author weights must sum to 1000000"
);
for (uint i = 0; i < references.length; i++) {
post.references.push(references[i]);
}
int totalReferenceWeightPos;
int totalReferenceWeightNeg;
for (uint i = 0; i < post.references.length; i++) {
int weight = post.references[i].weightPPM;
require(
weight >= -1000000,
"Each reference weight must be >= -1000000"
);
require(
weight <= 1000000,
"Each reference weight must be <= 1000000"
);
if (weight > 0) totalReferenceWeightPos += weight;
else totalReferenceWeightNeg += weight;
}
require(
totalReferenceWeightPos <= 1000000,
"Sum of positive references must be <= 1000000"
);
require(
totalReferenceWeightNeg >= -1000000,
"Sum of negative references must be >= -1000000"
);
emit PostAdded(postId);
}
function getPostAuthors(
string calldata postId
) external view returns (Author[] memory) {
Post storage post = posts[postId];
return post.authors;
}
}