Update core readme and refactor (#90)
This commit is contained in:
parent
02f2634bae
commit
3700dac0a7
|
@ -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
|
## 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
|
```ts
|
||||||
import { WakuVoting } from '@status-waku-voting/core'
|
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
|
- `getVotingRooms()` which return a list o VotingRoom
|
||||||
|
|
||||||
|
@ -85,9 +97,9 @@ export type VotingRoom = {
|
||||||
|
|
||||||
- `getVotingRoom(id: number)` gets VotingRoom with given id
|
- `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
|
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:
|
To vote on poll user has to send waku message on topic:
|
||||||
|
|
||||||
`/{dapp name}/waku-polling/votes/proto`
|
`/{dapp name}/waku-polling/votes/proto`
|
||||||
|
@ -129,4 +139,83 @@ message TimedPollVote {
|
||||||
optional bytes tokenAmount = 5; // amount of token used for WEIGHTED voting
|
optional bytes tokenAmount = 5; // amount of token used for WEIGHTED voting
|
||||||
bytes signature = 6; // signature of all above fields
|
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
|
||||||
|
}
|
||||||
```
|
```
|
|
@ -102,9 +102,11 @@ export class WakuMessaging {
|
||||||
.sort((a, b) => ((a?.timestamp ?? new Date(0)) > (b?.timestamp ?? new Date(0)) ? 1 : -1))
|
.sort((a, b) => ((a?.timestamp ?? new Date(0)) > (b?.timestamp ?? new Date(0)) ? 1 : -1))
|
||||||
.forEach((e) => {
|
.forEach((e) => {
|
||||||
if (e) {
|
if (e) {
|
||||||
if (filterFunction ? filterFunction(e) : true && !hashMap?.[e.id]) {
|
if (filterFunction ? filterFunction(e) : true) {
|
||||||
arr.unshift(e)
|
if (!hashMap?.[e.id]) {
|
||||||
hashMap[e.id] = true
|
arr.unshift(e)
|
||||||
|
hashMap[e.id] = true
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
|
@ -63,13 +63,13 @@ export class WakuPolling extends WakuMessaging {
|
||||||
}
|
}
|
||||||
|
|
||||||
public async createTimedPoll(
|
public async createTimedPoll(
|
||||||
signer: JsonRpcSigner | Wallet,
|
|
||||||
question: string,
|
question: string,
|
||||||
answers: string[],
|
answers: string[],
|
||||||
pollType: PollType,
|
pollType: PollType,
|
||||||
minToken?: BigNumber,
|
minToken?: BigNumber,
|
||||||
endTime?: number
|
endTime?: number
|
||||||
) {
|
) {
|
||||||
|
const signer = this.provider.getSigner()
|
||||||
const address = await signer.getAddress()
|
const address = await signer.getAddress()
|
||||||
await this.updateBalances([address])
|
await this.updateBalances([address])
|
||||||
if (this.addressesBalances[address] && this.addressesBalances[address]?.gt(minToken ?? BigNumber.from(0))) {
|
if (this.addressesBalances[address] && this.addressesBalances[address]?.gt(minToken ?? BigNumber.from(0))) {
|
||||||
|
@ -85,12 +85,8 @@ export class WakuPolling extends WakuMessaging {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public async sendTimedPollVote(
|
public async sendTimedPollVote(pollId: string, selectedAnswer: number, tokenAmount?: BigNumber) {
|
||||||
signer: JsonRpcSigner | Wallet,
|
const signer = this.provider.getSigner()
|
||||||
pollId: string,
|
|
||||||
selectedAnswer: number,
|
|
||||||
tokenAmount?: BigNumber
|
|
||||||
) {
|
|
||||||
const address = await signer.getAddress()
|
const address = await signer.getAddress()
|
||||||
const poll = this.wakuMessages['pollInit'].arr.find((poll: PollInitMsg): poll is PollInitMsg => poll.id === pollId)
|
const poll = this.wakuMessages['pollInit'].arr.find((poll: PollInitMsg): poll is PollInitMsg => poll.id === pollId)
|
||||||
if (poll) {
|
if (poll) {
|
||||||
|
|
|
@ -69,7 +69,6 @@ export function Poll({ poll, wakuPolling, theme, signer }: PollProps) {
|
||||||
onClick={async () => {
|
onClick={async () => {
|
||||||
if (wakuPolling && signer) {
|
if (wakuPolling && signer) {
|
||||||
const result = await wakuPolling.sendTimedPollVote(
|
const result = await wakuPolling.sendTimedPollVote(
|
||||||
signer,
|
|
||||||
poll.poll.id,
|
poll.poll.id,
|
||||||
selectedAnswer ?? 0,
|
selectedAnswer ?? 0,
|
||||||
poll.poll.pollType === PollType.WEIGHTED ? BigNumber.from(tokenAmount) : undefined
|
poll.poll.pollType === PollType.WEIGHTED ? BigNumber.from(tokenAmount) : undefined
|
||||||
|
|
|
@ -89,7 +89,6 @@ export function PollCreation({ signer, wakuPolling, theme, setShowPollCreation }
|
||||||
onClick={async (e) => {
|
onClick={async (e) => {
|
||||||
e.preventDefault()
|
e.preventDefault()
|
||||||
const result = await wakuPolling?.createTimedPoll(
|
const result = await wakuPolling?.createTimedPoll(
|
||||||
signer,
|
|
||||||
question,
|
question,
|
||||||
answers,
|
answers,
|
||||||
selectedType,
|
selectedType,
|
||||||
|
|
Loading…
Reference in New Issue