@leofcoin/standards 0.2.15 → 0.3.0

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 (59) 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 +108 -11
  23. package/exports/voting/public-voting.d.ts +45 -8
  24. package/exports/voting/types.d.ts +11 -7
  25. package/package.json +9 -14
  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/lock.d.ts +0 -37
  48. package/exports/staking.d.ts +0 -40
  49. package/exports/voting/interfaces/i-voting.d.ts +0 -5
  50. package/exports/voting/voting.d.ts +0 -38
  51. package/exports/voting-C0KVNQO3.js +0 -112
  52. package/exports/voting-xYjJlN2h.js +0 -112
  53. package/src/contract-creator.ts +0 -24
  54. package/src/decorators/time.ts +0 -9
  55. package/src/interfaces/i-token.ts +0 -9
  56. package/src/lock.ts +0 -167
  57. package/src/staking.ts +0 -166
  58. package/src/voting/interfaces/i-voting.ts +0 -5
  59. package/src/voting/voting.ts +0 -123
@@ -1,112 +0,0 @@
1
- import ContractCreator from './contract-creator.js';
2
-
3
- class Voting extends ContractCreator {
4
- #votes = {};
5
- #votingDisabled = false;
6
- #votingDuration = 172800000;
7
- constructor(state) {
8
- super(state);
9
- if (state) {
10
- this.#votes = state.votes;
11
- this.#votingDisabled = state.votingDisabled;
12
- this.#votingDuration = state.votingDuration;
13
- }
14
- }
15
- get votes() {
16
- return { ...this.#votes };
17
- }
18
- get votingDuration() {
19
- return this.#votingDuration;
20
- }
21
- get votingDisabled() {
22
- return this.#votingDisabled;
23
- }
24
- get state() {
25
- return {
26
- ...super.state,
27
- votes: this.#votes,
28
- votingDisabled: this.#votingDisabled,
29
- votingDuration: this.#votingDuration
30
- };
31
- }
32
- #canVote() {
33
- // @ts-expect-error
34
- return this._canVote?.();
35
- }
36
- #beforeVote() {
37
- // @ts-expect-error
38
- return this._beforeVote?.();
39
- }
40
- #afterVote() {
41
- // @ts-expect-error
42
- return this._afterVote?.();
43
- }
44
- /**
45
- * create vote
46
- * @param {string} vote
47
- * @param {string} description
48
- * @param {number} endTime
49
- * @param {string} method function to run when agree amount is bigger
50
- */
51
- createVote(title, description, endTime, method, args = []) {
52
- if (!this.#canVote())
53
- throw new Error(`Not allowed to create a vote`);
54
- const id = crypto.randomUUID();
55
- this.#votes[id] = {
56
- title,
57
- description,
58
- method,
59
- endTime,
60
- args
61
- };
62
- }
63
- #endVoting(voteId) {
64
- let agree = Object.values(this.#votes[voteId].results).filter((result) => result === 1);
65
- let disagree = Object.values(this.#votes[voteId].results).filter((result) => result === 0);
66
- if (agree.length > disagree.length)
67
- this[this.#votes[voteId].method](...this.#votes[voteId].args);
68
- this.#votes[voteId].finished = true;
69
- }
70
- async vote(voteId, vote) {
71
- vote = Number(vote);
72
- if (vote !== 0 && vote !== 0.5 && vote !== 1)
73
- throw new Error(`invalid vote value ${vote}`);
74
- if (!this.#votes[voteId])
75
- throw new Error(`Nothing found for ${voteId}`);
76
- const ended = new Date().getTime() > this.#votes[voteId].endTime;
77
- if (ended && !this.#votes[voteId].finished)
78
- this.#endVoting(voteId);
79
- if (ended)
80
- throw new Error('voting already ended');
81
- if (!this.#canVote())
82
- throw new Error(`Not allowed to vote`);
83
- await this.#beforeVote();
84
- this.#votes[voteId][msg.sender] = vote;
85
- await this.#afterVote();
86
- }
87
- get votesInProgress() {
88
- return Object.entries(this.#votes)
89
- .filter(([id, vote]) => !vote.finished)
90
- .map(([id, vote]) => {
91
- return { ...vote, id };
92
- });
93
- }
94
- #disableVoting() {
95
- this.#votingDisabled = true;
96
- }
97
- disableVoting() {
98
- if (!this.#canVote())
99
- throw new Error('not a allowed');
100
- else {
101
- this.createVote(`disable voting`, `Warning this disables all voting features forever`, new Date().getTime() + this.#votingDuration, '#disableVoting', []);
102
- }
103
- }
104
- _sync() {
105
- for (const vote of this.votesInProgress) {
106
- if (vote.endTime < new Date().getTime())
107
- this.#endVoting(vote.id);
108
- }
109
- }
110
- }
111
-
112
- export { Voting as V };
@@ -1,112 +0,0 @@
1
- import ContractCreator from './contract-creator.js';
2
-
3
- class Voting extends ContractCreator {
4
- #votes = {};
5
- #votingDisabled = false;
6
- #votingDuration = 172800000;
7
- constructor(state) {
8
- super(state);
9
- if (state) {
10
- this.#votes = state.votes;
11
- this.#votingDisabled = state.votingDisabled;
12
- this.#votingDuration = state.votingDuration;
13
- }
14
- }
15
- get votes() {
16
- return { ...this.#votes };
17
- }
18
- get votingDuration() {
19
- return this.#votingDuration;
20
- }
21
- get votingDisabled() {
22
- return this.#votingDisabled;
23
- }
24
- get state() {
25
- return {
26
- ...super.state,
27
- votes: this.#votes,
28
- votingDisabled: this.#votingDisabled,
29
- votingDuration: this.#votingDuration
30
- };
31
- }
32
- #canVote() {
33
- // @ts-expect-error
34
- return this._canVote?.();
35
- }
36
- #beforeVote() {
37
- // @ts-expect-error
38
- return this._beforeVote?.();
39
- }
40
- #afterVote() {
41
- // @ts-expect-error
42
- return this._afterVote?.();
43
- }
44
- /**
45
- * create vote
46
- * @param {string} vote
47
- * @param {string} description
48
- * @param {number} endTime
49
- * @param {string} method function to run when agree amount is bigger
50
- */
51
- createVote(title, description, endTime, method, args = []) {
52
- if (!this.#canVote())
53
- throw new Error(`Not allowed to create a vote`);
54
- const id = crypto.randomUUID();
55
- this.#votes[id] = {
56
- title,
57
- description,
58
- method,
59
- endTime,
60
- args
61
- };
62
- }
63
- #endVoting(voteId) {
64
- let agree = Object.values(this.#votes[voteId].results).filter((result) => result === 1);
65
- let disagree = Object.values(this.#votes[voteId].results).filter((result) => result === 0);
66
- if (agree.length > disagree.length)
67
- this[this.#votes[voteId].method](...this.#votes[voteId].args);
68
- this.#votes[voteId].finished = true;
69
- }
70
- async vote(voteId, vote) {
71
- vote = Number(vote);
72
- if (vote !== 0 && vote !== 0.5 && vote !== 1)
73
- throw new Error(`invalid vote value ${vote}`);
74
- if (!this.#votes[voteId])
75
- throw new Error(`Nothing found for ${voteId}`);
76
- const ended = new Date().getTime() > this.#votes[voteId].endTime;
77
- if (ended && !this.#votes[voteId].finished)
78
- this.#endVoting(voteId);
79
- if (ended)
80
- throw new Error('voting already ended');
81
- if (!this.#canVote())
82
- throw new Error(`Not allowed to vote`);
83
- await this.#beforeVote();
84
- this.#votes[voteId][msg.sender] = vote;
85
- await this.#afterVote();
86
- }
87
- get votesInProgress() {
88
- return Object.entries(this.#votes)
89
- .filter(([id, vote]) => !vote.finished)
90
- .map(([id, vote]) => {
91
- return { ...vote, id };
92
- });
93
- }
94
- #disableVoting() {
95
- this.#votingDisabled = true;
96
- }
97
- disableVoting() {
98
- if (!this.#canVote())
99
- throw new Error('not a allowed');
100
- else {
101
- this.createVote(`disable voting`, `Warning this disables all voting features forever`, new Date().getTime() + this.#votingDuration, '#disableVoting', []);
102
- }
103
- }
104
- sync() {
105
- for (const vote of this.votesInProgress) {
106
- if (vote.endTime < new Date().getTime())
107
- this.#endVoting(vote.id);
108
- }
109
- }
110
- }
111
-
112
- export { Voting as V };
@@ -1,24 +0,0 @@
1
- export type ContractCreatorState = {
2
- contractCreator: address
3
- }
4
-
5
- export default class ContractCreator {
6
- #creator: address
7
-
8
- constructor(state: ContractCreatorState) {
9
- if (state) this.#creator = state.contractCreator
10
- else this.#creator = msg.sender
11
- }
12
-
13
- get _contractCreator() {
14
- return this.#creator
15
- }
16
-
17
- get state(): ContractCreatorState {
18
- return { contractCreator: this.#creator }
19
- }
20
-
21
- get _isContractCreator() {
22
- return msg.sender === this.#creator
23
- }
24
- }
@@ -1,9 +0,0 @@
1
- export function time(target, descriptor) {
2
- console.log({ descriptor }, { target })
3
- if (descriptor.kind !== 'method') throw new Error(`expected ${descriptor.name} to be a method`)
4
- descriptor.addInitializer(function () {
5
- globalThis.pubsub.subscribe('time-interval', (value) => {
6
- this.onTimeChange?.(value)
7
- })
8
- })
9
- }
@@ -1,9 +0,0 @@
1
- export interface IToken {
2
- mint?(to: string, amount: bigint): void
3
- burn?(from: string, amount: bigint): void
4
- transfer?(from: string, to: string, amount: bigint): void
5
- balance?(): bigint
6
- balanceOf?(address: string): bigint
7
- setApproval?(operator: string, amount: bigint): void
8
- approved?(owner: string, operator: string, amount: bigint): boolean
9
- }
package/src/lock.ts DELETED
@@ -1,167 +0,0 @@
1
- import { restoreApprovals, restoreBalances } from './helpers.js'
2
- import Roles, { RolesState } from './roles.js'
3
- import { IVoting } from './voting/interfaces/i-voting.js'
4
- import PublicVoting from './voting/public-voting.js'
5
-
6
- export declare interface LockState extends RolesState {
7
- holders: bigint
8
- balances: { [address: address]: bigint }
9
- totalSupply: bigint
10
- }
11
-
12
- export default class Lock extends PublicVoting implements IVoting {
13
- /**
14
- * string
15
- */
16
- #name: string
17
- /**
18
- * String
19
- */
20
- #symbol: string
21
- /**
22
- * uint
23
- */
24
- #holders: bigint = BigInt(0)
25
- /**
26
- * Object => Object => uint
27
- */
28
- #balances = {}
29
- /**
30
- * Object => Object => uint
31
- */
32
- #approvals: { [owner: string]: { [operator: string]: bigint } } = {}
33
-
34
- #decimals = 18
35
-
36
- #totalSupply: bigint = BigInt(0)
37
-
38
- #stakingContract: address
39
-
40
- // this.#privateField2 = 1
41
- constructor(name: string, symbol: string, decimals: number = 18, state?: LockState) {
42
- if (!name) throw new Error(`name undefined`)
43
- if (!symbol) throw new Error(`symbol undefined`)
44
-
45
- super(state)
46
-
47
- if (state) {
48
- this.#balances = restoreBalances(state.balances)
49
- this.#approvals = restoreApprovals(state.approvals)
50
- this.#holders = BigInt(state.holders)
51
- this.#totalSupply = BigInt(state.totalSupply)
52
- } else {
53
- this.#name = name
54
- this.#symbol = symbol
55
- this.#decimals = decimals
56
- }
57
- }
58
-
59
- // enables snapshotting
60
- // needs dev attention so nothing breaks after snapshot happens
61
- // iow everything that is not static needs to be included in the stateObject
62
- /**
63
- * @return {Object} {holders, balances, ...}
64
- */
65
- get state(): LockState {
66
- return {
67
- ...super.state,
68
- holders: this.holders,
69
- balances: this.balances,
70
- approvals: { ...this.#approvals },
71
- totalSupply: this.totalSupply
72
- }
73
- }
74
-
75
- get totalSupply(): bigint {
76
- return this.#totalSupply
77
- }
78
-
79
- get name(): string {
80
- return this.#name
81
- }
82
-
83
- get symbol(): string {
84
- return this.#symbol
85
- }
86
-
87
- get holders(): LockState['holders'] {
88
- return this.#holders
89
- }
90
-
91
- get balances(): LockState['balances'] {
92
- return { ...this.#balances }
93
- }
94
-
95
- get approvals() {
96
- return this.#approvals
97
- }
98
-
99
- get decimals() {
100
- return this.#decimals
101
- }
102
-
103
- mint(to: address, amount: bigint) {
104
- if (!this.hasRole(msg.sender, 'MINT')) throw new Error('not allowed')
105
-
106
- this.#totalSupply = this.#totalSupply + amount
107
- this.#increaseBalance(to, amount)
108
- }
109
-
110
- burn(from: address, amount: bigint) {
111
- if (!this.hasRole(msg.sender, 'BURN') || msg.sender !== from) throw new Error('not allowed')
112
- const total = this.#totalSupply - amount
113
- if (total > 0n) {
114
- this.#totalSupply = total
115
- this.#decreaseBalance(from, amount)
116
- }
117
- }
118
-
119
- #beforeTransfer(from: address, to: address, amount: bigint) {
120
- if (!this.#balances[from] || this.#balances[from] < amount) throw new Error('amount exceeds balance')
121
- }
122
-
123
- #updateHolders(address: address, previousBalance: bigint) {
124
- if (this.#balances[address] === 0n) this.#holders -= 1n
125
- else if (this.#balances[address] !== 0n && previousBalance === 0n) this.#holders += 1n
126
- }
127
-
128
- #increaseBalance(address: address, amount: bigint) {
129
- if (!this.#balances[address]) this.#balances[address] = BigInt(0)
130
- const previousBalance = this.#balances[address]
131
-
132
- this.#balances[address] = this.#balances[address] += amount
133
- this.#updateHolders(address, previousBalance)
134
- }
135
-
136
- #decreaseBalance(address: address, amount: bigint) {
137
- const previousBalance = this.#balances[address]
138
- this.#balances[address] = this.#balances[address] -= amount
139
- this.#updateHolders(address, previousBalance)
140
- }
141
-
142
- balance() {
143
- return this.#balances[msg.sender]
144
- }
145
-
146
- balanceOf(address: address): bigint {
147
- return this.#balances[address]
148
- }
149
-
150
- setApproval(operator: address, amount: bigint) {
151
- const owner = msg.sender
152
- if (!this.#approvals[owner]) this.#approvals[owner] = {}
153
- this.#approvals[owner][operator] = BigInt(amount)
154
- }
155
-
156
- approved(owner: address, operator: address, amount: bigint): boolean {
157
- return this.#approvals[owner][operator] === amount
158
- }
159
-
160
- transfer(from: address, to: address, amount: bigint) {
161
- // TODO: is BigNumber?
162
- amount = BigInt(amount)
163
- this.#beforeTransfer(from, to, amount)
164
- this.#decreaseBalance(from, amount)
165
- this.#increaseBalance(to, amount)
166
- }
167
- }
package/src/staking.ts DELETED
@@ -1,166 +0,0 @@
1
- import { restoreApprovals, restoreBalances } from './helpers.js'
2
- import Roles, { RolesState } from './roles.js'
3
-
4
- export declare interface TokenState extends RolesState {
5
- holders: bigint
6
- balances: { [address: address]: bigint }
7
- approvals: { [owner: address]: { [operator: address]: bigint } }
8
- totalSupply: bigint
9
- }
10
-
11
- export default class Staking extends Roles {
12
- /**
13
- * string
14
- */
15
- #name: string
16
- /**
17
- * String
18
- */
19
- #symbol: string
20
- /**
21
- * uint
22
- */
23
- #holders: bigint = BigInt(0)
24
- /**
25
- * Object => Object => uint
26
- */
27
- #balances = {}
28
- /**
29
- * Object => Object => uint
30
- */
31
- #approvals: { [owner: string]: { [operator: string]: bigint } } = {}
32
-
33
- #decimals = 18
34
-
35
- #totalSupply: bigint = BigInt(0)
36
-
37
- #stakingContract: address
38
-
39
- // this.#privateField2 = 1
40
- constructor(name: string, symbol: string, decimals: number = 18, state?: TokenState) {
41
- if (!name) throw new Error(`name undefined`)
42
- if (!symbol) throw new Error(`symbol undefined`)
43
-
44
- super(state)
45
-
46
- if (state) {
47
- this.#balances = restoreBalances(state.balances)
48
- this.#approvals = restoreApprovals(state.approvals)
49
- this.#holders = BigInt(state.holders)
50
- this.#totalSupply = BigInt(state.totalSupply)
51
- } else {
52
- this.#name = name
53
- this.#symbol = symbol
54
- this.#decimals = decimals
55
- }
56
- }
57
-
58
- // enables snapshotting
59
- // needs dev attention so nothing breaks after snapshot happens
60
- // iow everything that is not static needs to be included in the stateObject
61
- /**
62
- * @return {Object} {holders, balances, ...}
63
- */
64
- get state(): TokenState {
65
- return {
66
- ...super.state,
67
- holders: this.holders,
68
- balances: this.balances,
69
- approvals: { ...this.#approvals },
70
- totalSupply: this.totalSupply
71
- }
72
- }
73
-
74
- get totalSupply(): bigint {
75
- return this.#totalSupply
76
- }
77
-
78
- get name(): string {
79
- return this.#name
80
- }
81
-
82
- get symbol(): string {
83
- return this.#symbol
84
- }
85
-
86
- get holders(): TokenState['holders'] {
87
- return this.#holders
88
- }
89
-
90
- get balances(): TokenState['balances'] {
91
- return { ...this.#balances }
92
- }
93
-
94
- get approvals() {
95
- return this.#approvals
96
- }
97
-
98
- get decimals() {
99
- return this.#decimals
100
- }
101
-
102
- mint(to: address, amount: bigint) {
103
- if (!this.hasRole(msg.sender, 'MINT')) throw new Error('not allowed')
104
-
105
- this.#totalSupply = this.#totalSupply + amount
106
- this.#increaseBalance(to, amount)
107
- }
108
-
109
- burn(from: address, amount: bigint) {
110
- if (!this.hasRole(msg.sender, 'BURN') || msg.sender !== from) throw new Error('not allowed')
111
- const total = this.#totalSupply - amount
112
- if (total > 0n) {
113
- this.#totalSupply = total
114
- this.#decreaseBalance(from, amount)
115
- }
116
- }
117
-
118
- #beforeTransfer(from: address, to: address, amount: bigint) {
119
- if (!this.#balances[from] || this.#balances[from] < amount) throw new Error('amount exceeds balance')
120
- }
121
-
122
- #updateHolders(address: address, previousBalance: bigint) {
123
- if (this.#balances[address] === 0n) this.#holders -= 1n
124
- else if (this.#balances[address] !== 0n && previousBalance === 0n) this.#holders += 1n
125
- }
126
-
127
- #increaseBalance(address: address, amount: bigint) {
128
- if (!this.#balances[address]) this.#balances[address] = BigInt(0)
129
- const previousBalance = this.#balances[address]
130
-
131
- this.#balances[address] = this.#balances[address] += amount
132
- this.#updateHolders(address, previousBalance)
133
- }
134
-
135
- #decreaseBalance(address: address, amount: bigint) {
136
- const previousBalance = this.#balances[address]
137
- this.#balances[address] = this.#balances[address] -= amount
138
- this.#updateHolders(address, previousBalance)
139
- }
140
-
141
- balance() {
142
- return this.#balances[msg.sender]
143
- }
144
-
145
- balanceOf(address: address): bigint {
146
- return this.#balances[address]
147
- }
148
-
149
- setApproval(operator: address, amount: bigint) {
150
- const owner = msg.sender
151
- if (!this.#approvals[owner]) this.#approvals[owner] = {}
152
- this.#approvals[owner][operator] = BigInt(amount)
153
- }
154
-
155
- approved(owner: address, operator: address, amount: bigint): boolean {
156
- return this.#approvals[owner][operator] === amount
157
- }
158
-
159
- transfer(from: address, to: address, amount: bigint) {
160
- // TODO: is BigNumber?
161
- amount = BigInt(amount)
162
- this.#beforeTransfer(from, to, amount)
163
- this.#decreaseBalance(from, amount)
164
- this.#increaseBalance(to, amount)
165
- }
166
- }
@@ -1,5 +0,0 @@
1
- export interface IVoting {
2
- _canVote?(): Promise<boolean> | boolean
3
- _beforeVote?(): Promise<void> | void
4
- _afterVote?(): Promise<void> | void
5
- }