@leofcoin/chain 1.3.5 → 1.3.6
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/dist/chain.js +1 -0
- package/dist/contracts/factory.js +1 -1
- package/dist/contracts/native-token.js +1 -1
- package/dist/contracts/validators.js +1 -1
- package/dist/module/chain.js +1 -0
- package/dist/standards/token.js +1 -1
- package/package.json +1 -1
- package/src/contracts/factory.js +1 -40
- package/src/contracts/native-token.js +2 -2
- package/src/machine.js +1 -1
- package/src/standards/roles.js +0 -1
- package/src/contracts/proxies/factory-proxy.js +0 -12
- package/src/contracts/proxies/name-service-proxy.js +0 -12
- package/src/contracts/proxies/native-token-proxy.js +0 -12
- package/src/contracts/proxies/validators-proxy.js +0 -12
- package/src/contracts/proxies/voting-proxy.js +0 -12
- package/src/contracts/proxy-manager.js +0 -7
- package/src/standards/proxy-manager.js +0 -66
- package/src/standards/proxy.js +0 -38
package/dist/chain.js
CHANGED
|
@@ -170,6 +170,7 @@ class Machine {
|
|
|
170
170
|
return new Promise((resolve, reject) => {
|
|
171
171
|
const id = node_crypto.randomBytes(20).toString('hex');
|
|
172
172
|
const message = message => {
|
|
173
|
+
pubsub.unsubscribe(id, message);
|
|
173
174
|
if (message?.error) reject(message.error);
|
|
174
175
|
else resolve(message);
|
|
175
176
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
class Factory{#name="ArtOnlineContractFactory";#totalContracts=0;#contracts=[];#implementations={};constructor(state){state&&(this.#contracts=state.contracts,this.#totalContracts=state.totalContracts,this.#implementations=state.implementations)}get state(){return{totalContracts:this.#totalContracts,contracts:this.#contracts,implementations:this.#implementations}}get name(){return this.#name}get contracts(){return[...this.#contracts]}get totalContracts(){return this.#totalContracts}get implementations(){return{...this.#implementations}}async registerContract(address){
|
|
1
|
+
class Factory{#name="ArtOnlineContractFactory";#totalContracts=0;#contracts=[];#implementations={};constructor(state){state&&(this.#contracts=state.contracts,this.#totalContracts=state.totalContracts,this.#implementations=state.implementations)}get state(){return{totalContracts:this.#totalContracts,contracts:this.#contracts,implementations:this.#implementations}}get name(){return this.#name}get contracts(){return[...this.#contracts]}get totalContracts(){return this.#totalContracts}get implementations(){return{...this.#implementations}}async registerContract(address){if(await msg.staticCall(address,"hasRole",[msg.sender,"OWNER"]),this.#implementations[address])throw new Error("already registered");this.#totalContracts+=1,this.#contracts.push(address)}}export{Factory as default};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
class Roles{#roles={
|
|
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"),this.#grantRole(msg.sender,"IMPLEMENTATION_MANAGER")}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=globalThis.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 +1 @@
|
|
|
1
|
-
class Roles{#roles={
|
|
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"),this.#grantRole(msg.sender,"IMPLEMENTATION_MANAGER")}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/dist/module/chain.js
CHANGED
|
@@ -161,6 +161,7 @@ class Machine {
|
|
|
161
161
|
return new Promise((resolve, reject) => {
|
|
162
162
|
const id = randomBytes(20).toString('hex');
|
|
163
163
|
const message = message => {
|
|
164
|
+
pubsub.unsubscribe(id, message);
|
|
164
165
|
if (message?.error) reject(message.error);
|
|
165
166
|
else resolve(message);
|
|
166
167
|
};
|
package/dist/standards/token.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
class Roles{#roles={
|
|
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"),this.#grantRole(msg.sender,"IMPLEMENTATION_MANAGER")}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=globalThis.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)}}export{Token as default};
|
package/package.json
CHANGED
package/src/contracts/factory.js
CHANGED
|
@@ -54,49 +54,10 @@ export default class Factory {
|
|
|
54
54
|
* @param {Address} address contract address to register
|
|
55
55
|
*/
|
|
56
56
|
async registerContract(address) {
|
|
57
|
-
|
|
58
|
-
isAllowed = await msg.staticCall(address, 'hasRole', [msg.sender, 'IMPLEMENTATION_MANAGER'])
|
|
59
|
-
if (!isAllowed) throw new Error('only the implementation manager can update')
|
|
57
|
+
await msg.staticCall(address, 'hasRole', [msg.sender, 'OWNER'])
|
|
60
58
|
if (this.#implementations[address]) throw new Error('already registered')
|
|
61
59
|
|
|
62
60
|
this.#totalContracts += 1
|
|
63
|
-
this.#implementations[address] = []
|
|
64
|
-
this.#implementations[address].push(address)
|
|
65
61
|
this.#contracts.push(address)
|
|
66
62
|
}
|
|
67
|
-
|
|
68
|
-
/**
|
|
69
|
-
* updates the current implementation to a new address
|
|
70
|
-
*
|
|
71
|
-
* @param {Address} address the current contract address
|
|
72
|
-
* @param {Address} newAddress the new contract address
|
|
73
|
-
*/
|
|
74
|
-
async updateImplementation(address, newAddress) {
|
|
75
|
-
let isAllowed = false
|
|
76
|
-
isAllowed = await msg.staticCall(address, 'hasRole', [msg.sender, 'IMPLEMENTATION_MANAGER'])
|
|
77
|
-
if (!isAllowed) throw new Error('only the implementation manager can update')
|
|
78
|
-
if (!this.#implementations[address]) throw new Error(`register ${address} before updating to ${newAddress}`)
|
|
79
|
-
|
|
80
|
-
this.#implementations[address].push(newAddress)
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
/**
|
|
84
|
-
*
|
|
85
|
-
* @param {Address} address the original contract address
|
|
86
|
-
* @returns {Address} all implementations of the original contract
|
|
87
|
-
*/
|
|
88
|
-
getImplementations(address) {
|
|
89
|
-
return this.#implementations[address]
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
/**
|
|
93
|
-
*
|
|
94
|
-
* @param {Address} address the original contract address
|
|
95
|
-
* @param {Number} index the index of the implmentation item or undefined (returns the latest implementation when undefined)
|
|
96
|
-
* @returns {Address} the latest/selected implementation of the original contract
|
|
97
|
-
*/
|
|
98
|
-
getImplementation(address, index) {
|
|
99
|
-
index = index || this.#implementations[address].length - 1
|
|
100
|
-
return this.#implementations[address][index]
|
|
101
|
-
}
|
|
102
63
|
}
|
package/src/machine.js
CHANGED
|
@@ -3,7 +3,6 @@ import { formatBytes } from './../../utils/src/utils'
|
|
|
3
3
|
import { randomBytes } from 'node:crypto'
|
|
4
4
|
import { join } from 'node:path'
|
|
5
5
|
import EasyWorker from '@vandeurenglenn/easy-worker'
|
|
6
|
-
|
|
7
6
|
// import State from './state'
|
|
8
7
|
|
|
9
8
|
export default class Machine {
|
|
@@ -117,6 +116,7 @@ export default class Machine {
|
|
|
117
116
|
return new Promise((resolve, reject) => {
|
|
118
117
|
const id = randomBytes(20).toString('hex')
|
|
119
118
|
const message = message => {
|
|
119
|
+
pubsub.unsubscribe(id, message)
|
|
120
120
|
if (message?.error) reject(message.error)
|
|
121
121
|
else resolve(message)
|
|
122
122
|
}
|
package/src/standards/roles.js
CHANGED
|
@@ -1,66 +0,0 @@
|
|
|
1
|
-
import Roles from './roles.js'
|
|
2
|
-
|
|
3
|
-
export default class ProxyManager extends Roles {
|
|
4
|
-
#name
|
|
5
|
-
#proxies = {}
|
|
6
|
-
#manager
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
constructor(name, state) {
|
|
10
|
-
super(state?.roles)
|
|
11
|
-
this.#name = name
|
|
12
|
-
|
|
13
|
-
if (state) {
|
|
14
|
-
this.#proxies = state.proxies
|
|
15
|
-
this.#manager = state.manager
|
|
16
|
-
} else {
|
|
17
|
-
this.#manager = msg.sender
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
*
|
|
23
|
-
*/
|
|
24
|
-
async #upgradeProxy(proxy, address) {
|
|
25
|
-
await msg.internalCall(this.#manager, proxy, 'setImplementation', [address])
|
|
26
|
-
this.#proxies[proxy] = address
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
*
|
|
31
|
-
*/
|
|
32
|
-
upgradeProxy(proxy, address) {
|
|
33
|
-
if (!this.hasRole(msg.sender, 'MANAGER')) throw new Error('Not allowed, expected MANAGER')
|
|
34
|
-
this.#upgradeProxy(proxy, address)
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
async #changeManager(address) {
|
|
38
|
-
this.#revokeRole(this.#manager, 'MANAGER')
|
|
39
|
-
this.#grantRole(address, 'MANAGER')
|
|
40
|
-
for (const proxy of Object.keys(this.#proxies)) {
|
|
41
|
-
await msg.internalCall(this.#manager, proxy, 'changeProxyManager', [address])
|
|
42
|
-
}
|
|
43
|
-
this.#manager = address
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
changeManager(address) {
|
|
47
|
-
if (!this.hasRole(msg.sender, 'OWNER')) throw new Error('Not allowed, expected OWNER')
|
|
48
|
-
return this.#changeManager(address)
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
get state() {
|
|
52
|
-
return {
|
|
53
|
-
...super.state,
|
|
54
|
-
proxies: this.proxies,
|
|
55
|
-
manager: this.manager
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
get manager() {
|
|
60
|
-
return this.#manager
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
get proxies() {
|
|
64
|
-
return { ...this.#proxies }
|
|
65
|
-
}
|
|
66
|
-
}
|
package/src/standards/proxy.js
DELETED
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
import Roles from './roles.js'
|
|
2
|
-
|
|
3
|
-
export default class Proxy extends Roles {
|
|
4
|
-
#proxyManager
|
|
5
|
-
#implementation
|
|
6
|
-
|
|
7
|
-
constructor(proxyManager, state) {
|
|
8
|
-
super(state?.roles)
|
|
9
|
-
this.#proxyManager = state ? state.proxyManager : proxyManager;
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
get state() {
|
|
13
|
-
return {
|
|
14
|
-
...super.state,
|
|
15
|
-
proxyManager: this.#proxyManager,
|
|
16
|
-
implementation: this.#implementation
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
async setImplementation(address) {
|
|
21
|
-
if (msg.sender !== this.#proxyManager) throw new Error(`not allowed, expected proxy manager`)
|
|
22
|
-
|
|
23
|
-
const state = await stateStore.get(this.#implementation)
|
|
24
|
-
await stateStore.put(address, state)
|
|
25
|
-
stateStore.delete(this.#implementation)
|
|
26
|
-
this.#implementation = address
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
async changeProxyManager(address) {
|
|
30
|
-
if (msg.sender !== this.#proxyManager) throw new Error(`not allowed, expected proxy manager`)
|
|
31
|
-
this.#proxyManager = address
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
fallback(method, parameters) {
|
|
35
|
-
if (msg.sender === this.proxyManager) return this[method](...parameters)
|
|
36
|
-
return msg.internalCall(msg.sender, this.#implementation, method, parameters)
|
|
37
|
-
}
|
|
38
|
-
}
|