@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.
Files changed (61) hide show
  1. package/.gitattributes +2 -2
  2. package/.github/workflows/test.yml +33 -0
  3. package/.prettierrc +8 -8
  4. package/CHANGELOG.md +16 -3
  5. package/LICENSE +21 -21
  6. package/README.md +25 -23
  7. package/exports/helpers.js +1 -1
  8. package/exports/i-public-voting.js +1 -0
  9. package/exports/index.d.ts +2 -1
  10. package/exports/index.js +2 -3
  11. package/exports/meta-D7uruGOw.js +28 -0
  12. package/exports/meta.d.ts +11 -0
  13. package/exports/private-voting.js +104 -11
  14. package/exports/public-voting.js +103 -4
  15. package/exports/roles.d.ts +5 -10
  16. package/exports/roles.js +7 -8
  17. package/exports/token-receiver.d.ts +5 -7
  18. package/exports/token-receiver.js +25 -14
  19. package/exports/token.d.ts +14 -27
  20. package/exports/token.js +14 -48
  21. package/exports/types.d.ts +21 -0
  22. package/exports/voting/private-voting.d.ts +109 -11
  23. package/exports/voting/public-voting.d.ts +45 -8
  24. package/exports/voting/types.d.ts +11 -7
  25. package/package.json +10 -18
  26. package/rollup.config.js +28 -28
  27. package/src/helpers.ts +19 -19
  28. package/src/index.ts +8 -7
  29. package/src/meta.ts +31 -0
  30. package/src/roles.ts +88 -89
  31. package/src/token-receiver.ts +196 -175
  32. package/src/token.ts +162 -198
  33. package/src/types.ts +15 -0
  34. package/src/voting/interfaces/i-public-voting.ts +4 -0
  35. package/src/voting/private-voting.ts +187 -69
  36. package/src/voting/public-voting.ts +134 -14
  37. package/src/voting/types.ts +30 -24
  38. package/test/helpers.js +51 -0
  39. package/test/public-voting.js +365 -6
  40. package/test/roles.js +186 -0
  41. package/test/token.js +211 -0
  42. package/tsconfig.json +16 -12
  43. package/.changeset/README.md +0 -8
  44. package/.changeset/config.json +0 -11
  45. package/exports/contract-creator.d.ts +0 -11
  46. package/exports/contract-creator.js +0 -20
  47. package/exports/decorators/time.d.ts +0 -1
  48. package/exports/interfaces/i-token.d.ts +0 -10
  49. package/exports/lock.d.ts +0 -37
  50. package/exports/staking.d.ts +0 -40
  51. package/exports/voting/interfaces/i-voting.d.ts +0 -6
  52. package/exports/voting/voting.d.ts +0 -38
  53. package/exports/voting-C0KVNQO3.js +0 -112
  54. package/exports/voting-xYjJlN2h.js +0 -112
  55. package/src/contract-creator.ts +0 -24
  56. package/src/decorators/time.ts +0 -9
  57. package/src/interfaces/i-token.ts +0 -10
  58. package/src/lock.ts +0 -167
  59. package/src/staking.ts +0 -166
  60. package/src/voting/interfaces/i-voting.ts +0 -7
  61. package/src/voting/voting.ts +0 -123
@@ -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
- }