@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.
@@ -0,0 +1,20 @@
1
+ export default class Factory {
2
+ #private;
3
+ constructor(state: {
4
+ contracts: any[];
5
+ totalContracts: number;
6
+ });
7
+ get state(): {
8
+ totalContracts: number;
9
+ contracts: any[];
10
+ };
11
+ get name(): string;
12
+ get contracts(): any[];
13
+ get totalContracts(): number;
14
+ isRegistered(address: any): boolean;
15
+ /**
16
+ *
17
+ * @param {Address} address contract address to register
18
+ */
19
+ registerContract(address: string): Promise<void>;
20
+ }
@@ -1,56 +1,51 @@
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
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
19
  }
20
- }
21
-
22
- get state() {
23
- return {
24
- totalContracts: this.#totalContracts,
25
- contracts: this.#contracts
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);
26
48
  }
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
49
  }
50
+
51
+ export { Factory as default };
@@ -0,0 +1,31 @@
1
+ type registry = {
2
+ name: {
3
+ address: address;
4
+ owner: address;
5
+ };
6
+ };
7
+ export default class NameService {
8
+ #private;
9
+ get name(): string;
10
+ get registry(): {};
11
+ get state(): {
12
+ owner: string;
13
+ registry: registry;
14
+ currency: string;
15
+ price: BigNumberish;
16
+ };
17
+ constructor(factoryAddress: address, currency: address, validatorAddress: address, price: BigNumberish, state: {
18
+ owner: address;
19
+ registry: registry;
20
+ currency: address;
21
+ price: BigNumberish;
22
+ });
23
+ changeOwner(owner: any): void;
24
+ changePrice(price: number): void;
25
+ changeCurrency(currency: any): void;
26
+ purchaseName(name: string | number, address: any): Promise<void>;
27
+ lookup(name: string | number): any;
28
+ transferOwnership(name: string | number, to: any): void;
29
+ changeAddress(name: string | number, address: any): void;
30
+ }
31
+ export {};
@@ -1,106 +1,107 @@
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
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;
60
24
  }
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
25
+ get registry() {
26
+ return { ...this.#registry };
85
27
  }
86
-
87
- this.#registry[name] = {
88
- owner: msg.sender,
89
- address
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;
90
104
  }
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
105
  }
106
+
107
+ export { NameService as default };
@@ -0,0 +1,4 @@
1
+ import Token from '@leofcoin/standards/token.js';
2
+ export default class ArtOnline extends Token {
3
+ constructor(state: {});
4
+ }
@@ -1,7 +1,205 @@
1
- import Token from '@leofcoin/standards/token.js'
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
+ }
2
198
 
3
- export default class ArtOnline extends Token {
4
- constructor(state) {
5
- super('ArtOnline', 'ART', 18, state)
6
- }
199
+ class ArtOnline extends Token {
200
+ constructor(state) {
201
+ super('ArtOnline', 'ART', 18, state);
202
+ }
7
203
  }
204
+
205
+ export { ArtOnline as default };
@@ -0,0 +1,4 @@
1
+ import Token from '@leofcoin/standards/token.js';
2
+ export default class Power extends Token {
3
+ constructor(state: {});
4
+ }