@leofcoin/standards 0.2.16 → 0.3.1
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/.gitattributes +2 -2
- package/.github/workflows/test.yml +33 -0
- package/.prettierrc +8 -8
- package/CHANGELOG.md +16 -3
- package/LICENSE +21 -21
- package/README.md +25 -23
- package/exports/helpers.js +1 -1
- package/exports/i-public-voting.js +1 -0
- package/exports/index.d.ts +2 -1
- package/exports/index.js +2 -3
- package/exports/meta-D7uruGOw.js +28 -0
- package/exports/meta.d.ts +11 -0
- package/exports/private-voting.js +104 -11
- package/exports/public-voting.js +103 -4
- package/exports/roles.d.ts +5 -10
- package/exports/roles.js +7 -8
- package/exports/token-receiver.d.ts +5 -7
- package/exports/token-receiver.js +25 -14
- package/exports/token.d.ts +14 -27
- package/exports/token.js +14 -48
- package/exports/types.d.ts +21 -0
- package/exports/voting/private-voting.d.ts +109 -11
- package/exports/voting/public-voting.d.ts +45 -8
- package/exports/voting/types.d.ts +11 -7
- package/package.json +10 -18
- package/rollup.config.js +28 -28
- package/src/helpers.ts +19 -19
- package/src/index.ts +8 -7
- package/src/meta.ts +31 -0
- package/src/roles.ts +88 -89
- package/src/token-receiver.ts +196 -175
- package/src/token.ts +162 -198
- package/src/types.ts +15 -0
- package/src/voting/interfaces/i-public-voting.ts +4 -0
- package/src/voting/private-voting.ts +187 -69
- package/src/voting/public-voting.ts +134 -14
- package/src/voting/types.ts +30 -24
- package/test/helpers.js +51 -0
- package/test/public-voting.js +365 -6
- package/test/roles.js +186 -0
- package/test/token.js +211 -0
- package/tsconfig.json +16 -12
- package/.changeset/README.md +0 -8
- package/.changeset/config.json +0 -11
- package/exports/contract-creator.d.ts +0 -11
- package/exports/contract-creator.js +0 -20
- package/exports/decorators/time.d.ts +0 -1
- package/exports/interfaces/i-token.d.ts +0 -10
- package/exports/lock.d.ts +0 -37
- package/exports/staking.d.ts +0 -40
- package/exports/voting/interfaces/i-voting.d.ts +0 -6
- package/exports/voting/voting.d.ts +0 -38
- package/exports/voting-C0KVNQO3.js +0 -112
- package/exports/voting-xYjJlN2h.js +0 -112
- package/src/contract-creator.ts +0 -24
- package/src/decorators/time.ts +0 -9
- package/src/interfaces/i-token.ts +0 -10
- package/src/lock.ts +0 -167
- package/src/staking.ts +0 -166
- package/src/voting/interfaces/i-voting.ts +0 -7
- package/src/voting/voting.ts +0 -123
package/src/voting/voting.ts
DELETED
|
@@ -1,123 +0,0 @@
|
|
|
1
|
-
import ContractCreator, { ContractCreatorState } from '../contract-creator.js'
|
|
2
|
-
import { Vote, VoteResult, VoteView, VotingState as _VotingState } from './types.js'
|
|
3
|
-
export declare interface VotingState extends _VotingState, ContractCreatorState {}
|
|
4
|
-
|
|
5
|
-
export default class Voting extends ContractCreator {
|
|
6
|
-
#votes: VotingState['votes'] = {}
|
|
7
|
-
#votingDisabled: boolean = false
|
|
8
|
-
#votingDuration: number = 172800000
|
|
9
|
-
constructor(state: VotingState) {
|
|
10
|
-
super(state)
|
|
11
|
-
if (state) {
|
|
12
|
-
this.#votes = state.votes
|
|
13
|
-
this.#votingDisabled = state.votingDisabled
|
|
14
|
-
this.#votingDuration = state.votingDuration
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
get votes() {
|
|
19
|
-
return { ...this.#votes }
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
get votingDuration() {
|
|
23
|
-
return this.#votingDuration
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
get votingDisabled() {
|
|
27
|
-
return this.#votingDisabled
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
get state(): VotingState {
|
|
31
|
-
return {
|
|
32
|
-
...super.state,
|
|
33
|
-
votes: this.#votes,
|
|
34
|
-
votingDisabled: this.#votingDisabled,
|
|
35
|
-
votingDuration: this.#votingDuration
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
#canVote() {
|
|
40
|
-
// @ts-expect-error
|
|
41
|
-
return this._canVote?.()
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
#beforeVote() {
|
|
45
|
-
// @ts-expect-error
|
|
46
|
-
return this._beforeVote?.()
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
#afterVote() {
|
|
50
|
-
// @ts-expect-error
|
|
51
|
-
return this._afterVote?.()
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
/**
|
|
55
|
-
* create vote
|
|
56
|
-
* @param {string} vote
|
|
57
|
-
* @param {string} description
|
|
58
|
-
* @param {number} endTime
|
|
59
|
-
* @param {string} method function to run when agree amount is bigger
|
|
60
|
-
*/
|
|
61
|
-
createVote(title: string, description: string, endTime: EpochTimeStamp, method: string, args: any[] = []) {
|
|
62
|
-
if (!this.#canVote()) throw new Error(`Not allowed to create a vote`)
|
|
63
|
-
const id = crypto.randomUUID()
|
|
64
|
-
this.#votes[id] = {
|
|
65
|
-
title,
|
|
66
|
-
description,
|
|
67
|
-
method,
|
|
68
|
-
endTime,
|
|
69
|
-
args
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
#endVoting(voteId) {
|
|
74
|
-
let agree = Object.values(this.#votes[voteId].results).filter((result) => result === 1)
|
|
75
|
-
let disagree = Object.values(this.#votes[voteId].results).filter((result) => result === 0)
|
|
76
|
-
if (agree.length > disagree.length) this[this.#votes[voteId].method](...this.#votes[voteId].args)
|
|
77
|
-
this.#votes[voteId].finished = true
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
async vote(voteId: string, vote: VoteResult) {
|
|
81
|
-
vote = Number(vote) as VoteResult
|
|
82
|
-
if (vote !== 0 && vote !== 0.5 && vote !== 1) throw new Error(`invalid vote value ${vote}`)
|
|
83
|
-
if (!this.#votes[voteId]) throw new Error(`Nothing found for ${voteId}`)
|
|
84
|
-
const ended = new Date().getTime() > this.#votes[voteId].endTime
|
|
85
|
-
if (ended && !this.#votes[voteId].finished) this.#endVoting(voteId)
|
|
86
|
-
if (ended) throw new Error('voting already ended')
|
|
87
|
-
if (!this.#canVote()) throw new Error(`Not allowed to vote`)
|
|
88
|
-
await this.#beforeVote()
|
|
89
|
-
|
|
90
|
-
this.#votes[voteId][msg.sender] = vote
|
|
91
|
-
await this.#afterVote()
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
get votesInProgress() {
|
|
95
|
-
return Object.entries(this.#votes)
|
|
96
|
-
.filter(([id, vote]) => !vote.finished)
|
|
97
|
-
.map(([id, vote]) => {
|
|
98
|
-
return { ...vote, id }
|
|
99
|
-
})
|
|
100
|
-
}
|
|
101
|
-
#disableVoting() {
|
|
102
|
-
this.#votingDisabled = true
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
disableVoting() {
|
|
106
|
-
if (!this.#canVote()) throw new Error('not a allowed')
|
|
107
|
-
else {
|
|
108
|
-
this.createVote(
|
|
109
|
-
`disable voting`,
|
|
110
|
-
`Warning this disables all voting features forever`,
|
|
111
|
-
new Date().getTime() + this.#votingDuration,
|
|
112
|
-
'#disableVoting',
|
|
113
|
-
[]
|
|
114
|
-
)
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
sync() {
|
|
119
|
-
for (const vote of this.votesInProgress) {
|
|
120
|
-
if (vote.endTime < new Date().getTime()) this.#endVoting(vote.id)
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
}
|