mirror of
https://github.com/status-im/dreddit-dapp.git
synced 2025-02-22 11:18:16 +00:00
Updates Instructions 4: Coding Smart Contract
Some more explanations for `revert with reason` and some grammar and stylistic changes.
This commit is contained in:
parent
0d78c99dc3
commit
29901c00bf
@ -24,7 +24,7 @@ emit NewPost(postId, msg.sender, _description);
|
|||||||
```
|
```
|
||||||
|
|
||||||
#### Upvote/Downvote a post
|
#### Upvote/Downvote a post
|
||||||
We want to be able to asign a score to the posts. For that, let's work on the function `vote()` from which the users will be able to upvote or downvote each post. NOTE: this voting mechanism is very naive and not recomended to be used on real life due to not being Sybil resistant:
|
We want to be able to asign a score to the posts. For that, let's work on the function `vote()` from which the users will be able to upvote or downvote each post. NOTE: this voting mechanism is very naive and we recomend that you do not use it in production because it is not Sybil resistant:
|
||||||
|
|
||||||
1. Determine if the post exists, and if we have already voted
|
1. Determine if the post exists, and if we have already voted
|
||||||
```
|
```
|
||||||
@ -33,6 +33,8 @@ require(p.creationDate != 0, "Post does not exist");
|
|||||||
require(p.voters[msg.sender] == Ballot.NONE, "You already voted on this post");
|
require(p.voters[msg.sender] == Ballot.NONE, "You already voted on this post");
|
||||||
```
|
```
|
||||||
|
|
||||||
|
You'll notice that we're using a new feature of Solidity here: `revert with reason`, which makes it significantly easier to debug your smart contracts. It should be fairly self-evident: just pass in a string stating the reason this `require` statement might fail as the second parameter so that you can track it easily. For a full discussion of this, go [here](https://github.com/ethereum/solidity/issues/1686).
|
||||||
|
|
||||||
2. Store the vote and update the post score
|
2. Store the vote and update the post score
|
||||||
```
|
```
|
||||||
Ballot b = Ballot(_vote);
|
Ballot b = Ballot(_vote);
|
||||||
@ -49,10 +51,10 @@ p.voters[msg.sender] = b;
|
|||||||
emit Vote(_postId, msg.sender, _vote);
|
emit Vote(_postId, msg.sender, _vote);
|
||||||
````
|
````
|
||||||
|
|
||||||
> Building a Sybil resistant voting mechanism using a Proof of Identity that takes in account Privacy is a great challenge . How would you solve this problem?
|
> Building a Sybil resistant voting mechanism using a Proof of Identity that takes into account the privacy of users is a great challenge. How would you solve this problem?
|
||||||
|
|
||||||
#### Determine if an user can vote for a post
|
#### Determine if an user can vote for a post
|
||||||
The users need to know somehow if they're able to vote on a post or not. There are two scenarios where a user should not be able to vote: a) The post doesn't exist, and b) The user already voted. This logic should be developed inside `canVote()`:
|
The users needs to know somehow if they're able to vote on a post or not. There are two scenarios where a user should not be able to vote: a) The post doesn't exist, and b) The user already voted. This logic should be developed inside `canVote()`:
|
||||||
|
|
||||||
1. Determine if the posts exists
|
1. Determine if the posts exists
|
||||||
```
|
```
|
||||||
@ -66,9 +68,9 @@ return (p.voters[msg.sender] == Ballot.NONE);
|
|||||||
```
|
```
|
||||||
|
|
||||||
#### Determine the user's vote
|
#### Determine the user's vote
|
||||||
It's possible an user would like to know what was his choice on a previously voted post. Implement this on `getVote()`.
|
It's possible that a user would like to be reminded about what was their choice was for a post they voted on previously. Implement this in `getVote()`.
|
||||||
```
|
```
|
||||||
Post storage p = posts[_postId];
|
Post storage p = posts[_postId];
|
||||||
return uint8(p.voters[msg.sender]);
|
return uint8(p.voters[msg.sender]);
|
||||||
```
|
```
|
||||||
> Notice that on the last two functions, the input and output values related to votes are uin8 instead of an enum. Enum are not a valid input/output value for functions at least on the current solidity version.
|
> Notice that, in the last two functions, the input and output values related to votes are `uint8` instead of an `enum`. This is because `enum`s are not a valid input/output value for functions, at least in the current version of Solidity.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user