@leofcoin/contracts 0.1.14 → 0.1.15
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/.github/workflows/test.yml +33 -0
- package/.prettierrc +11 -11
- package/.size-log.json +115 -0
- package/LICENSE +20 -20
- package/exports/factory.d.ts +6 -3
- package/exports/factory.js +1 -1
- package/exports/name-service.js +1 -1
- package/exports/native-token.d.ts +2 -2
- package/exports/native-token.js +1 -1
- package/exports/power-token.d.ts +1 -2
- package/exports/power-token.js +1 -1
- package/exports/validators.d.ts +2 -5
- package/exports/validators.js +1 -1
- package/package.json +42 -43
- package/rollup.config.js +86 -86
- package/scripts/track-size.js +129 -0
- package/src/chat.ts +25 -25
- package/src/factory.ts +79 -73
- package/src/name-service.ts +93 -92
- package/src/native-token.ts +13 -8
- package/src/power-token.ts +7 -8
- package/src/validators.ts +148 -147
- package/test/factory.js +65 -66
- package/test/native-token.js +86 -96
- package/test/validators.js +119 -120
- package/tsconfig.json +14 -13
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
name: Tests
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
branches: [main]
|
|
6
|
+
pull_request:
|
|
7
|
+
branches: [main]
|
|
8
|
+
|
|
9
|
+
jobs:
|
|
10
|
+
test:
|
|
11
|
+
runs-on: ubuntu-latest
|
|
12
|
+
|
|
13
|
+
strategy:
|
|
14
|
+
matrix:
|
|
15
|
+
node-version: [22.x, latest]
|
|
16
|
+
|
|
17
|
+
steps:
|
|
18
|
+
- uses: actions/checkout@v4
|
|
19
|
+
|
|
20
|
+
- name: Use Node.js ${{ matrix.node-version }}
|
|
21
|
+
uses: actions/setup-node@v4
|
|
22
|
+
with:
|
|
23
|
+
node-version: ${{ matrix.node-version }}
|
|
24
|
+
cache: 'npm'
|
|
25
|
+
|
|
26
|
+
- name: Install dependencies
|
|
27
|
+
run: npm ci
|
|
28
|
+
|
|
29
|
+
- name: Build project
|
|
30
|
+
run: npm run build
|
|
31
|
+
|
|
32
|
+
- name: Run tests
|
|
33
|
+
run: npm test
|
package/.prettierrc
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
{
|
|
2
|
-
"tabWidth": 2,
|
|
3
|
-
"semi": false,
|
|
4
|
-
"singleQuote": true,
|
|
5
|
-
"printWidth": 100,
|
|
6
|
-
"bracketSpacing": true,
|
|
7
|
-
"trailingComma": "none",
|
|
8
|
-
"bracketSameLine": true,
|
|
9
|
-
"arrowParens": "always",
|
|
10
|
-
"singleAttributePerLine": true
|
|
11
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"tabWidth": 2,
|
|
3
|
+
"semi": false,
|
|
4
|
+
"singleQuote": true,
|
|
5
|
+
"printWidth": 100,
|
|
6
|
+
"bracketSpacing": true,
|
|
7
|
+
"trailingComma": "none",
|
|
8
|
+
"bracketSameLine": true,
|
|
9
|
+
"arrowParens": "always",
|
|
10
|
+
"singleAttributePerLine": true
|
|
11
|
+
}
|
package/.size-log.json
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
{
|
|
2
|
+
"entries": [
|
|
3
|
+
{
|
|
4
|
+
"version": "0.1.14",
|
|
5
|
+
"timestamp": "2025-12-25T16:24:38.519Z",
|
|
6
|
+
"size": {
|
|
7
|
+
"bytes": 33464,
|
|
8
|
+
"formatted": "32.68 KB"
|
|
9
|
+
},
|
|
10
|
+
"dependencies": {
|
|
11
|
+
"production": 2,
|
|
12
|
+
"development": 9,
|
|
13
|
+
"total": 11
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
{
|
|
17
|
+
"version": "0.1.14",
|
|
18
|
+
"timestamp": "2025-12-25T16:25:00.970Z",
|
|
19
|
+
"size": {
|
|
20
|
+
"bytes": 33464,
|
|
21
|
+
"formatted": "32.68 KB"
|
|
22
|
+
},
|
|
23
|
+
"dependencies": {
|
|
24
|
+
"production": 2,
|
|
25
|
+
"development": 9,
|
|
26
|
+
"total": 11
|
|
27
|
+
}
|
|
28
|
+
},
|
|
29
|
+
{
|
|
30
|
+
"version": "0.1.14",
|
|
31
|
+
"timestamp": "2025-12-25T16:26:05.675Z",
|
|
32
|
+
"size": {
|
|
33
|
+
"bytes": 33464,
|
|
34
|
+
"formatted": "32.68 KB"
|
|
35
|
+
},
|
|
36
|
+
"nodeModules": {
|
|
37
|
+
"bytes": 49315966,
|
|
38
|
+
"formatted": "47.03 MB"
|
|
39
|
+
},
|
|
40
|
+
"dependencies": {
|
|
41
|
+
"production": 2,
|
|
42
|
+
"development": 9,
|
|
43
|
+
"total": 11
|
|
44
|
+
}
|
|
45
|
+
},
|
|
46
|
+
{
|
|
47
|
+
"version": "0.1.14",
|
|
48
|
+
"timestamp": "2025-12-25T16:26:07.387Z",
|
|
49
|
+
"size": {
|
|
50
|
+
"bytes": 33464,
|
|
51
|
+
"formatted": "32.68 KB"
|
|
52
|
+
},
|
|
53
|
+
"nodeModules": {
|
|
54
|
+
"bytes": 49315966,
|
|
55
|
+
"formatted": "47.03 MB"
|
|
56
|
+
},
|
|
57
|
+
"dependencies": {
|
|
58
|
+
"production": 2,
|
|
59
|
+
"development": 9,
|
|
60
|
+
"total": 11
|
|
61
|
+
}
|
|
62
|
+
},
|
|
63
|
+
{
|
|
64
|
+
"version": "0.1.14",
|
|
65
|
+
"timestamp": "2025-12-25T16:26:17.057Z",
|
|
66
|
+
"size": {
|
|
67
|
+
"bytes": 33464,
|
|
68
|
+
"formatted": "32.68 KB"
|
|
69
|
+
},
|
|
70
|
+
"nodeModules": {
|
|
71
|
+
"bytes": 49315966,
|
|
72
|
+
"formatted": "47.03 MB"
|
|
73
|
+
},
|
|
74
|
+
"dependencies": {
|
|
75
|
+
"production": 2,
|
|
76
|
+
"development": 9,
|
|
77
|
+
"total": 11
|
|
78
|
+
}
|
|
79
|
+
},
|
|
80
|
+
{
|
|
81
|
+
"version": "0.1.14",
|
|
82
|
+
"timestamp": "2025-12-25T16:30:39.118Z",
|
|
83
|
+
"size": {
|
|
84
|
+
"bytes": 33464,
|
|
85
|
+
"formatted": "32.68 KB"
|
|
86
|
+
},
|
|
87
|
+
"nodeModules": {
|
|
88
|
+
"bytes": 40282265,
|
|
89
|
+
"formatted": "38.42 MB"
|
|
90
|
+
},
|
|
91
|
+
"dependencies": {
|
|
92
|
+
"production": 2,
|
|
93
|
+
"development": 6,
|
|
94
|
+
"total": 8
|
|
95
|
+
}
|
|
96
|
+
},
|
|
97
|
+
{
|
|
98
|
+
"version": "0.1.14",
|
|
99
|
+
"timestamp": "2025-12-25T16:35:15.410Z",
|
|
100
|
+
"size": {
|
|
101
|
+
"bytes": 33464,
|
|
102
|
+
"formatted": "32.68 KB"
|
|
103
|
+
},
|
|
104
|
+
"nodeModules": {
|
|
105
|
+
"bytes": 40282265,
|
|
106
|
+
"formatted": "38.42 MB"
|
|
107
|
+
},
|
|
108
|
+
"dependencies": {
|
|
109
|
+
"production": 2,
|
|
110
|
+
"development": 6,
|
|
111
|
+
"total": 8
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
]
|
|
115
|
+
}
|
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.d.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { TokenReceiver } from '@leofcoin/standards';
|
|
2
|
-
import
|
|
3
|
-
import { TokenReceiverState } from '@leofcoin/standards/token-receiver';
|
|
2
|
+
import { TokenReceiverState } from '@leofcoin/standards/token-receiver.js';
|
|
4
3
|
export interface FactoryState extends TokenReceiverState {
|
|
5
4
|
contracts: any[];
|
|
6
5
|
totalContracts: bigint;
|
|
@@ -8,11 +7,15 @@ export interface FactoryState extends TokenReceiverState {
|
|
|
8
7
|
export default class Factory extends TokenReceiver {
|
|
9
8
|
#private;
|
|
10
9
|
constructor(tokenToReceive: address, tokenAmountToReceive: bigint, state: FactoryState);
|
|
11
|
-
get state():
|
|
10
|
+
get state(): FactoryState;
|
|
12
11
|
get name(): string;
|
|
13
12
|
get contracts(): string[];
|
|
14
13
|
get totalContracts(): bigint;
|
|
15
14
|
isRegistered(address: any): boolean;
|
|
15
|
+
/**
|
|
16
|
+
* Public hook for creator check to ease testing/stubbing.
|
|
17
|
+
*/
|
|
18
|
+
isCreator(address: address): boolean;
|
|
16
19
|
/**
|
|
17
20
|
*
|
|
18
21
|
* @param {Address} address contract address to register
|
package/exports/factory.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
class
|
|
1
|
+
class Meta{#creator;#createdAt=BigInt(Date.now());constructor(state){state?(this.#creator=state.creator,this.#createdAt=state.createdAt):(this.#creator=msg.sender,this.#createdAt=BigInt(Date.now()))}get state(){return{creator:this.#creator,createdAt:this.#createdAt}}get creator(){return this.#creator}get createdAt(){return this.#createdAt}}class PublicVoting extends Meta{#votes;#votingDisabled;#votingDuration=1728e5;constructor(state){super(state),state&&(this.#votes=state.votes,this.#votingDisabled=state.votingDisabled)}get votes(){return{...this.#votes}}get votingDuration(){return this.#votingDuration}get votingDisabled(){return this.#votingDisabled}get state(){return{...super.state,votes:this.#votes,votingDisabled:this.#votingDisabled,votingDuration:this.#votingDuration}}get inProgress(){return Object.entries(this.#votes).filter(([id,vote])=>!vote.finished).map(([id,vote])=>({...vote,id:id}))}createVote(title,description,endTime,method,args=[]){if(!this.#canVote())throw new Error("Not allowed to create a vote");const id=crypto.randomUUID();this.#votes[id]={title:title,description:description,method:method,endTime:endTime,args:args}}#canVote(){return this._canVote?.()}#beforeVote(){return this._beforeVote?.()}#endVoting(voteId){let agree=Object.values(this.#votes[voteId].results).filter(result=>1===result),disagree=Object.values(this.#votes[voteId].results).filter(result=>0===result);agree.length>disagree.length&&this.#votes[voteId].enoughVotes&&this[this.#votes[voteId].method](...this.#votes[voteId].args),this.#votes[voteId].finished=!0}async vote(voteId,vote){if(0!==(vote=Number(vote))&&.5!==vote&&1!==vote)throw new Error(`invalid vote value ${vote}`);if(!this.#votes[voteId])throw new Error(`Nothing found for ${voteId}`);const ended=(new Date).getTime()>this.#votes[voteId].endTime;if(ended&&!this.#votes[voteId].finished&&this.#endVoting(voteId),ended)throw new Error("voting already ended");if(!this.#canVote())throw new Error("Not allowed to vote");await this.#beforeVote(),this.#votes[voteId][msg.sender]=vote}#disableVoting(){this.#votingDisabled=!0}disableVoting(){if(!this.#canVote())throw new Error("not a allowed");this.createVote("disable voting","Warning this disables all voting features forever",(new Date).getTime()+this.#votingDuration,"#disableVoting",[])}_sync(){for(const vote of this.inProgress)vote.endTime<(new Date).getTime()&&this.#endVoting(vote.id)}}class TokenReceiver extends PublicVoting{#tokenToReceive;#tokenAmountToReceive;#tokenReceiver;#voteType="transfer";constructor(tokenToReceive,tokenAmountToReceive,burns,state){super(state),state?(this.#tokenReceiver=state.tokenReceiver,this.#tokenToReceive=state.tokenToReceive,this.#tokenAmountToReceive=BigInt(state.tokenAmountToReceive),this.#voteType=state.voteType):(this.#tokenReceiver=msg.contract,this.#tokenToReceive=tokenToReceive,this.#tokenAmountToReceive=BigInt(tokenAmountToReceive),burns&&(this.#voteType="burn"))}get tokenToReceive(){return this.#tokenToReceive}get tokenAmountToReceive(){return this.#tokenAmountToReceive}get tokenReceiver(){return this.#tokenReceiver}get state(){return{...super.state,tokenReceiver:this.#tokenReceiver,tokenToReceive:this.#tokenToReceive,tokenAmountToReceive:this.#tokenAmountToReceive,voteType:this.#voteType}}async#canVote(){return await msg.staticCall(this.#tokenToReceive,"balanceOf",[msg.sender])>=this.#tokenAmountToReceive}async _canVote(){return this.#canVote()}async#beforeVote(){return"burn"===this.#voteType?msg.staticCall(this.tokenToReceive,"burn",[this.tokenAmountToReceive]):msg.staticCall(this.tokenToReceive,"transfer",[msg.sender,this.tokenReceiver,this.tokenAmountToReceive])}async _beforeVote(){await this.#beforeVote()}async _payTokenToReceive(){return msg.staticCall(this.#tokenToReceive,"transfer",[msg.sender,this.#tokenReceiver,this.#tokenAmountToReceive])}async _burnTokenToReceive(){return msg.staticCall(this.#tokenToReceive,"burn",[this.#tokenAmountToReceive])}#changeTokenToReceive(address){this.#tokenToReceive=address}#changeTokenAmountToReceive(amount){this.#tokenAmountToReceive=amount}#changeVoteType(type){this.#voteType=type}#getTokensOut(amount,receiver){return msg.call(this.#tokenReceiver,"transfer",[this.#tokenReceiver,receiver,amount])}async changeVoteType(type){if(!this.#canVote())throw new Error("not a allowed");if("transfer"===this.#voteType&&await this.#balance()>0n)throw new Error("get tokens out first or they be lost forever");this.createVote("change the token amount to receive","set tokenAmountToReceive",(new Date).getTime()+this.votingDuration,"#changeVoteType",[type])}getTokensOut(amount,receiver){if(!this.#canVote())throw new Error("not a allowed");this.createVote("withdraw all tokens",`withdraw all tokens to ${receiver}`,(new Date).getTime()+this.votingDuration,"#getTokensOut",[amount,receiver])}changeTokenAmountToReceive(){if(!this.#canVote())throw new Error("not a allowed");this.createVote("change the token amount to receive","set tokenAmountToReceive",(new Date).getTime()+this.votingDuration,"#changeTokenAmountToReceive",[])}#balance(){return msg.staticCall(this.#tokenToReceive,"balanceOf",[this.#tokenReceiver])}async changeTokenToReceive(){if(!this.#canVote())throw new Error("not a allowed");if(0n!==await this.#balance()&&"transfer"===this.#voteType)throw new Error("get tokens out first or they be lost forever");this.createVote("change the token to receive","set tokenToReceive to a new address",(new Date).getTime()+this.votingDuration,"#changeTokenToReceive",[])}}class Factory extends TokenReceiver{#name="LeofcoinContractFactory";#totalContracts=BigInt(0);#contracts=[];constructor(tokenToReceive,tokenAmountToReceive,state){super(tokenToReceive,tokenAmountToReceive,!0,state),state&&(this.#contracts=state.contracts,this.#totalContracts=state.totalContracts)}get state(){return{...super.state,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)}#isCreator(address){return msg.staticCall(address,"creator",[msg.sender])}isCreator(address){return this.#isCreator(address)}async registerContract(address){if(!await this.isCreator(address))throw new Error("You don't own that contract");if(this.#contracts.includes(address))throw new Error("already registered");await this._payTokenToReceive(),this.#totalContracts+=1n,this.#contracts.push(address)}}export{Factory as default};
|
package/exports/name-service.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
class
|
|
1
|
+
class Meta{#creator;#createdAt=BigInt(Date.now());constructor(state){state?(this.#creator=state.creator,this.#createdAt=state.createdAt):(this.#creator=msg.sender,this.#createdAt=BigInt(Date.now()))}get state(){return{creator:this.#creator,createdAt:this.#createdAt}}get creator(){return this.#creator}get createdAt(){return this.#createdAt}}class PublicVoting extends Meta{#votes;#votingDisabled;#votingDuration=1728e5;constructor(state){super(state),state&&(this.#votes=state.votes,this.#votingDisabled=state.votingDisabled)}get votes(){return{...this.#votes}}get votingDuration(){return this.#votingDuration}get votingDisabled(){return this.#votingDisabled}get state(){return{...super.state,votes:this.#votes,votingDisabled:this.#votingDisabled,votingDuration:this.#votingDuration}}get inProgress(){return Object.entries(this.#votes).filter(([id,vote])=>!vote.finished).map(([id,vote])=>({...vote,id:id}))}createVote(title,description,endTime,method,args=[]){if(!this.#canVote())throw new Error("Not allowed to create a vote");const id=crypto.randomUUID();this.#votes[id]={title:title,description:description,method:method,endTime:endTime,args:args}}#canVote(){return this._canVote?.()}#beforeVote(){return this._beforeVote?.()}#endVoting(voteId){let agree=Object.values(this.#votes[voteId].results).filter(result=>1===result),disagree=Object.values(this.#votes[voteId].results).filter(result=>0===result);agree.length>disagree.length&&this.#votes[voteId].enoughVotes&&this[this.#votes[voteId].method](...this.#votes[voteId].args),this.#votes[voteId].finished=!0}async vote(voteId,vote){if(0!==(vote=Number(vote))&&.5!==vote&&1!==vote)throw new Error(`invalid vote value ${vote}`);if(!this.#votes[voteId])throw new Error(`Nothing found for ${voteId}`);const ended=(new Date).getTime()>this.#votes[voteId].endTime;if(ended&&!this.#votes[voteId].finished&&this.#endVoting(voteId),ended)throw new Error("voting already ended");if(!this.#canVote())throw new Error("Not allowed to vote");await this.#beforeVote(),this.#votes[voteId][msg.sender]=vote}#disableVoting(){this.#votingDisabled=!0}disableVoting(){if(!this.#canVote())throw new Error("not a allowed");this.createVote("disable voting","Warning this disables all voting features forever",(new Date).getTime()+this.#votingDuration,"#disableVoting",[])}_sync(){for(const vote of this.inProgress)vote.endTime<(new Date).getTime()&&this.#endVoting(vote.id)}}class TokenReceiver extends PublicVoting{#tokenToReceive;#tokenAmountToReceive;#tokenReceiver;#voteType="transfer";constructor(tokenToReceive,tokenAmountToReceive,burns,state){super(state),state?(this.#tokenReceiver=state.tokenReceiver,this.#tokenToReceive=state.tokenToReceive,this.#tokenAmountToReceive=BigInt(state.tokenAmountToReceive),this.#voteType=state.voteType):(this.#tokenReceiver=msg.contract,this.#tokenToReceive=tokenToReceive,this.#tokenAmountToReceive=BigInt(tokenAmountToReceive),burns&&(this.#voteType="burn"))}get tokenToReceive(){return this.#tokenToReceive}get tokenAmountToReceive(){return this.#tokenAmountToReceive}get tokenReceiver(){return this.#tokenReceiver}get state(){return{...super.state,tokenReceiver:this.#tokenReceiver,tokenToReceive:this.#tokenToReceive,tokenAmountToReceive:this.#tokenAmountToReceive,voteType:this.#voteType}}async#canVote(){return await msg.staticCall(this.#tokenToReceive,"balanceOf",[msg.sender])>=this.#tokenAmountToReceive}async _canVote(){return this.#canVote()}async#beforeVote(){return"burn"===this.#voteType?msg.staticCall(this.tokenToReceive,"burn",[this.tokenAmountToReceive]):msg.staticCall(this.tokenToReceive,"transfer",[msg.sender,this.tokenReceiver,this.tokenAmountToReceive])}async _beforeVote(){await this.#beforeVote()}async _payTokenToReceive(){return msg.staticCall(this.#tokenToReceive,"transfer",[msg.sender,this.#tokenReceiver,this.#tokenAmountToReceive])}async _burnTokenToReceive(){return msg.staticCall(this.#tokenToReceive,"burn",[this.#tokenAmountToReceive])}#changeTokenToReceive(address){this.#tokenToReceive=address}#changeTokenAmountToReceive(amount){this.#tokenAmountToReceive=amount}#changeVoteType(type){this.#voteType=type}#getTokensOut(amount,receiver){return msg.call(this.#tokenReceiver,"transfer",[this.#tokenReceiver,receiver,amount])}async changeVoteType(type){if(!this.#canVote())throw new Error("not a allowed");if("transfer"===this.#voteType&&await this.#balance()>0n)throw new Error("get tokens out first or they be lost forever");this.createVote("change the token amount to receive","set tokenAmountToReceive",(new Date).getTime()+this.votingDuration,"#changeVoteType",[type])}getTokensOut(amount,receiver){if(!this.#canVote())throw new Error("not a allowed");this.createVote("withdraw all tokens",`withdraw all tokens to ${receiver}`,(new Date).getTime()+this.votingDuration,"#getTokensOut",[amount,receiver])}changeTokenAmountToReceive(){if(!this.#canVote())throw new Error("not a allowed");this.createVote("change the token amount to receive","set tokenAmountToReceive",(new Date).getTime()+this.votingDuration,"#changeTokenAmountToReceive",[])}#balance(){return msg.staticCall(this.#tokenToReceive,"balanceOf",[this.#tokenReceiver])}async changeTokenToReceive(){if(!this.#canVote())throw new Error("not a allowed");if(0n!==await this.#balance()&&"transfer"===this.#voteType)throw new Error("get tokens out first or they be lost forever");this.createVote("change the token to receive","set tokenToReceive to a new address",(new Date).getTime()+this.votingDuration,"#changeTokenToReceive",[])}}class NameService extends TokenReceiver{#name="LeofcoinNameService";#registry={};get name(){return this.#name}get registry(){return{...this.#registry}}get state(){return{...super.state,registry:this.#registry}}constructor(factoryAddress,tokenToReceive,validatorAddress,tokenAmountToReceive,state){super(tokenToReceive,tokenAmountToReceive,!0,state),state?this.#registry=state.registry:(this.#registry.LeofcoinContractFactory={owner:msg.sender,address:msg.contract},this.#registry.LeofcoinToken={owner:msg.sender,address:tokenToReceive},this.#registry.LeofcoinValidators={owner:msg.sender,address:validatorAddress})}async purchaseName(name,address){if(this.#registry[name])throw new Error("name already registered");await this._payTokenToReceive(),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 allowed");this.#registry[name].owner=to}changeAddress(name,address){if(msg.sender!==this.#registry[name].owner)throw new Error("not allowed");this.#registry[name].address=address}}export{NameService as default};
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { IToken } from '@leofcoin/standards/interfaces/i-token';
|
|
2
1
|
import Token, { TokenState } from '@leofcoin/standards/token.js';
|
|
3
|
-
export default class Leofcoin extends Token
|
|
2
|
+
export default class Leofcoin extends Token {
|
|
4
3
|
constructor(state: TokenState);
|
|
4
|
+
burn(from: address, amount: bigint): void;
|
|
5
5
|
}
|
package/exports/native-token.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
class
|
|
1
|
+
class Meta{#creator;#createdAt=BigInt(Date.now());constructor(state){state?(this.#creator=state.creator,this.#createdAt=state.createdAt):(this.#creator=msg.sender,this.#createdAt=BigInt(Date.now()))}get state(){return{creator:this.#creator,createdAt:this.#createdAt}}get creator(){return this.#creator}get createdAt(){return this.#createdAt}}class Roles extends Meta{#roles={OWNER:[],MINT:[],BURN:[]};constructor(state){if(super(state),state?.roles){if(!(state.roles instanceof Object))throw new TypeError("expected roles to be an object");this.#roles={...state.roles}}else this.#grantRole(msg.sender,"OWNER")}get state(){return{...super.state,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=0n;#balances={};#approvals={};#decimals=18;#totalSupply=0n;constructor(name,symbol,decimals=18,state){if(!name)throw new Error("name undefined");if(!symbol)throw new Error("symbol undefined");super(state),state?(this.#balances=(balances=>{const _balances={};for(const address in balances)_balances[address]=BigInt(balances[address]);return _balances})(state.balances),this.#approvals=(approvals=>{const _approvals={};for(const owner in approvals){_approvals[owner]={};for(const operator in approvals[owner])_approvals[owner][operator]=BigInt(approvals[owner][operator])}return _approvals})(state.approvals),this.#holders=BigInt(state.holders),this.#totalSupply=BigInt(state.totalSupply)):(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}}get approvals(){return this.#approvals}get decimals(){return this.#decimals}mint(to,amount){if(!this.hasRole(msg.sender,"MINT"))throw new Error("not allowed");this.#totalSupply=this.#totalSupply+amount,this.#increaseBalance(to,amount)}burn(from,amount){if(!this.hasRole(msg.sender,"BURN"))throw new Error("not allowed");this.#totalSupply=this.#totalSupply-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){0n===this.#balances[address]?this.#holders-=1n:0n!==this.#balances[address]&&0n===previousBalance&&(this.#holders+=1n)}#increaseBalance(address,amount){this.#balances[address]||(this.#balances[address]=0n);const previousBalance=this.#balances[address];this.#balances[address]=this.#balances[address]+amount,this.#updateHolders(address,previousBalance)}#decreaseBalance(address,amount){const previousBalance=this.#balances[address];this.#balances[address]=this.#balances[address]-amount,this.#updateHolders(address,previousBalance)}balance(){return this.#balances[msg.sender]}balanceOf(address){return this.#balances[address]}setApproval(operator,amount){const owner=msg.sender;this.#approvals[owner]||(this.#approvals[owner]={}),this.#approvals[owner][operator]=BigInt(amount)}approved(owner,operator,amount){return this.#approvals[owner][operator]===amount}transfer(from,to,amount){amount=BigInt(amount),this.#beforeTransfer(from,to,amount),this.#decreaseBalance(from,amount),this.#increaseBalance(to,amount)}}class Leofcoin extends Token{constructor(state){super("Leofcoin","LFC",18,state)}burn(from,amount){if(this.balanceOf(from)<amount)throw new Error("amount exceeds balance");return super.burn(from,amount)}}export{Leofcoin as default};
|
package/exports/power-token.d.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import { IToken } from '@leofcoin/standards/interfaces/i-token';
|
|
2
1
|
import Token, { TokenState } from '@leofcoin/standards/token.js';
|
|
3
|
-
export default class Power extends Token
|
|
2
|
+
export default class Power extends Token {
|
|
4
3
|
constructor(state: TokenState);
|
|
5
4
|
}
|
package/exports/power-token.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
class
|
|
1
|
+
class Meta{#creator;#createdAt=BigInt(Date.now());constructor(state){state?(this.#creator=state.creator,this.#createdAt=state.createdAt):(this.#creator=msg.sender,this.#createdAt=BigInt(Date.now()))}get state(){return{creator:this.#creator,createdAt:this.#createdAt}}get creator(){return this.#creator}get createdAt(){return this.#createdAt}}class Roles extends Meta{#roles={OWNER:[],MINT:[],BURN:[]};constructor(state){if(super(state),state?.roles){if(!(state.roles instanceof Object))throw new TypeError("expected roles to be an object");this.#roles={...state.roles}}else this.#grantRole(msg.sender,"OWNER")}get state(){return{...super.state,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=0n;#balances={};#approvals={};#decimals=18;#totalSupply=0n;constructor(name,symbol,decimals=18,state){if(!name)throw new Error("name undefined");if(!symbol)throw new Error("symbol undefined");super(state),state?(this.#balances=(balances=>{const _balances={};for(const address in balances)_balances[address]=BigInt(balances[address]);return _balances})(state.balances),this.#approvals=(approvals=>{const _approvals={};for(const owner in approvals){_approvals[owner]={};for(const operator in approvals[owner])_approvals[owner][operator]=BigInt(approvals[owner][operator])}return _approvals})(state.approvals),this.#holders=BigInt(state.holders),this.#totalSupply=BigInt(state.totalSupply)):(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}}get approvals(){return this.#approvals}get decimals(){return this.#decimals}mint(to,amount){if(!this.hasRole(msg.sender,"MINT"))throw new Error("not allowed");this.#totalSupply=this.#totalSupply+amount,this.#increaseBalance(to,amount)}burn(from,amount){if(!this.hasRole(msg.sender,"BURN"))throw new Error("not allowed");this.#totalSupply=this.#totalSupply-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){0n===this.#balances[address]?this.#holders-=1n:0n!==this.#balances[address]&&0n===previousBalance&&(this.#holders+=1n)}#increaseBalance(address,amount){this.#balances[address]||(this.#balances[address]=0n);const previousBalance=this.#balances[address];this.#balances[address]=this.#balances[address]+amount,this.#updateHolders(address,previousBalance)}#decreaseBalance(address,amount){const previousBalance=this.#balances[address];this.#balances[address]=this.#balances[address]-amount,this.#updateHolders(address,previousBalance)}balance(){return this.#balances[msg.sender]}balanceOf(address){return this.#balances[address]}setApproval(operator,amount){const owner=msg.sender;this.#approvals[owner]||(this.#approvals[owner]={}),this.#approvals[owner][operator]=BigInt(amount)}approved(owner,operator,amount){return this.#approvals[owner][operator]===amount}transfer(from,to,amount){amount=BigInt(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};
|
package/exports/validators.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import Roles
|
|
1
|
+
import Roles from '@leofcoin/standards/roles.js';
|
|
2
|
+
import type { RolesState } from '@leofcoin/standards/interfaces.js';
|
|
2
3
|
export declare interface ValidatorsState extends RolesState {
|
|
3
4
|
balances: {
|
|
4
5
|
[address: string]: bigint;
|
|
@@ -18,10 +19,6 @@ export default class Validators extends Roles {
|
|
|
18
19
|
currency: string;
|
|
19
20
|
validators: string[];
|
|
20
21
|
currentValidator: string;
|
|
21
|
-
roles: {
|
|
22
|
-
[index: string]: address[];
|
|
23
|
-
};
|
|
24
|
-
contractCreator: address;
|
|
25
22
|
};
|
|
26
23
|
constructor(tokenAddress: address, state: ValidatorsState);
|
|
27
24
|
get currentValidator(): string;
|
package/exports/validators.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
const between=(min,max,length=1)=>{let arr=[];for(let i=0;i<length;++i)arr=[...arr,Math.floor(Math.random()*(max+1-min)+min)];return arr},lottery=(numbers=7,max=100,min=0,length=100)=>{let arr=[],ran=between(min,max,length);ran=(arr=>arr.filter((
|
|
1
|
+
const between=(min,max,length=1)=>{let arr=[];for(let i=0;i<length;++i)arr=[...arr,Math.floor(Math.random()*(max+1-min)+min)];return arr},lottery=(numbers=7,max=100,min=0,length=100)=>{let arr=[],ran=between(min,max,length);ran=(arr=>arr.filter((el,pos,arr)=>arr.indexOf(el)==pos))(ran);for(let i=0;i<numbers;++i){const _ran=between(min,ran.length-1,ran.length)[0];arr=[...arr,ran[_ran]]}return arr};class Meta{#creator;#createdAt=BigInt(Date.now());constructor(state){state?(this.#creator=state.creator,this.#createdAt=state.createdAt):(this.#creator=msg.sender,this.#createdAt=BigInt(Date.now()))}get state(){return{creator:this.#creator,createdAt:this.#createdAt}}get creator(){return this.#creator}get createdAt(){return this.#createdAt}}class Roles extends Meta{#roles={OWNER:[],MINT:[],BURN:[]};constructor(state){if(super(state),state?.roles){if(!(state.roles instanceof Object))throw new TypeError("expected roles to be an object");this.#roles={...state.roles}}else this.#grantRole(msg.sender,"OWNER")}get state(){return{...super.state,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="LeofcoinValidators";#validators=[];#currentValidator;#currency;#minimumBalance=BigInt(5e4);#balances={};get state(){return{...super.state,balances:this.#balances,minimumBalance:this.#minimumBalance,currency:this.#currency,validators:this.#validators,currentValidator:this.#currentValidator}}constructor(tokenAddress,state){super(state),state?(this.#minimumBalance=BigInt(state.minimumBalance),this.#currency=state.currency,this.#validators=state.validators,this.#balances=state.balances,this.#currentValidator=state.currentValidator):(this.#currency=tokenAddress,this.#validators.push(msg.sender),this.#currentValidator=msg.sender)}get currentValidator(){return this.#currentValidator}get name(){return this.#name}get currency(){return this.#currency}get validators(){return this.#validators}get totalValidators(){return this.#validators.length}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 this.#validators.includes(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("validator already exists");const balance=await msg.staticCall(this.currency,"balanceOf",[validator]);if(this.minimumBalance>balance)throw new Error(`balance to low! got: ${balance} need: ${this.#minimumBalance}`);await msg.call(this.currency,"transfer",[validator,msg.contract,this.#minimumBalance]),this.#balances[validator]=this.#minimumBalance,this.#validators.push(validator)}async removeValidator(validator){if(this.#isAllowed(validator),!this.has(validator))throw new Error("validator not found");await msg.call(this.currency,"transfer",[msg.contract,validator,this.#minimumBalance]),delete this.#balances[validator],this.#validators.splice(this.#validators.indexOf(validator),1)}shuffleValidator(){const _peers=state.peers,peers=_peers.filter(peer=>this.#validators.includes(peer[0])).map(peer=>(peer[1].totalBytes=peer[1].bw.up+peer[1].bw.down,peer)).sort((a,b)=>b[1].totalBytes-a[1].totalBytes).splice(0,_peers.length>128?128:_peers.length);let nextValidator=peers[lottery(1,peers.length-1)[0]][0];if(this.#currentValidator===nextValidator&&1!==peers.length){nextValidator=peers[lottery(1,peers.length-1)[0]][0]}this.#currentValidator=nextValidator}}export{Validators as default};
|
package/package.json
CHANGED
|
@@ -1,43 +1,42 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@leofcoin/contracts",
|
|
3
|
-
"version": "0.1.
|
|
4
|
-
"description": "",
|
|
5
|
-
"type": "module",
|
|
6
|
-
"exports": {
|
|
7
|
-
"./factory": "./exports/factory.js",
|
|
8
|
-
"./name-token": "./exports/name-token.js",
|
|
9
|
-
"./power-token": "./exports/power-token.js",
|
|
10
|
-
"./native-token": "./exports/native-token.js",
|
|
11
|
-
"./validators": "./exports/validators.js"
|
|
12
|
-
},
|
|
13
|
-
"scripts": {
|
|
14
|
-
"build": "rollup -c",
|
|
15
|
-
"
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
"
|
|
24
|
-
"
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
"@
|
|
36
|
-
"@rollup/plugin-
|
|
37
|
-
"
|
|
38
|
-
"
|
|
39
|
-
"
|
|
40
|
-
"
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "@leofcoin/contracts",
|
|
3
|
+
"version": "0.1.15",
|
|
4
|
+
"description": "",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"exports": {
|
|
7
|
+
"./factory": "./exports/factory.js",
|
|
8
|
+
"./name-token": "./exports/name-token.js",
|
|
9
|
+
"./power-token": "./exports/power-token.js",
|
|
10
|
+
"./native-token": "./exports/native-token.js",
|
|
11
|
+
"./validators": "./exports/validators.js"
|
|
12
|
+
},
|
|
13
|
+
"scripts": {
|
|
14
|
+
"build": "rollup -c",
|
|
15
|
+
"postbuild": "node scripts/track-size.js",
|
|
16
|
+
"test": "node --test test/**/*.js",
|
|
17
|
+
"size": "node scripts/track-size.js"
|
|
18
|
+
},
|
|
19
|
+
"repository": {
|
|
20
|
+
"type": "git",
|
|
21
|
+
"url": "git+https://github.com/leofcoin/contracts.git"
|
|
22
|
+
},
|
|
23
|
+
"keywords": [],
|
|
24
|
+
"author": "",
|
|
25
|
+
"license": "MIT",
|
|
26
|
+
"bugs": {
|
|
27
|
+
"url": "https://github.com/leofcoin/contracts/issues"
|
|
28
|
+
},
|
|
29
|
+
"homepage": "https://github.com/leofcoin/contracts#readme",
|
|
30
|
+
"dependencies": {
|
|
31
|
+
"@leofcoin/standards": "^0.3.4",
|
|
32
|
+
"lucky-numbers": "^1.2.1"
|
|
33
|
+
},
|
|
34
|
+
"devDependencies": {
|
|
35
|
+
"@leofcoin/types": "^1.0.22",
|
|
36
|
+
"@rollup/plugin-node-resolve": "^16.0.3",
|
|
37
|
+
"@rollup/plugin-terser": "^0.4.4",
|
|
38
|
+
"@rollup/plugin-typescript": "^12.3.0",
|
|
39
|
+
"rollup": "^4.54.0",
|
|
40
|
+
"tslib": "^2.8.1"
|
|
41
|
+
}
|
|
42
|
+
}
|