From 3700dac0a7ad25144c00c86071e7bb60d46760cc Mon Sep 17 00:00:00 2001 From: Szymon Szlachtowicz <38212223+Szymx95@users.noreply.github.com> Date: Thu, 23 Sep 2021 12:22:54 +0200 Subject: [PATCH] Update core readme and refactor (#90) --- packages/core/README.md | 107 ++++++++++++++++-- packages/core/src/classes/WakuMessaging.ts | 8 +- packages/core/src/classes/WakuPolling.ts | 10 +- .../src/components/Poll.tsx | 1 - .../src/components/PollCreation.tsx | 1 - 5 files changed, 106 insertions(+), 21 deletions(-) diff --git a/packages/core/README.md b/packages/core/README.md index cd8d6b1..bd1c3ca 100644 --- a/packages/core/README.md +++ b/packages/core/README.md @@ -39,21 +39,33 @@ type WakuMessageStore = { } ``` +# Voting + +Voting is done on-chain and allows to accumulate votes over waku network to allow user for voting without gas (although those votes won't be counted until someone commits those votes to blockchain). + +To make it easier to use WakuVoting class makes it easier to create and use this kind of voting. + ## Waku Voting -Objects of type of WakuVoting, hold their own Waku objects and also store list of polls and votes for later use. +Objects of class of WakuVoting, hold their own Waku objects and also store list of proposals and votes for later use. -Creating instance of WakuVoting: +### Creating instance of WakuVoting -WakuVoting constructor expects name of DApp and address of a token, web3provider, also as optional parameter can take custom Waku object. +WakuVoting create function expects name of DApp and address of a voting contract, web3provider, also as optional parameter can take custom Waku object. + +Address of token is derived from votingContract ```ts import { WakuVoting } from '@status-waku-voting/core' - await WakuVoting.create(appName, contractAddress, provider, multicallAddress) + await WakuVoting.create(appName, contractAddress, provider, multicallAddress, waku) ``` -objects of type WakuVoting expose functions: +### API + +All api functions use `provider.getSigner()` to define signer + +- `createVote(question: string, descripiton: string, tokenAmount: BigNumber)` creates new votingRoom on blockchain - `getVotingRooms()` which return a list o VotingRoom @@ -85,9 +97,9 @@ export type VotingRoom = { - `getVotingRoom(id: number)` gets VotingRoom with given id -## Polls +# Polls -### Creating time-limited poll +Polls are similar to twitter polls with main difference being that they aren't immutable and only appear on network for limited time. To create a poll user has to send a message over waku network on specific topic @@ -112,8 +124,6 @@ message PollInit { } ``` -### Voting on timed poll - To vote on poll user has to send waku message on topic: `/{dapp name}/waku-polling/votes/proto` @@ -129,4 +139,83 @@ message TimedPollVote { optional bytes tokenAmount = 5; // amount of token used for WEIGHTED voting bytes signature = 6; // signature of all above fields } +``` + +To make it easier to use WakuPolling class was created + +## Creating instance of WakuPolling + +WakuPolling create expects name of DApp and address of a token contract, web3provider, also as optional parameter can take custom Waku object. + +```ts + import { WakuPolling } from '@status-waku-voting/core' + + await WakuPolling.create(appName, tokenAddress, provider, multicallAddress, waku) +``` + + +### API + +All api functions use `provider.getSigner()` to define signer + +- `createTimedPoll(question: string, answers: string[], pollType: PollType, minToken?: BigNumber, endTime?: number)` creates new poll on waku network + +`question` is a question of a poll +`answers` is a array of possible answers in poll, order of answers stays the same + +`pollType` determines type of poll + +``` +export enum PollType { + WEIGHTED = 0, + NON_WEIGHTED = 1, +} +``` + +`minToken` determines how much tokens user need to hold to be able to vote in NON_WEIGHTED poll + +- `sendTimedPollVote(pollId: string, selectedAnswer: number, tokenAmount?: BigNumber)` sends a vote on a given poll + +`pollId` id of poll +`selectedAnswer` id of given answer in a poll +`tokenAmount` if poll is weighted this determines with how much tokens user votes + +-`getDetailedTimedPolls()` returns a list of `DetailedTimedPoll` + +``` +DetailedTimedPoll { + answers: TimedPollAnswer[] + poll: PollInitMsg + votesMessages: TimedPollVoteMsg[] + numberOfVotes: BigNumber +} + +TimedPollAnswer { + text: string + votes: BigNumber +} + +PollInitMsg { + owner: string + timestamp: number + question: string + answers: string[] + pollType: PollType + minToken?: BigNumber + endTime: number + signature: string + id: string + chainId: number +} + +TimedPollVoteMsg { + pollId: string + voter: string + timestamp: number + answer: number + tokenAmount?: BigNumber + signature: string + id: string + chainId: number +} ``` \ No newline at end of file diff --git a/packages/core/src/classes/WakuMessaging.ts b/packages/core/src/classes/WakuMessaging.ts index 9cb0780..2becaec 100644 --- a/packages/core/src/classes/WakuMessaging.ts +++ b/packages/core/src/classes/WakuMessaging.ts @@ -102,9 +102,11 @@ export class WakuMessaging { .sort((a, b) => ((a?.timestamp ?? new Date(0)) > (b?.timestamp ?? new Date(0)) ? 1 : -1)) .forEach((e) => { if (e) { - if (filterFunction ? filterFunction(e) : true && !hashMap?.[e.id]) { - arr.unshift(e) - hashMap[e.id] = true + if (filterFunction ? filterFunction(e) : true) { + if (!hashMap?.[e.id]) { + arr.unshift(e) + hashMap[e.id] = true + } } } }) diff --git a/packages/core/src/classes/WakuPolling.ts b/packages/core/src/classes/WakuPolling.ts index a5ae7c1..3fc39ea 100644 --- a/packages/core/src/classes/WakuPolling.ts +++ b/packages/core/src/classes/WakuPolling.ts @@ -63,13 +63,13 @@ export class WakuPolling extends WakuMessaging { } public async createTimedPoll( - signer: JsonRpcSigner | Wallet, question: string, answers: string[], pollType: PollType, minToken?: BigNumber, endTime?: number ) { + const signer = this.provider.getSigner() const address = await signer.getAddress() await this.updateBalances([address]) if (this.addressesBalances[address] && this.addressesBalances[address]?.gt(minToken ?? BigNumber.from(0))) { @@ -85,12 +85,8 @@ export class WakuPolling extends WakuMessaging { } } - public async sendTimedPollVote( - signer: JsonRpcSigner | Wallet, - pollId: string, - selectedAnswer: number, - tokenAmount?: BigNumber - ) { + public async sendTimedPollVote(pollId: string, selectedAnswer: number, tokenAmount?: BigNumber) { + const signer = this.provider.getSigner() const address = await signer.getAddress() const poll = this.wakuMessages['pollInit'].arr.find((poll: PollInitMsg): poll is PollInitMsg => poll.id === pollId) if (poll) { diff --git a/packages/polling-components/src/components/Poll.tsx b/packages/polling-components/src/components/Poll.tsx index bb6ba79..8112d64 100644 --- a/packages/polling-components/src/components/Poll.tsx +++ b/packages/polling-components/src/components/Poll.tsx @@ -69,7 +69,6 @@ export function Poll({ poll, wakuPolling, theme, signer }: PollProps) { onClick={async () => { if (wakuPolling && signer) { const result = await wakuPolling.sendTimedPollVote( - signer, poll.poll.id, selectedAnswer ?? 0, poll.poll.pollType === PollType.WEIGHTED ? BigNumber.from(tokenAmount) : undefined diff --git a/packages/polling-components/src/components/PollCreation.tsx b/packages/polling-components/src/components/PollCreation.tsx index f2e848d..83d906d 100644 --- a/packages/polling-components/src/components/PollCreation.tsx +++ b/packages/polling-components/src/components/PollCreation.tsx @@ -89,7 +89,6 @@ export function PollCreation({ signer, wakuPolling, theme, setShowPollCreation } onClick={async (e) => { e.preventDefault() const result = await wakuPolling?.createTimedPoll( - signer, question, answers, selectedType,