From 5cddb61d72f6ab47857e04e641a1abf5ec18b45e Mon Sep 17 00:00:00 2001 From: Szymon Szlachtowicz <38212223+Szymx95@users.noreply.github.com> Date: Mon, 9 Aug 2021 11:57:02 +0200 Subject: [PATCH] Introduce WakuVoting (#2) --- packages/core/README.md | 25 ++++++++++++++-- packages/core/src/index.ts | 30 ++++++++++++++++++-- packages/core/test/index.test.ts | 14 ++++----- packages/react-components/src/index.tsx | 8 +----- packages/react-components/test/index.test.ts | 3 +- 5 files changed, 59 insertions(+), 21 deletions(-) diff --git a/packages/core/README.md b/packages/core/README.md index 546f0c3..d6b3a18 100644 --- a/packages/core/README.md +++ b/packages/core/README.md @@ -1,12 +1,33 @@ #Gassless voting over waku +##Waku Voting + +Objects of type of WakuVoting, hold their own Waku objects and also store list of polls and votes for later use. + +Creating instance of WakuVoting: + +WakuVoting constructor expects name of DApp and also as optional parameter can take custom Waku object. + +```ts + import WakuVoting from 'core' + + const wakuVoting = new WakuVoting('myDapp') +``` + +objects of type WakuVoting expose functions: + +createTimedPoll(signer: JsonRpcSigner, question:string, answers: string[], pollType: enum, minToken?: BigNumber, endTime?: number) +getTimedPolls() +sendTimedPollVote(signer: JsonRpcSigner, pollHash: string, selectedAnswer:number, sntAmount?: number) + + ##Polls ###Creating time-limited poll To create a poll user has to send a message over waku network on specific topic -`/{dapp name}/waku-polling/polls-init/proto` +`/{dapp name}/waku-polling/timed-polls-init/proto` For a poll to be started waku message has to have specific fields: @@ -25,4 +46,4 @@ message PollInit { int64 endTime = 7 // UNIX timestamp of poll end bytes signature = 8 // signature of all above fields } -``` \ No newline at end of file +``` diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index 36d5e48..8e0580c 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -1,5 +1,29 @@ -function foo() { - return 'Hello world' +import { Waku, getStatusFleetNodes } from 'js-waku' + +class WakuVoting { + private appName: string + private waku: Waku | undefined + + private async createWaku() { + this.waku = await Waku.create() + const nodes = await getStatusFleetNodes() + await Promise.all( + nodes.map((addr) => { + if (this.waku) { + return this.waku.dial(addr) + } + }) + ) + } + + constructor(appName: string, waku?: Waku) { + this.appName = appName + if (waku) { + this.waku = waku + } else { + this.createWaku() + } + } } -export { foo } +export default WakuVoting diff --git a/packages/core/test/index.test.ts b/packages/core/test/index.test.ts index f4da420..04a06d5 100644 --- a/packages/core/test/index.test.ts +++ b/packages/core/test/index.test.ts @@ -1,11 +1,11 @@ import { expect } from 'chai' -import { foo } from '../src' +import { Waku } from 'js-waku' +import WakuVoting from '../src' -describe('test', () => { - it('foo', async () => { - expect(foo()).to.eq('Hello world') - }) - it('not foo', async () => { - expect(foo()).to.not.eq('asdas') +describe('WakuVoting', () => { + it('success', async () => { + const wakuVoting = new WakuVoting('test', {} as unknown as Waku) + + expect(wakuVoting).to.not.be.undefined }) }) diff --git a/packages/react-components/src/index.tsx b/packages/react-components/src/index.tsx index c6dfb5d..5e581a1 100644 --- a/packages/react-components/src/index.tsx +++ b/packages/react-components/src/index.tsx @@ -1,13 +1,7 @@ import React from 'react' -import { foo } from '@status-waku-voting/core' function Example() { - return ( -