@leofcoin/standards 0.2.6 → 0.2.8
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.
- package/exports/index.js +1 -0
- package/exports/private-voting.js +11 -105
- package/exports/public-voting.js +2 -110
- package/exports/staking.d.ts +39 -0
- package/exports/token-receiver.d.ts +2 -2
- package/exports/token-receiver.js +2 -1
- package/exports/token.d.ts +3 -0
- package/exports/token.js +13 -3
- package/exports/voting/interfaces/i-voting.d.ts +5 -0
- package/exports/voting/private-voting.d.ts +7 -25
- package/exports/voting/voting.d.ts +7 -12
- package/exports/voting-C0KVNQO3.js +112 -0
- package/exports/voting-xYjJlN2h.js +112 -0
- package/package.json +1 -1
- package/src/decorators/time.ts +1 -1
- package/src/interfaces/i-token.ts +0 -0
- package/src/lock.ts +166 -0
- package/src/staking.ts +165 -0
- package/src/token-receiver.ts +3 -3
- package/src/token.ts +17 -4
- package/src/voting/interfaces/i-voting.ts +5 -0
- package/src/voting/private-voting.ts +12 -113
- package/src/voting/voting.ts +7 -6
- package/src/voting/interfaces/i-private-voting.ts +0 -4
- package/src/voting/interfaces/i-public-voting.ts +0 -4
|
@@ -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
|
|
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
|
-
|
|
52
|
-
return
|
|
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.
|
|
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
|
|
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.
|
|
151
|
-
if (this.#voters.length === 1 && address === msg.sender && !this
|
|
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
|
|
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
|
}
|
package/src/voting/voting.ts
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
import ContractCreator, { ContractCreatorState } from '../contract-creator.js'
|
|
2
|
-
import { Vote, VoteResult, VoteView, VotingState } from './types.js'
|
|
2
|
+
import { Vote, VoteResult, VoteView, VotingState as _VotingState } from './types.js'
|
|
3
|
+
export declare interface VotingState extends _VotingState, ContractCreatorState {}
|
|
4
|
+
|
|
3
5
|
export default class Voting extends ContractCreator {
|
|
4
6
|
#votes: VotingState['votes'] = {}
|
|
5
7
|
#votingDisabled: boolean = false
|
|
6
8
|
#votingDuration: number = 172800000
|
|
7
|
-
constructor(state) {
|
|
9
|
+
constructor(state: VotingState) {
|
|
8
10
|
super(state)
|
|
9
11
|
if (state) {
|
|
10
12
|
this.#votes = state.votes
|
|
@@ -25,7 +27,7 @@ export default class Voting extends ContractCreator {
|
|
|
25
27
|
return this.#votingDisabled
|
|
26
28
|
}
|
|
27
29
|
|
|
28
|
-
get state() {
|
|
30
|
+
get state(): VotingState {
|
|
29
31
|
return {
|
|
30
32
|
...super.state,
|
|
31
33
|
votes: this.#votes,
|
|
@@ -71,8 +73,7 @@ export default class Voting extends ContractCreator {
|
|
|
71
73
|
#endVoting(voteId) {
|
|
72
74
|
let agree = Object.values(this.#votes[voteId].results).filter((result) => result === 1)
|
|
73
75
|
let disagree = Object.values(this.#votes[voteId].results).filter((result) => result === 0)
|
|
74
|
-
if (agree.length > disagree.length
|
|
75
|
-
this[this.#votes[voteId].method](...this.#votes[voteId].args)
|
|
76
|
+
if (agree.length > disagree.length) this[this.#votes[voteId].method](...this.#votes[voteId].args)
|
|
76
77
|
this.#votes[voteId].finished = true
|
|
77
78
|
}
|
|
78
79
|
|
|
@@ -114,7 +115,7 @@ export default class Voting extends ContractCreator {
|
|
|
114
115
|
}
|
|
115
116
|
}
|
|
116
117
|
|
|
117
|
-
|
|
118
|
+
sync() {
|
|
118
119
|
for (const vote of this.votesInProgress) {
|
|
119
120
|
if (vote.endTime < new Date().getTime()) this.#endVoting(vote.id)
|
|
120
121
|
}
|