@leofcoin/contracts 0.1.2 → 0.1.4

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,51 +1 @@
1
- class Factory {
2
- /**
3
- * string
4
- */
5
- #name = 'ArtOnlineContractFactory';
6
- /**
7
- * uint
8
- */
9
- #totalContracts = 0;
10
- /**
11
- * Array => string
12
- */
13
- #contracts = [];
14
- constructor(state) {
15
- if (state) {
16
- this.#contracts = state.contracts;
17
- this.#totalContracts = state.totalContracts;
18
- }
19
- }
20
- get state() {
21
- return {
22
- totalContracts: this.#totalContracts,
23
- contracts: this.#contracts
24
- };
25
- }
26
- get name() {
27
- return this.#name;
28
- }
29
- get contracts() {
30
- return [...this.#contracts];
31
- }
32
- get totalContracts() {
33
- return this.#totalContracts;
34
- }
35
- isRegistered(address) {
36
- return this.#contracts.includes(address);
37
- }
38
- /**
39
- *
40
- * @param {Address} address contract address to register
41
- */
42
- async registerContract(address) {
43
- await msg.staticCall(address, 'hasRole', [msg.sender, 'OWNER']);
44
- if (this.#contracts.includes(address))
45
- throw new Error('already registered');
46
- this.#totalContracts += 1;
47
- this.#contracts.push(address);
48
- }
49
- }
50
-
51
- export { Factory as default };
1
+ class Factory{#name="ArtOnlineContractFactory";#totalContracts=0;#contracts=[];constructor(state){state&&(this.#contracts=state.contracts,this.#totalContracts=state.totalContracts)}get state(){return{totalContracts:this.#totalContracts,contracts:this.#contracts}}get name(){return this.#name}get contracts(){return[...this.#contracts]}get totalContracts(){return this.#totalContracts}isRegistered(address){return this.#contracts.includes(address)}async registerContract(address){if(await msg.staticCall(address,"hasRole",[msg.sender,"OWNER"]),this.#contracts.includes(address))throw new Error("already registered");this.#totalContracts+=1,this.#contracts.push(address)}}export{Factory as default};
@@ -1,5 +1,5 @@
1
1
  type registry = {
2
- name: {
2
+ name?: {
3
3
  address: address;
4
4
  owner: address;
5
5
  };
@@ -1,107 +1 @@
1
- class NameService {
2
- /**
3
- * string
4
- */
5
- #name = 'ArtOnlineNameService';
6
- /**
7
- * string
8
- */
9
- #owner;
10
- /**
11
- * number
12
- */
13
- #price = 0;
14
- /**
15
- * Object => string
16
- */
17
- #registry;
18
- /**
19
- * => string
20
- */
21
- #currency;
22
- get name() {
23
- return this.#name;
24
- }
25
- get registry() {
26
- return { ...this.#registry };
27
- }
28
- get state() {
29
- return {
30
- owner: this.#owner,
31
- registry: this.#registry,
32
- currency: this.#currency,
33
- price: this.#price
34
- };
35
- }
36
- // TODO: control with contract
37
- constructor(factoryAddress, currency, validatorAddress, price, state) {
38
- if (state) {
39
- this.#owner = state.owner;
40
- this.#registry = state.registry;
41
- this.#currency = state.currency;
42
- this.#price = state.price;
43
- }
44
- else {
45
- this.#owner = msg.sender;
46
- this.#price = price;
47
- this.#registry['ArtOnlineContractFactory'] = {
48
- owner: msg.sender,
49
- address: factoryAddress
50
- };
51
- this.#registry['ArtOnlineToken'] = {
52
- owner: msg.sender,
53
- address: currency
54
- };
55
- this.#registry['ArtOnlineValidators'] = {
56
- owner: msg.sender,
57
- address: validatorAddress
58
- };
59
- this.#currency = currency;
60
- }
61
- }
62
- changeOwner(owner) {
63
- if (msg.sender !== this.#owner)
64
- throw new Error('no owner');
65
- this.#owner = owner;
66
- }
67
- changePrice(price) {
68
- if (msg.sender !== this.#owner)
69
- throw new Error('no owner');
70
- this.#price = price;
71
- }
72
- changeCurrency(currency) {
73
- if (msg.sender !== this.#owner)
74
- throw new Error('no owner');
75
- this.#currency = currency;
76
- }
77
- async purchaseName(name, address) {
78
- const balance = await msg.call(this.#currency, 'balanceOf', [msg.sender]);
79
- if (balance < this.#price)
80
- throw new Error('price exceeds balance');
81
- try {
82
- await msg.call(this.#currency, 'transfer', [msg.sender, this.#owner, this.#price]);
83
- }
84
- catch (error) {
85
- throw error;
86
- }
87
- this.#registry[name] = {
88
- owner: msg.sender,
89
- address
90
- };
91
- }
92
- lookup(name) {
93
- return this.#registry[name];
94
- }
95
- transferOwnership(name, to) {
96
- if (msg.sender !== this.#registry[name].owner)
97
- throw new Error('not a owner');
98
- this.#registry[name].owner = to;
99
- }
100
- changeAddress(name, address) {
101
- if (msg.sender !== this.#registry[name].owner)
102
- throw new Error('not a owner');
103
- this.#registry[name].address = address;
104
- }
105
- }
106
-
107
- export { NameService as default };
1
+ class NameService{#name="ArtOnlineNameService";#owner;#price=0;#registry={};#currency;get name(){return this.#name}get registry(){return{...this.#registry}}get state(){return{owner:this.#owner,registry:this.#registry,currency:this.#currency,price:this.#price}}constructor(factoryAddress,currency,validatorAddress,price,state){state?(this.#owner=state.owner,this.#registry=state.registry,this.#currency=state.currency,this.#price=state.price):(this.#owner=msg.sender,this.#price=price,this.#registry.ArtOnlineContractFactory={owner:msg.sender,address:factoryAddress},this.#registry.ArtOnlineToken={owner:msg.sender,address:currency},this.#registry.ArtOnlineValidators={owner:msg.sender,address:validatorAddress},this.#currency=currency)}changeOwner(owner){if(msg.sender!==this.#owner)throw new Error("no owner");this.#owner=owner}changePrice(price){if(msg.sender!==this.#owner)throw new Error("no owner");this.#price=price}changeCurrency(currency){if(msg.sender!==this.#owner)throw new Error("no owner");this.#currency=currency}async purchaseName(name,address){if(await msg.call(this.#currency,"balanceOf",[msg.sender])<this.#price)throw new Error("price exceeds balance");try{await msg.call(this.#currency,"transfer",[msg.sender,this.#owner,this.#price])}catch(error){throw error}this.#registry[name]={owner:msg.sender,address:address}}lookup(name){return this.#registry[name]}transferOwnership(name,to){if(msg.sender!==this.#registry[name].owner)throw new Error("not a owner");this.#registry[name].owner=to}changeAddress(name,address){if(msg.sender!==this.#registry[name].owner)throw new Error("not a owner");this.#registry[name].address=address}}export{NameService as default};
@@ -1,205 +1 @@
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
- }
198
-
199
- class ArtOnline extends Token {
200
- constructor(state) {
201
- super('ArtOnline', 'ART', 18, state);
202
- }
203
- }
204
-
205
- export { ArtOnline as default };
1
+ class Roles{#roles={OWNER:[],MINT:[],BURN:[]};constructor(roles){if(roles){if(!(roles instanceof Object))throw new TypeError("expected roles to be an object");this.#roles={...roles,...this.#roles}}else this.#grantRole(msg.sender,"OWNER")}get state(){return{roles:this.roles}}get roles(){return{...this.#roles}}hasRole(address,role){return!!this.#roles[role]&&this.#roles[role].includes(address)}#grantRole(address,role){if(this.hasRole(address,role))throw new Error(`${role} role already granted for ${address}`);this.#roles[role].push(address)}#revokeRole(address,role){if(!this.hasRole(address,role))throw new Error(`${role} role already revoked for ${address}`);if("OWNER"===role&&1===this.#roles[role].length)throw new Error("atleast one owner is needed!");this.#roles[role].splice(this.#roles[role].indexOf(address))}grantRole(address,role){if(!this.hasRole(address,"OWNER"))throw new Error("Not allowed");this.#grantRole(address,role)}revokeRole(address,role){if(!this.hasRole(address,"OWNER"))throw new Error("Not allowed");this.#revokeRole(address,role)}}class Token extends Roles{#name;#symbol;#holders=0;#balances={};#approvals={};#decimals=18;#totalSupply=BigNumber.from(0);constructor(name,symbol,decimals=18,state){if(!name)throw new Error("name undefined");if(!symbol)throw new Error("symbol undefined");super(state?.roles),this.#name=name,this.#symbol=symbol,this.#decimals=decimals}get state(){return{...super.state,holders:this.holders,balances:this.balances,approvals:{...this.#approvals},totalSupply:this.totalSupply}}get totalSupply(){return this.#totalSupply}get name(){return this.#name}get symbol(){return this.#symbol}get holders(){return this.#holders}get balances(){return{...this.#balances}}mint(to,amount){if(!this.hasRole(msg.sender,"MINT"))throw new Error("not allowed");this.#totalSupply=this.#totalSupply.add(amount),this.#increaseBalance(to,amount)}burn(from,amount){if(!this.hasRole(msg.sender,"BURN"))throw new Error("not allowed");this.#totalSupply=this.#totalSupply.sub(amount),this.#decreaseBalance(from,amount)}#beforeTransfer(from,to,amount){if(!this.#balances[from]||this.#balances[from]<amount)throw new Error("amount exceeds balance")}#updateHolders(address,previousBalance){"0x00"===this.#balances[address].toHexString()?this.#holders-=1:"0x00"!==this.#balances[address].toHexString()&&"0x00"===previousBalance.toHexString()&&(this.#holders+=1)}#increaseBalance(address,amount){this.#balances[address]||(this.#balances[address]=BigNumber.from(0));const previousBalance=this.#balances[address];this.#balances[address]=this.#balances[address].add(amount),this.#updateHolders(address,previousBalance)}#decreaseBalance(address,amount){const previousBalance=this.#balances[address];this.#balances[address]=this.#balances[address].sub(amount),this.#updateHolders(address,previousBalance)}balanceOf(address){return this.#balances[address]}setApproval(operator,amount){const owner=msg.sender;this.#approvals[owner]||(this.#approvals[owner]={}),this.#approvals[owner][operator]=amount}approved(owner,operator,amount){return this.#approvals[owner][operator]===amount}transfer(from,to,amount){amount=BigNumber.from(amount),this.#beforeTransfer(from,to,amount),this.#decreaseBalance(from,amount),this.#increaseBalance(to,amount)}}class ArtOnline extends Token{constructor(state){super("ArtOnline","ART",18,state)}}export{ArtOnline as default};
@@ -1,205 +1 @@
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
- }
198
-
199
- class Power extends Token {
200
- constructor(state) {
201
- super('Power', 'PWR', 18, state);
202
- }
203
- }
204
-
205
- export { Power as default };
1
+ class Roles{#roles={OWNER:[],MINT:[],BURN:[]};constructor(roles){if(roles){if(!(roles instanceof Object))throw new TypeError("expected roles to be an object");this.#roles={...roles,...this.#roles}}else this.#grantRole(msg.sender,"OWNER")}get state(){return{roles:this.roles}}get roles(){return{...this.#roles}}hasRole(address,role){return!!this.#roles[role]&&this.#roles[role].includes(address)}#grantRole(address,role){if(this.hasRole(address,role))throw new Error(`${role} role already granted for ${address}`);this.#roles[role].push(address)}#revokeRole(address,role){if(!this.hasRole(address,role))throw new Error(`${role} role already revoked for ${address}`);if("OWNER"===role&&1===this.#roles[role].length)throw new Error("atleast one owner is needed!");this.#roles[role].splice(this.#roles[role].indexOf(address))}grantRole(address,role){if(!this.hasRole(address,"OWNER"))throw new Error("Not allowed");this.#grantRole(address,role)}revokeRole(address,role){if(!this.hasRole(address,"OWNER"))throw new Error("Not allowed");this.#revokeRole(address,role)}}class Token extends Roles{#name;#symbol;#holders=0;#balances={};#approvals={};#decimals=18;#totalSupply=BigNumber.from(0);constructor(name,symbol,decimals=18,state){if(!name)throw new Error("name undefined");if(!symbol)throw new Error("symbol undefined");super(state?.roles),this.#name=name,this.#symbol=symbol,this.#decimals=decimals}get state(){return{...super.state,holders:this.holders,balances:this.balances,approvals:{...this.#approvals},totalSupply:this.totalSupply}}get totalSupply(){return this.#totalSupply}get name(){return this.#name}get symbol(){return this.#symbol}get holders(){return this.#holders}get balances(){return{...this.#balances}}mint(to,amount){if(!this.hasRole(msg.sender,"MINT"))throw new Error("not allowed");this.#totalSupply=this.#totalSupply.add(amount),this.#increaseBalance(to,amount)}burn(from,amount){if(!this.hasRole(msg.sender,"BURN"))throw new Error("not allowed");this.#totalSupply=this.#totalSupply.sub(amount),this.#decreaseBalance(from,amount)}#beforeTransfer(from,to,amount){if(!this.#balances[from]||this.#balances[from]<amount)throw new Error("amount exceeds balance")}#updateHolders(address,previousBalance){"0x00"===this.#balances[address].toHexString()?this.#holders-=1:"0x00"!==this.#balances[address].toHexString()&&"0x00"===previousBalance.toHexString()&&(this.#holders+=1)}#increaseBalance(address,amount){this.#balances[address]||(this.#balances[address]=BigNumber.from(0));const previousBalance=this.#balances[address];this.#balances[address]=this.#balances[address].add(amount),this.#updateHolders(address,previousBalance)}#decreaseBalance(address,amount){const previousBalance=this.#balances[address];this.#balances[address]=this.#balances[address].sub(amount),this.#updateHolders(address,previousBalance)}balanceOf(address){return this.#balances[address]}setApproval(operator,amount){const owner=msg.sender;this.#approvals[owner]||(this.#approvals[owner]={}),this.#approvals[owner][operator]=amount}approved(owner,operator,amount){return this.#approvals[owner][operator]===amount}transfer(from,to,amount){amount=BigNumber.from(amount),this.#beforeTransfer(from,to,amount),this.#decreaseBalance(from,amount),this.#increaseBalance(to,amount)}}class Power extends Token{constructor(state){super("Power","PWR",18,state)}}export{Power as default};
@@ -1,197 +1 @@
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 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
- }
195
- }
196
-
197
- export { Validators as default };
1
+ class Roles{#roles={OWNER:[],MINT:[],BURN:[]};constructor(roles){if(roles){if(!(roles instanceof Object))throw new TypeError("expected roles to be an object");this.#roles={...roles,...this.#roles}}else this.#grantRole(msg.sender,"OWNER")}get state(){return{roles:this.roles}}get roles(){return{...this.#roles}}hasRole(address,role){return!!this.#roles[role]&&this.#roles[role].includes(address)}#grantRole(address,role){if(this.hasRole(address,role))throw new Error(`${role} role already granted for ${address}`);this.#roles[role].push(address)}#revokeRole(address,role){if(!this.hasRole(address,role))throw new Error(`${role} role already revoked for ${address}`);if("OWNER"===role&&1===this.#roles[role].length)throw new Error("atleast one owner is needed!");this.#roles[role].splice(this.#roles[role].indexOf(address))}grantRole(address,role){if(!this.hasRole(address,"OWNER"))throw new Error("Not allowed");this.#grantRole(address,role)}revokeRole(address,role){if(!this.hasRole(address,"OWNER"))throw new Error("Not allowed");this.#revokeRole(address,role)}}class Validators extends Roles{#name="ArtOnlineValidators";#totalValidators=0;#activeValidators=0;#validators={};#currency;#minimumBalance;get state(){return{...super.state,minimumBalance:this.#minimumBalance,currency:this.#currency,totalValidators:this.#totalValidators,activeValidators:this.#activeValidators,validators:this.#validators}}constructor(tokenAddress,state){super(state?.roles),state?(this.#minimumBalance=state.minimumBalance,this.#currency=state.currency,this.#totalValidators=state.totalValidators,this.#activeValidators=state.activeValidators,this.#validators=state.validators):(this.#minimumBalance=5e4,this.#currency=tokenAddress,this.#totalValidators+=1,this.#activeValidators+=1,this.#validators[msg.sender]={firstSeen:Date.now(),lastSeen:Date.now(),active:!0})}get name(){return this.#name}get currency(){return this.#currency}get validators(){return{...this.#validators}}get totalValidators(){return this.#totalValidators}get minimumBalance(){return this.#minimumBalance}changeCurrency(currency){if(!this.hasRole(msg.sender,"OWNER"))throw new Error("not an owner");this.#currency=currency}has(validator){return Boolean(void 0!==this.#validators[validator])}#isAllowed(address){if(msg.sender!==address&&!this.hasRole(msg.sender,"OWNER"))throw new Error("sender is not the validator or owner");return!0}async addValidator(validator){if(this.#isAllowed(validator),this.has(validator))throw new Error("already a validator");const balance=await msg.staticCall(this.currency,"balanceOf",[validator]);if(balance<this.minimumBalance)throw new Error(`balance to low! got: ${balance} need: ${this.#minimumBalance}`);this.#totalValidators+=1,this.#activeValidators+=1,this.#validators[validator]={firstSeen:Date.now(),lastSeen:Date.now(),active:!0}}removeValidator(validator){if(this.#isAllowed(validator),!this.has(validator))throw new Error("validator not found");this.#totalValidators-=1,this.#validators[validator].active&&(this.#activeValidators-=1),delete this.#validators[validator]}async updateValidator(validator,active){if(this.#isAllowed(validator),!this.has(validator))throw new Error("validator not found");const balance=await msg.staticCall(this.currency,"balanceOf",[validator]);if(balance<this.minimumBalance&&active)throw new Error(`balance to low! got: ${balance} need: ${this.#minimumBalance}`);if(this.#validators[validator].active===active)throw new Error("already "+(active?"activated":"deactivated"));active?this.#activeValidators+=1:this.#activeValidators-=1,this.#validators[validator].active=active}}export{Validators as default};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@leofcoin/contracts",
3
- "version": "0.1.2",
3
+ "version": "0.1.4",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "exports": {
@@ -31,6 +31,7 @@
31
31
  },
32
32
  "devDependencies": {
33
33
  "@rollup/plugin-node-resolve": "^15.0.1",
34
+ "@rollup/plugin-terser": "^0.4.0",
34
35
  "@rollup/plugin-typescript": "^11.0.0",
35
36
  "rollup": "^3.17.2",
36
37
  "tslib": "^2.5.0"
package/rollup.config.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import typescript from '@rollup/plugin-typescript'
2
2
  import tsConfig from './tsconfig.json' assert { type: 'json'}
3
-
3
+ import terser from '@rollup/plugin-terser'
4
4
  import resolve from '@rollup/plugin-node-resolve'
5
5
 
6
6
 
@@ -14,7 +14,10 @@ export default [{
14
14
  resolve({
15
15
  mainFields: ['exports']
16
16
  }),
17
- typescript(tsConfig)
17
+ typescript(tsConfig),
18
+ terser({
19
+ mangle: false
20
+ })
18
21
  ]
19
22
  }, {
20
23
  input: './src/name-service.ts',
@@ -26,7 +29,10 @@ export default [{
26
29
  resolve({
27
30
  mainFields: ['exports']
28
31
  }),
29
- typescript(tsConfig)
32
+ typescript(tsConfig),
33
+ terser({
34
+ mangle: false
35
+ })
30
36
  ]
31
37
  }, {
32
38
  input: './src/native-token.ts',
@@ -38,7 +44,10 @@ export default [{
38
44
  resolve({
39
45
  mainFields: ['exports']
40
46
  }),
41
- typescript(tsConfig)
47
+ typescript(tsConfig),
48
+ terser({
49
+ mangle: false
50
+ })
42
51
  ]
43
52
  }, {
44
53
  input: './src/power-token.ts',
@@ -50,7 +59,10 @@ export default [{
50
59
  resolve({
51
60
  mainFields: ['exports']
52
61
  }),
53
- typescript(tsConfig)
62
+ typescript(tsConfig),
63
+ terser({
64
+ mangle: false
65
+ })
54
66
  ]
55
67
  }, {
56
68
  input: './src/validators.ts',
@@ -62,6 +74,9 @@ export default [{
62
74
  resolve({
63
75
  mainFields: ['exports']
64
76
  }),
65
- typescript(tsConfig)
77
+ typescript(tsConfig),
78
+ terser({
79
+ mangle: false
80
+ })
66
81
  ]
67
82
  }]
@@ -1,5 +1,5 @@
1
1
  type registry = {
2
- name: {
2
+ name?: {
3
3
  address: address,
4
4
  owner: address
5
5
  }
@@ -21,7 +21,7 @@ export default class NameService {
21
21
  /**
22
22
  * Object => string
23
23
  */
24
- #registry: registry
24
+ #registry: registry = {}
25
25
 
26
26
  /**
27
27
  * => string
package/src/validators.ts CHANGED
@@ -16,9 +16,9 @@ export default class Validators extends Roles {
16
16
  */
17
17
  #validators = {}
18
18
 
19
- #currency
19
+ #currency: address
20
20
 
21
- #minimumBalance
21
+ #minimumBalance: BigNumberish
22
22
 
23
23
  get state() {
24
24
  return {
@@ -31,7 +31,7 @@ export default class Validators extends Roles {
31
31
  }
32
32
  }
33
33
 
34
- constructor(tokenAddress, state) {
34
+ constructor(tokenAddress: address, state) {
35
35
  super(state?.roles)
36
36
  if (state) {
37
37
  this.#minimumBalance = state.minimumBalance