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

90 lines
2.3 KiB
JavaScript
Raw Normal View History

2022-12-31 16:08:42 -06:00
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';
2022-12-31 16:08:42 -06:00
/**
* 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 = {
2022-12-31 16:08:42 -06:00
createValidationPool: new Action('create validation pool', scene),
};
2022-11-13 12:23:30 -06:00
this.activate();
}
listValidationPools() {
Array.from(this.validationPools.values());
}
listActiveVoters() {
const now = new Date();
const thresholdSet = !!params.activeVoterThreshold;
2022-11-30 09:13:52 -06:00
return Array.from(this.voters.values()).filter((voter) => {
const hasVoted = !!voter.dateLastVote;
2022-12-31 16:08:42 -06:00
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()
2022-12-31 16:08:42 -06:00
.map(({ reputationPublicKey }) => this.reputations.getTokens(reputationPublicKey))
2022-11-30 09:13:52 -06:00
.reduce((acc, cur) => (acc += cur), 0);
}
getTotalActiveAvailableReputation() {
return this.listActiveVoters()
2022-12-31 16:08:42 -06:00
.map(({ reputationPublicKey }) => this.reputations.getAvailableTokens(reputationPublicKey))
2022-11-30 09:13:52 -06:00
.reduce((acc, cur) => (acc += cur), 0);
}
2022-11-30 09:13:52 -06:00
initiateValidationPool(
authorId,
{
postId,
fee,
duration,
tokenLossRatio,
contentiousDebate,
signingPublicKey,
2022-12-31 16:08:42 -06:00
},
2022-11-30 09:13:52 -06:00
) {
2022-11-13 12:23:30 -06:00
const validationPoolNumber = this.validationPools.size + 1;
2022-11-30 09:13:52 -06:00
const validationPool = new ValidationPool(
this,
authorId,
{
postId,
fee,
duration,
tokenLossRatio,
contentiousDebate,
signingPublicKey,
},
`pool${validationPoolNumber}`,
2022-12-31 16:08:42 -06:00
this.scene,
2022-11-30 09:13:52 -06:00
);
2022-11-13 12:23:30 -06:00
this.validationPools.set(validationPool.id, validationPool);
2022-11-30 09:13:52 -06:00
this.actions.createValidationPool.log(this, validationPool);
2022-11-13 12:23:30 -06:00
validationPool.activate();
return validationPool;
}
}