dao-governance-framework/forum-network/public/classes/bench.js

65 lines
2.0 KiB
JavaScript

import { Actor } from "./actor.js";
import { Reputations } from "./reputation.js";
import { ValidationPool } from "./validation-pool.js";
import params from "./params.js";
import { Action } from "./action.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 = {
createValidationPool: new Action('create validation pool', scene),
};
this.activate();
}
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 validationPoolNumber = this.validationPools.size + 1;
const validationPool = new ValidationPool(this, authorId, {fee, duration, tokenLossRatio, contentiousDebate},
`pool${validationPoolNumber}`, scene);
this.validationPools.set(validationPool.id, validationPool);
this.actions.createValidationPool.log(this, validationPool)
validationPool.activate();
return validationPool;
}
}