@leofcoin/standards 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.
package/LICENSE CHANGED
@@ -1,21 +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.
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 CHANGED
@@ -1,21 +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
-
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,17 @@
1
+ export default class Roles {
2
+ #private;
3
+ constructor(roles: {});
4
+ /**
5
+ *
6
+ */
7
+ get state(): {};
8
+ get roles(): {};
9
+ /**
10
+ * @param {address} address
11
+ * @param {string} role
12
+ * @returns true | false
13
+ */
14
+ hasRole(address: address, role: string): boolean;
15
+ grantRole(address: address, role: string): void;
16
+ revokeRole(address: address, role: string): void;
17
+ }
package/exports/roles.js CHANGED
@@ -1,83 +1,80 @@
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
- }
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
+ export { Roles as default };
@@ -0,0 +1,22 @@
1
+ import Roles from './roles.js';
2
+ export default class Token extends Roles {
3
+ #private;
4
+ constructor(name: string, symbol: string, decimals: number, state: {
5
+ roles?: {};
6
+ });
7
+ /**
8
+ * @return {Object} {holders, balances, ...}
9
+ */
10
+ get state(): {};
11
+ get totalSupply(): any;
12
+ get name(): string;
13
+ get symbol(): string;
14
+ get holders(): number;
15
+ get balances(): {};
16
+ mint(to: any, amount: any): void;
17
+ burn(from: address, amount: BigNumber): void;
18
+ balanceOf(address: address): BigNumber;
19
+ setApproval(operator: address, amount: BigNumber): void;
20
+ approved(owner: address, operator: address, amount: BigNumber): boolean;
21
+ transfer(from: address, to: address, amount: BigNumber): void;
22
+ }
package/exports/token.js CHANGED
@@ -1,135 +1,122 @@
1
- import Roles from './roles.js'
2
-
3
- export default class Token extends Roles {
4
- /**
5
- * string
6
- */
7
- #name
8
- /**
9
- * String
10
- */
11
- #symbol
12
- /**
13
- * uint
14
- */
15
- #holders = 0
16
- /**
17
- * Object => Object => uint
18
- */
19
- #balances = {}
20
- /**
21
- * Object => Object => uint
22
- */
23
- #approvals = {}
24
-
25
- #decimals = 18
26
-
27
- #totalSupply = BigNumber.from(0)
28
-
29
- // this.#privateField2 = 1
30
- constructor(name, symbol, decimals = 18, state) {
31
- if (!name) throw new Error(`name undefined`)
32
- if (!symbol) throw new Error(`symbol undefined`)
33
-
34
- super(state?.roles)
35
-
36
- this.#name = name
37
- this.#symbol = symbol
38
- this.#decimals = decimals
39
- }
40
-
41
- // enables snapshotting
42
- // needs dev attention so nothing breaks after snapshot happens
43
- // iow everything that is not static needs to be included in the stateObject
44
- /**
45
- * @return {Object} {holders, balances, ...}
46
- */
47
- get state() {
48
- return {
49
- ...super.state,
50
- holders: this.holders,
51
- balances: this.balances,
52
- approvals: { ...this.#approvals },
53
- totalSupply: this.totalSupply
54
- }
55
- }
56
-
57
- get totalSupply() {
58
- return this.#totalSupply
59
- }
60
-
61
- get name() {
62
- return this.#name
63
- }
64
-
65
- get symbol() {
66
- return this.#symbol
67
- }
68
-
69
- get holders() {
70
- return this.#holders
71
- }
72
-
73
- get balances() {
74
- return {...this.#balances}
75
- }
76
-
77
- mint(to, amount) {
78
- if (!this.hasRole(msg.sender, 'MINT')) throw new Error('not allowed')
79
-
80
- this.#totalSupply = this.#totalSupply.add(amount)
81
- this.#increaseBalance(to, amount)
82
- }
83
-
84
- burn(from, amount) {
85
- if (!this.hasRole(msg.sender, 'BURN')) throw new Error('not allowed')
86
-
87
- this.#totalSupply = this.#totalSupply.sub(amount)
88
- this.#decreaseBalance(from, amount)
89
- }
90
-
91
- #beforeTransfer(from, to, amount) {
92
- if (!this.#balances[from] || this.#balances[from] < amount) throw new Error('amount exceeds balance')
93
- }
94
-
95
- #updateHolders(address, previousBalance) {
96
- if (this.#balances[address].toHexString() === '0x00') this.#holders -= 1
97
- else if (this.#balances[address].toHexString() !== '0x00' && previousBalance.toHexString() === '0x00') this.#holders += 1
98
- }
99
-
100
- #increaseBalance(address, amount) {
101
- if (!this.#balances[address]) this.#balances[address] = BigNumber.from(0)
102
- const previousBalance = this.#balances[address]
103
-
104
- this.#balances[address] = this.#balances[address].add(amount)
105
- this.#updateHolders(address, previousBalance)
106
- }
107
-
108
- #decreaseBalance(address, amount) {
109
- const previousBalance = this.#balances[address]
110
- this.#balances[address] = this.#balances[address].sub(amount)
111
- this.#updateHolders(address, previousBalance)
112
- }
113
-
114
- balanceOf(address) {
115
- return this.#balances[address]
116
- }
117
-
118
- setApproval(operator, amount) {
119
- const owner = globalThis.msg.sender
120
- if (!this.#approvals[owner]) this.#approvals[owner] = {}
121
- this.#approvals[owner][operator] = amount
122
- }
123
-
124
- approved(owner, operator, amount) {
125
- return this.#approvals[owner][operator] === amount
126
- }
127
-
128
- transfer(from, to, amount) {
129
- // TODO: is BigNumber?
130
- amount = BigNumber.from(amount)
131
- this.#beforeTransfer(from, to, amount)
132
- this.#decreaseBalance(from, amount)
133
- this.#increaseBalance(to, amount)
134
- }
135
- }
1
+ import Roles from './roles.js';
2
+
3
+ class Token extends Roles {
4
+ /**
5
+ * string
6
+ */
7
+ #name;
8
+ /**
9
+ * String
10
+ */
11
+ #symbol;
12
+ /**
13
+ * uint
14
+ */
15
+ #holders = 0;
16
+ /**
17
+ * Object => Object => uint
18
+ */
19
+ #balances = {};
20
+ /**
21
+ * Object => Object => uint
22
+ */
23
+ #approvals = {};
24
+ #decimals = 18;
25
+ #totalSupply = BigNumber.from(0);
26
+ // this.#privateField2 = 1
27
+ constructor(name, symbol, decimals = 18, state) {
28
+ if (!name)
29
+ throw new Error(`name undefined`);
30
+ if (!symbol)
31
+ throw new Error(`symbol undefined`);
32
+ super(state?.roles);
33
+ this.#name = name;
34
+ this.#symbol = symbol;
35
+ this.#decimals = decimals;
36
+ }
37
+ // enables snapshotting
38
+ // needs dev attention so nothing breaks after snapshot happens
39
+ // iow everything that is not static needs to be included in the stateObject
40
+ /**
41
+ * @return {Object} {holders, balances, ...}
42
+ */
43
+ get state() {
44
+ return {
45
+ ...super.state,
46
+ holders: this.holders,
47
+ balances: this.balances,
48
+ approvals: { ...this.#approvals },
49
+ totalSupply: this.totalSupply
50
+ };
51
+ }
52
+ get totalSupply() {
53
+ return this.#totalSupply;
54
+ }
55
+ get name() {
56
+ return this.#name;
57
+ }
58
+ get symbol() {
59
+ return this.#symbol;
60
+ }
61
+ get holders() {
62
+ return this.#holders;
63
+ }
64
+ get balances() {
65
+ return { ...this.#balances };
66
+ }
67
+ mint(to, amount) {
68
+ if (!this.hasRole(msg.sender, 'MINT'))
69
+ throw new Error('not allowed');
70
+ this.#totalSupply = this.#totalSupply.add(amount);
71
+ this.#increaseBalance(to, amount);
72
+ }
73
+ burn(from, amount) {
74
+ if (!this.hasRole(msg.sender, 'BURN'))
75
+ throw new Error('not allowed');
76
+ this.#totalSupply = this.#totalSupply.sub(amount);
77
+ this.#decreaseBalance(from, amount);
78
+ }
79
+ #beforeTransfer(from, to, amount) {
80
+ if (!this.#balances[from] || this.#balances[from] < amount)
81
+ throw new Error('amount exceeds balance');
82
+ }
83
+ #updateHolders(address, previousBalance) {
84
+ if (this.#balances[address].toHexString() === '0x00')
85
+ this.#holders -= 1;
86
+ else if (this.#balances[address].toHexString() !== '0x00' && previousBalance.toHexString() === '0x00')
87
+ this.#holders += 1;
88
+ }
89
+ #increaseBalance(address, amount) {
90
+ if (!this.#balances[address])
91
+ this.#balances[address] = BigNumber.from(0);
92
+ const previousBalance = this.#balances[address];
93
+ this.#balances[address] = this.#balances[address].add(amount);
94
+ this.#updateHolders(address, previousBalance);
95
+ }
96
+ #decreaseBalance(address, amount) {
97
+ const previousBalance = this.#balances[address];
98
+ this.#balances[address] = this.#balances[address].sub(amount);
99
+ this.#updateHolders(address, previousBalance);
100
+ }
101
+ balanceOf(address) {
102
+ return this.#balances[address];
103
+ }
104
+ setApproval(operator, amount) {
105
+ const owner = msg.sender;
106
+ if (!this.#approvals[owner])
107
+ this.#approvals[owner] = {};
108
+ this.#approvals[owner][operator] = amount;
109
+ }
110
+ approved(owner, operator, amount) {
111
+ return this.#approvals[owner][operator] === amount;
112
+ }
113
+ transfer(from, to, amount) {
114
+ // TODO: is BigNumber?
115
+ amount = BigNumber.from(amount);
116
+ this.#beforeTransfer(from, to, amount);
117
+ this.#decreaseBalance(from, amount);
118
+ this.#increaseBalance(to, amount);
119
+ }
120
+ }
121
+
122
+ export { Token as default };
package/package.json CHANGED
@@ -1,12 +1,22 @@
1
1
  {
2
2
  "name": "@leofcoin/standards",
3
- "version": "0.1.1",
3
+ "version": "0.1.2",
4
4
  "description": "Contract standards",
5
+ "type": "module",
5
6
  "exports": {
6
- "token": "./exports/token.js",
7
- "roles": "./exports/roles.js"
7
+ "./token": {
8
+ "import": "./exports/token.js",
9
+ "types": "./exports/token.d.ts"
10
+ },
11
+ "./roles": {
12
+ "import": "./exports/roles.js",
13
+ "types": "./exports/roles.d.ts"
14
+ },
15
+ "./token.js": "./exports/token.js",
16
+ "./roles.js": "./exports/roles.js"
8
17
  },
9
18
  "scripts": {
19
+ "build": "rollup -c",
10
20
  "test": "echo \"Error: no test specified\" && exit 1"
11
21
  },
12
22
  "repository": {
@@ -19,5 +29,14 @@
19
29
  "bugs": {
20
30
  "url": "https://github.com/ArteonToken/standards/issues"
21
31
  },
22
- "homepage": "https://github.com/ArteonToken/standards#readme"
32
+ "homepage": "https://github.com/ArteonToken/standards#readme",
33
+ "devDependencies": {
34
+ "@leofcoin/global-types": "^1.0.0",
35
+ "@rollup/plugin-typescript": "^11.0.0",
36
+ "rollup": "^3.17.1",
37
+ "tslib": "^2.5.0"
38
+ },
39
+ "dependencies": {
40
+ "@ethersproject/bignumber": "^5.7.0"
41
+ }
23
42
  }
@@ -0,0 +1,23 @@
1
+ import typescript from '@rollup/plugin-typescript'
2
+ import tsConfig from './tsconfig.json' assert { type: 'json'}
3
+ import { execSync } from 'child_process'
4
+
5
+
6
+
7
+
8
+ // const templates = (await readdir('./src/templates')).map(path => join('./src/templates', path))
9
+ const clean = () => {
10
+ execSync('rm -rf www/*.js')
11
+ return
12
+ }
13
+
14
+ export default [{
15
+ input: ['src/token.ts', 'src/roles.ts'],
16
+ output: {
17
+ dir: './exports',
18
+ format: 'es'
19
+ },
20
+ plugins: [
21
+ typescript(tsConfig)
22
+ ]
23
+ }]
package/src/roles.ts ADDED
@@ -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: address, role: string): boolean {
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: address, role: string): void {
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: address, role: string) {
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: address, role: string) {
73
+ if (!this.hasRole(address, 'OWNER')) throw new Error('Not allowed')
74
+
75
+ this.#grantRole(address, role)
76
+ }
77
+
78
+ revokeRole(address: address, role: string) {
79
+ if (!this.hasRole(address, 'OWNER')) throw new Error('Not allowed')
80
+
81
+ this.#revokeRole(address, role)
82
+ }
83
+ }
package/src/token.ts ADDED
@@ -0,0 +1,135 @@
1
+ import Roles from './roles.js'
2
+
3
+ export default class Token extends Roles {
4
+ /**
5
+ * string
6
+ */
7
+ #name: string;
8
+ /**
9
+ * String
10
+ */
11
+ #symbol: string;
12
+ /**
13
+ * uint
14
+ */
15
+ #holders = 0;
16
+ /**
17
+ * Object => Object => uint
18
+ */
19
+ #balances = {};
20
+ /**
21
+ * Object => Object => uint
22
+ */
23
+ #approvals = {};
24
+
25
+ #decimals = 18;
26
+
27
+ #totalSupply = BigNumber.from(0);
28
+
29
+ // this.#privateField2 = 1
30
+ constructor(name: string, symbol: string, decimals: number = 18, state: {roles?: {}}) {
31
+ if (!name) throw new Error(`name undefined`)
32
+ if (!symbol) throw new Error(`symbol undefined`)
33
+
34
+ super(state?.roles)
35
+
36
+ this.#name = name
37
+ this.#symbol = symbol
38
+ this.#decimals = decimals
39
+ }
40
+
41
+ // enables snapshotting
42
+ // needs dev attention so nothing breaks after snapshot happens
43
+ // iow everything that is not static needs to be included in the stateObject
44
+ /**
45
+ * @return {Object} {holders, balances, ...}
46
+ */
47
+ get state(): {} {
48
+ return {
49
+ ...super.state,
50
+ holders: this.holders,
51
+ balances: this.balances,
52
+ approvals: { ...this.#approvals },
53
+ totalSupply: this.totalSupply
54
+ }
55
+ }
56
+
57
+ get totalSupply(): BigNumber {
58
+ return this.#totalSupply
59
+ }
60
+
61
+ get name(): string {
62
+ return this.#name
63
+ }
64
+
65
+ get symbol(): string {
66
+ return this.#symbol
67
+ }
68
+
69
+ get holders(): {} {
70
+ return this.#holders
71
+ }
72
+
73
+ get balances(): {} {
74
+ return {...this.#balances}
75
+ }
76
+
77
+ mint(to: address, amount: BigNumber) {
78
+ if (!this.hasRole(msg.sender, 'MINT')) throw new Error('not allowed')
79
+
80
+ this.#totalSupply = this.#totalSupply.add(amount)
81
+ this.#increaseBalance(to, amount)
82
+ }
83
+
84
+ burn(from: address, amount: BigNumber) {
85
+ if (!this.hasRole(msg.sender, 'BURN')) throw new Error('not allowed')
86
+
87
+ this.#totalSupply = this.#totalSupply.sub(amount)
88
+ this.#decreaseBalance(from, amount)
89
+ }
90
+
91
+ #beforeTransfer(from: address, to: address, amount: BigNumber) {
92
+ if (!this.#balances[from] || this.#balances[from] < amount) throw new Error('amount exceeds balance')
93
+ }
94
+
95
+ #updateHolders(address: address, previousBalance: BigNumber) {
96
+ if (this.#balances[address].toHexString() === '0x00') this.#holders -= 1
97
+ else if (this.#balances[address].toHexString() !== '0x00' && previousBalance.toHexString() === '0x00') this.#holders += 1
98
+ }
99
+
100
+ #increaseBalance(address: address, amount: BigNumber) {
101
+ if (!this.#balances[address]) this.#balances[address] = BigNumber.from(0)
102
+ const previousBalance = this.#balances[address]
103
+
104
+ this.#balances[address] = this.#balances[address].add(amount)
105
+ this.#updateHolders(address, previousBalance)
106
+ }
107
+
108
+ #decreaseBalance(address: address, amount: BigNumber) {
109
+ const previousBalance = this.#balances[address]
110
+ this.#balances[address] = this.#balances[address].sub(amount)
111
+ this.#updateHolders(address, previousBalance)
112
+ }
113
+
114
+ balanceOf(address: address): BigNumber {
115
+ return this.#balances[address]
116
+ }
117
+
118
+ setApproval(operator: address, amount: BigNumber) {
119
+ const owner = msg.sender
120
+ if (!this.#approvals[owner]) this.#approvals[owner] = {}
121
+ this.#approvals[owner][operator] = amount
122
+ }
123
+
124
+ approved(owner: address, operator: address, amount: BigNumber): boolean {
125
+ return this.#approvals[owner][operator] === amount
126
+ }
127
+
128
+ transfer(from: address, to: address, amount: BigNumber) {
129
+ // TODO: is BigNumber?
130
+ amount = BigNumber.from(amount)
131
+ this.#beforeTransfer(from, to, amount)
132
+ this.#decreaseBalance(from, amount)
133
+ this.#increaseBalance(to, amount)
134
+ }
135
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,17 @@
1
+ {
2
+ "compilerOptions": {
3
+ "module": "es2022",
4
+ "target": "es2022",
5
+ "outDir": "./exports",
6
+ "moduleResolution":"NodeNext",
7
+ "allowJs": true,
8
+ "allowSyntheticDefaultImports": true,
9
+ "resolveJsonModule": true,
10
+ "declaration": true,
11
+ "declarationDir": "./exports"
12
+ },
13
+ "include": [
14
+ "./src/*",
15
+ "./node_modules/@leofcoin/global-types/*"
16
+ ]
17
+ }