@leofcoin/standards 0.1.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.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2022 ArteonToken
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,21 @@
1
+ # standards
2
+ > Contract standards
3
+
4
+ standards used in leofcoin chain kinda like erc20's but the native coin included
5
+
6
+ ## install
7
+ ```sh
8
+ npm i @leofcoin/standards
9
+ ```
10
+
11
+ ## usage
12
+ ```js
13
+ import {Token} from '@leofcoin/standards'
14
+
15
+ class myCoolToken extends Token {
16
+ constructor() {
17
+ super('myCoolToken', 'MCT', 18, state?)
18
+ }
19
+ }
20
+ ```
21
+
@@ -0,0 +1,83 @@
1
+ export default class Roles {
2
+
3
+ /**
4
+ * Object => Array
5
+ */
6
+ #roles = {
7
+ 'OWNER': [],
8
+ 'MINT': [],
9
+ 'BURN': []
10
+ }
11
+
12
+ constructor(roles) {
13
+ // allow devs to set their own roles but always keep the default ones included
14
+ // also allows roles to be loaded from the stateStore
15
+ // carefull when including the roles make sure to add the owner
16
+ // because no roles are granted by default when using custom roles
17
+ if (roles) {
18
+ if (roles instanceof Object) {
19
+ this.#roles = {...roles, ...this.#roles}
20
+ } else {
21
+ throw new TypeError(`expected roles to be an object`)
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
+ */
32
+ get state() {
33
+ return { roles: this.roles }
34
+ }
35
+
36
+ get roles() {
37
+ return {...this.#roles}
38
+ }
39
+ /**
40
+ * @param {address} address
41
+ * @param {string} role
42
+ * @returns true | false
43
+ */
44
+ hasRole(address, role) {
45
+ return this.#roles[role] ? this.#roles[role].includes(address) : false
46
+ }
47
+
48
+ /**
49
+ * @private
50
+ * @param {address} address address to grant the role to
51
+ * @param {string} role role to give
52
+ */
53
+ #grantRole(address, role) {
54
+ if (this.hasRole(address, role)) throw new Error(`${role} role already granted for ${address}`)
55
+
56
+ this.#roles[role].push(address)
57
+ }
58
+
59
+ /**
60
+ * remove role for address
61
+ * @private
62
+ * @param {address} address address to revoke role from
63
+ * @param {string} role role to evoke
64
+ */
65
+ #revokeRole(address, role) {
66
+ if (!this.hasRole(address, role)) throw new Error(`${role} role already revoked for ${address}`)
67
+ if (role === 'OWNER' && this.#roles[role].length === 1) throw new Error(`atleast one owner is needed!`)
68
+
69
+ this.#roles[role].splice(this.#roles[role].indexOf(address))
70
+ }
71
+
72
+ grantRole(address, role) {
73
+ if (!this.hasRole(address, 'OWNER')) throw new Error('Not allowed')
74
+
75
+ this.#grantRole(address, role)
76
+ }
77
+
78
+ revokeRole(address, role) {
79
+ if (!this.hasRole(address, 'OWNER')) throw new Error('Not allowed')
80
+
81
+ this.#revokeRole(address, role)
82
+ }
83
+ }
File without changes
package/package.json ADDED
@@ -0,0 +1,23 @@
1
+ {
2
+ "name": "@leofcoin/standards",
3
+ "version": "0.1.0",
4
+ "description": "Contract standards",
5
+ "exports": {
6
+ "token": "./exports/token.js",
7
+ "roles": "./exports/roles.js"
8
+ },
9
+ "scripts": {
10
+ "test": "echo \"Error: no test specified\" && exit 1"
11
+ },
12
+ "repository": {
13
+ "type": "git",
14
+ "url": "git+https://github.com/ArteonToken/standards.git"
15
+ },
16
+ "keywords": [],
17
+ "author": "",
18
+ "license": "MIT",
19
+ "bugs": {
20
+ "url": "https://github.com/ArteonToken/standards/issues"
21
+ },
22
+ "homepage": "https://github.com/ArteonToken/standards#readme"
23
+ }