-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathacpJob.ts
More file actions
92 lines (73 loc) · 2.22 KB
/
acpJob.ts
File metadata and controls
92 lines (73 loc) · 2.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import { Address } from "viem";
import AcpClient from "./acpClient";
import { AcpJobPhases } from "./acpContractClient";
import AcpMemo from "./acpMemo";
class AcpJob {
constructor(
private acpClient: AcpClient,
public id: number,
public clientAddress: Address,
public providerAddress: Address,
public evaluatorAddress: Address,
public price: number,
public memos: AcpMemo[],
public phase: AcpJobPhases,
public context: Record<string, any> | null
) { }
public get serviceRequirement() {
return this.memos.find((m) => m.nextPhase === AcpJobPhases.NEGOTIATION)
?.content;
}
public get deliverable() {
return this.memos.find((m) => m.nextPhase === AcpJobPhases.COMPLETED)
?.content;
}
public get providerAgent() {
return this.acpClient.getAgent(this.providerAddress);
}
public get clientAgent() {
return this.acpClient.getAgent(this.clientAddress);
}
public get evaluatorAgent() {
return this.acpClient.getAgent(this.evaluatorAddress);
}
async pay(amount: number, reason?: string) {
const memo = this.memos.find(
(m) => m.nextPhase === AcpJobPhases.TRANSACTION
);
if (!memo) {
throw new Error("No transaction memo found");
}
return await this.acpClient.payJob(this.id, amount, memo.id, reason);
}
async respond(accept: boolean, reason?: string) {
const memo = this.memos.find(
(m) => m.nextPhase === AcpJobPhases.NEGOTIATION
);
if (!memo) {
throw new Error("No negotiation memo found");
}
return await this.acpClient.respondJob(this.id, memo.id, accept, reason);
}
async deliver(deliverable: string) {
const memo = this.memos.find(
(m) => m.nextPhase === AcpJobPhases.EVALUATION
);
if (!memo) {
throw new Error("No transaction memo found");
}
return await this.acpClient.deliverJob(this.id, deliverable);
}
async evaluate(accept: boolean, reason?: string) {
const memo = this.memos.find((m) => m.nextPhase === AcpJobPhases.COMPLETED);
if (!memo) {
throw new Error("No evaluation memo found");
}
return await this.acpClient.acpContractClient.signMemo(
memo.id,
accept,
reason
);
}
}
export default AcpJob;