From 282d9478dfa37bafbd26128530093bb73969d2b7 Mon Sep 17 00:00:00 2001 From: Ladd Hoffman Date: Fri, 28 Jun 2024 10:46:21 -0500 Subject: [PATCH] add global forum contract --- ethereum/contracts/GlobalForum.sol | 90 ++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 ethereum/contracts/GlobalForum.sol diff --git a/ethereum/contracts/GlobalForum.sol b/ethereum/contracts/GlobalForum.sol new file mode 100644 index 0000000..dc063f5 --- /dev/null +++ b/ethereum/contracts/GlobalForum.sol @@ -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; + } +}