-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathacpJobOffering.ts
More file actions
160 lines (137 loc) · 4.34 KB
/
acpJobOffering.ts
File metadata and controls
160 lines (137 loc) · 4.34 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import { Address, zeroAddress } from "viem";
import AcpClient from "./acpClient";
import Ajv from "ajv";
import addFormats from "ajv-formats";
import { FareAmount } from "./acpFare";
import AcpError from "./acpError";
import BaseAcpContractClient, {
AcpJobPhases,
MemoType,
OperationPayload,
} from "./contractClients/baseAcpContractClient";
import {
baseAcpConfig,
baseAcpX402Config,
baseSepoliaAcpConfig,
baseSepoliaAcpX402Config,
} from "./configs/acpConfigs";
import { USDC_TOKEN_ADDRESS } from "./constants";
export enum PriceType {
FIXED = "fixed",
PERCENTAGE = "percentage",
}
class AcpJobOffering {
private ajv: Ajv;
constructor(
private readonly acpClient: AcpClient,
private readonly acpContractClient: BaseAcpContractClient,
public providerAddress: Address,
public name: string,
public price: number,
public priceType: PriceType,
public requiredFunds: boolean,
public requirement?: Object | string,
public deliverable?: Object | string
) {
this.ajv = new Ajv({ allErrors: true });
addFormats(this.ajv);
this.ajv.addFormat("address", /^0x[a-fA-F0-9]{40}$/);
}
async initiateJob(
serviceRequirement: Object | string,
evaluatorAddress?: Address,
expiredAt: Date = new Date(Date.now() + 1000 * 60 * 60 * 24) // default: 1 day
) {
if (this.providerAddress === this.acpClient.walletAddress) {
throw new AcpError(
"Provider address cannot be the same as the client address"
);
}
if (this.requirement && typeof this.requirement === "object") {
const validator = this.ajv.compile(this.requirement);
const valid = validator(serviceRequirement);
if (!valid) {
throw new AcpError(this.ajv.errorsText(validator.errors));
}
}
const finalServiceRequirement: Record<string, any> = {
name: this.name,
requirement: serviceRequirement,
priceValue: this.price,
priceType: this.priceType,
};
const fareAmount = new FareAmount(
this.priceType === PriceType.FIXED ? this.price : 0,
this.acpContractClient.config.baseFare
);
const account = await this.acpClient.getByClientAndProvider(
this.acpContractClient.walletAddress,
this.providerAddress,
this.acpContractClient
);
const isV1 = [
baseSepoliaAcpConfig.contractAddress,
baseSepoliaAcpX402Config.contractAddress,
baseAcpConfig.contractAddress,
baseAcpX402Config.contractAddress,
].includes(this.acpContractClient.config.contractAddress);
let createJobPayload: OperationPayload;
const chainId = this.acpContractClient.config.chain
.id as keyof typeof USDC_TOKEN_ADDRESS;
const isUsdcPaymentToken =
USDC_TOKEN_ADDRESS[chainId].toLowerCase() ===
fareAmount.fare.contractAddress.toLowerCase();
const isX402Job =
this.acpContractClient.config.x402Config && isUsdcPaymentToken;
if (isV1 || !account) {
createJobPayload = this.acpContractClient.createJob(
this.providerAddress,
evaluatorAddress || this.acpContractClient.walletAddress,
expiredAt,
fareAmount.fare.contractAddress,
fareAmount.amount,
"",
isX402Job
);
} else {
createJobPayload = this.acpContractClient.createJobWithAccount(
account.id,
evaluatorAddress || zeroAddress,
fareAmount.amount,
fareAmount.fare.contractAddress,
expiredAt,
isX402Job
);
}
const { userOpHash } = await this.acpContractClient.handleOperation([
createJobPayload,
]);
const jobId = await this.acpContractClient.getJobId(
userOpHash,
this.acpContractClient.walletAddress,
this.providerAddress
);
const payloads: OperationPayload[] = [];
const setBudgetWithPaymentTokenPayload =
this.acpContractClient.setBudgetWithPaymentToken(
jobId,
fareAmount.amount,
fareAmount.fare.contractAddress
);
if (setBudgetWithPaymentTokenPayload) {
payloads.push(setBudgetWithPaymentTokenPayload);
}
payloads.push(
this.acpContractClient.createMemo(
jobId,
JSON.stringify(finalServiceRequirement),
MemoType.MESSAGE,
true,
AcpJobPhases.NEGOTIATION
)
);
await this.acpContractClient.handleOperation(payloads);
return jobId;
}
}
export default AcpJobOffering;