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

85 lines
2.7 KiB
JavaScript
Raw Normal View History

2023-01-02 13:14:32 -06:00
import { Action } from './action.js';
2022-12-31 16:08:42 -06:00
import { Actor } from './actor.js';
2023-01-01 21:09:02 -06:00
import { CryptoUtil } from './crypto.js';
import { PostContent } from './post.js';
class Request {
constructor(fee, content) {
this.id = CryptoUtil.randomUUID();
this.fee = fee;
this.content = content;
}
}
2022-12-31 16:08:42 -06:00
/**
* Purpose: Enable fee-driven work requests, to be completed by workers from the availability pool
*/
export class Business extends Actor {
2023-01-01 21:09:02 -06:00
requests = new Map();
constructor(bench, forum, availability, name, scene) {
super(name, scene);
this.bench = bench;
this.forum = forum;
this.availability = availability;
2023-01-02 13:14:32 -06:00
this.actions = {
assignWork: new Action('assign work', scene),
addPost: new Action('add post', scene),
initiateValidationPool: new Action('initiate validation pool', scene),
};
2023-01-01 21:09:02 -06:00
}
/**
* Fee should be held in escrow.
* That means there should be specific conditions under which the fee will be refunded.
* That means the submission should include some time value to indicate when it expires.
* There could be separate thresholds to indicate the earliest that the job may be cancelled,
* and the time at which the job will be automatically cancelled.
*/
async submitRequest(fee, content) {
const request = new Request(fee, content);
this.requests.set(request.id, request);
2023-01-02 13:14:32 -06:00
this.actions.assignWork.log(this, this.availability);
2023-01-01 21:09:02 -06:00
await this.availability.assignWork(request.id);
return request.id;
}
2023-01-02 13:14:32 -06:00
async getRequest(requestId) {
const request = this.requests.get(requestId);
return request;
}
2023-01-01 21:09:02 -06:00
async submitWork(reputationPublicKey, requestId, workEvidence, { tokenLossRatio, duration }) {
2023-01-02 13:14:32 -06:00
const request = this.requests.get(requestId);
if (!request) {
throw new Error(`Request not found! id: ${requestId}`);
}
2023-01-01 21:09:02 -06:00
// Create a post representing this submission.
const post = new PostContent({
requestId,
workEvidence,
});
2023-01-02 13:14:32 -06:00
this.actions.addPost.log(this, this.forum);
2023-01-01 21:09:02 -06:00
await this.forum.addPost(reputationPublicKey, post);
// Initiate a validation pool for this work evidence.
// Validation pool supports secret ballots but we aren't using that here, since we want
// the post to be attributable to the reputation holder.
2023-01-02 13:14:32 -06:00
this.actions.initiateValidationPool.log(this, this.bench);
2023-01-01 21:09:02 -06:00
const pool = await this.bench.initiateValidationPool(reputationPublicKey, {
postId: post.id,
2023-01-02 13:14:32 -06:00
fee: request.fee,
2023-01-01 21:09:02 -06:00
duration,
tokenLossRatio,
signingPublicKey: reputationPublicKey,
2023-01-02 13:14:32 -06:00
anonymous: false,
2023-01-01 21:09:02 -06:00
});
2022-12-31 16:08:42 -06:00
2023-01-01 21:09:02 -06:00
// When the validation pool concludes,
// reputation should be awarded and fees should be distributed.
return pool;
}
2022-12-31 16:08:42 -06:00
}