@leofcoin/standards 0.1.3 → 0.1.5

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/.gitattributes ADDED
@@ -0,0 +1,2 @@
1
+ # Auto detect text files and perform LF normalization
2
+ * text=LF
package/.prettierrc ADDED
@@ -0,0 +1,8 @@
1
+ {
2
+ "tabWidth": 2,
3
+ "semi": false,
4
+ "singleQuote": true,
5
+ "printWidth": 120,
6
+ "trailingComma": "none",
7
+ "bracketSameLine": true
8
+ }
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
+
@@ -1,6 +1,8 @@
1
1
  export default class Roles {
2
2
  #private;
3
- constructor(roles: {});
3
+ constructor(roles: {
4
+ [index: string]: address[];
5
+ });
4
6
  /**
5
7
  *
6
8
  */
package/exports/roles.js CHANGED
@@ -3,9 +3,9 @@ class Roles {
3
3
  * Object => Array
4
4
  */
5
5
  #roles = {
6
- 'OWNER': [],
7
- 'MINT': [],
8
- 'BURN': []
6
+ OWNER: [],
7
+ MINT: [],
8
+ BURN: []
9
9
  };
10
10
  constructor(roles) {
11
11
  // allow devs to set their own roles but always keep the default ones included
@@ -1,22 +1,32 @@
1
1
  import Roles from './roles.js';
2
+ export declare type TokenState = {
3
+ roles: {
4
+ [index: string]: address[];
5
+ };
6
+ holders: number;
7
+ balances: {
8
+ [index: string]: BigNumberish;
9
+ };
10
+ approvals: {};
11
+ totalSupply: BigNumberish;
12
+ };
2
13
  export default class Token extends Roles {
3
14
  #private;
4
- constructor(name: string, symbol: string, decimals: number, state: {
5
- roles?: {};
6
- });
15
+ constructor(name: string, symbol: string, decimals?: number, state?: TokenState);
7
16
  /**
8
17
  * @return {Object} {holders, balances, ...}
9
18
  */
10
19
  get state(): {};
11
- get totalSupply(): any;
20
+ get totalSupply(): BigNumberish;
12
21
  get name(): string;
13
22
  get symbol(): string;
14
- get holders(): number;
23
+ get holders(): {};
15
24
  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;
25
+ get decimals(): number;
26
+ mint(to: address, amount: BigNumberish): void;
27
+ burn(from: address, amount: BigNumberish): void;
28
+ balanceOf(address: address): BigNumberish;
29
+ setApproval(operator: address, amount: BigNumberish): void;
30
+ approved(owner: address, operator: address, amount: BigNumberish): boolean;
31
+ transfer(from: address, to: address, amount: BigNumberish): void;
22
32
  }
package/exports/token.js CHANGED
@@ -12,7 +12,7 @@ class Token extends Roles {
12
12
  /**
13
13
  * uint
14
14
  */
15
- #holders = 0;
15
+ #holders = BigNumber['from'](0);
16
16
  /**
17
17
  * Object => Object => uint
18
18
  */
@@ -22,7 +22,7 @@ class Token extends Roles {
22
22
  */
23
23
  #approvals = {};
24
24
  #decimals = 18;
25
- #totalSupply = BigNumber.from(0);
25
+ #totalSupply = BigNumber['from'](0);
26
26
  // this.#privateField2 = 1
27
27
  constructor(name, symbol, decimals = 18, state) {
28
28
  if (!name)
@@ -30,9 +30,17 @@ class Token extends Roles {
30
30
  if (!symbol)
31
31
  throw new Error(`symbol undefined`);
32
32
  super(state?.roles);
33
- this.#name = name;
34
- this.#symbol = symbol;
35
- this.#decimals = decimals;
33
+ if (state) {
34
+ this.#holders = BigNumber['from'](state.holders);
35
+ this.#balances = BigNumber['from'](state.balances);
36
+ this.#approvals = BigNumber['from'](state.approvals);
37
+ this.#totalSupply = BigNumber['from'](state.totalSupply);
38
+ }
39
+ else {
40
+ this.#name = name;
41
+ this.#symbol = symbol;
42
+ this.#decimals = decimals;
43
+ }
36
44
  }
37
45
  // enables snapshotting
38
46
  // needs dev attention so nothing breaks after snapshot happens
@@ -64,6 +72,9 @@ class Token extends Roles {
64
72
  get balances() {
65
73
  return { ...this.#balances };
66
74
  }
75
+ get decimals() {
76
+ return this.#decimals;
77
+ }
67
78
  mint(to, amount) {
68
79
  if (!this.hasRole(msg.sender, 'MINT'))
69
80
  throw new Error('not allowed');
@@ -82,13 +93,13 @@ class Token extends Roles {
82
93
  }
83
94
  #updateHolders(address, previousBalance) {
84
95
  if (this.#balances[address].toHexString() === '0x00')
85
- this.#holders -= 1;
96
+ this.#holders.sub(1);
86
97
  else if (this.#balances[address].toHexString() !== '0x00' && previousBalance.toHexString() === '0x00')
87
- this.#holders += 1;
98
+ this.#holders.add(1);
88
99
  }
89
100
  #increaseBalance(address, amount) {
90
101
  if (!this.#balances[address])
91
- this.#balances[address] = BigNumber.from(0);
102
+ this.#balances[address] = BigNumber['from'](0);
92
103
  const previousBalance = this.#balances[address];
93
104
  this.#balances[address] = this.#balances[address].add(amount);
94
105
  this.#updateHolders(address, previousBalance);
@@ -112,7 +123,7 @@ class Token extends Roles {
112
123
  }
113
124
  transfer(from, to, amount) {
114
125
  // TODO: is BigNumber?
115
- amount = BigNumber.from(amount);
126
+ amount = BigNumber['from'](amount);
116
127
  this.#beforeTransfer(from, to, amount);
117
128
  this.#decreaseBalance(from, amount);
118
129
  this.#increaseBalance(to, amount);
package/package.json CHANGED
@@ -1,46 +1,46 @@
1
- {
2
- "name": "@leofcoin/standards",
3
- "version": "0.1.3",
4
- "description": "Contract standards",
5
- "type": "module",
6
- "exports": {
7
- ".": {
8
- "import": "./exports/index.js",
9
- "types": "./exports/index.d.ts"
10
- },
11
- "./token": {
12
- "import": "./exports/token.js",
13
- "types": "./exports/token.d.ts"
14
- },
15
- "./roles": {
16
- "import": "./exports/roles.js",
17
- "types": "./exports/roles.d.ts"
18
- },
19
- "./token.js": "./exports/token.js",
20
- "./roles.js": "./exports/roles.js"
21
- },
22
- "scripts": {
23
- "build": "rollup -c",
24
- "test": "echo \"Error: no test specified\" && exit 1"
25
- },
26
- "repository": {
27
- "type": "git",
28
- "url": "git+https://github.com/ArteonToken/standards.git"
29
- },
30
- "keywords": [],
31
- "author": "",
32
- "license": "MIT",
33
- "bugs": {
34
- "url": "https://github.com/ArteonToken/standards/issues"
35
- },
36
- "homepage": "https://github.com/ArteonToken/standards#readme",
37
- "devDependencies": {
38
- "@leofcoin/global-types": "^1.0.0",
39
- "@rollup/plugin-typescript": "^11.0.0",
40
- "rollup": "^3.17.1",
41
- "tslib": "^2.5.0"
42
- },
43
- "dependencies": {
44
- "@ethersproject/bignumber": "^5.7.0"
45
- }
46
- }
1
+ {
2
+ "name": "@leofcoin/standards",
3
+ "version": "0.1.5",
4
+ "description": "Contract standards",
5
+ "type": "module",
6
+ "exports": {
7
+ ".": {
8
+ "import": "./exports/index.js",
9
+ "types": "./exports/index.d.ts"
10
+ },
11
+ "./token": {
12
+ "import": "./exports/token.js",
13
+ "types": "./exports/token.d.ts"
14
+ },
15
+ "./roles": {
16
+ "import": "./exports/roles.js",
17
+ "types": "./exports/roles.d.ts"
18
+ },
19
+ "./token.js": "./exports/token.js",
20
+ "./roles.js": "./exports/roles.js"
21
+ },
22
+ "scripts": {
23
+ "build": "rollup -c",
24
+ "test": "echo \"Error: no test specified\" && exit 1"
25
+ },
26
+ "repository": {
27
+ "type": "git",
28
+ "url": "git+https://github.com/ArteonToken/standards.git"
29
+ },
30
+ "keywords": [],
31
+ "author": "",
32
+ "license": "MIT",
33
+ "bugs": {
34
+ "url": "https://github.com/ArteonToken/standards/issues"
35
+ },
36
+ "homepage": "https://github.com/ArteonToken/standards#readme",
37
+ "devDependencies": {
38
+ "@leofcoin/global-types": "^1.0.0",
39
+ "@rollup/plugin-typescript": "^11.0.0",
40
+ "rollup": "^3.17.1",
41
+ "tslib": "^2.5.0"
42
+ },
43
+ "dependencies": {
44
+ "@ethersproject/bignumber": "^5.7.0"
45
+ }
46
+ }
package/rollup.config.js CHANGED
@@ -1,23 +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/index.ts', 'src/token.ts', 'src/roles.ts'],
16
- output: {
17
- dir: './exports',
18
- format: 'es'
19
- },
20
- plugins: [
21
- typescript(tsConfig)
22
- ]
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/index.ts', 'src/token.ts', 'src/roles.ts'],
16
+ output: {
17
+ dir: './exports',
18
+ format: 'es'
19
+ },
20
+ plugins: [
21
+ typescript(tsConfig)
22
+ ]
23
+ }]
package/src/index.ts CHANGED
@@ -1,2 +1,2 @@
1
- export { default as Token } from './token.js'
2
- export { default as Roles } from './roles.js'
1
+ export { default as Token } from './token.js'
2
+ export { default as Roles } from './roles.js'
package/src/roles.ts CHANGED
@@ -1,83 +1,82 @@
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
- }
1
+ export default class Roles {
2
+ /**
3
+ * Object => Array
4
+ */
5
+ #roles = {
6
+ OWNER: [],
7
+ MINT: [],
8
+ BURN: []
9
+ }
10
+
11
+ constructor(roles: { [index: string]: address[] }) {
12
+ // allow devs to set their own roles but always keep the default ones included
13
+ // also allows roles to be loaded from the stateStore
14
+ // carefull when including the roles make sure to add the owner
15
+ // because no roles are granted by default when using custom roles
16
+ if (roles) {
17
+ if (roles instanceof Object) {
18
+ this.#roles = { ...roles, ...this.#roles }
19
+ } else {
20
+ throw new TypeError(`expected roles to be an object`)
21
+ }
22
+ } else {
23
+ // no roles given so fallback to default to the msg sender
24
+ this.#grantRole(msg.sender, 'OWNER')
25
+ }
26
+ }
27
+
28
+ /**
29
+ *
30
+ */
31
+ get state(): {} {
32
+ return { roles: this.roles }
33
+ }
34
+
35
+ get roles(): {} {
36
+ return { ...this.#roles }
37
+ }
38
+ /**
39
+ * @param {address} address
40
+ * @param {string} role
41
+ * @returns true | false
42
+ */
43
+ hasRole(address: address, role: string): boolean {
44
+ return this.#roles[role] ? this.#roles[role].includes(address) : false
45
+ }
46
+
47
+ /**
48
+ * @private
49
+ * @param {address} address address to grant the role to
50
+ * @param {string} role role to give
51
+ */
52
+ #grantRole(address: address, role: string): void {
53
+ if (this.hasRole(address, role)) throw new Error(`${role} role already granted for ${address}`)
54
+
55
+ this.#roles[role].push(address)
56
+ }
57
+
58
+ /**
59
+ * remove role for address
60
+ * @private
61
+ * @param {address} address address to revoke role from
62
+ * @param {string} role role to evoke
63
+ */
64
+ #revokeRole(address: address, role: string) {
65
+ if (!this.hasRole(address, role)) throw new Error(`${role} role already revoked for ${address}`)
66
+ if (role === 'OWNER' && this.#roles[role].length === 1) throw new Error(`atleast one owner is needed!`)
67
+
68
+ this.#roles[role].splice(this.#roles[role].indexOf(address))
69
+ }
70
+
71
+ grantRole(address: address, role: string) {
72
+ if (!this.hasRole(address, 'OWNER')) throw new Error('Not allowed')
73
+
74
+ this.#grantRole(address, role)
75
+ }
76
+
77
+ revokeRole(address: address, role: string) {
78
+ if (!this.hasRole(address, 'OWNER')) throw new Error('Not allowed')
79
+
80
+ this.#revokeRole(address, role)
81
+ }
82
+ }
package/src/token.ts CHANGED
@@ -1,135 +1,155 @@
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
- }
1
+ import Roles from './roles.js'
2
+
3
+ export declare type TokenState = {
4
+ roles: { [index: string]: address[] }
5
+ holders: number
6
+ balances: { [index: string]: BigNumberish }
7
+ approvals: {}
8
+ totalSupply: BigNumberish
9
+ }
10
+
11
+ export default class Token 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 = {}
32
+
33
+ #decimals = 18
34
+
35
+ #totalSupply = BigNumber['from'](0)
36
+
37
+ // this.#privateField2 = 1
38
+ constructor(name: string, symbol: string, decimals: number = 18, state?: TokenState) {
39
+ if (!name) throw new Error(`name undefined`)
40
+ if (!symbol) throw new Error(`symbol undefined`)
41
+
42
+ super(state?.roles)
43
+
44
+ if (state) {
45
+ this.#holders = BigNumber['from'](state.holders)
46
+ this.#balances = BigNumber['from'](state.balances)
47
+ this.#approvals = BigNumber['from'](state.approvals)
48
+ this.#totalSupply = BigNumber['from'](state.totalSupply)
49
+ } else {
50
+ this.#name = name
51
+ this.#symbol = symbol
52
+ this.#decimals = decimals
53
+ }
54
+ }
55
+
56
+ // enables snapshotting
57
+ // needs dev attention so nothing breaks after snapshot happens
58
+ // iow everything that is not static needs to be included in the stateObject
59
+ /**
60
+ * @return {Object} {holders, balances, ...}
61
+ */
62
+ get state(): {} {
63
+ return {
64
+ ...super.state,
65
+ holders: this.holders,
66
+ balances: this.balances,
67
+ approvals: { ...this.#approvals },
68
+ totalSupply: this.totalSupply
69
+ }
70
+ }
71
+
72
+ get totalSupply(): BigNumberish {
73
+ return this.#totalSupply
74
+ }
75
+
76
+ get name(): string {
77
+ return this.#name
78
+ }
79
+
80
+ get symbol(): string {
81
+ return this.#symbol
82
+ }
83
+
84
+ get holders(): {} {
85
+ return this.#holders
86
+ }
87
+
88
+ get balances(): {} {
89
+ return { ...this.#balances }
90
+ }
91
+
92
+ get decimals() {
93
+ return this.#decimals
94
+ }
95
+
96
+ mint(to: address, amount: BigNumberish) {
97
+ if (!this.hasRole(msg.sender, 'MINT')) throw new Error('not allowed')
98
+
99
+ this.#totalSupply = this.#totalSupply.add(amount)
100
+ this.#increaseBalance(to, amount)
101
+ }
102
+
103
+ burn(from: address, amount: BigNumberish) {
104
+ if (!this.hasRole(msg.sender, 'BURN')) throw new Error('not allowed')
105
+
106
+ this.#totalSupply = this.#totalSupply.sub(amount)
107
+ this.#decreaseBalance(from, amount)
108
+ }
109
+
110
+ #beforeTransfer(from: address, to: address, amount: BigNumberish) {
111
+ if (!this.#balances[from] || this.#balances[from] < amount) throw new Error('amount exceeds balance')
112
+ }
113
+
114
+ #updateHolders(address: address, previousBalance: typeof BigNumber) {
115
+ if (this.#balances[address].toHexString() === '0x00') this.#holders.sub(1)
116
+ else if (this.#balances[address].toHexString() !== '0x00' && previousBalance.toHexString() === '0x00')
117
+ this.#holders.add(1)
118
+ }
119
+
120
+ #increaseBalance(address: address, amount: BigNumberish) {
121
+ if (!this.#balances[address]) this.#balances[address] = BigNumber['from'](0)
122
+ const previousBalance = this.#balances[address]
123
+
124
+ this.#balances[address] = this.#balances[address].add(amount)
125
+ this.#updateHolders(address, previousBalance)
126
+ }
127
+
128
+ #decreaseBalance(address: address, amount: BigNumberish) {
129
+ const previousBalance = this.#balances[address]
130
+ this.#balances[address] = this.#balances[address].sub(amount)
131
+ this.#updateHolders(address, previousBalance)
132
+ }
133
+
134
+ balanceOf(address: address): BigNumberish {
135
+ return this.#balances[address]
136
+ }
137
+
138
+ setApproval(operator: address, amount: BigNumberish) {
139
+ const owner = msg.sender
140
+ if (!this.#approvals[owner]) this.#approvals[owner] = {}
141
+ this.#approvals[owner][operator] = amount
142
+ }
143
+
144
+ approved(owner: address, operator: address, amount: BigNumberish): boolean {
145
+ return this.#approvals[owner][operator] === amount
146
+ }
147
+
148
+ transfer(from: address, to: address, amount: BigNumberish) {
149
+ // TODO: is BigNumber?
150
+ amount = BigNumber['from'](amount)
151
+ this.#beforeTransfer(from, to, amount)
152
+ this.#decreaseBalance(from, amount)
153
+ this.#increaseBalance(to, amount)
154
+ }
155
+ }
package/tsconfig.json CHANGED
@@ -1,17 +1,14 @@
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
- ]
1
+ {
2
+ "compilerOptions": {
3
+ "module": "NodeNext",
4
+ "target": "es2022",
5
+ "outDir": "./exports",
6
+ "moduleResolution":"NodeNext",
7
+ "declaration": true,
8
+ "declarationDir": "./exports"
9
+ },
10
+ "include": [
11
+ "./src/*",
12
+ "./node_modules/@leofcoin/global-types/*"
13
+ ]
17
14
  }