dao-governance-framework/forum-network/src/classes/expert.js

102 lines
3.7 KiB
JavaScript
Raw Normal View History

2022-12-31 16:08:42 -06:00
import { Action } from './action.js';
import { PostMessage } from './message.js';
import { CryptoUtil } from './crypto.js';
2023-01-18 01:07:10 -06:00
import { ReputationHolder } from './reputation-holder.js';
2023-01-18 01:07:10 -06:00
export class Expert extends ReputationHolder {
constructor(name, scene) {
2023-01-18 01:07:10 -06:00
super(undefined, name, scene);
this.actions = {
submitPostViaNetwork: new Action('submit post via network', scene),
2022-12-31 16:08:42 -06:00
submitPost: new Action('submit post', scene),
initiateValidationPool: new Action('initiate validation pool', scene),
2023-01-12 16:41:55 -06:00
stake: new Action('stake on post', scene),
2023-01-02 13:14:32 -06:00
registerAvailability: new Action('register availability', scene),
getAssignedWork: new Action('get assigned work', scene),
submitWork: new Action('submit work evidence', scene),
};
2022-11-13 12:23:30 -06:00
this.validationPools = new Map();
2023-01-18 01:07:10 -06:00
this.tokens = [];
}
async initialize() {
2022-11-12 16:20:42 -06:00
this.reputationKey = await CryptoUtil.generateAsymmetricKey();
2022-11-17 08:30:06 -06:00
// this.reputationPublicKey = await CryptoUtil.exportKey(this.reputationKey.publicKey);
this.reputationPublicKey = this.name;
2022-12-31 16:08:42 -06:00
this.status.set('Initialized');
return this;
}
async submitPostViaNetwork(forumNode, post, stake) {
2022-11-12 16:20:42 -06:00
// TODO: Include fee
const postMessage = new PostMessage({ post, stake });
2022-11-12 16:20:42 -06:00
await postMessage.sign(this.reputationKey);
await this.actions.submitPostViaNetwork.log(this, forumNode, null, { id: post.id });
// For now, directly call forumNode.receiveMessage();
await forumNode.receiveMessage(JSON.stringify(postMessage.toJSON()));
}
2022-11-11 16:52:57 -06:00
async submitPost(forum, postContent) {
// TODO: Include fee
await this.actions.submitPost.log(this, forum);
return forum.addPost(this.reputationPublicKey, postContent);
}
async submitPostWithFee(bench, forum, postContent, poolOptions) {
await this.actions.submitPost.log(this, forum);
const postId = await forum.addPost(this.reputationPublicKey, postContent);
const pool = await this.initiateValidationPool(bench, { ...poolOptions, postId });
2023-01-18 01:07:10 -06:00
this.tokens.push(pool.tokenId);
return { postId, pool };
}
async initiateValidationPool(bench, poolOptions) {
// For now, directly call bench.initiateValidationPool();
2023-01-18 01:07:10 -06:00
poolOptions.reputationPublicKey = this.reputationPublicKey;
await this.actions.initiateValidationPool.log(
2022-11-30 09:13:52 -06:00
this,
bench,
2023-01-18 01:07:10 -06:00
`(fee: ${poolOptions.fee}, stake: ${poolOptions.authorStakeAmount ?? 0})`,
2022-11-30 09:13:52 -06:00
);
const pool = await bench.initiateValidationPool(poolOptions);
2023-01-18 01:07:10 -06:00
this.tokens.push(pool.tokenId);
this.validationPools.set(pool.id, poolOptions);
2022-11-17 08:30:06 -06:00
return pool;
2022-11-12 16:20:42 -06:00
}
2023-01-12 16:41:55 -06:00
async stake(validationPool, {
2023-01-18 01:07:10 -06:00
position, amount, lockingTime,
2023-01-02 13:14:32 -06:00
}) {
2023-01-12 16:41:55 -06:00
// TODO: encrypt stake
2022-11-12 16:20:42 -06:00
// TODO: sign message
2023-01-12 16:41:55 -06:00
await this.actions.stake.log(
2022-11-30 09:13:52 -06:00
this,
validationPool,
2023-01-18 01:07:10 -06:00
`(${position ? 'for' : 'against'}, stake: ${amount})`,
2022-11-30 09:13:52 -06:00
);
return validationPool.stake(this.reputationPublicKey, {
2023-01-18 01:07:10 -06:00
position, amount, lockingTime, tokenId: this.tokens[0],
2023-01-02 13:14:32 -06:00
});
2022-11-11 16:52:57 -06:00
}
async registerAvailability(availability, stakeAmount, duration) {
await this.actions.registerAvailability.log(this, availability, `(stake: ${stakeAmount}, duration: ${duration})`);
this.workerId = await availability.register(this.reputationPublicKey, {
stakeAmount,
tokenId: this.tokens[0],
duration,
});
2023-01-02 13:14:32 -06:00
}
async getAssignedWork(availability, business) {
const requestId = await availability.getAssignedWork(this.workerId);
2023-01-02 13:14:32 -06:00
const request = await business.getRequest(requestId);
return request;
}
async submitWork(business, requestId, evidence, { tokenLossRatio, duration }) {
await this.actions.submitWork.log(this, business);
2023-01-02 13:14:32 -06:00
return business.submitWork(this.reputationPublicKey, requestId, evidence, { tokenLossRatio, duration });
2023-01-01 21:09:02 -06:00
}
}