@leofcoin/standards 0.2.7 → 0.2.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,114 +1,33 @@
1
1
  import ContractCreator, { ContractCreatorState } from '../contract-creator.js'
2
+ import { IVoting } from './interfaces/i-voting.js'
2
3
  import { VoteResult, VoteView, VotingState } from './types.js'
4
+ import Voting from './voting.js'
3
5
 
4
6
  export interface PrivateVotingState extends VotingState, ContractCreatorState {
5
- voters
7
+ voters: address[]
6
8
  }
7
9
 
8
- export default class PrivateVoting extends ContractCreator {
10
+ export default class PrivateVoting extends Voting implements IVoting {
9
11
  #voters: PrivateVotingState['voters']
10
- #votes: PrivateVotingState['votes']
11
- #votingDisabled: boolean
12
- #votingDuration: number = 172800000
13
12
 
14
13
  constructor(state: PrivateVotingState) {
15
14
  super(state)
16
15
  if (state) {
17
16
  this.#voters = state.voters
18
- this.#votes = state.votes
19
- this.#votingDisabled = state.votingDisabled
20
- this.#votingDuration = state.votingDuration
21
17
  } else {
22
18
  this.#voters = [msg.sender]
23
19
  }
24
20
  }
25
21
 
26
- get votes() {
27
- return { ...this.#votes }
28
- }
29
-
30
- get voters() {
31
- return { ...this.#voters }
32
- }
33
-
34
- get votingDisabled() {
35
- return this.#votingDisabled
36
- }
37
-
38
- /**
39
- *
40
- */
41
22
  get state(): PrivateVotingState {
42
23
  return {
43
24
  ...super.state,
44
- voters: this.#voters,
45
- votes: this.#votes,
46
- votingDisabled: this.#votingDisabled,
47
- votingDuration: this.#votingDuration
25
+ voters: this.#voters
48
26
  }
49
27
  }
50
28
 
51
- get inProgress(): VoteView[] {
52
- return Object.entries(this.#votes)
53
- .filter(([id, vote]) => !vote.finished)
54
- .map(([id, vote]) => {
55
- return { ...vote, id }
56
- })
57
- }
58
- /**
59
- * create vote
60
- * @param {string} vote
61
- * @param {string} description
62
- * @param {number} endTime
63
- * @param {string} method function to run when agree amount is bigger
64
- */
65
-
66
- createVote(title: string, description: string, endTime: EpochTimeStamp, method: string, args: any[] = []) {
67
- if (!this.canVote(msg.sender)) throw new Error(`Not allowed to create a vote`)
68
- const id = crypto.randomUUID()
69
- this.#votes[id] = {
70
- title,
71
- description,
72
- method,
73
- endTime,
74
- args
75
- }
76
- }
77
-
78
- canVote(address: address) {
79
- return this.#voters.includes(address)
80
- }
81
-
82
- #enoughVotes(id) {
83
- return this.#voters.length - 2 <= Object.keys(this.#votes[id]).length
84
- }
85
-
86
- #endVoting(voteId) {
87
- let agree = Object.values(this.#votes[voteId].results).filter((result) => result === 1)
88
- let disagree = Object.values(this.#votes[voteId].results).filter((result) => result === 0)
89
- this.#votes[voteId].enoughVotes = this.#enoughVotes(voteId)
90
- if (agree.length > disagree.length && this.#votes[voteId].enoughVotes)
91
- this[this.#votes[voteId].method](...this.#votes[voteId].args)
92
- this.#votes[voteId].finished = true
93
- }
94
-
95
- vote(voteId: string, vote: VoteResult) {
96
- vote = Number(vote) as VoteResult
97
- if (vote !== 0 && vote !== 0.5 && vote !== 1) throw new Error(`invalid vote value ${vote}`)
98
- if (!this.#votes[voteId]) throw new Error(`Nothing found for ${voteId}`)
99
- const ended = new Date().getTime() > this.#votes[voteId].endTime
100
- if (ended && !this.#votes[voteId].finished) this.#endVoting(voteId)
101
- if (ended) throw new Error('voting already ended')
102
- if (!this.canVote(msg.sender)) throw new Error(`Not allowed to vote`)
103
- this.#votes[voteId][msg.sender] = vote
104
- if (this.#enoughVotes(voteId)) {
105
- this.#endVoting(voteId)
106
- }
107
- }
108
-
109
- #disableVoting() {
110
- this.#votingDisabled = true
111
- this.#voters = []
29
+ _canVote(): boolean {
30
+ return this.#voters.includes(msg.sender)
112
31
  }
113
32
 
114
33
  #grantVotingPower(address) {
@@ -119,27 +38,13 @@ export default class PrivateVoting extends ContractCreator {
119
38
  this.#voters.splice(this.#voters.indexOf(address))
120
39
  }
121
40
 
122
- disableVoting() {
123
- if (!this.canVote(msg.sender)) throw new Error('not a allowed')
124
- if (this.#voters.length === 1) this.#disableVoting()
125
- else {
126
- this.createVote(
127
- `disable voting`,
128
- `Warning this disables all voting features forever`,
129
- new Date().getTime() + this.#votingDuration,
130
- '#disableVoting',
131
- []
132
- )
133
- }
134
- }
135
-
136
41
  grantVotingPower(address: address, voteId: string) {
137
- if (this.#voters.length === 1 && this.canVote(msg.sender)) this.#grantVotingPower(address)
42
+ if (this.#voters.length === 1 && this._canVote()) this.#grantVotingPower(address)
138
43
  else {
139
44
  this.createVote(
140
45
  `grant voting power to ${address}`,
141
46
  `Should we grant ${address} voting power?`,
142
- new Date().getTime() + this.#votingDuration,
47
+ new Date().getTime() + this.votingDuration,
143
48
  '#grantVotingPower',
144
49
  [address]
145
50
  )
@@ -147,24 +52,18 @@ export default class PrivateVoting extends ContractCreator {
147
52
  }
148
53
 
149
54
  revokeVotingPower(address: address, voteId: string) {
150
- if (!this.canVote(msg.sender)) throw new Error('not a allowed to vote')
151
- if (this.#voters.length === 1 && address === msg.sender && !this.#votingDisabled)
55
+ if (!this._canVote()) throw new Error('not a allowed to vote')
56
+ if (this.#voters.length === 1 && address === msg.sender && !this.votingDisabled)
152
57
  throw new Error('only one voter left, disable voting before making this contract voteless')
153
58
  if (this.#voters.length === 1) this.#revokeVotingPower(address)
154
59
  else {
155
60
  this.createVote(
156
61
  `revoke voting power for ${address}`,
157
62
  `Should we revoke ${address} it's voting power?`,
158
- new Date().getTime() + this.#votingDuration,
63
+ new Date().getTime() + this.votingDuration,
159
64
  '#revokeVotingPower',
160
65
  [address]
161
66
  )
162
67
  }
163
68
  }
164
-
165
- sync() {
166
- for (const vote of this.inProgress) {
167
- if (vote.endTime < new Date().getTime()) this.#endVoting(vote.id)
168
- }
169
- }
170
69
  }
@@ -1,11 +1,12 @@
1
1
  import ContractCreator, { ContractCreatorState } from '../contract-creator.js'
2
- import { Vote, VoteResult, VoteView, VotingState } from './types.js'
3
- export declare interface _VotingState extends VotingState, ContractCreatorState {}
2
+ import { Vote, VoteResult, VoteView, VotingState as _VotingState } from './types.js'
3
+ export declare interface VotingState extends _VotingState, ContractCreatorState {}
4
+
4
5
  export default class Voting extends ContractCreator {
5
6
  #votes: VotingState['votes'] = {}
6
7
  #votingDisabled: boolean = false
7
8
  #votingDuration: number = 172800000
8
- constructor(state) {
9
+ constructor(state: VotingState) {
9
10
  super(state)
10
11
  if (state) {
11
12
  this.#votes = state.votes
@@ -26,7 +27,7 @@ export default class Voting extends ContractCreator {
26
27
  return this.#votingDisabled
27
28
  }
28
29
 
29
- get state(): _VotingState {
30
+ get state(): VotingState {
30
31
  return {
31
32
  ...super.state,
32
33
  votes: this.#votes,
@@ -72,8 +73,7 @@ export default class Voting extends ContractCreator {
72
73
  #endVoting(voteId) {
73
74
  let agree = Object.values(this.#votes[voteId].results).filter((result) => result === 1)
74
75
  let disagree = Object.values(this.#votes[voteId].results).filter((result) => result === 0)
75
- if (agree.length > disagree.length && this.#votes[voteId].enoughVotes)
76
- this[this.#votes[voteId].method](...this.#votes[voteId].args)
76
+ if (agree.length > disagree.length) this[this.#votes[voteId].method](...this.#votes[voteId].args)
77
77
  this.#votes[voteId].finished = true
78
78
  }
79
79
 
@@ -115,7 +115,7 @@ export default class Voting extends ContractCreator {
115
115
  }
116
116
  }
117
117
 
118
- _sync() {
118
+ sync() {
119
119
  for (const vote of this.votesInProgress) {
120
120
  if (vote.endTime < new Date().getTime()) this.#endVoting(vote.id)
121
121
  }
@@ -1,4 +0,0 @@
1
- export interface IPublicVoting {
2
- _canVote(): Promise<any>
3
- _beforeVote(): Promise<any>
4
- }
@@ -1,4 +0,0 @@
1
- export interface IPublicVoting {
2
- _canVote(): Promise<any>
3
- _beforeVote(): Promise<any>
4
- }