Skip to content

Commit 965e63a

Browse files
committed
refactor: update acp helper functions
1 parent 1a3bb44 commit 965e63a

3 files changed

Lines changed: 56 additions & 25 deletions

File tree

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,40 @@
11
import AcpClient, { AcpContractClientV2 } from "@virtuals-protocol/acp-node";
22
import * as dotenv from "dotenv";
3-
import { Address } from "viem";
3+
import { BUYER_AGENT_WALLET_ADDRESS, BUYER_ENTITY_ID, WHITELISTED_WALLET_PRIVATE_KEY } from "./env";
44

55
// Load environment variables
66
dotenv.config({override: true});
77

8+
function subsection(title: string) {
9+
console.log(`\n--- ${title} ---`);
10+
}
11+
812
async function testHelperFunctions() {
9-
console.log("Testing ACP helper functions...");
13+
console.log(`\n${"=".repeat(60)}`);
14+
console.log("🔹 ACP Helper Functions Test");
15+
console.log(`${"=".repeat(60)}\n`);
1016

11-
// Initialize AcpClient
17+
console.log("Initializing ACP client...\n");
1218
const acpClient = new AcpClient({
1319
acpContractClient: await AcpContractClientV2.build(
14-
process.env.WHITELISTED_WALLET_PRIVATE_KEY as `0x${string}`,
15-
Number(process.env.WHITELISTED_WALLET_ENTITY_ID),
16-
process.env.BUYER_AGENT_WALLET_ADDRESS as Address
20+
WHITELISTED_WALLET_PRIVATE_KEY,
21+
BUYER_ENTITY_ID,
22+
BUYER_AGENT_WALLET_ADDRESS,
1723
)
1824
});
1925

20-
// Get active jobs
21-
const activeJobs = await acpClient.getActiveJobs(1, 10);
26+
/* ---------------- ACTIVE JOBS ---------------- */
27+
subsection("Active Jobs");
28+
const activeJobs = await acpClient.getActiveJobs(1, 3);
2229
console.log("\n🔵 Active Jobs:");
2330
console.log(activeJobs.length > 0 ? activeJobs : "No active jobs found.");
2431

25-
// Get completed jobs
26-
const completedJobs = await acpClient.getCompletedJobs(1, 10);
32+
/* ---------------- COMPLETED JOBS ---------------- */
33+
const completedJobs = await acpClient.getCompletedJobs(1, 3);
2734
console.log("\n✅ Completed Jobs:");
28-
console.log(completedJobs.length > 0 ? completedJobs : "No completed jobs found.");
29-
30-
// Get cancelled jobs
31-
const cancelledJobs = await acpClient.getCancelledJobs(1, 10);
32-
console.log("\n❌ Cancelled Jobs:");
33-
console.log(cancelledJobs.length > 0 ? cancelledJobs : "No cancelled jobs found.");
34-
3535
if (completedJobs.length > 0) {
36+
console.log(completedJobs);
37+
3638
const onChainJobId = completedJobs[0].id;
3739
if (onChainJobId) {
3840
const job = await acpClient.getJobById(onChainJobId);
@@ -50,26 +52,31 @@ async function testHelperFunctions() {
5052
}
5153
}
5254
} else {
53-
console.log("\n⚠️ No completed jobs available for detailed inspection.");
55+
console.log("No completed jobs found.");
5456
}
5557

56-
// Get jobs with pending memos
57-
const jobsWithPendingMemos = await acpClient.getPendingMemoJobs();
58+
/* ---------------- CANCELLED JOBS ---------------- */
59+
const cancelledJobs = await acpClient.getCancelledJobs(1, 3);
60+
console.log("\n❌ Cancelled Jobs:");
61+
console.log(cancelledJobs.length > 0 ? cancelledJobs : "No cancelled jobs found.");
62+
63+
/* ---------------- PENDING MEMO JOBS ---------------- */
64+
const jobsWithPendingMemos = await acpClient.getPendingMemoJobs(1, 3);
5865
console.log(jobsWithPendingMemos.length > 0 ? jobsWithPendingMemos : "No jobs with pending memos jobs found.");
5966

60-
// Get agent
67+
/* ---------------- AGENT INFO ---------------- */
6168
const agentWalletAddress = acpClient.walletAddress;
6269
const agent = await acpClient.getAgent(agentWalletAddress);
6370
console.log(agent ? agent : `No agent with wallet address ${jobsWithPendingMemos} found.`);
6471
}
6572

66-
// Run the test
6773
testHelperFunctions()
6874
.then(() => {
6975
console.log("\n✨ Test completed successfully");
7076
process.exit(0);
7177
})
72-
.catch(error => {
73-
console.error("Error in helper functions test:", error);
78+
.catch((error) => {
79+
console.error("\n❌ Error in helper functions test:");
80+
console.error(error);
7481
process.exit(1);
7582
});

examples/acp-base/helpers/env.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import dotenv from "dotenv";
2+
import { Address } from "viem";
3+
4+
dotenv.config({ path: __dirname + "/.env" });
5+
6+
function getEnvVar<T extends string = string>(key: string, required = true): T {
7+
const value = process.env[key];
8+
if (required && (value === undefined || value === "")) {
9+
throw new Error(`${key} is not defined or is empty in the .env file`);
10+
}
11+
return value as T;
12+
}
13+
14+
export const WHITELISTED_WALLET_PRIVATE_KEY = getEnvVar<Address>(
15+
"WHITELISTED_WALLET_PRIVATE_KEY"
16+
);
17+
18+
export const BUYER_AGENT_WALLET_ADDRESS = getEnvVar<Address>(
19+
"BUYER_AGENT_WALLET_ADDRESS"
20+
);
21+
22+
export const BUYER_ENTITY_ID = parseInt(getEnvVar("BUYER_ENTITY_ID"));
23+
24+
if (isNaN(BUYER_ENTITY_ID)) throw new Error("BUYER_ENTITY_ID must be a valid number");

src/acpClient.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -539,7 +539,7 @@ class AcpClient {
539539

540540
if (errors.length > 0) {
541541
console.warn(
542-
`[ACP] ${options?.logPrefix ?? "Skipped"} ${errors.length} malformed job(s)`,
542+
`${options?.logPrefix ?? "Skipped"} ${errors.length} malformed job(s)`,
543543
errors.map(e => ({ jobId: e.jobId, message: e.error.message }))
544544
);
545545
}

0 commit comments

Comments
 (0)