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

94 lines
2.4 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';
/**
* Purpose: Keep track of reputation holders
*/
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,
{
postId,
fee,
duration,
tokenLossRatio,
contentiousDebate,
signingPublicKey,
authorStake,
anonymous,
},
) {
const validationPoolNumber = this.validationPools.size + 1;
const validationPool = new ValidationPool(
this,
authorId,
{
postId,
fee,
duration,
tokenLossRatio,
contentiousDebate,
signingPublicKey,
authorStake,
anonymous,
},
`pool${validationPoolNumber}`,
this.scene,
);
this.validationPools.set(validationPool.id, validationPool);
this.actions.createValidationPool.log(this, validationPool);
validationPool.activate();
return validationPool;
}
}