Skip to content

Commit f83419e

Browse files
committed
refactor: use _hydrateJob function
1 parent fec3dd6 commit f83419e

1 file changed

Lines changed: 38 additions & 129 deletions

File tree

src/acpClient.ts

Lines changed: 38 additions & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,40 @@ class AcpClient {
118118
return this.acpContractClient.walletAddress;
119119
}
120120

121+
private _hydrateJob(data: IAcpJob["data"]): AcpJob {
122+
const memos = data.memos.map((memo) =>
123+
new AcpMemo(
124+
this.contractClientByAddress(data.contractAddress),
125+
memo.id,
126+
memo.memoType,
127+
memo.content,
128+
memo.nextPhase,
129+
memo.status,
130+
memo.senderAddress,
131+
memo.signedReason,
132+
memo.expiry ? new Date(parseInt(memo.expiry) * 1000) : undefined,
133+
memo.payableDetails,
134+
memo.txHash,
135+
memo.signedTxHash,
136+
)
137+
);
138+
139+
return new AcpJob(
140+
this,
141+
data.id,
142+
data.clientAddress,
143+
data.providerAddress,
144+
data.evaluatorAddress,
145+
data.price,
146+
data.priceTokenAddress,
147+
memos,
148+
data.phase,
149+
data.context,
150+
data.contractAddress,
151+
data.deliverable,
152+
);
153+
}
154+
121155
async init(skipSocketConnection: boolean = false) {
122156
if (skipSocketConnection) {
123157
return;
@@ -146,38 +180,7 @@ class AcpClient {
146180
callback(true);
147181

148182
if (this.onEvaluate) {
149-
const job = new AcpJob(
150-
this,
151-
data.id,
152-
data.clientAddress,
153-
data.providerAddress,
154-
data.evaluatorAddress,
155-
data.price,
156-
data.priceTokenAddress,
157-
data.memos.map((memo) => {
158-
return new AcpMemo(
159-
this.contractClientByAddress(data.contractAddress),
160-
memo.id,
161-
memo.memoType,
162-
memo.content,
163-
memo.nextPhase,
164-
memo.status,
165-
memo.senderAddress,
166-
memo.signedReason,
167-
memo.expiry
168-
? new Date(parseInt(memo.expiry) * 1000)
169-
: undefined,
170-
memo.payableDetails,
171-
memo.txHash,
172-
memo.signedTxHash,
173-
);
174-
}),
175-
data.phase,
176-
data.context,
177-
data.contractAddress,
178-
data.netPayableAmount,
179-
);
180-
183+
const job = this._hydrateJob(data);
181184
this.onEvaluate(job);
182185
}
183186
},
@@ -189,38 +192,7 @@ class AcpClient {
189192
callback(true);
190193

191194
if (this.onNewTask) {
192-
const job = new AcpJob(
193-
this,
194-
data.id,
195-
data.clientAddress,
196-
data.providerAddress,
197-
data.evaluatorAddress,
198-
data.price,
199-
data.priceTokenAddress,
200-
data.memos.map((memo) => {
201-
return new AcpMemo(
202-
this.contractClientByAddress(data.contractAddress),
203-
memo.id,
204-
memo.memoType,
205-
memo.content,
206-
memo.nextPhase,
207-
memo.status,
208-
memo.senderAddress,
209-
memo.signedReason,
210-
memo.expiry
211-
? new Date(parseInt(memo.expiry) * 1000)
212-
: undefined,
213-
memo.payableDetails,
214-
memo.txHash,
215-
memo.signedTxHash,
216-
);
217-
}),
218-
data.phase,
219-
data.context,
220-
data.contractAddress,
221-
data.netPayableAmount,
222-
);
223-
195+
const job = this._hydrateJob(data);
224196
this.onNewTask(
225197
job,
226198
job.memos.find((m) => m.id == data.memoToSign),
@@ -499,39 +471,7 @@ class AcpClient {
499471

500472
for (const job of rawJobs) {
501473
try {
502-
const memos = job.memos.map((memo) =>
503-
new AcpMemo(
504-
this.contractClientByAddress(job.contractAddress),
505-
memo.id,
506-
memo.memoType,
507-
memo.content,
508-
memo.nextPhase,
509-
memo.status,
510-
memo.senderAddress,
511-
memo.signedReason,
512-
memo.expiry ? new Date(parseInt(memo.expiry) * 1000) : undefined,
513-
memo.payableDetails,
514-
memo.txHash,
515-
memo.signedTxHash,
516-
)
517-
);
518-
519-
jobs.push(
520-
new AcpJob(
521-
this,
522-
job.id,
523-
job.clientAddress,
524-
job.providerAddress,
525-
job.evaluatorAddress,
526-
job.price,
527-
job.priceTokenAddress,
528-
memos,
529-
job.phase,
530-
job.context,
531-
job.contractAddress,
532-
job.netPayableAmount,
533-
)
534-
);
474+
jobs.push(this._hydrateJob(job));
535475
} catch (err) {
536476
errors.push({ jobId: job.id, error: err as Error });
537477
}
@@ -582,38 +522,7 @@ class AcpClient {
582522
}
583523

584524
try {
585-
const memos = job.memos.map(
586-
(memo) =>
587-
new AcpMemo(
588-
this.contractClientByAddress(job.contractAddress),
589-
memo.id,
590-
memo.memoType,
591-
memo.content,
592-
memo.nextPhase,
593-
memo.status,
594-
memo.senderAddress,
595-
memo.signedReason,
596-
memo.expiry ? new Date(parseInt(memo.expiry) * 1000) : undefined,
597-
memo.payableDetails,
598-
memo.txHash,
599-
memo.signedTxHash,
600-
)
601-
);
602-
603-
return new AcpJob(
604-
this,
605-
job.id,
606-
job.clientAddress,
607-
job.providerAddress,
608-
job.evaluatorAddress,
609-
job.price,
610-
job.priceTokenAddress,
611-
memos,
612-
job.phase,
613-
job.context,
614-
job.contractAddress,
615-
job.netPayableAmount,
616-
);
525+
return this._hydrateJob(job);
617526
} catch (err) {
618527
throw new AcpError(`Failed to hydrate job ${jobId}`, err);
619528
}

0 commit comments

Comments
 (0)