@leofcoin/contracts 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/src/factory.ts ADDED
@@ -0,0 +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: { contracts: any[]; totalContracts: number }) {
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: any) {
42
+ return this.#contracts.includes(address)
43
+ }
44
+
45
+ /**
46
+ *
47
+ * @param {Address} address contract address to register
48
+ */
49
+ async registerContract(address: string) {
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
+ }
@@ -0,0 +1,120 @@
1
+ type registry = {
2
+ name: {
3
+ address: address,
4
+ owner: address
5
+ }
6
+ }
7
+
8
+ export default class NameService {
9
+ /**
10
+ * string
11
+ */
12
+ #name: string = 'ArtOnlineNameService'
13
+ /**
14
+ * string
15
+ */
16
+ #owner: address
17
+ /**
18
+ * number
19
+ */
20
+ #price: BigNumberish = 0
21
+ /**
22
+ * Object => string
23
+ */
24
+ #registry: registry
25
+
26
+ /**
27
+ * => string
28
+ */
29
+ #currency: address
30
+
31
+ get name(): string {
32
+ return this.#name
33
+ }
34
+
35
+ get registry(): {} {
36
+ return {...this.#registry}
37
+ }
38
+
39
+ get state() {
40
+ return {
41
+ owner: this.#owner,
42
+ registry: this.#registry,
43
+ currency: this.#currency,
44
+ price: this.#price
45
+ }
46
+ }
47
+
48
+ // TODO: control with contract
49
+ constructor(factoryAddress: address, currency: address, validatorAddress: address, price: BigNumberish, state: { owner: address; registry: registry; currency: address; price: BigNumberish }) {
50
+ if (state) {
51
+ this.#owner = state.owner
52
+ this.#registry = state.registry
53
+ this.#currency = state.currency
54
+ this.#price = state.price
55
+ } else {
56
+ this.#owner = msg.sender
57
+ this.#price = price
58
+ this.#registry['ArtOnlineContractFactory'] = {
59
+ owner: msg.sender,
60
+ address: factoryAddress
61
+ }
62
+
63
+ this.#registry['ArtOnlineToken'] = {
64
+ owner: msg.sender,
65
+ address: currency
66
+ }
67
+
68
+ this.#registry['ArtOnlineValidators'] = {
69
+ owner: msg.sender,
70
+ address: validatorAddress
71
+ }
72
+
73
+ this.#currency = currency
74
+ }
75
+ }
76
+
77
+ changeOwner(owner: any) {
78
+ if (msg.sender !== this.#owner) throw new Error('no owner')
79
+ this.#owner = owner
80
+ }
81
+
82
+ changePrice(price: number) {
83
+ if (msg.sender !== this.#owner) throw new Error('no owner')
84
+ this.#price = price
85
+ }
86
+
87
+ changeCurrency(currency: any) {
88
+ if (msg.sender !== this.#owner) throw new Error('no owner')
89
+ this.#currency = currency
90
+ }
91
+
92
+ async purchaseName(name: string | number, address: any) {
93
+ const balance = await msg.call(this.#currency, 'balanceOf', [msg.sender])
94
+ if (balance < this.#price) throw new Error('price exceeds balance')
95
+ try {
96
+ await msg.call(this.#currency, 'transfer', [msg.sender, this.#owner, this.#price])
97
+ } catch (error) {
98
+ throw error
99
+ }
100
+
101
+ this.#registry[name] = {
102
+ owner: msg.sender,
103
+ address
104
+ }
105
+ }
106
+
107
+ lookup(name: string | number) {
108
+ return this.#registry[name]
109
+ }
110
+
111
+ transferOwnership(name: string | number, to: any) {
112
+ if (msg.sender !== this.#registry[name].owner) throw new Error('not a owner')
113
+ this.#registry[name].owner = to
114
+ }
115
+
116
+ changeAddress(name: string | number, address: any) {
117
+ if (msg.sender !== this.#registry[name].owner) throw new Error('not a owner')
118
+ this.#registry[name].address = address
119
+ }
120
+ }
@@ -0,0 +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
+ }
@@ -0,0 +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
+ }
package/src/types.ts ADDED
@@ -0,0 +1,3 @@
1
+ import globals from '@leofcoin/global-types'
2
+
3
+ export default globals
@@ -0,0 +1,129 @@
1
+ import Roles from '@leofcoin/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: address) {
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
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,15 @@
1
+ {
2
+ "compilerOptions": {
3
+ "module": "es2022",
4
+ "target": "es2022",
5
+ "declarationDir": "./exports",
6
+ "moduleResolution": "nodenext",
7
+ "allowJs": true,
8
+ "allowSyntheticDefaultImports": true,
9
+ "resolveJsonModule": true,
10
+ "declaration": true
11
+ },
12
+ "include": [
13
+ "./src/**/*"
14
+ ]
15
+ }