@leofcoin/contracts 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.
- package/LICENSE +20 -20
- package/exports/factory.js +56 -52
- package/exports/name-service.js +106 -106
- package/exports/native-token.js +7 -7
- package/exports/power-token.js +7 -7
- package/exports/validators.js +129 -129
- package/package.json +1 -1
package/LICENSE
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2022 vandeurenglenn
|
|
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
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022 vandeurenglenn
|
|
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
21
|
SOFTWARE.
|
package/exports/factory.js
CHANGED
|
@@ -1,52 +1,56 @@
|
|
|
1
|
-
export default class Factory {
|
|
2
|
-
/**
|
|
3
|
-
* string
|
|
4
|
-
*/
|
|
5
|
-
#name = 'ArtOnlineContractFactory'
|
|
6
|
-
/**
|
|
7
|
-
* uint
|
|
8
|
-
*/
|
|
9
|
-
#totalContracts = 0
|
|
10
|
-
/**
|
|
11
|
-
* Array => string
|
|
12
|
-
*/
|
|
13
|
-
#contracts = []
|
|
14
|
-
|
|
15
|
-
constructor(state) {
|
|
16
|
-
if (state) {
|
|
17
|
-
this.#contracts = state.contracts
|
|
18
|
-
this.#totalContracts = state.totalContracts
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
get state() {
|
|
23
|
-
return {
|
|
24
|
-
totalContracts: this.#totalContracts,
|
|
25
|
-
contracts: this.#contracts
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
get name() {
|
|
30
|
-
return this.#name
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
get contracts() {
|
|
34
|
-
return [...this.#contracts]
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
get totalContracts() {
|
|
38
|
-
return this.#totalContracts
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
1
|
+
export default class Factory {
|
|
2
|
+
/**
|
|
3
|
+
* string
|
|
4
|
+
*/
|
|
5
|
+
#name = 'ArtOnlineContractFactory'
|
|
6
|
+
/**
|
|
7
|
+
* uint
|
|
8
|
+
*/
|
|
9
|
+
#totalContracts = 0
|
|
10
|
+
/**
|
|
11
|
+
* Array => string
|
|
12
|
+
*/
|
|
13
|
+
#contracts = []
|
|
14
|
+
|
|
15
|
+
constructor(state) {
|
|
16
|
+
if (state) {
|
|
17
|
+
this.#contracts = state.contracts
|
|
18
|
+
this.#totalContracts = state.totalContracts
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
get state() {
|
|
23
|
+
return {
|
|
24
|
+
totalContracts: this.#totalContracts,
|
|
25
|
+
contracts: this.#contracts
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
get name() {
|
|
30
|
+
return this.#name
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
get contracts() {
|
|
34
|
+
return [...this.#contracts]
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
get totalContracts() {
|
|
38
|
+
return this.#totalContracts
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
isRegistered(address) {
|
|
42
|
+
return this.#contracts.includes(address)
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
*
|
|
47
|
+
* @param {Address} address contract address to register
|
|
48
|
+
*/
|
|
49
|
+
async registerContract(address) {
|
|
50
|
+
await msg.staticCall(address, 'hasRole', [msg.sender, 'OWNER'])
|
|
51
|
+
if (this.#contracts.includes(address)) throw new Error('already registered')
|
|
52
|
+
|
|
53
|
+
this.#totalContracts += 1
|
|
54
|
+
this.#contracts.push(address)
|
|
55
|
+
}
|
|
56
|
+
}
|
package/exports/name-service.js
CHANGED
|
@@ -1,106 +1,106 @@
|
|
|
1
|
-
export default 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
|
-
/**
|
|
20
|
-
* => string
|
|
21
|
-
*/
|
|
22
|
-
#currency
|
|
23
|
-
|
|
24
|
-
get name() {
|
|
25
|
-
return this.#name
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
get registry() {
|
|
29
|
-
return {...this.#registry}
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
get state() {}
|
|
33
|
-
|
|
34
|
-
// TODO: control with contract
|
|
35
|
-
constructor(factoryAddress, currency, validatorAddress, price, state) {
|
|
36
|
-
if (state) {
|
|
37
|
-
this.#owner = state.owner
|
|
38
|
-
this.#registry = state.registry
|
|
39
|
-
this.#currency = state.currency
|
|
40
|
-
this.#price = state.price
|
|
41
|
-
} else {
|
|
42
|
-
this.#owner = msg.sender
|
|
43
|
-
this.#price = price
|
|
44
|
-
this.#registry['ArtOnlineContractFactory'] = {
|
|
45
|
-
owner: msg.sender,
|
|
46
|
-
address: factoryAddress
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
this.#registry['ArtOnlineToken'] = {
|
|
50
|
-
owner: msg.sender,
|
|
51
|
-
address: currency
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
this.#registry['ArtOnlineValidators'] = {
|
|
55
|
-
owner: msg.sender,
|
|
56
|
-
address: validatorAddress
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
this.#currency = currency
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
changeOwner(owner) {
|
|
64
|
-
if (msg.sender !== this.#owner) throw new Error('no owner')
|
|
65
|
-
this.#owner = owner
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
changePrice(price) {
|
|
69
|
-
if (msg.sender !== this.#owner) throw new Error('no owner')
|
|
70
|
-
this.#price = price
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
changeCurrency(currency) {
|
|
74
|
-
if (msg.sender !== this.#owner) throw new Error('no owner')
|
|
75
|
-
this.#currency = currency
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
async purchaseName(name, address) {
|
|
79
|
-
const balance = await msg.call(this.#currency, 'balanceOf', [msg.sender])
|
|
80
|
-
if (balance < this.#price) throw new Error('price exceeds balance')
|
|
81
|
-
try {
|
|
82
|
-
await msg.call(this.#currency, 'transfer', [msg.sender, this.#owner, this.#price])
|
|
83
|
-
} catch (error) {
|
|
84
|
-
throw error
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
this.#registry[name] = {
|
|
88
|
-
owner: msg.sender,
|
|
89
|
-
address
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
lookup(name) {
|
|
94
|
-
return this.#registry[name]
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
transferOwnership(name, to) {
|
|
98
|
-
if (msg.sender !== this.#registry.owner) throw new Error('not a owner')
|
|
99
|
-
this.#registry[name].owner = to
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
changeAddress(name, address) {
|
|
103
|
-
if (msg.sender !== this.#registry.owner) throw new Error('not a owner')
|
|
104
|
-
this.#registry[name].address = address
|
|
105
|
-
}
|
|
106
|
-
}
|
|
1
|
+
export default 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
|
+
/**
|
|
20
|
+
* => string
|
|
21
|
+
*/
|
|
22
|
+
#currency
|
|
23
|
+
|
|
24
|
+
get name() {
|
|
25
|
+
return this.#name
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
get registry() {
|
|
29
|
+
return {...this.#registry}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
get state() {}
|
|
33
|
+
|
|
34
|
+
// TODO: control with contract
|
|
35
|
+
constructor(factoryAddress, currency, validatorAddress, price, state) {
|
|
36
|
+
if (state) {
|
|
37
|
+
this.#owner = state.owner
|
|
38
|
+
this.#registry = state.registry
|
|
39
|
+
this.#currency = state.currency
|
|
40
|
+
this.#price = state.price
|
|
41
|
+
} else {
|
|
42
|
+
this.#owner = msg.sender
|
|
43
|
+
this.#price = price
|
|
44
|
+
this.#registry['ArtOnlineContractFactory'] = {
|
|
45
|
+
owner: msg.sender,
|
|
46
|
+
address: factoryAddress
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
this.#registry['ArtOnlineToken'] = {
|
|
50
|
+
owner: msg.sender,
|
|
51
|
+
address: currency
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
this.#registry['ArtOnlineValidators'] = {
|
|
55
|
+
owner: msg.sender,
|
|
56
|
+
address: validatorAddress
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
this.#currency = currency
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
changeOwner(owner) {
|
|
64
|
+
if (msg.sender !== this.#owner) throw new Error('no owner')
|
|
65
|
+
this.#owner = owner
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
changePrice(price) {
|
|
69
|
+
if (msg.sender !== this.#owner) throw new Error('no owner')
|
|
70
|
+
this.#price = price
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
changeCurrency(currency) {
|
|
74
|
+
if (msg.sender !== this.#owner) throw new Error('no owner')
|
|
75
|
+
this.#currency = currency
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
async purchaseName(name, address) {
|
|
79
|
+
const balance = await msg.call(this.#currency, 'balanceOf', [msg.sender])
|
|
80
|
+
if (balance < this.#price) throw new Error('price exceeds balance')
|
|
81
|
+
try {
|
|
82
|
+
await msg.call(this.#currency, 'transfer', [msg.sender, this.#owner, this.#price])
|
|
83
|
+
} catch (error) {
|
|
84
|
+
throw error
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
this.#registry[name] = {
|
|
88
|
+
owner: msg.sender,
|
|
89
|
+
address
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
lookup(name) {
|
|
94
|
+
return this.#registry[name]
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
transferOwnership(name, to) {
|
|
98
|
+
if (msg.sender !== this.#registry.owner) throw new Error('not a owner')
|
|
99
|
+
this.#registry[name].owner = to
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
changeAddress(name, address) {
|
|
103
|
+
if (msg.sender !== this.#registry.owner) throw new Error('not a owner')
|
|
104
|
+
this.#registry[name].address = address
|
|
105
|
+
}
|
|
106
|
+
}
|
package/exports/native-token.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import Token from '@leofcoin/standards/token.js'
|
|
2
|
-
|
|
3
|
-
export default class ArtOnline extends Token {
|
|
4
|
-
constructor(state) {
|
|
5
|
-
super('ArtOnline', 'ART', 18, state)
|
|
6
|
-
}
|
|
7
|
-
}
|
|
1
|
+
import Token from '@leofcoin/standards/token.js'
|
|
2
|
+
|
|
3
|
+
export default class ArtOnline extends Token {
|
|
4
|
+
constructor(state) {
|
|
5
|
+
super('ArtOnline', 'ART', 18, state)
|
|
6
|
+
}
|
|
7
|
+
}
|
package/exports/power-token.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import Token from '@leofcoin/standards/token.js'
|
|
2
|
-
|
|
3
|
-
export default class Power extends Token {
|
|
4
|
-
constructor(state) {
|
|
5
|
-
super('Power', 'PWR', 18, state)
|
|
6
|
-
}
|
|
7
|
-
}
|
|
1
|
+
import Token from '@leofcoin/standards/token.js'
|
|
2
|
+
|
|
3
|
+
export default class Power extends Token {
|
|
4
|
+
constructor(state) {
|
|
5
|
+
super('Power', 'PWR', 18, state)
|
|
6
|
+
}
|
|
7
|
+
}
|
package/exports/validators.js
CHANGED
|
@@ -1,129 +1,129 @@
|
|
|
1
|
-
import Roles from '@lefocoin/standards/roles.js'
|
|
2
|
-
|
|
3
|
-
export default class Validators extends Roles {
|
|
4
|
-
/**
|
|
5
|
-
* string
|
|
6
|
-
*/
|
|
7
|
-
#name = 'ArtOnlineValidators'
|
|
8
|
-
/**
|
|
9
|
-
* uint
|
|
10
|
-
*/
|
|
11
|
-
#totalValidators = 0
|
|
12
|
-
|
|
13
|
-
#activeValidators = 0
|
|
14
|
-
/**
|
|
15
|
-
* Object => string(address) => Object
|
|
16
|
-
*/
|
|
17
|
-
#validators = {}
|
|
18
|
-
|
|
19
|
-
#currency
|
|
20
|
-
|
|
21
|
-
#minimumBalance
|
|
22
|
-
|
|
23
|
-
get state() {
|
|
24
|
-
return {
|
|
25
|
-
...super.state,
|
|
26
|
-
minimumBalance: this.#minimumBalance,
|
|
27
|
-
currency: this.#currency,
|
|
28
|
-
totalValidators: this.#totalValidators,
|
|
29
|
-
activeValidators: this.#activeValidators,
|
|
30
|
-
validators: this.#validators
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
constructor(tokenAddress, state) {
|
|
35
|
-
super(state?.roles)
|
|
36
|
-
if (state) {
|
|
37
|
-
this.#minimumBalance = state.minimumBalance
|
|
38
|
-
this.#currency = state.currency
|
|
39
|
-
|
|
40
|
-
this.#totalValidators = state.totalValidators
|
|
41
|
-
this.#activeValidators = state.activeValidators
|
|
42
|
-
this.#validators = state.validators
|
|
43
|
-
} else {
|
|
44
|
-
this.#minimumBalance = 50_000
|
|
45
|
-
this.#currency = tokenAddress
|
|
46
|
-
|
|
47
|
-
this.#totalValidators += 1
|
|
48
|
-
this.#activeValidators += 1
|
|
49
|
-
this.#validators[msg.sender] = {
|
|
50
|
-
firstSeen: Date.now(),
|
|
51
|
-
lastSeen: Date.now(),
|
|
52
|
-
active: true
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
get name() {
|
|
59
|
-
return this.#name
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
get currency() {
|
|
63
|
-
return this.#currency
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
get validators() {
|
|
67
|
-
return {...this.#validators}
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
get totalValidators() {
|
|
71
|
-
return this.#totalValidators
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
get minimumBalance() {
|
|
75
|
-
return this.#minimumBalance
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
changeCurrency(currency) {
|
|
79
|
-
if (!this.hasRole(msg.sender, 'OWNER')) throw new Error('not an owner')
|
|
80
|
-
this.#currency = currency
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
has(validator) {
|
|
84
|
-
return Boolean(this.#validators[validator] !== undefined)
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
#isAllowed(address) {
|
|
88
|
-
if (msg.sender !== address && !this.hasRole(msg.sender, 'OWNER')) throw new Error('sender is not the validator or owner')
|
|
89
|
-
return true
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
async addValidator(validator) {
|
|
93
|
-
this.#isAllowed(validator)
|
|
94
|
-
if (this.has(validator)) throw new Error('already a validator')
|
|
95
|
-
|
|
96
|
-
const balance = await msg.staticCall(this.currency, 'balanceOf', [validator])
|
|
97
|
-
|
|
98
|
-
if (balance < this.minimumBalance) throw new Error(`balance to low! got: ${balance} need: ${this.#minimumBalance}`)
|
|
99
|
-
|
|
100
|
-
this.#totalValidators += 1
|
|
101
|
-
this.#activeValidators += 1
|
|
102
|
-
this.#validators[validator] = {
|
|
103
|
-
firstSeen: Date.now(),
|
|
104
|
-
lastSeen: Date.now(),
|
|
105
|
-
active: true
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
removeValidator(validator) {
|
|
110
|
-
this.#isAllowed(validator)
|
|
111
|
-
if (!this.has(validator)) throw new Error('validator not found')
|
|
112
|
-
|
|
113
|
-
this.#totalValidators -= 1
|
|
114
|
-
if (this.#validators[validator].active) this.#activeValidators -= 1
|
|
115
|
-
delete this.#validators[validator]
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
async updateValidator(validator, active) {
|
|
119
|
-
this.#isAllowed(validator)
|
|
120
|
-
if (!this.has(validator)) throw new Error('validator not found')
|
|
121
|
-
const balance = await msg.staticCall(this.currency, 'balanceOf', [validator])
|
|
122
|
-
if (balance < this.minimumBalance && active) throw new Error(`balance to low! got: ${balance} need: ${this.#minimumBalance}`)
|
|
123
|
-
if (this.#validators[validator].active === active) throw new Error(`already ${active ? 'activated' : 'deactivated'}`)
|
|
124
|
-
if (active) this.#activeValidators += 1
|
|
125
|
-
else this.#activeValidators -= 1
|
|
126
|
-
/** minimum balance always needs to be met */
|
|
127
|
-
this.#validators[validator].active = active
|
|
128
|
-
}
|
|
129
|
-
}
|
|
1
|
+
import Roles from '@lefocoin/standards/roles.js'
|
|
2
|
+
|
|
3
|
+
export default class Validators extends Roles {
|
|
4
|
+
/**
|
|
5
|
+
* string
|
|
6
|
+
*/
|
|
7
|
+
#name = 'ArtOnlineValidators'
|
|
8
|
+
/**
|
|
9
|
+
* uint
|
|
10
|
+
*/
|
|
11
|
+
#totalValidators = 0
|
|
12
|
+
|
|
13
|
+
#activeValidators = 0
|
|
14
|
+
/**
|
|
15
|
+
* Object => string(address) => Object
|
|
16
|
+
*/
|
|
17
|
+
#validators = {}
|
|
18
|
+
|
|
19
|
+
#currency
|
|
20
|
+
|
|
21
|
+
#minimumBalance
|
|
22
|
+
|
|
23
|
+
get state() {
|
|
24
|
+
return {
|
|
25
|
+
...super.state,
|
|
26
|
+
minimumBalance: this.#minimumBalance,
|
|
27
|
+
currency: this.#currency,
|
|
28
|
+
totalValidators: this.#totalValidators,
|
|
29
|
+
activeValidators: this.#activeValidators,
|
|
30
|
+
validators: this.#validators
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
constructor(tokenAddress, state) {
|
|
35
|
+
super(state?.roles)
|
|
36
|
+
if (state) {
|
|
37
|
+
this.#minimumBalance = state.minimumBalance
|
|
38
|
+
this.#currency = state.currency
|
|
39
|
+
|
|
40
|
+
this.#totalValidators = state.totalValidators
|
|
41
|
+
this.#activeValidators = state.activeValidators
|
|
42
|
+
this.#validators = state.validators
|
|
43
|
+
} else {
|
|
44
|
+
this.#minimumBalance = 50_000
|
|
45
|
+
this.#currency = tokenAddress
|
|
46
|
+
|
|
47
|
+
this.#totalValidators += 1
|
|
48
|
+
this.#activeValidators += 1
|
|
49
|
+
this.#validators[msg.sender] = {
|
|
50
|
+
firstSeen: Date.now(),
|
|
51
|
+
lastSeen: Date.now(),
|
|
52
|
+
active: true
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
get name() {
|
|
59
|
+
return this.#name
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
get currency() {
|
|
63
|
+
return this.#currency
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
get validators() {
|
|
67
|
+
return {...this.#validators}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
get totalValidators() {
|
|
71
|
+
return this.#totalValidators
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
get minimumBalance() {
|
|
75
|
+
return this.#minimumBalance
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
changeCurrency(currency) {
|
|
79
|
+
if (!this.hasRole(msg.sender, 'OWNER')) throw new Error('not an owner')
|
|
80
|
+
this.#currency = currency
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
has(validator) {
|
|
84
|
+
return Boolean(this.#validators[validator] !== undefined)
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
#isAllowed(address) {
|
|
88
|
+
if (msg.sender !== address && !this.hasRole(msg.sender, 'OWNER')) throw new Error('sender is not the validator or owner')
|
|
89
|
+
return true
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
async addValidator(validator) {
|
|
93
|
+
this.#isAllowed(validator)
|
|
94
|
+
if (this.has(validator)) throw new Error('already a validator')
|
|
95
|
+
|
|
96
|
+
const balance = await msg.staticCall(this.currency, 'balanceOf', [validator])
|
|
97
|
+
|
|
98
|
+
if (balance < this.minimumBalance) throw new Error(`balance to low! got: ${balance} need: ${this.#minimumBalance}`)
|
|
99
|
+
|
|
100
|
+
this.#totalValidators += 1
|
|
101
|
+
this.#activeValidators += 1
|
|
102
|
+
this.#validators[validator] = {
|
|
103
|
+
firstSeen: Date.now(),
|
|
104
|
+
lastSeen: Date.now(),
|
|
105
|
+
active: true
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
removeValidator(validator) {
|
|
110
|
+
this.#isAllowed(validator)
|
|
111
|
+
if (!this.has(validator)) throw new Error('validator not found')
|
|
112
|
+
|
|
113
|
+
this.#totalValidators -= 1
|
|
114
|
+
if (this.#validators[validator].active) this.#activeValidators -= 1
|
|
115
|
+
delete this.#validators[validator]
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
async updateValidator(validator, active) {
|
|
119
|
+
this.#isAllowed(validator)
|
|
120
|
+
if (!this.has(validator)) throw new Error('validator not found')
|
|
121
|
+
const balance = await msg.staticCall(this.currency, 'balanceOf', [validator])
|
|
122
|
+
if (balance < this.minimumBalance && active) throw new Error(`balance to low! got: ${balance} need: ${this.#minimumBalance}`)
|
|
123
|
+
if (this.#validators[validator].active === active) throw new Error(`already ${active ? 'activated' : 'deactivated'}`)
|
|
124
|
+
if (active) this.#activeValidators += 1
|
|
125
|
+
else this.#activeValidators -= 1
|
|
126
|
+
/** minimum balance always needs to be met */
|
|
127
|
+
this.#validators[validator].active = active
|
|
128
|
+
}
|
|
129
|
+
}
|