@leofcoin/standards 0.1.0 → 0.1.1

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.
Files changed (2) hide show
  1. package/exports/token.js +135 -0
  2. package/package.json +1 -1
package/exports/token.js CHANGED
@@ -0,0 +1,135 @@
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
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@leofcoin/standards",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "description": "Contract standards",
5
5
  "exports": {
6
6
  "token": "./exports/token.js",