@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.
@@ -0,0 +1,112 @@
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 };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@leofcoin/standards",
3
- "version": "0.2.7",
3
+ "version": "0.2.9",
4
4
  "description": "Contract standards",
5
5
  "type": "module",
6
6
  "exports": {
@@ -62,13 +62,13 @@
62
62
  },
63
63
  "homepage": "https://github.com/ArteonToken/standards#readme",
64
64
  "devDependencies": {
65
+ "@changesets/cli": "^2.27.7",
65
66
  "@leofcoin/global-types": "^1.0.0",
66
67
  "@rollup/plugin-typescript": "^11.1.6",
67
68
  "rollup": "^4.12.0",
68
69
  "tslib": "^2.5.0"
69
70
  },
70
71
  "dependencies": {
71
- "@changesets/cli": "^2.27.1",
72
72
  "@ethersproject/bignumber": "^5.7.0"
73
73
  }
74
74
  }
@@ -2,7 +2,7 @@ export function time(target, descriptor) {
2
2
  console.log({ descriptor }, { target })
3
3
  if (descriptor.kind !== 'method') throw new Error(`expected ${descriptor.name} to be a method`)
4
4
  descriptor.addInitializer(function () {
5
- pubsub.subscribe('time-interval', (value) => {
5
+ globalThis.pubsub.subscribe('time-interval', (value) => {
6
6
  this.onTimeChange?.(value)
7
7
  })
8
8
  })
File without changes
package/src/lock.ts ADDED
@@ -0,0 +1,166 @@
1
+ import { restoreApprovals, restoreBalances } from './helpers.js'
2
+ import Roles, { RolesState } from './roles.js'
3
+
4
+ export declare interface LockState extends RolesState {
5
+ holders: BigNumberish
6
+ balances: { [address: address]: BigNumberish }
7
+ totalSupply: BigNumberish
8
+ }
9
+
10
+ export default class Lock extends PublicVoting {
11
+ /**
12
+ * string
13
+ */
14
+ #name: string
15
+ /**
16
+ * String
17
+ */
18
+ #symbol: string
19
+ /**
20
+ * uint
21
+ */
22
+ #holders: typeof BigNumber = BigNumber['from'](0)
23
+ /**
24
+ * Object => Object => uint
25
+ */
26
+ #balances = {}
27
+ /**
28
+ * Object => Object => uint
29
+ */
30
+ #approvals: { [owner: string]: { [operator: string]: typeof BigNumber } } = {}
31
+
32
+ #decimals = 18
33
+
34
+ #totalSupply: typeof BigNumber = BigNumber['from'](0)
35
+
36
+ #stakingContract: address
37
+
38
+ // this.#privateField2 = 1
39
+ constructor(name: string, symbol: string, decimals: number = 18, state?: TokenState) {
40
+ if (!name) throw new Error(`name undefined`)
41
+ if (!symbol) throw new Error(`symbol undefined`)
42
+
43
+ super(state)
44
+
45
+ if (state) {
46
+ this.#balances = restoreBalances(state.balances)
47
+ this.#approvals = restoreApprovals(state.approvals)
48
+ this.#holders = BigNumber['from'](state.holders)
49
+ this.#totalSupply = BigNumber['from'](state.totalSupply)
50
+ } else {
51
+ this.#name = name
52
+ this.#symbol = symbol
53
+ this.#decimals = decimals
54
+ }
55
+ }
56
+
57
+ // enables snapshotting
58
+ // needs dev attention so nothing breaks after snapshot happens
59
+ // iow everything that is not static needs to be included in the stateObject
60
+ /**
61
+ * @return {Object} {holders, balances, ...}
62
+ */
63
+ get state(): TokenState {
64
+ return {
65
+ ...super.state,
66
+ holders: this.holders,
67
+ balances: this.balances,
68
+ approvals: { ...this.#approvals },
69
+ totalSupply: this.totalSupply
70
+ }
71
+ }
72
+
73
+ get totalSupply(): BigNumberish {
74
+ return this.#totalSupply
75
+ }
76
+
77
+ get name(): string {
78
+ return this.#name
79
+ }
80
+
81
+ get symbol(): string {
82
+ return this.#symbol
83
+ }
84
+
85
+ get holders(): TokenState['holders'] {
86
+ return this.#holders
87
+ }
88
+
89
+ get balances(): TokenState['balances'] {
90
+ return { ...this.#balances }
91
+ }
92
+
93
+ get approvals() {
94
+ return this.#approvals
95
+ }
96
+
97
+ get decimals() {
98
+ return this.#decimals
99
+ }
100
+
101
+ mint(to: address, amount: BigNumberish) {
102
+ if (!this.hasRole(msg.sender, 'MINT')) throw new Error('not allowed')
103
+
104
+ this.#totalSupply = this.#totalSupply.add(amount)
105
+ this.#increaseBalance(to, amount)
106
+ }
107
+
108
+ burn(from: address, amount: BigNumberish) {
109
+ if (!this.hasRole(msg.sender, 'BURN') || msg.sender !== from) throw new Error('not allowed')
110
+ const total = this.#totalSupply.sub(amount)
111
+ if (total.gte(0)) {
112
+ this.#totalSupply = total
113
+ this.#decreaseBalance(from, amount)
114
+ }
115
+ }
116
+
117
+ #beforeTransfer(from: address, to: address, amount: BigNumberish) {
118
+ if (!this.#balances[from] || this.#balances[from] < amount) throw new Error('amount exceeds balance')
119
+ }
120
+
121
+ #updateHolders(address: address, previousBalance: typeof BigNumber) {
122
+ if (this.#balances[address].toHexString() === '0x00') this.#holders.sub(1)
123
+ else if (this.#balances[address].toHexString() !== '0x00' && previousBalance.toHexString() === '0x00')
124
+ this.#holders.add(1)
125
+ }
126
+
127
+ #increaseBalance(address: address, amount: BigNumberish) {
128
+ if (!this.#balances[address]) this.#balances[address] = BigNumber['from'](0)
129
+ const previousBalance = this.#balances[address]
130
+
131
+ this.#balances[address] = this.#balances[address].add(amount)
132
+ this.#updateHolders(address, previousBalance)
133
+ }
134
+
135
+ #decreaseBalance(address: address, amount: BigNumberish) {
136
+ const previousBalance = this.#balances[address]
137
+ this.#balances[address] = this.#balances[address].sub(amount)
138
+ this.#updateHolders(address, previousBalance)
139
+ }
140
+
141
+ balance() {
142
+ return this.#balances[msg.sender]
143
+ }
144
+
145
+ balanceOf(address: address): BigNumberish {
146
+ return this.#balances[address]
147
+ }
148
+
149
+ setApproval(operator: address, amount: BigNumberish) {
150
+ const owner = msg.sender
151
+ if (!this.#approvals[owner]) this.#approvals[owner] = {}
152
+ this.#approvals[owner][operator] = BigNumber['from'](amount)
153
+ }
154
+
155
+ approved(owner: address, operator: address, amount: BigNumberish): boolean {
156
+ return this.#approvals[owner][operator] === amount
157
+ }
158
+
159
+ transfer(from: address, to: address, amount: BigNumberish) {
160
+ // TODO: is BigNumber?
161
+ amount = BigNumber['from'](amount)
162
+ this.#beforeTransfer(from, to, amount)
163
+ this.#decreaseBalance(from, amount)
164
+ this.#increaseBalance(to, amount)
165
+ }
166
+ }
package/src/staking.ts ADDED
@@ -0,0 +1,167 @@
1
+ import { restoreApprovals, restoreBalances } from './helpers.js'
2
+ import Roles, { RolesState } from './roles.js'
3
+
4
+ export declare interface TokenState extends RolesState {
5
+ holders: BigNumberish
6
+ balances: { [address: address]: BigNumberish }
7
+ approvals: { [owner: address]: { [operator: address]: BigNumberish } }
8
+ totalSupply: BigNumberish
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: typeof BigNumber = BigNumber['from'](0)
24
+ /**
25
+ * Object => Object => uint
26
+ */
27
+ #balances = {}
28
+ /**
29
+ * Object => Object => uint
30
+ */
31
+ #approvals: { [owner: string]: { [operator: string]: typeof BigNumber } } = {}
32
+
33
+ #decimals = 18
34
+
35
+ #totalSupply: typeof BigNumber = BigNumber['from'](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 = BigNumber['from'](state.holders)
50
+ this.#totalSupply = BigNumber['from'](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(): BigNumberish {
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: BigNumberish) {
103
+ if (!this.hasRole(msg.sender, 'MINT')) throw new Error('not allowed')
104
+
105
+ this.#totalSupply = this.#totalSupply.add(amount)
106
+ this.#increaseBalance(to, amount)
107
+ }
108
+
109
+ burn(from: address, amount: BigNumberish) {
110
+ if (!this.hasRole(msg.sender, 'BURN') || msg.sender !== from) throw new Error('not allowed')
111
+ const total = this.#totalSupply.sub(amount)
112
+ if (total.gte(0)) {
113
+ this.#totalSupply = total
114
+ this.#decreaseBalance(from, amount)
115
+ }
116
+ }
117
+
118
+ #beforeTransfer(from: address, to: address, amount: BigNumberish) {
119
+ if (!this.#balances[from] || this.#balances[from] < amount) throw new Error('amount exceeds balance')
120
+ }
121
+
122
+ #updateHolders(address: address, previousBalance: typeof BigNumber) {
123
+ if (this.#balances[address].toHexString() === '0x00') this.#holders.sub(1)
124
+ else if (this.#balances[address].toHexString() !== '0x00' && previousBalance.toHexString() === '0x00')
125
+ this.#holders.add(1)
126
+ }
127
+
128
+ #increaseBalance(address: address, amount: BigNumberish) {
129
+ if (!this.#balances[address]) this.#balances[address] = BigNumber['from'](0)
130
+ const previousBalance = this.#balances[address]
131
+
132
+ this.#balances[address] = this.#balances[address].add(amount)
133
+ this.#updateHolders(address, previousBalance)
134
+ }
135
+
136
+ #decreaseBalance(address: address, amount: BigNumberish) {
137
+ const previousBalance = this.#balances[address]
138
+ this.#balances[address] = this.#balances[address].sub(amount)
139
+ this.#updateHolders(address, previousBalance)
140
+ }
141
+
142
+ balance() {
143
+ return this.#balances[msg.sender]
144
+ }
145
+
146
+ balanceOf(address: address): BigNumberish {
147
+ return this.#balances[address]
148
+ }
149
+
150
+ setApproval(operator: address, amount: BigNumberish) {
151
+ const owner = msg.sender
152
+ if (!this.#approvals[owner]) this.#approvals[owner] = {}
153
+ this.#approvals[owner][operator] = BigNumber['from'](amount)
154
+ }
155
+
156
+ approved(owner: address, operator: address, amount: BigNumberish): boolean {
157
+ return this.#approvals[owner][operator] === amount
158
+ }
159
+
160
+ transfer(from: address, to: address, amount: BigNumberish) {
161
+ // TODO: is BigNumber?
162
+ amount = BigNumber['from'](amount)
163
+ this.#beforeTransfer(from, to, amount)
164
+ this.#decreaseBalance(from, amount)
165
+ this.#increaseBalance(to, amount)
166
+ }
167
+ }
@@ -1,4 +1,4 @@
1
- import { IPublicVoting } from './voting/interfaces/i-public-voting.js'
1
+ import { IVoting } from './voting/interfaces/i-voting.js'
2
2
  import PublicVoting, { PublicVotingState } from './voting/public-voting.js'
3
3
 
4
4
  export interface TokenReceiverState extends PublicVotingState {
@@ -7,7 +7,7 @@ export interface TokenReceiverState extends PublicVotingState {
7
7
  tokenAmountToReceive: typeof BigNumber
8
8
  voteType: 'burn' | 'transfer'
9
9
  }
10
- export default class TokenReceiver extends PublicVoting implements IPublicVoting {
10
+ export default class TokenReceiver extends PublicVoting implements IVoting {
11
11
  #tokenToReceive: address
12
12
  #tokenAmountToReceive: typeof BigNumber
13
13
  #tokenReceiver: address
@@ -74,7 +74,7 @@ export default class TokenReceiver extends PublicVoting implements IPublicVoting
74
74
  }
75
75
 
76
76
  async _beforeVote(): Promise<any> {
77
- await this.#beforeVote()
77
+ return this.#beforeVote()
78
78
  }
79
79
 
80
80
  /**
package/src/token.ts CHANGED
@@ -2,6 +2,9 @@ import { restoreApprovals, restoreBalances } from './helpers.js'
2
2
  import Roles, { RolesState } from './roles.js'
3
3
 
4
4
  export declare interface TokenState extends RolesState {
5
+ name: string
6
+ symbol: string
7
+ decimals: number
5
8
  holders: BigNumberish
6
9
  balances: { [address: address]: BigNumberish }
7
10
  approvals: { [owner: address]: { [operator: address]: BigNumberish } }
@@ -34,6 +37,8 @@ export default class Token extends Roles {
34
37
 
35
38
  #totalSupply: typeof BigNumber = BigNumber['from'](0)
36
39
 
40
+ #stakingContract: address
41
+
37
42
  // this.#privateField2 = 1
38
43
  constructor(name: string, symbol: string, decimals: number = 18, state?: TokenState) {
39
44
  if (!name) throw new Error(`name undefined`)
@@ -46,6 +51,9 @@ export default class Token extends Roles {
46
51
  this.#approvals = restoreApprovals(state.approvals)
47
52
  this.#holders = BigNumber['from'](state.holders)
48
53
  this.#totalSupply = BigNumber['from'](state.totalSupply)
54
+ this.#name = name
55
+ this.#symbol = symbol
56
+ this.#decimals = decimals
49
57
  } else {
50
58
  this.#name = name
51
59
  this.#symbol = symbol
@@ -62,6 +70,9 @@ export default class Token extends Roles {
62
70
  get state(): TokenState {
63
71
  return {
64
72
  ...super.state,
73
+ name: this.#name,
74
+ symbol: this.#symbol,
75
+ decimals: this.#decimals,
65
76
  holders: this.holders,
66
77
  balances: this.balances,
67
78
  approvals: { ...this.#approvals },
@@ -105,10 +116,12 @@ export default class Token extends Roles {
105
116
  }
106
117
 
107
118
  burn(from: address, amount: BigNumberish) {
108
- if (!this.hasRole(msg.sender, 'BURN')) throw new Error('not allowed')
109
-
110
- this.#totalSupply = this.#totalSupply.sub(amount)
111
- this.#decreaseBalance(from, amount)
119
+ if (!this.hasRole(msg.sender, 'BURN') || msg.sender !== from) throw new Error('not allowed')
120
+ const total = this.#totalSupply.sub(amount)
121
+ if (total.gte(0)) {
122
+ this.#totalSupply = total
123
+ this.#decreaseBalance(from, amount)
124
+ }
112
125
  }
113
126
 
114
127
  #beforeTransfer(from: address, to: address, amount: BigNumberish) {
@@ -0,0 +1,5 @@
1
+ export interface IVoting {
2
+ _canVote?(): Promise<boolean> | boolean
3
+ _beforeVote?(): Promise<void> | void
4
+ _afterVote?(): Promise<void> | void
5
+ }