Skip to content

Commit

Permalink
Merge pull request #28 from BigWhaleLabs/use-postId
Browse files Browse the repository at this point in the history
Contract updates to match new frontends
  • Loading branch information
T-Damer authored Jun 7, 2023
2 parents 0d8d185 + a6cae04 commit b02ed93
Show file tree
Hide file tree
Showing 16 changed files with 283 additions and 145 deletions.
1 change: 1 addition & 0 deletions .env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ ETHERSCAN_API_KEY=0000000000000000000000000000000000000000
ETH_RPC=https://example.com/rpc
CONTRACT_OWNER_PRIVATE_KEY=0000000000000000000000000000000000000000000000000000000000000000
COINMARKETCAP_API_KEY=0000000000000000000000000000000000000000
GSN_PAYMASTER_CONTRACT=0x0000000000000000000000000000000000000000
17 changes: 9 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,22 @@ The main storage contract made specially for [Big Whale Labs projects](https://g

## Environment variables

| Name | Description |
| ---------------------------- | --------------------------------------------------------- |
| `ETHERSCAN_API_KEY` | Etherscan API key |
| `ETH_RPC` | Ethereum RPC URL |
| `CONTRACT_OWNER_PRIVATE_KEY` | Private key of the contract owner to deploy the contracts |
| `COINMARKETCAP_API_KEY` | Coinmarketcap API key |
| Name | Description |
| ---------------------------- | --------------------------------------------------------------------------------------------- |
| `ETHERSCAN_API_KEY` | Etherscan API key |
| `ETH_RPC` | Ethereum RPC URL |
| `CONTRACT_OWNER_PRIVATE_KEY` | Private key of the contract owner to deploy the contracts |
| `COINMARKETCAP_API_KEY` | Coinmarketcap API key |
| `GSN_PAYMASTER_CONTRACT` | Paymaster contract to add deployed OBSS into targets automatically, defaults to BWL constants |

Also check out the `.env.sample` file for more information.

## Available scripts

- `yarn build` — compiles the contract ts interface to the `typechain` directory
- `yarn test` — runs the test suite
- `yarn deploy` — deploys the contract to the network
- `yarn deploy` — deploys the contract to the network. After deploying go to blockchain scanner address in the console and verify your contract as proxy to use it
- `yarn eth-lint` — runs the linter for the solidity contract
- `yarn lint` — runs all the linters
- `yarn prettify` — prettifies the code in th project
- `yarn release`relases the `typechain` directory to NPM
- `yarn release`releases the `typechain` directory to NPM
5 changes: 3 additions & 2 deletions contracts/OBSSStorage.sol
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ contract OBSSStorage is OwnableUpgradeable, ERC2771Recipient {
Karma public karma;
Profiles public profiles;
Feeds public feeds;
mapping(uint => mapping(uint => mapping(uint => mapping(address => bool)))) karmaGranted;
mapping(uint => mapping(uint => mapping(uint => mapping(address => bool))))
public karmaGranted;

// Constructor
function initialize(
Expand Down Expand Up @@ -137,7 +138,7 @@ contract OBSSStorage is OwnableUpgradeable, ERC2771Recipient {
}

function pinOrUnpinFeedPost(uint feedId, uint postId, bool pin) public {
profiles.pinOrUnpinPost(_msgSender(), feedId, postId, pin);
feeds.pinOrUnpinPost(_msgSender(), feedId, postId, pin);
}

function addBatchFeedPosts(PostRequest[] memory postRequests) public {
Expand Down
1 change: 1 addition & 0 deletions contracts/models/Reaction.sol
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,6 @@ struct Reaction {
uint postId;
uint commentId;
uint8 reactionType;
uint reactionId;
uint value;
}
2 changes: 0 additions & 2 deletions contracts/models/ReactionRequests.sol
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,6 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.19;

import "./CID.sol";

struct AddReactionRequest {
uint feedId;
uint postId;
Expand Down
43 changes: 31 additions & 12 deletions contracts/superclasses/Posts.sol
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,12 @@ contract Posts is KetlGuarded {
public usersToReactions;

// Events
event PostAdded(uint indexed feedId, uint indexed postId, Post post);
event PostAdded(
uint indexed feedId,
uint indexed postId,
Post post,
address indexed author
);
event PostPinned(uint indexed feedId, uint indexed postId);
event PostUnpinned(uint indexed feedId, uint indexed postId);
event CommentAdded(
Expand Down Expand Up @@ -153,7 +158,7 @@ contract Posts is KetlGuarded {
participants[feedId][currentPostId].push(sender);
participantsMap[feedId][currentPostId][sender] = true;
// Emit the event
emit PostAdded(feedId, lastPostIds[feedId].current(), post);
emit PostAdded(feedId, lastPostIds[feedId].current(), post, sender);
// Increment current post id
lastPostIds[feedId].increment();
}
Expand Down Expand Up @@ -240,8 +245,10 @@ contract Posts is KetlGuarded {
require(parentPost.author != address(0), "Post not found");
// Fetch parent comment and check if it exists
if (replyTo > 0) {
Post memory parentComment = comments[feedId][postId][replyTo];
require(parentComment.author != address(0), "Comment not found");
require(
comments[feedId][postId][replyTo - 1].author != address(0),
"Comment not found"
);
}
// Increment comment id (so that we start with 1)
lastCommentIds[feedId][postId].increment();
Expand Down Expand Up @@ -323,31 +330,43 @@ contract Posts is KetlGuarded {
// Fetch post or comment
Post memory post = commentId == 0
? posts[feedId][postId]
: comments[feedId][postId][commentId];
: comments[feedId][postId][commentId - 1]; // comments start with 1
// Check if post or comment exists
require(post.author != address(0), "Post or comment not found");
// Get old reaction if it exists
Reaction memory oldReaction = usersToReactions[feedId][postId][commentId][
sender
];
// Check if reaction already exists
require(oldReaction.sender == address(0), "Reaction already exists");
require(
oldReaction.reactionType != reactionType,
"Reaction of this type already exists"
);
if (oldReaction.reactionType != 0) {
removeReaction(
sender,
RemoveReactionRequest(feedId, postId, commentId, oldReaction.reactionId)
);
}
// Get lastReactionIds
uint reactionId = lastReactionIds[feedId][postId][commentId].current();
// Create reaction
Reaction memory reaction = Reaction(
sender,
feedId,
postId,
commentId,
reactionType,
reactionId,
msg.value
);
// Add reaction
reactions[feedId][postId][commentId].push(reaction);
// Remember the reaction for user
usersToReactions[feedId][postId][commentId][sender] = reaction;
// Increment reaction id
// Increment lastReactionId
lastReactionIds[feedId][postId][commentId].increment();
// If ether was sent, transfer it to the author
// If ether was sent, transfer it to the sender
if (msg.value > 0) {
Address.sendValue(payable(post.author), msg.value);
}
Expand All @@ -358,7 +377,7 @@ contract Posts is KetlGuarded {
postId,
commentId,
reactionType,
lastReactionIds[feedId][postId][commentId].current(),
reactionId,
msg.value
);
}
Expand All @@ -367,7 +386,7 @@ contract Posts is KetlGuarded {
address sender,
RemoveReactionRequest memory reactionRequest
)
external
public
onlyAllowedCaller
onlyKetlTokenOwners(sender)
onlyAllowedFeedId(reactionRequest.feedId)
Expand All @@ -379,7 +398,7 @@ contract Posts is KetlGuarded {
// Fetch post or comment
Post memory post = commentId == 0
? posts[feedId][postId]
: comments[feedId][postId][commentId];
: comments[feedId][postId][commentId - 1]; // comments start with 1
// Check if post or comment exists
require(post.author != address(0), "Post or comment not found");
// Check if sent by the owner
Expand All @@ -402,7 +421,7 @@ contract Posts is KetlGuarded {
// Fetch post or comment
Post memory post = commentId == 0
? posts[feedId][postId]
: comments[feedId][postId][commentId];
: comments[feedId][postId][commentId - 1]; // comments start with 1
// Check if post or comment exists
require(post.author != address(0), "Post or comment not found");
// Get the number of reactions
Expand Down
11 changes: 9 additions & 2 deletions hardhat.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,21 @@ import '@typechain/hardhat'
import 'hardhat-gas-reporter'
import 'solidity-coverage'
import * as dotenv from 'dotenv'
import { ETH_RPC as FALLBACK_ETH_RPC } from '@big-whale-labs/constants'
import {
ETH_RPC as FALLBACK_ETH_RPC,
GSN_MUMBAI_PAYMASTER_CONTRACT_ADDRESS,
} from '@big-whale-labs/constants'
import { HardhatUserConfig } from 'hardhat/config'
import { cleanEnv, str, testOnly } from 'envalid'

dotenv.config()

const {
export const {
CONTRACT_OWNER_PRIVATE_KEY,
ETH_RPC,
ETHERSCAN_API_KEY,
COINMARKETCAP_API_KEY,
GSN_PAYMASTER_CONTRACT,
} = cleanEnv(process.env, {
CONTRACT_OWNER_PRIVATE_KEY: str({
devDefault: testOnly(
Expand All @@ -24,6 +28,9 @@ const {
ETH_RPC: str({ default: FALLBACK_ETH_RPC }),
ETHERSCAN_API_KEY: str({ devDefault: testOnly('') }),
COINMARKETCAP_API_KEY: str({ devDefault: testOnly('') }),
GSN_PAYMASTER_CONTRACT: str({
default: GSN_MUMBAI_PAYMASTER_CONTRACT_ADDRESS,
}),
})

const config: HardhatUserConfig = {
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@big-whale-labs/obss-storage-contract",
"version": "0.2.5-0",
"version": "0.2.7-1",
"description": "Storage contract for OBSS",
"repository": {
"type": "git",
Expand Down Expand Up @@ -76,6 +76,7 @@
"packageManager": "[email protected]",
"dependencies": {
"@big-whale-labs/constants": "^0.1.85",
"@big-whale-labs/gsn-paymaster-contract": "^0.1.1",
"@big-whale-labs/ketl-attestation-token": "^0.0.10",
"@openzeppelin/contracts-upgradeable": "^4.8.3",
"@openzeppelin/hardhat-upgrades": "^1.26.0",
Expand Down
Loading

0 comments on commit b02ed93

Please sign in to comment.