@leofcoin/contracts 0.1.8 → 0.1.11
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/exports/factory.js +1 -1
- package/exports/name-service.js +1 -1
- package/exports/native-token.js +1 -1
- package/exports/power-token.js +1 -1
- package/exports/src/chat.d.ts +12 -0
- package/exports/src/factory.d.ts +21 -0
- package/exports/src/name-service.d.ts +23 -0
- package/exports/src/native-token.d.ts +4 -0
- package/exports/src/power-token.d.ts +4 -0
- package/exports/src/types.d.ts +1 -0
- package/exports/src/validators.d.ts +24 -0
- package/exports/validators.js +1 -1
- package/package.json +3 -2
- package/rollup.config.js +78 -74
- package/src/chat.ts +25 -0
- package/src/validators.ts +40 -2
package/exports/factory.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
class ContractCreator{#creator;constructor(state){this.#creator=state?state.contractCreator:msg.sender}get _contractCreator(){return this.#creator}get state(){return{contractCreator:this.#creator}}get _isContractCreator(){return msg.sender===this.#creator}}class Voting extends ContractCreator{#votes={};#votingDisabled=!1;#votingDuration=1728e5;constructor(state){super(state),state&&(this.#votes=state.votes,this.#votingDisabled=state.votingDisabled,this.#votingDuration=state.votingDuration)}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}}#canVote(){return this._canVote?.()}#beforeVote(){return this._beforeVote?.()}#afterVote(){return this._afterVote?.()}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}}#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
|
|
1
|
+
class ContractCreator{#creator;constructor(state){this.#creator=state?state.contractCreator:msg.sender}get _contractCreator(){return this.#creator}get state(){return{contractCreator:this.#creator}}get _isContractCreator(){return msg.sender===this.#creator}}class Voting extends ContractCreator{#votes={};#votingDisabled=!1;#votingDuration=1728e5;constructor(state){super(state),state&&(this.#votes=state.votes,this.#votingDisabled=state.votingDisabled,this.#votingDuration=state.votingDuration)}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}}#canVote(){return this._canVote?.()}#beforeVote(){return this._beforeVote?.()}#afterVote(){return this._afterVote?.()}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}}#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[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,await this.#afterVote()}get votesInProgress(){return Object.entries(this.#votes).filter((([id,vote])=>!vote.finished)).map((([id,vote])=>({...vote,id:id})))}#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.votesInProgress)vote.endTime<(new Date).getTime()&&this.#endVoting(vote.id)}}class PublicVoting extends Voting{constructor(state){super(state)}}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=BigNumber.from(state.tokenAmountToReceive),this.#voteType=state.voteType):(this.#tokenReceiver=msg.contract,this.#tokenToReceive=tokenToReceive,this.#tokenAmountToReceive=BigNumber.from(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])).gte(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(){return 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])}async _canPay(){return(await msg.call(this.#tokenToReceive,"balance",[])).gte(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()).gt(0))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(!(await this.#balance()).eq(0)&&"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=BigNumber.from(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,"_isContractCreator")}async registerContract(address){if(!this._canPay())throw new Error("can't register, balance to low");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.add(1),this.#contracts.push(address)}}export{Factory as default};
|
package/exports/name-service.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
class ContractCreator{#creator;constructor(state){this.#creator=state?state.contractCreator:msg.sender}get _contractCreator(){return this.#creator}get state(){return{contractCreator:this.#creator}}get _isContractCreator(){return msg.sender===this.#creator}}class Voting extends ContractCreator{#votes={};#votingDisabled=!1;#votingDuration=1728e5;constructor(state){super(state),state&&(this.#votes=state.votes,this.#votingDisabled=state.votingDisabled,this.#votingDuration=state.votingDuration)}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}}#canVote(){return this._canVote?.()}#beforeVote(){return this._beforeVote?.()}#afterVote(){return this._afterVote?.()}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}}#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
|
|
1
|
+
class ContractCreator{#creator;constructor(state){this.#creator=state?state.contractCreator:msg.sender}get _contractCreator(){return this.#creator}get state(){return{contractCreator:this.#creator}}get _isContractCreator(){return msg.sender===this.#creator}}class Voting extends ContractCreator{#votes={};#votingDisabled=!1;#votingDuration=1728e5;constructor(state){super(state),state&&(this.#votes=state.votes,this.#votingDisabled=state.votingDisabled,this.#votingDuration=state.votingDuration)}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}}#canVote(){return this._canVote?.()}#beforeVote(){return this._beforeVote?.()}#afterVote(){return this._afterVote?.()}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}}#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[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,await this.#afterVote()}get votesInProgress(){return Object.entries(this.#votes).filter((([id,vote])=>!vote.finished)).map((([id,vote])=>({...vote,id:id})))}#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.votesInProgress)vote.endTime<(new Date).getTime()&&this.#endVoting(vote.id)}}class PublicVoting extends Voting{constructor(state){super(state)}}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=BigNumber.from(state.tokenAmountToReceive),this.#voteType=state.voteType):(this.#tokenReceiver=msg.contract,this.#tokenToReceive=tokenToReceive,this.#tokenAmountToReceive=BigNumber.from(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])).gte(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(){return 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])}async _canPay(){return(await msg.call(this.#tokenToReceive,"balance",[])).gte(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()).gt(0))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(!(await this.#balance()).eq(0)&&"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:factoryAddress},this.#registry.LeofcoinToken={owner:msg.sender,address:tokenToReceive},this.#registry.LeofcoinValidators={owner:msg.sender,address:validatorAddress})}async purchaseName(name,address){await this._canPay(),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};
|
package/exports/native-token.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
class ContractCreator{#creator;constructor(state){this.#creator=state?state.contractCreator:msg.sender}get _contractCreator(){return this.#creator}get state(){return{contractCreator:this.#creator}}get _isContractCreator(){return msg.sender===this.#creator}}class Roles extends ContractCreator{#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,...this.#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=BigNumber.from(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),state?(this.#balances=(balances=>{const _balances={};for(const address in balances)_balances[address]=BigNumber.from(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]=BigNumber.from(approvals[owner][operator])}return _approvals})(state.approvals),this.#holders=BigNumber.from(state.holders),this.#totalSupply=BigNumber.from(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.add(amount),this.#increaseBalance(to,amount)}burn(from,amount){if(!this.hasRole(msg.sender,"BURN"))throw new Error("not allowed");
|
|
1
|
+
class ContractCreator{#creator;constructor(state){this.#creator=state?state.contractCreator:msg.sender}get _contractCreator(){return this.#creator}get state(){return{contractCreator:this.#creator}}get _isContractCreator(){return msg.sender===this.#creator}}class Roles extends ContractCreator{#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,...this.#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=BigNumber.from(0);#balances={};#approvals={};#decimals=18;#totalSupply=BigNumber.from(0);#stakingContract;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]=BigNumber.from(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]=BigNumber.from(approvals[owner][operator])}return _approvals})(state.approvals),this.#holders=BigNumber.from(state.holders),this.#totalSupply=BigNumber.from(state.totalSupply),this.#name=name,this.#symbol=symbol,this.#decimals=decimals):(this.#name=name,this.#symbol=symbol,this.#decimals=decimals)}get state(){return{...super.state,name:this.#name,symbol:this.#symbol,decimals:this.#decimals,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.add(amount),this.#increaseBalance(to,amount)}burn(from,amount){if(!this.hasRole(msg.sender,"BURN")||msg.sender!==from)throw new Error("not allowed");const total=this.#totalSupply.sub(amount);total.gte(0)&&(this.#totalSupply=total,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.sub(1):"0x00"!==this.#balances[address].toHexString()&&"0x00"===previousBalance.toHexString()&&this.#holders.add(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)}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]=BigNumber.from(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 Leofcoin extends Token{constructor(state){super("Leofcoin","LFC",18,state)}}export{Leofcoin as default};
|
package/exports/power-token.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
class ContractCreator{#creator;constructor(state){this.#creator=state?state.contractCreator:msg.sender}get _contractCreator(){return this.#creator}get state(){return{contractCreator:this.#creator}}get _isContractCreator(){return msg.sender===this.#creator}}class Roles extends ContractCreator{#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,...this.#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=BigNumber.from(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),state?(this.#balances=(balances=>{const _balances={};for(const address in balances)_balances[address]=BigNumber.from(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]=BigNumber.from(approvals[owner][operator])}return _approvals})(state.approvals),this.#holders=BigNumber.from(state.holders),this.#totalSupply=BigNumber.from(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.add(amount),this.#increaseBalance(to,amount)}burn(from,amount){if(!this.hasRole(msg.sender,"BURN"))throw new Error("not allowed");
|
|
1
|
+
class ContractCreator{#creator;constructor(state){this.#creator=state?state.contractCreator:msg.sender}get _contractCreator(){return this.#creator}get state(){return{contractCreator:this.#creator}}get _isContractCreator(){return msg.sender===this.#creator}}class Roles extends ContractCreator{#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,...this.#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=BigNumber.from(0);#balances={};#approvals={};#decimals=18;#totalSupply=BigNumber.from(0);#stakingContract;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]=BigNumber.from(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]=BigNumber.from(approvals[owner][operator])}return _approvals})(state.approvals),this.#holders=BigNumber.from(state.holders),this.#totalSupply=BigNumber.from(state.totalSupply),this.#name=name,this.#symbol=symbol,this.#decimals=decimals):(this.#name=name,this.#symbol=symbol,this.#decimals=decimals)}get state(){return{...super.state,name:this.#name,symbol:this.#symbol,decimals:this.#decimals,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.add(amount),this.#increaseBalance(to,amount)}burn(from,amount){if(!this.hasRole(msg.sender,"BURN")||msg.sender!==from)throw new Error("not allowed");const total=this.#totalSupply.sub(amount);total.gte(0)&&(this.#totalSupply=total,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.sub(1):"0x00"!==this.#balances[address].toHexString()&&"0x00"===previousBalance.toHexString()&&this.#holders.add(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)}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]=BigNumber.from(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 Power extends Token{constructor(state){super("Power","PWR",18,state)}}export{Power as default};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { TokenReceiver } from '@leofcoin/standards';
|
|
2
|
+
import { TokenReceiverState } from '@leofcoin/standards/token-receiver';
|
|
3
|
+
export interface chatState extends TokenReceiverState {
|
|
4
|
+
nicknames: {
|
|
5
|
+
[address: string]: string;
|
|
6
|
+
};
|
|
7
|
+
}
|
|
8
|
+
export default class Chat extends TokenReceiver {
|
|
9
|
+
#private;
|
|
10
|
+
constructor(tokenToReceive: any, tokenAmountToReceive: any, state: any);
|
|
11
|
+
changeNickName(newName: string): Promise<void>;
|
|
12
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { TokenReceiver } from '@leofcoin/standards';
|
|
2
|
+
import type { PublicVotingState } from '@leofcoin/standards/public-voting';
|
|
3
|
+
import { TokenReceiverState } from '@leofcoin/standards/token-receiver';
|
|
4
|
+
export interface FactoryState extends TokenReceiverState {
|
|
5
|
+
contracts: any[];
|
|
6
|
+
totalContracts: typeof BigNumber;
|
|
7
|
+
}
|
|
8
|
+
export default class Factory extends TokenReceiver {
|
|
9
|
+
#private;
|
|
10
|
+
constructor(tokenToReceive: address, tokenAmountToReceive: typeof BigNumber, state: FactoryState);
|
|
11
|
+
get state(): PublicVotingState;
|
|
12
|
+
get name(): string;
|
|
13
|
+
get contracts(): string[];
|
|
14
|
+
get totalContracts(): import("@ethersproject/bignumber").BigNumber;
|
|
15
|
+
isRegistered(address: any): boolean;
|
|
16
|
+
/**
|
|
17
|
+
*
|
|
18
|
+
* @param {Address} address contract address to register
|
|
19
|
+
*/
|
|
20
|
+
registerContract(address: string): Promise<void>;
|
|
21
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { TokenReceiver } from '@leofcoin/standards';
|
|
2
|
+
import { TokenReceiverState } from '@leofcoin/standards/token-receiver';
|
|
3
|
+
type registry = {
|
|
4
|
+
name?: {
|
|
5
|
+
address: address;
|
|
6
|
+
owner: address;
|
|
7
|
+
};
|
|
8
|
+
};
|
|
9
|
+
export interface NameServiceState extends TokenReceiverState {
|
|
10
|
+
registry: registry;
|
|
11
|
+
}
|
|
12
|
+
export default class NameService extends TokenReceiver {
|
|
13
|
+
#private;
|
|
14
|
+
get name(): string;
|
|
15
|
+
get registry(): {};
|
|
16
|
+
get state(): NameServiceState;
|
|
17
|
+
constructor(factoryAddress: address, tokenToReceive: address, validatorAddress: address, tokenAmountToReceive: typeof BigNumber, state: NameServiceState);
|
|
18
|
+
purchaseName(name: string | number, address: any): Promise<void>;
|
|
19
|
+
lookup(name: string | number): any;
|
|
20
|
+
transferOwnership(name: string | number, to: any): void;
|
|
21
|
+
changeAddress(name: string | number, address: any): void;
|
|
22
|
+
}
|
|
23
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import '@leofcoin/global-types';
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import Roles from '@leofcoin/standards/roles.js';
|
|
2
|
+
export default class Validators extends Roles {
|
|
3
|
+
#private;
|
|
4
|
+
get state(): {
|
|
5
|
+
minimumBalance: import("@ethersproject/bignumber").BigNumber;
|
|
6
|
+
currency: string;
|
|
7
|
+
validators: string[];
|
|
8
|
+
roles: {
|
|
9
|
+
[index: string]: string[];
|
|
10
|
+
};
|
|
11
|
+
contractCreator: string;
|
|
12
|
+
};
|
|
13
|
+
constructor(tokenAddress: address, state: any);
|
|
14
|
+
get name(): string;
|
|
15
|
+
get currency(): string;
|
|
16
|
+
get validators(): string[];
|
|
17
|
+
get totalValidators(): number;
|
|
18
|
+
get minimumBalance(): import("@ethersproject/bignumber").BigNumber;
|
|
19
|
+
changeCurrency(currency: any): void;
|
|
20
|
+
has(validator: any): boolean;
|
|
21
|
+
addValidator(validator: address): Promise<void>;
|
|
22
|
+
removeValidator(validator: any): Promise<void>;
|
|
23
|
+
shuffleValidator(): void;
|
|
24
|
+
}
|
package/exports/validators.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
class ContractCreator{#creator;constructor(state){this.#creator=state?state.contractCreator:msg.sender}get _contractCreator(){return this.#creator}get state(){return{contractCreator:this.#creator}}get _isContractCreator(){return msg.sender===this.#creator}}class Roles extends ContractCreator{#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,...this.#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=[];#currency;#minimumBalance;get state(){return{...super.state,minimumBalance:this.#minimumBalance,currency:this.#currency,validators:this.#validators}}constructor(tokenAddress,state){super(state?.roles),state?(this.#minimumBalance=BigNumber.from(state.minimumBalance),this.#currency=state.currency,this.#validators=state.validators):(this.#minimumBalance=
|
|
1
|
+
class ContractCreator{#creator;constructor(state){this.#creator=state?state.contractCreator:msg.sender}get _contractCreator(){return this.#creator}get state(){return{contractCreator:this.#creator}}get _isContractCreator(){return msg.sender===this.#creator}}class Roles extends ContractCreator{#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,...this.#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)}}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 Validators extends Roles{#name="LeofcoinValidators";#validators=[];#currentValidator;#currency;#minimumBalance;#balances;get state(){return{...super.state,minimumBalance:this.#minimumBalance,currency:this.#currency,validators:this.#validators}}constructor(tokenAddress,state){super(state?.roles),state?(this.#minimumBalance=BigNumber.from(state.minimumBalance),this.#currency=state.currency,this.#validators=state.validators):(this.#minimumBalance=BigNumber.from(5e4),this.#currency=tokenAddress,this.#validators.push(msg.sender))}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("already a validator");const balance=await msg.staticCall(this.currency,"balanceOf",[validator]);if(this.minimumBalance.gt(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))}shuffleValidator(){const _peers=globalThis.peernet.peers,peers=_peers.filter((peer=>this.#validators[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),luckyNumber=lottery(1,peers.length);let nextValidator=this.#validators[peers[luckyNumber[0]][0]];if(this.#currentValidator===nextValidator&&1!==peers.length){const luckyNumber=lottery(1,peers.length);nextValidator=this.#validators[peers[luckyNumber[0]][0]]}this.#currentValidator=nextValidator}}export{Validators as default};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@leofcoin/contracts",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.11",
|
|
4
4
|
"description": "",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
@@ -28,7 +28,8 @@
|
|
|
28
28
|
"dependencies": {
|
|
29
29
|
"@changesets/cli": "^2.27.1",
|
|
30
30
|
"@leofcoin/global-types": "^1.0.0",
|
|
31
|
-
"@leofcoin/standards": "^0.2.2"
|
|
31
|
+
"@leofcoin/standards": "^0.2.2",
|
|
32
|
+
"lucky-numbers": "^1.2.0"
|
|
32
33
|
},
|
|
33
34
|
"devDependencies": {
|
|
34
35
|
"@rollup/plugin-node-resolve": "^15.0.1",
|
package/rollup.config.js
CHANGED
|
@@ -1,82 +1,86 @@
|
|
|
1
1
|
import typescript from '@rollup/plugin-typescript'
|
|
2
|
-
import tsConfig from './tsconfig.json' assert { type: 'json'}
|
|
3
2
|
import terser from '@rollup/plugin-terser'
|
|
4
3
|
import resolve from '@rollup/plugin-node-resolve'
|
|
5
4
|
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
}, {
|
|
23
|
-
input: './src/name-service.ts',
|
|
24
|
-
output: {
|
|
25
|
-
dir: './exports',
|
|
26
|
-
format: 'es'
|
|
5
|
+
export default [
|
|
6
|
+
{
|
|
7
|
+
input: './src/factory.ts',
|
|
8
|
+
output: {
|
|
9
|
+
dir: './exports',
|
|
10
|
+
format: 'es'
|
|
11
|
+
},
|
|
12
|
+
plugins: [
|
|
13
|
+
resolve({
|
|
14
|
+
mainFields: ['exports']
|
|
15
|
+
}),
|
|
16
|
+
typescript(),
|
|
17
|
+
terser({
|
|
18
|
+
mangle: false
|
|
19
|
+
})
|
|
20
|
+
]
|
|
27
21
|
},
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
},
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
22
|
+
{
|
|
23
|
+
input: './src/name-service.ts',
|
|
24
|
+
output: {
|
|
25
|
+
dir: './exports',
|
|
26
|
+
format: 'es'
|
|
27
|
+
},
|
|
28
|
+
plugins: [
|
|
29
|
+
resolve({
|
|
30
|
+
mainFields: ['exports']
|
|
31
|
+
}),
|
|
32
|
+
typescript(),
|
|
33
|
+
terser({
|
|
34
|
+
mangle: false
|
|
35
|
+
})
|
|
36
|
+
]
|
|
42
37
|
},
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
},
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
38
|
+
{
|
|
39
|
+
input: './src/native-token.ts',
|
|
40
|
+
output: {
|
|
41
|
+
dir: './exports',
|
|
42
|
+
format: 'es'
|
|
43
|
+
},
|
|
44
|
+
plugins: [
|
|
45
|
+
resolve({
|
|
46
|
+
mainFields: ['exports']
|
|
47
|
+
}),
|
|
48
|
+
typescript(),
|
|
49
|
+
terser({
|
|
50
|
+
mangle: false
|
|
51
|
+
})
|
|
52
|
+
]
|
|
57
53
|
},
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
},
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
54
|
+
{
|
|
55
|
+
input: './src/power-token.ts',
|
|
56
|
+
output: {
|
|
57
|
+
dir: './exports',
|
|
58
|
+
format: 'es'
|
|
59
|
+
},
|
|
60
|
+
plugins: [
|
|
61
|
+
resolve({
|
|
62
|
+
mainFields: ['exports']
|
|
63
|
+
}),
|
|
64
|
+
typescript(),
|
|
65
|
+
terser({
|
|
66
|
+
mangle: false
|
|
67
|
+
})
|
|
68
|
+
]
|
|
72
69
|
},
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
}
|
|
70
|
+
{
|
|
71
|
+
input: './src/validators.ts',
|
|
72
|
+
output: {
|
|
73
|
+
dir: './exports',
|
|
74
|
+
format: 'es'
|
|
75
|
+
},
|
|
76
|
+
plugins: [
|
|
77
|
+
resolve({
|
|
78
|
+
mainFields: ['exports']
|
|
79
|
+
}),
|
|
80
|
+
typescript(),
|
|
81
|
+
terser({
|
|
82
|
+
mangle: false
|
|
83
|
+
})
|
|
84
|
+
]
|
|
85
|
+
}
|
|
86
|
+
]
|
package/src/chat.ts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { TokenReceiver } from '@leofcoin/standards'
|
|
2
|
+
import { TokenReceiverState } from '@leofcoin/standards/token-receiver'
|
|
3
|
+
|
|
4
|
+
export interface chatState extends TokenReceiverState {
|
|
5
|
+
nicknames: { [address: string]: string }
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export default class Chat extends TokenReceiver {
|
|
9
|
+
#name = 'LeofcoinChat'
|
|
10
|
+
|
|
11
|
+
#nicknames: chatState['nicknames'] = {}
|
|
12
|
+
|
|
13
|
+
constructor(tokenToReceive, tokenAmountToReceive, state) {
|
|
14
|
+
super(tokenToReceive, tokenAmountToReceive, true, state as TokenReceiverState)
|
|
15
|
+
if (state) {
|
|
16
|
+
this.#nicknames = state.nicknames || {}
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
async changeNickName(newName: string) {
|
|
21
|
+
await this._canPay()
|
|
22
|
+
await this._payTokenToReceive()
|
|
23
|
+
this.#nicknames[msg.sender] = newName
|
|
24
|
+
}
|
|
25
|
+
}
|
package/src/validators.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import Roles from '@leofcoin/standards/roles.js'
|
|
2
|
+
import { lottery } from 'lucky-numbers'
|
|
2
3
|
|
|
3
4
|
export default class Validators extends Roles {
|
|
4
5
|
/**
|
|
@@ -10,10 +11,14 @@ export default class Validators extends Roles {
|
|
|
10
11
|
*/
|
|
11
12
|
#validators: address[] = []
|
|
12
13
|
|
|
14
|
+
#currentValidator
|
|
15
|
+
|
|
13
16
|
#currency: address
|
|
14
17
|
|
|
15
18
|
#minimumBalance: typeof BigNumber
|
|
16
19
|
|
|
20
|
+
#balances: { [address: address]: typeof BigNumber }
|
|
21
|
+
|
|
17
22
|
get state() {
|
|
18
23
|
return {
|
|
19
24
|
...super.state,
|
|
@@ -30,7 +35,7 @@ export default class Validators extends Roles {
|
|
|
30
35
|
this.#currency = state.currency
|
|
31
36
|
this.#validators = state.validators
|
|
32
37
|
} else {
|
|
33
|
-
this.#minimumBalance =
|
|
38
|
+
this.#minimumBalance = BigNumber['from'](50_000)
|
|
34
39
|
this.#currency = tokenAddress
|
|
35
40
|
this.#validators.push(msg.sender)
|
|
36
41
|
}
|
|
@@ -80,12 +85,45 @@ export default class Validators extends Roles {
|
|
|
80
85
|
if (this.minimumBalance.gt(balance))
|
|
81
86
|
throw new Error(`balance to low! got: ${balance} need: ${this.#minimumBalance}`)
|
|
82
87
|
|
|
88
|
+
await msg.call(this.currency, 'transfer', [validator, msg.contract, this.#minimumBalance])
|
|
89
|
+
this.#balances[validator] = this.#minimumBalance
|
|
83
90
|
this.#validators.push(validator)
|
|
84
91
|
}
|
|
85
92
|
|
|
86
|
-
removeValidator(validator) {
|
|
93
|
+
async removeValidator(validator) {
|
|
87
94
|
this.#isAllowed(validator)
|
|
88
95
|
if (!this.has(validator)) throw new Error('validator not found')
|
|
96
|
+
await msg.call(this.currency, 'transfer', [msg.contract, validator, this.#minimumBalance])
|
|
97
|
+
delete this.#balances[validator]
|
|
89
98
|
this.#validators.splice(this.#validators.indexOf(validator))
|
|
90
99
|
}
|
|
100
|
+
|
|
101
|
+
shuffleValidator() {
|
|
102
|
+
// todo introduce voting mechanism
|
|
103
|
+
// select amount of peers to vote & pass when 2/3 select the same peer
|
|
104
|
+
// this.vote
|
|
105
|
+
// todo only ids should be accessable
|
|
106
|
+
const _peers = globalThis.peernet.peers
|
|
107
|
+
const peers = _peers
|
|
108
|
+
// only validators make a chance
|
|
109
|
+
.filter((peer) => this.#validators[peer[0]])
|
|
110
|
+
// add up the bytes
|
|
111
|
+
.map((peer) => {
|
|
112
|
+
peer[1].totalBytes = peer[1].bw.up + peer[1].bw.down
|
|
113
|
+
return peer
|
|
114
|
+
})
|
|
115
|
+
.sort((a, b) => b[1].totalBytes - a[1].totalBytes)
|
|
116
|
+
// only return 128 best participating max
|
|
117
|
+
.splice(0, _peers.length > 128 ? 128 : _peers.length)
|
|
118
|
+
|
|
119
|
+
const luckyNumber = lottery(1, peers.length)
|
|
120
|
+
let nextValidator = this.#validators[peers[luckyNumber[0]][0]]
|
|
121
|
+
// redraw when the validator is the same
|
|
122
|
+
// but keep the net alive when only one validator is found
|
|
123
|
+
if (this.#currentValidator === nextValidator && peers.length !== 1) {
|
|
124
|
+
const luckyNumber = lottery(1, peers.length)
|
|
125
|
+
nextValidator = this.#validators[peers[luckyNumber[0]][0]]
|
|
126
|
+
}
|
|
127
|
+
this.#currentValidator = nextValidator
|
|
128
|
+
}
|
|
91
129
|
}
|