import { Action } from './action.js'; import { Actor } from './actor.js'; import { CryptoUtil } from './crypto.js'; import { PostContent } from './post-content.js'; class Request { constructor(fee, content) { this.id = CryptoUtil.randomUUID(); this.fee = fee; this.content = content; } } /** * Purpose: Enable fee-driven work requests, to be completed by workers from the availability pool */ export class Business extends Actor { requests = new Map(); constructor(bench, forum, availability, name, scene) { super(name, scene); this.bench = bench; this.forum = forum; this.availability = availability; this.actions = { assignWork: new Action('assign work', scene), submitPost: new Action('submit post', scene), initiateValidationPool: new Action('initiate validation pool', scene), }; } async submitRequest(fee, content) { const request = new Request(fee, content); this.requests.set(request.id, request); await this.actions.assignWork.log(this, this.availability); this.worker = await this.availability.assignWork(request.id); return request.id; } async getRequest(requestId) { const request = this.requests.get(requestId); return request; } async submitWork(reputationPublicKey, requestId, workEvidence, { tokenLossRatio, duration }) { const request = this.requests.get(requestId); if (!request) { throw new Error(`Request not found! id: ${requestId}`); } // Create a post representing this submission. const post = new PostContent({ requestId, workEvidence, }); await this.actions.submitPost.log(this, this.forum); const postId = 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. await this.actions.initiateValidationPool.log(this, this.bench); const pool = await this.bench.initiateValidationPool({ postId, fee: request.fee, duration, tokenLossRatio, reputationPublicKey, authorStakeAmount: this.worker.stakeAmount, tokenId: this.worker.tokenId, }); // When the validation pool concludes, // reputation should be awarded and fees should be distributed. return pool; } }