@leofcoin/contracts 0.1.1 → 0.1.2

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.
@@ -1,7 +1,205 @@
1
- import Token from '@leofcoin/standards/token.js'
1
+ class Roles {
2
+ /**
3
+ * Object => Array
4
+ */
5
+ #roles = {
6
+ 'OWNER': [],
7
+ 'MINT': [],
8
+ 'BURN': []
9
+ };
10
+ constructor(roles) {
11
+ // allow devs to set their own roles but always keep the default ones included
12
+ // also allows roles to be loaded from the stateStore
13
+ // carefull when including the roles make sure to add the owner
14
+ // because no roles are granted by default when using custom roles
15
+ if (roles) {
16
+ if (roles instanceof Object) {
17
+ this.#roles = { ...roles, ...this.#roles };
18
+ }
19
+ else {
20
+ throw new TypeError(`expected roles to be an object`);
21
+ }
22
+ }
23
+ else {
24
+ // no roles given so fallback to default to the msg sender
25
+ this.#grantRole(msg.sender, 'OWNER');
26
+ }
27
+ }
28
+ /**
29
+ *
30
+ */
31
+ get state() {
32
+ return { roles: this.roles };
33
+ }
34
+ get roles() {
35
+ return { ...this.#roles };
36
+ }
37
+ /**
38
+ * @param {address} address
39
+ * @param {string} role
40
+ * @returns true | false
41
+ */
42
+ hasRole(address, role) {
43
+ return this.#roles[role] ? this.#roles[role].includes(address) : false;
44
+ }
45
+ /**
46
+ * @private
47
+ * @param {address} address address to grant the role to
48
+ * @param {string} role role to give
49
+ */
50
+ #grantRole(address, role) {
51
+ if (this.hasRole(address, role))
52
+ throw new Error(`${role} role already granted for ${address}`);
53
+ this.#roles[role].push(address);
54
+ }
55
+ /**
56
+ * remove role for address
57
+ * @private
58
+ * @param {address} address address to revoke role from
59
+ * @param {string} role role to evoke
60
+ */
61
+ #revokeRole(address, role) {
62
+ if (!this.hasRole(address, role))
63
+ throw new Error(`${role} role already revoked for ${address}`);
64
+ if (role === 'OWNER' && this.#roles[role].length === 1)
65
+ throw new Error(`atleast one owner is needed!`);
66
+ this.#roles[role].splice(this.#roles[role].indexOf(address));
67
+ }
68
+ grantRole(address, role) {
69
+ if (!this.hasRole(address, 'OWNER'))
70
+ throw new Error('Not allowed');
71
+ this.#grantRole(address, role);
72
+ }
73
+ revokeRole(address, role) {
74
+ if (!this.hasRole(address, 'OWNER'))
75
+ throw new Error('Not allowed');
76
+ this.#revokeRole(address, role);
77
+ }
78
+ }
79
+
80
+ class Token extends Roles {
81
+ /**
82
+ * string
83
+ */
84
+ #name;
85
+ /**
86
+ * String
87
+ */
88
+ #symbol;
89
+ /**
90
+ * uint
91
+ */
92
+ #holders = 0;
93
+ /**
94
+ * Object => Object => uint
95
+ */
96
+ #balances = {};
97
+ /**
98
+ * Object => Object => uint
99
+ */
100
+ #approvals = {};
101
+ #decimals = 18;
102
+ #totalSupply = BigNumber.from(0);
103
+ // this.#privateField2 = 1
104
+ constructor(name, symbol, decimals = 18, state) {
105
+ if (!name)
106
+ throw new Error(`name undefined`);
107
+ if (!symbol)
108
+ throw new Error(`symbol undefined`);
109
+ super(state?.roles);
110
+ this.#name = name;
111
+ this.#symbol = symbol;
112
+ this.#decimals = decimals;
113
+ }
114
+ // enables snapshotting
115
+ // needs dev attention so nothing breaks after snapshot happens
116
+ // iow everything that is not static needs to be included in the stateObject
117
+ /**
118
+ * @return {Object} {holders, balances, ...}
119
+ */
120
+ get state() {
121
+ return {
122
+ ...super.state,
123
+ holders: this.holders,
124
+ balances: this.balances,
125
+ approvals: { ...this.#approvals },
126
+ totalSupply: this.totalSupply
127
+ };
128
+ }
129
+ get totalSupply() {
130
+ return this.#totalSupply;
131
+ }
132
+ get name() {
133
+ return this.#name;
134
+ }
135
+ get symbol() {
136
+ return this.#symbol;
137
+ }
138
+ get holders() {
139
+ return this.#holders;
140
+ }
141
+ get balances() {
142
+ return { ...this.#balances };
143
+ }
144
+ mint(to, amount) {
145
+ if (!this.hasRole(msg.sender, 'MINT'))
146
+ throw new Error('not allowed');
147
+ this.#totalSupply = this.#totalSupply.add(amount);
148
+ this.#increaseBalance(to, amount);
149
+ }
150
+ burn(from, amount) {
151
+ if (!this.hasRole(msg.sender, 'BURN'))
152
+ throw new Error('not allowed');
153
+ this.#totalSupply = this.#totalSupply.sub(amount);
154
+ this.#decreaseBalance(from, amount);
155
+ }
156
+ #beforeTransfer(from, to, amount) {
157
+ if (!this.#balances[from] || this.#balances[from] < amount)
158
+ throw new Error('amount exceeds balance');
159
+ }
160
+ #updateHolders(address, previousBalance) {
161
+ if (this.#balances[address].toHexString() === '0x00')
162
+ this.#holders -= 1;
163
+ else if (this.#balances[address].toHexString() !== '0x00' && previousBalance.toHexString() === '0x00')
164
+ this.#holders += 1;
165
+ }
166
+ #increaseBalance(address, amount) {
167
+ if (!this.#balances[address])
168
+ this.#balances[address] = BigNumber.from(0);
169
+ const previousBalance = this.#balances[address];
170
+ this.#balances[address] = this.#balances[address].add(amount);
171
+ this.#updateHolders(address, previousBalance);
172
+ }
173
+ #decreaseBalance(address, amount) {
174
+ const previousBalance = this.#balances[address];
175
+ this.#balances[address] = this.#balances[address].sub(amount);
176
+ this.#updateHolders(address, previousBalance);
177
+ }
178
+ balanceOf(address) {
179
+ return this.#balances[address];
180
+ }
181
+ setApproval(operator, amount) {
182
+ const owner = msg.sender;
183
+ if (!this.#approvals[owner])
184
+ this.#approvals[owner] = {};
185
+ this.#approvals[owner][operator] = amount;
186
+ }
187
+ approved(owner, operator, amount) {
188
+ return this.#approvals[owner][operator] === amount;
189
+ }
190
+ transfer(from, to, amount) {
191
+ // TODO: is BigNumber?
192
+ amount = BigNumber.from(amount);
193
+ this.#beforeTransfer(from, to, amount);
194
+ this.#decreaseBalance(from, amount);
195
+ this.#increaseBalance(to, amount);
196
+ }
197
+ }
2
198
 
3
- export default class Power extends Token {
4
- constructor(state) {
5
- super('Power', 'PWR', 18, state)
6
- }
199
+ class Power extends Token {
200
+ constructor(state) {
201
+ super('Power', 'PWR', 18, state);
202
+ }
7
203
  }
204
+
205
+ export { Power as default };
@@ -0,0 +1,2 @@
1
+ import globals from '@leofcoin/global-types';
2
+ export default globals;
@@ -0,0 +1,16 @@
1
+ import Roles from '@leofcoin/standards/roles.js';
2
+ export default class Validators extends Roles {
3
+ #private;
4
+ get state(): any;
5
+ constructor(tokenAddress: any, state: any);
6
+ get name(): string;
7
+ get currency(): any;
8
+ get validators(): {};
9
+ get totalValidators(): number;
10
+ get minimumBalance(): any;
11
+ changeCurrency(currency: any): void;
12
+ has(validator: any): boolean;
13
+ addValidator(validator: address): Promise<void>;
14
+ removeValidator(validator: any): void;
15
+ updateValidator(validator: any, active: any): Promise<void>;
16
+ }
@@ -1,129 +1,197 @@
1
- import Roles from '@lefocoin/standards/roles.js'
2
-
3
- export default class Validators extends Roles {
4
- /**
5
- * string
6
- */
7
- #name = 'ArtOnlineValidators'
8
- /**
9
- * uint
10
- */
11
- #totalValidators = 0
12
-
13
- #activeValidators = 0
14
- /**
15
- * Object => string(address) => Object
16
- */
17
- #validators = {}
18
-
19
- #currency
20
-
21
- #minimumBalance
22
-
23
- get state() {
24
- return {
25
- ...super.state,
26
- minimumBalance: this.#minimumBalance,
27
- currency: this.#currency,
28
- totalValidators: this.#totalValidators,
29
- activeValidators: this.#activeValidators,
30
- validators: this.#validators
31
- }
32
- }
33
-
34
- constructor(tokenAddress, state) {
35
- super(state?.roles)
36
- if (state) {
37
- this.#minimumBalance = state.minimumBalance
38
- this.#currency = state.currency
39
-
40
- this.#totalValidators = state.totalValidators
41
- this.#activeValidators = state.activeValidators
42
- this.#validators = state.validators
43
- } else {
44
- this.#minimumBalance = 50_000
45
- this.#currency = tokenAddress
46
-
47
- this.#totalValidators += 1
48
- this.#activeValidators += 1
49
- this.#validators[msg.sender] = {
50
- firstSeen: Date.now(),
51
- lastSeen: Date.now(),
52
- active: true
53
- }
1
+ class Roles {
2
+ /**
3
+ * Object => Array
4
+ */
5
+ #roles = {
6
+ 'OWNER': [],
7
+ 'MINT': [],
8
+ 'BURN': []
9
+ };
10
+ constructor(roles) {
11
+ // allow devs to set their own roles but always keep the default ones included
12
+ // also allows roles to be loaded from the stateStore
13
+ // carefull when including the roles make sure to add the owner
14
+ // because no roles are granted by default when using custom roles
15
+ if (roles) {
16
+ if (roles instanceof Object) {
17
+ this.#roles = { ...roles, ...this.#roles };
18
+ }
19
+ else {
20
+ throw new TypeError(`expected roles to be an object`);
21
+ }
22
+ }
23
+ else {
24
+ // no roles given so fallback to default to the msg sender
25
+ this.#grantRole(msg.sender, 'OWNER');
26
+ }
54
27
  }
28
+ /**
29
+ *
30
+ */
31
+ get state() {
32
+ return { roles: this.roles };
33
+ }
34
+ get roles() {
35
+ return { ...this.#roles };
36
+ }
37
+ /**
38
+ * @param {address} address
39
+ * @param {string} role
40
+ * @returns true | false
41
+ */
42
+ hasRole(address, role) {
43
+ return this.#roles[role] ? this.#roles[role].includes(address) : false;
44
+ }
45
+ /**
46
+ * @private
47
+ * @param {address} address address to grant the role to
48
+ * @param {string} role role to give
49
+ */
50
+ #grantRole(address, role) {
51
+ if (this.hasRole(address, role))
52
+ throw new Error(`${role} role already granted for ${address}`);
53
+ this.#roles[role].push(address);
54
+ }
55
+ /**
56
+ * remove role for address
57
+ * @private
58
+ * @param {address} address address to revoke role from
59
+ * @param {string} role role to evoke
60
+ */
61
+ #revokeRole(address, role) {
62
+ if (!this.hasRole(address, role))
63
+ throw new Error(`${role} role already revoked for ${address}`);
64
+ if (role === 'OWNER' && this.#roles[role].length === 1)
65
+ throw new Error(`atleast one owner is needed!`);
66
+ this.#roles[role].splice(this.#roles[role].indexOf(address));
67
+ }
68
+ grantRole(address, role) {
69
+ if (!this.hasRole(address, 'OWNER'))
70
+ throw new Error('Not allowed');
71
+ this.#grantRole(address, role);
72
+ }
73
+ revokeRole(address, role) {
74
+ if (!this.hasRole(address, 'OWNER'))
75
+ throw new Error('Not allowed');
76
+ this.#revokeRole(address, role);
77
+ }
78
+ }
55
79
 
56
- }
57
-
58
- get name() {
59
- return this.#name
60
- }
61
-
62
- get currency() {
63
- return this.#currency
64
- }
65
-
66
- get validators() {
67
- return {...this.#validators}
68
- }
69
-
70
- get totalValidators() {
71
- return this.#totalValidators
72
- }
73
-
74
- get minimumBalance() {
75
- return this.#minimumBalance
76
- }
77
-
78
- changeCurrency(currency) {
79
- if (!this.hasRole(msg.sender, 'OWNER')) throw new Error('not an owner')
80
- this.#currency = currency
81
- }
82
-
83
- has(validator) {
84
- return Boolean(this.#validators[validator] !== undefined)
85
- }
86
-
87
- #isAllowed(address) {
88
- if (msg.sender !== address && !this.hasRole(msg.sender, 'OWNER')) throw new Error('sender is not the validator or owner')
89
- return true
90
- }
91
-
92
- async addValidator(validator) {
93
- this.#isAllowed(validator)
94
- if (this.has(validator)) throw new Error('already a validator')
95
-
96
- const balance = await msg.staticCall(this.currency, 'balanceOf', [validator])
97
-
98
- if (balance < this.minimumBalance) throw new Error(`balance to low! got: ${balance} need: ${this.#minimumBalance}`)
99
-
100
- this.#totalValidators += 1
101
- this.#activeValidators += 1
102
- this.#validators[validator] = {
103
- firstSeen: Date.now(),
104
- lastSeen: Date.now(),
105
- active: true
106
- }
107
- }
108
-
109
- removeValidator(validator) {
110
- this.#isAllowed(validator)
111
- if (!this.has(validator)) throw new Error('validator not found')
112
-
113
- this.#totalValidators -= 1
114
- if (this.#validators[validator].active) this.#activeValidators -= 1
115
- delete this.#validators[validator]
116
- }
117
-
118
- async updateValidator(validator, active) {
119
- this.#isAllowed(validator)
120
- if (!this.has(validator)) throw new Error('validator not found')
121
- const balance = await msg.staticCall(this.currency, 'balanceOf', [validator])
122
- if (balance < this.minimumBalance && active) throw new Error(`balance to low! got: ${balance} need: ${this.#minimumBalance}`)
123
- if (this.#validators[validator].active === active) throw new Error(`already ${active ? 'activated' : 'deactivated'}`)
124
- if (active) this.#activeValidators += 1
125
- else this.#activeValidators -= 1
126
- /** minimum balance always needs to be met */
127
- this.#validators[validator].active = active
128
- }
80
+ class Validators extends Roles {
81
+ /**
82
+ * string
83
+ */
84
+ #name = 'ArtOnlineValidators';
85
+ /**
86
+ * uint
87
+ */
88
+ #totalValidators = 0;
89
+ #activeValidators = 0;
90
+ /**
91
+ * Object => string(address) => Object
92
+ */
93
+ #validators = {};
94
+ #currency;
95
+ #minimumBalance;
96
+ get state() {
97
+ return {
98
+ ...super.state,
99
+ minimumBalance: this.#minimumBalance,
100
+ currency: this.#currency,
101
+ totalValidators: this.#totalValidators,
102
+ activeValidators: this.#activeValidators,
103
+ validators: this.#validators
104
+ };
105
+ }
106
+ constructor(tokenAddress, state) {
107
+ super(state?.roles);
108
+ if (state) {
109
+ this.#minimumBalance = state.minimumBalance;
110
+ this.#currency = state.currency;
111
+ this.#totalValidators = state.totalValidators;
112
+ this.#activeValidators = state.activeValidators;
113
+ this.#validators = state.validators;
114
+ }
115
+ else {
116
+ this.#minimumBalance = 50000;
117
+ this.#currency = tokenAddress;
118
+ this.#totalValidators += 1;
119
+ this.#activeValidators += 1;
120
+ this.#validators[msg.sender] = {
121
+ firstSeen: Date.now(),
122
+ lastSeen: Date.now(),
123
+ active: true
124
+ };
125
+ }
126
+ }
127
+ get name() {
128
+ return this.#name;
129
+ }
130
+ get currency() {
131
+ return this.#currency;
132
+ }
133
+ get validators() {
134
+ return { ...this.#validators };
135
+ }
136
+ get totalValidators() {
137
+ return this.#totalValidators;
138
+ }
139
+ get minimumBalance() {
140
+ return this.#minimumBalance;
141
+ }
142
+ changeCurrency(currency) {
143
+ if (!this.hasRole(msg.sender, 'OWNER'))
144
+ throw new Error('not an owner');
145
+ this.#currency = currency;
146
+ }
147
+ has(validator) {
148
+ return Boolean(this.#validators[validator] !== undefined);
149
+ }
150
+ #isAllowed(address) {
151
+ if (msg.sender !== address && !this.hasRole(msg.sender, 'OWNER'))
152
+ throw new Error('sender is not the validator or owner');
153
+ return true;
154
+ }
155
+ async addValidator(validator) {
156
+ this.#isAllowed(validator);
157
+ if (this.has(validator))
158
+ throw new Error('already a validator');
159
+ const balance = await msg.staticCall(this.currency, 'balanceOf', [validator]);
160
+ if (balance < this.minimumBalance)
161
+ throw new Error(`balance to low! got: ${balance} need: ${this.#minimumBalance}`);
162
+ this.#totalValidators += 1;
163
+ this.#activeValidators += 1;
164
+ this.#validators[validator] = {
165
+ firstSeen: Date.now(),
166
+ lastSeen: Date.now(),
167
+ active: true
168
+ };
169
+ }
170
+ removeValidator(validator) {
171
+ this.#isAllowed(validator);
172
+ if (!this.has(validator))
173
+ throw new Error('validator not found');
174
+ this.#totalValidators -= 1;
175
+ if (this.#validators[validator].active)
176
+ this.#activeValidators -= 1;
177
+ delete this.#validators[validator];
178
+ }
179
+ async updateValidator(validator, active) {
180
+ this.#isAllowed(validator);
181
+ if (!this.has(validator))
182
+ throw new Error('validator not found');
183
+ const balance = await msg.staticCall(this.currency, 'balanceOf', [validator]);
184
+ if (balance < this.minimumBalance && active)
185
+ throw new Error(`balance to low! got: ${balance} need: ${this.#minimumBalance}`);
186
+ if (this.#validators[validator].active === active)
187
+ throw new Error(`already ${active ? 'activated' : 'deactivated'}`);
188
+ if (active)
189
+ this.#activeValidators += 1;
190
+ else
191
+ this.#activeValidators -= 1;
192
+ /** minimum balance always needs to be met */
193
+ this.#validators[validator].active = active;
194
+ }
129
195
  }
196
+
197
+ export { Validators as default };
package/package.json CHANGED
@@ -1,7 +1,8 @@
1
1
  {
2
2
  "name": "@leofcoin/contracts",
3
- "version": "0.1.1",
3
+ "version": "0.1.2",
4
4
  "description": "",
5
+ "type": "module",
5
6
  "exports": {
6
7
  "./factory": "./exports/factory.js",
7
8
  "./name-token": "./exports/name-token.js",
@@ -10,6 +11,7 @@
10
11
  "./validators": "./exports/validators.js"
11
12
  },
12
13
  "scripts": {
14
+ "build": "rollup -c",
13
15
  "test": "echo \"Error: no test specified\" && exit 1"
14
16
  },
15
17
  "repository": {
@@ -24,6 +26,13 @@
24
26
  },
25
27
  "homepage": "https://github.com/leofcoin/contracts#readme",
26
28
  "dependencies": {
27
- "@leofcoin/standards": "^0.1.1"
29
+ "@leofcoin/global-types": "^1.0.0",
30
+ "@leofcoin/standards": "^0.1.2"
31
+ },
32
+ "devDependencies": {
33
+ "@rollup/plugin-node-resolve": "^15.0.1",
34
+ "@rollup/plugin-typescript": "^11.0.0",
35
+ "rollup": "^3.17.2",
36
+ "tslib": "^2.5.0"
28
37
  }
29
38
  }
@@ -0,0 +1,67 @@
1
+ import typescript from '@rollup/plugin-typescript'
2
+ import tsConfig from './tsconfig.json' assert { type: 'json'}
3
+
4
+ import resolve from '@rollup/plugin-node-resolve'
5
+
6
+
7
+ export default [{
8
+ input: './src/factory.ts',
9
+ output: {
10
+ dir: './exports',
11
+ format: 'es'
12
+ },
13
+ plugins: [
14
+ resolve({
15
+ mainFields: ['exports']
16
+ }),
17
+ typescript(tsConfig)
18
+ ]
19
+ }, {
20
+ input: './src/name-service.ts',
21
+ output: {
22
+ dir: './exports',
23
+ format: 'es'
24
+ },
25
+ plugins: [
26
+ resolve({
27
+ mainFields: ['exports']
28
+ }),
29
+ typescript(tsConfig)
30
+ ]
31
+ }, {
32
+ input: './src/native-token.ts',
33
+ output: {
34
+ dir: './exports',
35
+ format: 'es'
36
+ },
37
+ plugins: [
38
+ resolve({
39
+ mainFields: ['exports']
40
+ }),
41
+ typescript(tsConfig)
42
+ ]
43
+ }, {
44
+ input: './src/power-token.ts',
45
+ output: {
46
+ dir: './exports',
47
+ format: 'es'
48
+ },
49
+ plugins: [
50
+ resolve({
51
+ mainFields: ['exports']
52
+ }),
53
+ typescript(tsConfig)
54
+ ]
55
+ }, {
56
+ input: './src/validators.ts',
57
+ output: {
58
+ dir: './exports',
59
+ format: 'es'
60
+ },
61
+ plugins: [
62
+ resolve({
63
+ mainFields: ['exports']
64
+ }),
65
+ typescript(tsConfig)
66
+ ]
67
+ }]