73 lines
2.3 KiB
JavaScript
73 lines
2.3 KiB
JavaScript
|
import { Actor } from "./actor.js";
|
||
|
import { Reputations } from "./reputation.js";
|
||
|
import { ValidationPool } from "./validation-pool.js";
|
||
|
import { Vote } from "./vote.js";
|
||
|
import { Voter } from "./voter.js";
|
||
|
import params from "./params.js";
|
||
|
|
||
|
export class Bench extends Actor {
|
||
|
constructor(name, scene) {
|
||
|
super(name, scene);
|
||
|
this.validationPools = new Map();
|
||
|
this.voters = new Map();
|
||
|
this.reputations = new Reputations();
|
||
|
|
||
|
this.actions = {
|
||
|
};
|
||
|
}
|
||
|
|
||
|
listValidationPools() {
|
||
|
Array.from(this.validationPools.values());
|
||
|
}
|
||
|
|
||
|
listActiveVoters() {
|
||
|
const now = new Date();
|
||
|
const thresholdSet = !!params.activeVoterThreshold;
|
||
|
return Array.from(this.voters.values()).filter(voter => {
|
||
|
const hasVoted = !!voter.dateLastVote;
|
||
|
const withinThreshold = now - voter.dateLastVote >= params.activeVoterThreshold;
|
||
|
return hasVoted && (!thresholdSet || withinThreshold);
|
||
|
});
|
||
|
}
|
||
|
|
||
|
getTotalReputation() {
|
||
|
return this.reputations.getTotal();
|
||
|
}
|
||
|
|
||
|
getTotalAvailableReputation() {
|
||
|
return this.reputations.getTotalAvailable();
|
||
|
}
|
||
|
|
||
|
getTotalActiveReputation() {
|
||
|
return this.listActiveVoters()
|
||
|
.map(({reputationPublicKey}) => this.reputations.getTokens(reputationPublicKey))
|
||
|
.reduce((acc, cur) => acc += cur, 0);
|
||
|
}
|
||
|
|
||
|
getTotalActiveAvailableReputation() {
|
||
|
return this.listActiveVoters()
|
||
|
.map(({reputationPublicKey}) => this.reputations.getAvailableTokens(reputationPublicKey))
|
||
|
.reduce((acc, cur) => acc += cur, 0);
|
||
|
}
|
||
|
|
||
|
initiateValidationPool(authorId, {fee, duration, tokenLossRatio, contentiousDebate}) {
|
||
|
const vote = new ValidationPool(this, authorId, {fee, duration, tokenLossRatio, contentiousDebate});
|
||
|
this.validationPools.set(vote.id, vote);
|
||
|
return vote.id;
|
||
|
}
|
||
|
|
||
|
castVote(voteId, signingPublicKey, position, stake, lockingTime) {
|
||
|
const vote = new Vote(position, stake, lockingTime);
|
||
|
const validationPool = this.validationPools.get(voteId);
|
||
|
validationPool.castVote(signingPublicKey, vote);
|
||
|
}
|
||
|
|
||
|
revealIdentity(voteId, signingPublicKey, reputationPublicKey) {
|
||
|
const validationPool = this.validationPools.get(voteId);
|
||
|
const voter = this.voters.get(reputationPublicKey) ?? new Voter(reputationPublicKey);
|
||
|
voter.addVoteRecord(validationPool);
|
||
|
this.voters.set(reputationPublicKey, voter);
|
||
|
validationPool.revealIdentity(signingPublicKey, voter);
|
||
|
}
|
||
|
}
|