@virtuals-protocol/acp-node 0.1.0-beta.9 → 0.2.0-beta.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.mjs CHANGED
@@ -1,4 +1,5 @@
1
1
  var __defProp = Object.defineProperty;
2
+ var __getOwnPropNames = Object.getOwnPropertyNames;
2
3
  var __getOwnPropSymbols = Object.getOwnPropertySymbols;
3
4
  var __hasOwnProp = Object.prototype.hasOwnProperty;
4
5
  var __propIsEnum = Object.prototype.propertyIsEnumerable;
@@ -14,6 +15,9 @@ var __spreadValues = (a, b) => {
14
15
  }
15
16
  return a;
16
17
  };
18
+ var __commonJS = (cb, mod) => function __require() {
19
+ return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
20
+ };
17
21
  var __async = (__this, __arguments, generator) => {
18
22
  return new Promise((resolve, reject) => {
19
23
  var fulfilled = (value) => {
@@ -35,6 +39,41 @@ var __async = (__this, __arguments, generator) => {
35
39
  });
36
40
  };
37
41
 
42
+ // package.json
43
+ var require_package = __commonJS({
44
+ "package.json"(exports, module) {
45
+ module.exports = {
46
+ name: "@virtuals-protocol/acp-node",
47
+ version: "0.2.0-beta.2",
48
+ main: "./dist/index.js",
49
+ module: "./dist/index.mjs",
50
+ types: "./dist/index.d.ts",
51
+ scripts: {
52
+ test: 'echo "Error: no test specified" && exit 1',
53
+ tsup: "tsup src/index.ts --dts --format cjs,esm --out-dir dist"
54
+ },
55
+ author: "",
56
+ license: "ISC",
57
+ description: "",
58
+ devDependencies: {
59
+ typescript: "^5.8.3"
60
+ },
61
+ dependencies: {
62
+ "@aa-sdk/core": "^4.30.0",
63
+ "@account-kit/infra": "^4.30.0",
64
+ "@account-kit/smart-contracts": "^4.30.0",
65
+ ajv: "^8.17.1",
66
+ "socket.io-client": "^4.8.1",
67
+ tsup: "^8.5.0",
68
+ viem: "^2.28.2"
69
+ },
70
+ files: [
71
+ "dist"
72
+ ]
73
+ };
74
+ }
75
+ });
76
+
38
77
  // src/acpAbi.ts
39
78
  var ACP_ABI = [
40
79
  { inputs: [], stateMutability: "nonpayable", type: "constructor" },
@@ -244,11 +283,176 @@ var ACP_ABI = [
244
283
  internalType: "uint256",
245
284
  name: "memoId",
246
285
  type: "uint256"
286
+ },
287
+ {
288
+ indexed: false,
289
+ internalType: "string",
290
+ name: "content",
291
+ type: "string"
247
292
  }
248
293
  ],
249
294
  name: "NewMemo",
250
295
  type: "event"
251
296
  },
297
+ {
298
+ anonymous: false,
299
+ inputs: [
300
+ {
301
+ indexed: true,
302
+ internalType: "uint256",
303
+ name: "jobId",
304
+ type: "uint256"
305
+ },
306
+ {
307
+ indexed: true,
308
+ internalType: "uint256",
309
+ name: "memoId",
310
+ type: "uint256"
311
+ },
312
+ {
313
+ indexed: true,
314
+ internalType: "address",
315
+ name: "payer",
316
+ type: "address"
317
+ },
318
+ {
319
+ indexed: false,
320
+ internalType: "uint256",
321
+ name: "amount",
322
+ type: "uint256"
323
+ }
324
+ ],
325
+ name: "PayableFeeCollected",
326
+ type: "event"
327
+ },
328
+ {
329
+ anonymous: false,
330
+ inputs: [
331
+ {
332
+ indexed: true,
333
+ internalType: "uint256",
334
+ name: "jobId",
335
+ type: "uint256"
336
+ },
337
+ {
338
+ indexed: true,
339
+ internalType: "uint256",
340
+ name: "memoId",
341
+ type: "uint256"
342
+ },
343
+ {
344
+ indexed: true,
345
+ internalType: "address",
346
+ name: "payer",
347
+ type: "address"
348
+ },
349
+ {
350
+ indexed: false,
351
+ internalType: "address",
352
+ name: "recipient",
353
+ type: "address"
354
+ },
355
+ {
356
+ indexed: false,
357
+ internalType: "uint256",
358
+ name: "netAmount",
359
+ type: "uint256"
360
+ }
361
+ ],
362
+ name: "PayableFeeRequestExecuted",
363
+ type: "event"
364
+ },
365
+ {
366
+ anonymous: false,
367
+ inputs: [
368
+ {
369
+ indexed: true,
370
+ internalType: "uint256",
371
+ name: "jobId",
372
+ type: "uint256"
373
+ },
374
+ {
375
+ indexed: true,
376
+ internalType: "uint256",
377
+ name: "memoId",
378
+ type: "uint256"
379
+ },
380
+ { indexed: true, internalType: "address", name: "from", type: "address" },
381
+ { indexed: false, internalType: "address", name: "to", type: "address" },
382
+ {
383
+ indexed: false,
384
+ internalType: "address",
385
+ name: "token",
386
+ type: "address"
387
+ },
388
+ {
389
+ indexed: false,
390
+ internalType: "uint256",
391
+ name: "amount",
392
+ type: "uint256"
393
+ }
394
+ ],
395
+ name: "PayableRequestExecuted",
396
+ type: "event"
397
+ },
398
+ {
399
+ anonymous: false,
400
+ inputs: [
401
+ {
402
+ indexed: true,
403
+ internalType: "uint256",
404
+ name: "jobId",
405
+ type: "uint256"
406
+ },
407
+ {
408
+ indexed: true,
409
+ internalType: "uint256",
410
+ name: "memoId",
411
+ type: "uint256"
412
+ },
413
+ { indexed: true, internalType: "address", name: "from", type: "address" },
414
+ { indexed: false, internalType: "address", name: "to", type: "address" },
415
+ {
416
+ indexed: false,
417
+ internalType: "address",
418
+ name: "token",
419
+ type: "address"
420
+ },
421
+ {
422
+ indexed: false,
423
+ internalType: "uint256",
424
+ name: "amount",
425
+ type: "uint256"
426
+ }
427
+ ],
428
+ name: "PayableTransferExecuted",
429
+ type: "event"
430
+ },
431
+ {
432
+ anonymous: false,
433
+ inputs: [
434
+ {
435
+ indexed: true,
436
+ internalType: "uint256",
437
+ name: "jobId",
438
+ type: "uint256"
439
+ },
440
+ {
441
+ indexed: true,
442
+ internalType: "address",
443
+ name: "client",
444
+ type: "address"
445
+ },
446
+ {
447
+ indexed: false,
448
+ internalType: "uint256",
449
+ name: "amount",
450
+ type: "uint256"
451
+ }
452
+ ],
453
+ name: "RefundedAdditionalFees",
454
+ type: "event"
455
+ },
252
456
  {
253
457
  anonymous: false,
254
458
  inputs: [
@@ -362,6 +566,13 @@ var ACP_ABI = [
362
566
  stateMutability: "view",
363
567
  type: "function"
364
568
  },
569
+ {
570
+ inputs: [],
571
+ name: "PHASE_EXPIRED",
572
+ outputs: [{ internalType: "uint8", name: "", type: "uint8" }],
573
+ stateMutability: "view",
574
+ type: "function"
575
+ },
365
576
  {
366
577
  inputs: [],
367
578
  name: "PHASE_NEGOTIATION",
@@ -400,11 +611,26 @@ var ACP_ABI = [
400
611
  {
401
612
  inputs: [
402
613
  { internalType: "address", name: "account", type: "address" },
403
- { internalType: "uint256", name: "jobId", type: "uint256" }
614
+ {
615
+ components: [
616
+ { internalType: "uint256", name: "id", type: "uint256" },
617
+ { internalType: "address", name: "client", type: "address" },
618
+ { internalType: "address", name: "provider", type: "address" },
619
+ { internalType: "uint256", name: "budget", type: "uint256" },
620
+ { internalType: "uint256", name: "amountClaimed", type: "uint256" },
621
+ { internalType: "uint8", name: "phase", type: "uint8" },
622
+ { internalType: "uint256", name: "memoCount", type: "uint256" },
623
+ { internalType: "uint256", name: "expiredAt", type: "uint256" },
624
+ { internalType: "address", name: "evaluator", type: "address" }
625
+ ],
626
+ internalType: "struct ACPSimple.Job",
627
+ name: "job",
628
+ type: "tuple"
629
+ }
404
630
  ],
405
631
  name: "canSign",
406
632
  outputs: [{ internalType: "bool", name: "", type: "bool" }],
407
- stateMutability: "view",
633
+ stateMutability: "pure",
408
634
  type: "function"
409
635
  },
410
636
  {
@@ -442,6 +668,32 @@ var ACP_ABI = [
442
668
  stateMutability: "nonpayable",
443
669
  type: "function"
444
670
  },
671
+ {
672
+ inputs: [
673
+ { internalType: "uint256", name: "jobId", type: "uint256" },
674
+ { internalType: "string", name: "content", type: "string" },
675
+ { internalType: "address", name: "token", type: "address" },
676
+ { internalType: "uint256", name: "amount", type: "uint256" },
677
+ { internalType: "address", name: "recipient", type: "address" },
678
+ { internalType: "uint256", name: "feeAmount", type: "uint256" },
679
+ {
680
+ internalType: "enum ACPSimple.FeeType",
681
+ name: "feeType",
682
+ type: "uint8"
683
+ },
684
+ {
685
+ internalType: "enum InteractionLedger.MemoType",
686
+ name: "memoType",
687
+ type: "uint8"
688
+ },
689
+ { internalType: "uint8", name: "nextPhase", type: "uint8" },
690
+ { internalType: "uint256", name: "expiredAt", type: "uint256" }
691
+ ],
692
+ name: "createPayableMemo",
693
+ outputs: [{ internalType: "uint256", name: "", type: "uint256" }],
694
+ stateMutability: "nonpayable",
695
+ type: "function"
696
+ },
445
697
  {
446
698
  inputs: [],
447
699
  name: "evaluatorFeeBP",
@@ -479,6 +731,16 @@ var ACP_ABI = [
479
731
  stateMutability: "view",
480
732
  type: "function"
481
733
  },
734
+ {
735
+ inputs: [
736
+ { internalType: "uint256", name: "jobId", type: "uint256" },
737
+ { internalType: "uint8", name: "phase", type: "uint8" }
738
+ ],
739
+ name: "getJobPhaseMemoIds",
740
+ outputs: [{ internalType: "uint256[]", name: "", type: "uint256[]" }],
741
+ stateMutability: "view",
742
+ type: "function"
743
+ },
482
744
  {
483
745
  inputs: [
484
746
  { internalType: "uint256", name: "jobId", type: "uint256" },
@@ -513,7 +775,7 @@ var ACP_ABI = [
513
775
  {
514
776
  inputs: [],
515
777
  name: "getPhases",
516
- outputs: [{ internalType: "string[6]", name: "", type: "string[6]" }],
778
+ outputs: [{ internalType: "string[7]", name: "", type: "string[7]" }],
517
779
  stateMutability: "pure",
518
780
  type: "function"
519
781
  },
@@ -566,6 +828,20 @@ var ACP_ABI = [
566
828
  stateMutability: "view",
567
829
  type: "function"
568
830
  },
831
+ {
832
+ inputs: [{ internalType: "uint256", name: "memoId", type: "uint256" }],
833
+ name: "isPayableMemo",
834
+ outputs: [{ internalType: "bool", name: "", type: "bool" }],
835
+ stateMutability: "view",
836
+ type: "function"
837
+ },
838
+ {
839
+ inputs: [{ internalType: "uint256", name: "jobId", type: "uint256" }],
840
+ name: "jobAdditionalFees",
841
+ outputs: [{ internalType: "uint256", name: "", type: "uint256" }],
842
+ stateMutability: "view",
843
+ type: "function"
844
+ },
569
845
  {
570
846
  inputs: [],
571
847
  name: "jobCounter",
@@ -608,6 +884,31 @@ var ACP_ABI = [
608
884
  stateMutability: "view",
609
885
  type: "function"
610
886
  },
887
+ {
888
+ inputs: [{ internalType: "uint256", name: "memoId", type: "uint256" }],
889
+ name: "memoExpiredAt",
890
+ outputs: [{ internalType: "uint256", name: "expiredAt", type: "uint256" }],
891
+ stateMutability: "view",
892
+ type: "function"
893
+ },
894
+ {
895
+ inputs: [{ internalType: "uint256", name: "", type: "uint256" }],
896
+ name: "memos",
897
+ outputs: [
898
+ { internalType: "string", name: "content", type: "string" },
899
+ {
900
+ internalType: "enum InteractionLedger.MemoType",
901
+ name: "memoType",
902
+ type: "uint8"
903
+ },
904
+ { internalType: "bool", name: "isSecured", type: "bool" },
905
+ { internalType: "uint8", name: "nextPhase", type: "uint8" },
906
+ { internalType: "uint256", name: "jobId", type: "uint256" },
907
+ { internalType: "address", name: "sender", type: "address" }
908
+ ],
909
+ stateMutability: "view",
910
+ type: "function"
911
+ },
611
912
  {
612
913
  inputs: [],
613
914
  name: "numEvaluatorsPerJob",
@@ -615,6 +916,24 @@ var ACP_ABI = [
615
916
  stateMutability: "view",
616
917
  type: "function"
617
918
  },
919
+ {
920
+ inputs: [{ internalType: "uint256", name: "memoId", type: "uint256" }],
921
+ name: "payableDetails",
922
+ outputs: [
923
+ { internalType: "address", name: "token", type: "address" },
924
+ { internalType: "uint256", name: "amount", type: "uint256" },
925
+ { internalType: "address", name: "recipient", type: "address" },
926
+ { internalType: "uint256", name: "feeAmount", type: "uint256" },
927
+ {
928
+ internalType: "enum ACPSimple.FeeType",
929
+ name: "feeType",
930
+ type: "uint8"
931
+ },
932
+ { internalType: "bool", name: "isExecuted", type: "bool" }
933
+ ],
934
+ stateMutability: "view",
935
+ type: "function"
936
+ },
618
937
  {
619
938
  inputs: [],
620
939
  name: "paymentToken",
@@ -731,21 +1050,35 @@ import {
731
1050
  import { baseSepolia, base } from "@account-kit/infra";
732
1051
  var baseSepoliaAcpConfig = {
733
1052
  chain: baseSepolia,
734
- contractAddress: "0x2422c1c43451Eb69Ff49dfD39c4Dc8C5230fA1e6",
1053
+ contractAddress: "0x8Db6B1c839Fc8f6bd35777E194677B67b4D51928",
735
1054
  virtualsTokenAddress: "0xbfAB80ccc15DF6fb7185f9498d6039317331846a",
736
1055
  alchemyRpcUrl: "https://alchemy-proxy.virtuals.io/api/proxy/rpc",
737
- acpUrl: "https://acpx-staging.virtuals.io"
1056
+ acpUrl: "https://acpx.virtuals.gg",
1057
+ priorityFeeMultiplier: 2,
1058
+ maxFeePerGas: 2e7,
1059
+ maxPriorityFeePerGas: 21e6
738
1060
  };
739
1061
  var baseAcpConfig = {
740
1062
  chain: base,
741
1063
  contractAddress: "0x6a1FE26D54ab0d3E1e3168f2e0c0cDa5cC0A0A4A",
742
1064
  virtualsTokenAddress: "0x0b3e328455c4059EEb9e3f84b5543F74E24e7E1b",
743
1065
  alchemyRpcUrl: "https://alchemy-proxy-prod.virtuals.io/api/proxy/rpc",
744
- acpUrl: "https://acpx.virtuals.io"
1066
+ acpUrl: "https://acpx.virtuals.io",
1067
+ priorityFeeMultiplier: 2,
1068
+ maxFeePerGas: 2e7,
1069
+ maxPriorityFeePerGas: 21e6
745
1070
  };
746
1071
 
747
1072
  // src/acpContractClient.ts
748
- import { encodeFunctionData, erc20Abi, fromHex } from "viem";
1073
+ import {
1074
+ createPublicClient,
1075
+ decodeEventLog,
1076
+ encodeFunctionData,
1077
+ erc20Abi,
1078
+ fromHex,
1079
+ http
1080
+ } from "viem";
1081
+ import { publicActionsL2 } from "viem/op-stack";
749
1082
  var MemoType = /* @__PURE__ */ ((MemoType2) => {
750
1083
  MemoType2[MemoType2["MESSAGE"] = 0] = "MESSAGE";
751
1084
  MemoType2[MemoType2["CONTEXT_URL"] = 1] = "CONTEXT_URL";
@@ -753,6 +1086,10 @@ var MemoType = /* @__PURE__ */ ((MemoType2) => {
753
1086
  MemoType2[MemoType2["VOICE_URL"] = 3] = "VOICE_URL";
754
1087
  MemoType2[MemoType2["OBJECT_URL"] = 4] = "OBJECT_URL";
755
1088
  MemoType2[MemoType2["TXHASH"] = 5] = "TXHASH";
1089
+ MemoType2[MemoType2["PAYABLE_REQUEST"] = 6] = "PAYABLE_REQUEST";
1090
+ MemoType2[MemoType2["PAYABLE_TRANSFER"] = 7] = "PAYABLE_TRANSFER";
1091
+ MemoType2[MemoType2["PAYABLE_FEE"] = 8] = "PAYABLE_FEE";
1092
+ MemoType2[MemoType2["PAYABLE_FEE_REQUEST"] = 9] = "PAYABLE_FEE_REQUEST";
756
1093
  return MemoType2;
757
1094
  })(MemoType || {});
758
1095
  var AcpJobPhases = /* @__PURE__ */ ((AcpJobPhases2) => {
@@ -762,25 +1099,34 @@ var AcpJobPhases = /* @__PURE__ */ ((AcpJobPhases2) => {
762
1099
  AcpJobPhases2[AcpJobPhases2["EVALUATION"] = 3] = "EVALUATION";
763
1100
  AcpJobPhases2[AcpJobPhases2["COMPLETED"] = 4] = "COMPLETED";
764
1101
  AcpJobPhases2[AcpJobPhases2["REJECTED"] = 5] = "REJECTED";
1102
+ AcpJobPhases2[AcpJobPhases2["EXPIRED"] = 6] = "EXPIRED";
765
1103
  return AcpJobPhases2;
766
1104
  })(AcpJobPhases || {});
767
1105
  var AcpContractClient = class _AcpContractClient {
768
- constructor(walletPrivateKey, sessionEntityKeyId, agentWalletAddress, config = baseAcpConfig) {
1106
+ constructor(walletPrivateKey, sessionEntityKeyId, agentWalletAddress, config = baseAcpConfig, customRpcUrl) {
769
1107
  this.walletPrivateKey = walletPrivateKey;
770
1108
  this.sessionEntityKeyId = sessionEntityKeyId;
771
1109
  this.agentWalletAddress = agentWalletAddress;
772
1110
  this.config = config;
1111
+ this.customRpcUrl = customRpcUrl;
1112
+ this.MAX_RETRIES = 3;
773
1113
  this.chain = config.chain;
774
1114
  this.contractAddress = config.contractAddress;
775
1115
  this.virtualsTokenAddress = config.virtualsTokenAddress;
1116
+ this.customRpcUrl = customRpcUrl;
1117
+ this.customRpcClient = createPublicClient({
1118
+ chain: this.chain,
1119
+ transport: this.customRpcUrl ? http(this.customRpcUrl) : http()
1120
+ }).extend(publicActionsL2());
776
1121
  }
777
- static build(_0, _1, _2) {
778
- return __async(this, arguments, function* (walletPrivateKey, sessionEntityKeyId, agentWalletAddress, config = baseAcpConfig) {
1122
+ static build(_0, _1, _2, _3) {
1123
+ return __async(this, arguments, function* (walletPrivateKey, sessionEntityKeyId, agentWalletAddress, customRpcUrl, config = baseAcpConfig) {
779
1124
  const acpContractClient = new _AcpContractClient(
780
1125
  walletPrivateKey,
781
1126
  sessionEntityKeyId,
782
1127
  agentWalletAddress,
783
- config
1128
+ config,
1129
+ customRpcUrl
784
1130
  );
785
1131
  yield acpContractClient.init();
786
1132
  return acpContractClient;
@@ -813,6 +1159,54 @@ var AcpContractClient = class _AcpContractClient {
813
1159
  get walletAddress() {
814
1160
  return this.sessionKeyClient.account.address;
815
1161
  }
1162
+ calculateGasFees() {
1163
+ return __async(this, null, function* () {
1164
+ const { maxFeePerGas, maxPriorityFeePerGas } = yield this.customRpcClient.estimateFeesPerGas();
1165
+ let finalMaxFeePerGas = maxFeePerGas;
1166
+ let priorityFeeMultiplier = Number(this.config.priorityFeeMultiplier) || 2;
1167
+ const overrideMaxFeePerGas = this.config.maxFeePerGas || maxFeePerGas;
1168
+ const overrideMaxPriorityFeePerGas = this.config.maxPriorityFeePerGas || maxPriorityFeePerGas;
1169
+ finalMaxFeePerGas = BigInt(overrideMaxFeePerGas) + BigInt(overrideMaxPriorityFeePerGas) * BigInt(Math.max(0, priorityFeeMultiplier - 1));
1170
+ return finalMaxFeePerGas;
1171
+ });
1172
+ }
1173
+ handleSendUserOperation(_0) {
1174
+ return __async(this, arguments, function* (data, contractAddress = this.contractAddress) {
1175
+ const payload = {
1176
+ uo: {
1177
+ target: contractAddress,
1178
+ data
1179
+ },
1180
+ overrides: {}
1181
+ };
1182
+ let retries = this.MAX_RETRIES;
1183
+ let finalError;
1184
+ while (retries > 0) {
1185
+ try {
1186
+ if (this.MAX_RETRIES > retries) {
1187
+ const gasFees = yield this.calculateGasFees();
1188
+ payload["overrides"] = {
1189
+ maxFeePerGas: `0x${gasFees.toString(16)}`
1190
+ };
1191
+ }
1192
+ const { hash } = yield this.sessionKeyClient.sendUserOperation(payload);
1193
+ yield this.sessionKeyClient.waitForUserOperationTransaction({
1194
+ hash
1195
+ });
1196
+ return hash;
1197
+ } catch (error) {
1198
+ console.debug("Failed to send user operation", error);
1199
+ retries -= 1;
1200
+ if (retries === 0) {
1201
+ finalError = error;
1202
+ break;
1203
+ }
1204
+ yield new Promise((resolve) => setTimeout(resolve, 2e3 * retries));
1205
+ }
1206
+ }
1207
+ throw new Error(`Failed to send user operation ${finalError}`);
1208
+ });
1209
+ }
816
1210
  getJobId(hash) {
817
1211
  return __async(this, null, function* () {
818
1212
  const result = yield this.sessionKeyClient.getUserOperationReceipt(hash);
@@ -840,51 +1234,42 @@ var AcpContractClient = class _AcpContractClient {
840
1234
  Math.floor(expireAt.getTime() / 1e3)
841
1235
  ]
842
1236
  });
843
- const { hash } = yield this.sessionKeyClient.sendUserOperation({
844
- uo: {
845
- target: this.contractAddress,
846
- data
847
- }
848
- });
849
- yield this.sessionKeyClient.waitForUserOperationTransaction({
850
- hash
851
- });
1237
+ const hash = yield this.handleSendUserOperation(data);
852
1238
  const jobId = yield this.getJobId(hash);
853
1239
  return { txHash: hash, jobId };
854
1240
  } catch (error) {
855
- console.error(error);
1241
+ console.error(`Failed to create job ${error}`);
856
1242
  throw new Error("Failed to create job");
857
1243
  }
858
1244
  });
859
1245
  }
860
1246
  approveAllowance(priceInWei) {
861
1247
  return __async(this, null, function* () {
862
- const data = encodeFunctionData({
863
- abi: erc20Abi,
864
- functionName: "approve",
865
- args: [this.contractAddress, priceInWei]
866
- });
867
- const { hash } = yield this.sessionKeyClient.sendUserOperation({
868
- uo: {
869
- target: this.virtualsTokenAddress,
870
- data
871
- }
872
- });
873
- yield this.sessionKeyClient.waitForUserOperationTransaction({
874
- hash
875
- });
876
- return hash;
1248
+ try {
1249
+ const data = encodeFunctionData({
1250
+ abi: erc20Abi,
1251
+ functionName: "approve",
1252
+ args: [this.contractAddress, priceInWei]
1253
+ });
1254
+ return yield this.handleSendUserOperation(
1255
+ data,
1256
+ this.virtualsTokenAddress
1257
+ );
1258
+ } catch (error) {
1259
+ console.error(`Failed to approve allowance ${error}`);
1260
+ throw new Error("Failed to approve allowance");
1261
+ }
877
1262
  });
878
1263
  }
879
- createMemo(jobId, content, type, isSecured, nextPhase) {
1264
+ createPayableFeeMemo(jobId, content, amount, memoType, nextPhase) {
880
1265
  return __async(this, null, function* () {
881
1266
  let retries = 3;
882
1267
  while (retries > 0) {
883
1268
  try {
884
1269
  const data = encodeFunctionData({
885
1270
  abi: acpAbi_default,
886
- functionName: "createMemo",
887
- args: [jobId, content, type, isSecured, nextPhase]
1271
+ functionName: "createPayableFeeMemo",
1272
+ args: [jobId, content, amount, memoType, nextPhase]
888
1273
  });
889
1274
  const { hash } = yield this.sessionKeyClient.sendUserOperation({
890
1275
  uo: {
@@ -897,23 +1282,36 @@ var AcpContractClient = class _AcpContractClient {
897
1282
  });
898
1283
  return hash;
899
1284
  } catch (error) {
900
- console.error(`failed to create memo ${jobId} ${content} ${error}`);
1285
+ console.error(
1286
+ `failed to create payable fee memo ${jobId} ${content} ${error}`
1287
+ );
901
1288
  retries -= 1;
902
1289
  yield new Promise((resolve) => setTimeout(resolve, 2e3 * retries));
903
1290
  }
904
1291
  }
905
- throw new Error("Failed to create memo");
1292
+ throw new Error("Failed to create payable fee memo");
906
1293
  });
907
1294
  }
908
- signMemo(memoId, isApproved, reason) {
909
- return __async(this, null, function* () {
1295
+ createPayableMemo(_0, _1, _2, _3, _4, _5, _6, _7, _8) {
1296
+ return __async(this, arguments, function* (jobId, content, amount, recipient, feeAmount, feeType, nextPhase, type, expiredAt, token = this.config.virtualsTokenAddress) {
910
1297
  let retries = 3;
911
1298
  while (retries > 0) {
912
1299
  try {
913
1300
  const data = encodeFunctionData({
914
1301
  abi: acpAbi_default,
915
- functionName: "signMemo",
916
- args: [memoId, isApproved, reason]
1302
+ functionName: "createPayableMemo",
1303
+ args: [
1304
+ jobId,
1305
+ content,
1306
+ token,
1307
+ amount,
1308
+ recipient,
1309
+ feeAmount,
1310
+ feeType,
1311
+ type,
1312
+ nextPhase,
1313
+ expiredAt ? Math.floor(expiredAt.getTime() / 1e3) : 0
1314
+ ]
917
1315
  });
918
1316
  const { hash } = yield this.sessionKeyClient.sendUserOperation({
919
1317
  uo: {
@@ -926,12 +1324,67 @@ var AcpContractClient = class _AcpContractClient {
926
1324
  });
927
1325
  return hash;
928
1326
  } catch (error) {
929
- console.error(`failed to sign memo ${error}`);
1327
+ console.error(
1328
+ `failed to create payable memo ${jobId} ${content} ${error}`
1329
+ );
930
1330
  retries -= 1;
931
1331
  yield new Promise((resolve) => setTimeout(resolve, 2e3 * retries));
932
1332
  }
933
1333
  }
934
- throw new Error("Failed to sign memo");
1334
+ throw new Error("Failed to create payable memo");
1335
+ });
1336
+ }
1337
+ createMemo(jobId, content, type, isSecured, nextPhase) {
1338
+ return __async(this, null, function* () {
1339
+ try {
1340
+ const data = encodeFunctionData({
1341
+ abi: acpAbi_default,
1342
+ functionName: "createMemo",
1343
+ args: [jobId, content, type, isSecured, nextPhase]
1344
+ });
1345
+ return yield this.handleSendUserOperation(data);
1346
+ } catch (error) {
1347
+ console.error(`Failed to create memo ${jobId} ${content} ${error}`);
1348
+ throw new Error("Failed to create memo");
1349
+ }
1350
+ });
1351
+ }
1352
+ getMemoId(hash) {
1353
+ return __async(this, null, function* () {
1354
+ const result = yield this.sessionKeyClient.getUserOperationReceipt(hash);
1355
+ if (!result) {
1356
+ throw new Error("Failed to get user operation receipt");
1357
+ }
1358
+ const contractLogs = result.logs.find(
1359
+ (log) => log.address.toLowerCase() === this.contractAddress.toLowerCase()
1360
+ );
1361
+ if (!contractLogs) {
1362
+ throw new Error("Failed to get contract logs");
1363
+ }
1364
+ const decoded = decodeEventLog({
1365
+ abi: acpAbi_default,
1366
+ data: contractLogs.data,
1367
+ topics: contractLogs.topics
1368
+ });
1369
+ if (!decoded.args) {
1370
+ throw new Error("Failed to decode event logs");
1371
+ }
1372
+ return parseInt(decoded.args.memoId);
1373
+ });
1374
+ }
1375
+ signMemo(memoId, isApproved, reason) {
1376
+ return __async(this, null, function* () {
1377
+ try {
1378
+ const data = encodeFunctionData({
1379
+ abi: acpAbi_default,
1380
+ functionName: "signMemo",
1381
+ args: [memoId, isApproved, reason]
1382
+ });
1383
+ return yield this.handleSendUserOperation(data);
1384
+ } catch (error) {
1385
+ console.error(`Failed to sign memo ${error}`);
1386
+ throw new Error("Failed to sign memo");
1387
+ }
935
1388
  });
936
1389
  }
937
1390
  setBudget(jobId, budget) {
@@ -942,18 +1395,9 @@ var AcpContractClient = class _AcpContractClient {
942
1395
  functionName: "setBudget",
943
1396
  args: [jobId, budget]
944
1397
  });
945
- const { hash } = yield this.sessionKeyClient.sendUserOperation({
946
- uo: {
947
- target: this.contractAddress,
948
- data
949
- }
950
- });
951
- yield this.sessionKeyClient.waitForUserOperationTransaction({
952
- hash
953
- });
954
- return hash;
1398
+ return yield this.handleSendUserOperation(data);
955
1399
  } catch (error) {
956
- console.error(error);
1400
+ console.error(`Failed to set budget ${error}`);
957
1401
  throw new Error("Failed to set budget");
958
1402
  }
959
1403
  });
@@ -961,6 +1405,46 @@ var AcpContractClient = class _AcpContractClient {
961
1405
  };
962
1406
  var acpContractClient_default = AcpContractClient;
963
1407
 
1408
+ // src/interfaces.ts
1409
+ var AcpAgentSort = /* @__PURE__ */ ((AcpAgentSort2) => {
1410
+ AcpAgentSort2["SUCCESSFUL_JOB_COUNT"] = "successfulJobCount";
1411
+ AcpAgentSort2["SUCCESS_RATE"] = "successRate";
1412
+ AcpAgentSort2["UNIQUE_BUYER_COUNT"] = "uniqueBuyerCount";
1413
+ AcpAgentSort2["MINS_FROM_LAST_ONLINE"] = "minsFromLastOnlineTime";
1414
+ return AcpAgentSort2;
1415
+ })(AcpAgentSort || {});
1416
+ var AcpGraduationStatus = /* @__PURE__ */ ((AcpGraduationStatus2) => {
1417
+ AcpGraduationStatus2["ALL"] = "all";
1418
+ AcpGraduationStatus2["GRADUATED"] = "graduated";
1419
+ AcpGraduationStatus2["NOT_GRADUATED"] = "not_graduated";
1420
+ return AcpGraduationStatus2;
1421
+ })(AcpGraduationStatus || {});
1422
+ var AcpOnlineStatus = /* @__PURE__ */ ((AcpOnlineStatus2) => {
1423
+ AcpOnlineStatus2["ALL"] = "all";
1424
+ AcpOnlineStatus2["ONLINE"] = "online";
1425
+ AcpOnlineStatus2["OFFLINE"] = "offline";
1426
+ return AcpOnlineStatus2;
1427
+ })(AcpOnlineStatus || {});
1428
+ var PayloadType = /* @__PURE__ */ ((PayloadType2) => {
1429
+ PayloadType2["FUND_RESPONSE"] = "fund_response";
1430
+ PayloadType2["OPEN_POSITION"] = "open_position";
1431
+ PayloadType2["CLOSE_PARTIAL_POSITION"] = "close_partial_position";
1432
+ PayloadType2["CLOSE_POSITION"] = "close_position";
1433
+ PayloadType2["POSITION_FULFILLED"] = "position_fulfilled";
1434
+ PayloadType2["CLOSE_JOB_AND_WITHDRAW"] = "close_job_and_withdraw";
1435
+ PayloadType2["UNFULFILLED_POSITION"] = "unfulfilled_position";
1436
+ return PayloadType2;
1437
+ })(PayloadType || {});
1438
+
1439
+ // src/utils.ts
1440
+ function tryParseJson(content) {
1441
+ try {
1442
+ return JSON.parse(content);
1443
+ } catch (error) {
1444
+ return null;
1445
+ }
1446
+ }
1447
+
964
1448
  // src/acpJob.ts
965
1449
  var AcpJob = class {
966
1450
  constructor(acpClient, id, clientAddress, providerAddress, evaluatorAddress, price, memos, phase, context) {
@@ -991,6 +1475,9 @@ var AcpJob = class {
991
1475
  get evaluatorAgent() {
992
1476
  return this.acpClient.getAgent(this.evaluatorAddress);
993
1477
  }
1478
+ get latestMemo() {
1479
+ return this.memos[this.memos.length - 1];
1480
+ }
994
1481
  pay(amount, reason) {
995
1482
  return __async(this, null, function* () {
996
1483
  const memo = this.memos.find(
@@ -1002,23 +1489,25 @@ var AcpJob = class {
1002
1489
  return yield this.acpClient.payJob(this.id, amount, memo.id, reason);
1003
1490
  });
1004
1491
  }
1005
- respond(accept, reason) {
1492
+ respond(accept, payload, reason) {
1006
1493
  return __async(this, null, function* () {
1007
- const memo = this.memos.find(
1008
- (m) => m.nextPhase === 1 /* NEGOTIATION */
1009
- );
1010
- if (!memo) {
1494
+ var _a;
1495
+ if (((_a = this.latestMemo) == null ? void 0 : _a.nextPhase) !== 1 /* NEGOTIATION */) {
1011
1496
  throw new Error("No negotiation memo found");
1012
1497
  }
1013
- return yield this.acpClient.respondJob(this.id, memo.id, accept, reason);
1498
+ return yield this.acpClient.respondJob(
1499
+ this.id,
1500
+ this.latestMemo.id,
1501
+ accept,
1502
+ payload ? JSON.stringify(payload) : void 0,
1503
+ reason
1504
+ );
1014
1505
  });
1015
1506
  }
1016
1507
  deliver(deliverable) {
1017
1508
  return __async(this, null, function* () {
1018
- const memo = this.memos.find(
1019
- (m) => m.nextPhase === 3 /* EVALUATION */
1020
- );
1021
- if (!memo) {
1509
+ var _a;
1510
+ if (((_a = this.latestMemo) == null ? void 0 : _a.nextPhase) !== 3 /* EVALUATION */) {
1022
1511
  throw new Error("No transaction memo found");
1023
1512
  }
1024
1513
  return yield this.acpClient.deliverJob(this.id, deliverable);
@@ -1026,28 +1515,297 @@ var AcpJob = class {
1026
1515
  }
1027
1516
  evaluate(accept, reason) {
1028
1517
  return __async(this, null, function* () {
1029
- const memo = this.memos.find((m) => m.nextPhase === 4 /* COMPLETED */);
1030
- if (!memo) {
1518
+ var _a;
1519
+ if (((_a = this.latestMemo) == null ? void 0 : _a.nextPhase) !== 4 /* COMPLETED */) {
1031
1520
  throw new Error("No evaluation memo found");
1032
1521
  }
1033
1522
  return yield this.acpClient.acpContractClient.signMemo(
1523
+ this.latestMemo.id,
1524
+ accept,
1525
+ reason
1526
+ );
1527
+ });
1528
+ }
1529
+ openPosition(payload, feeAmount, expiredAt, walletAddress) {
1530
+ return __async(this, null, function* () {
1531
+ if (payload.length === 0) {
1532
+ throw new Error("No positions to open");
1533
+ }
1534
+ return yield this.acpClient.transferFunds(
1535
+ this.id,
1536
+ payload.reduce((acc, curr) => acc + curr.amount, 0),
1537
+ walletAddress || this.providerAddress,
1538
+ feeAmount,
1539
+ 1 /* IMMEDIATE_FEE */,
1540
+ {
1541
+ type: "open_position" /* OPEN_POSITION */,
1542
+ data: payload
1543
+ },
1544
+ 2 /* TRANSACTION */,
1545
+ expiredAt
1546
+ );
1547
+ });
1548
+ }
1549
+ responseOpenPosition(memoId, accept, reason) {
1550
+ return __async(this, null, function* () {
1551
+ const memo = this.memos.find((m) => m.id === memoId);
1552
+ if ((memo == null ? void 0 : memo.nextPhase) !== 2 /* TRANSACTION */ || (memo == null ? void 0 : memo.type) !== 7 /* PAYABLE_TRANSFER */) {
1553
+ throw new Error("No open position memo found");
1554
+ }
1555
+ const payload = tryParseJson(
1556
+ memo.content
1557
+ );
1558
+ if ((payload == null ? void 0 : payload.type) !== "open_position" /* OPEN_POSITION */) {
1559
+ throw new Error("Invalid open position memo");
1560
+ }
1561
+ return yield this.acpClient.responseFundsTransfer(memo.id, accept, reason);
1562
+ });
1563
+ }
1564
+ closePartialPosition(payload) {
1565
+ return __async(this, null, function* () {
1566
+ return yield this.acpClient.requestFunds(
1567
+ this.id,
1568
+ payload.amount,
1569
+ this.clientAddress,
1570
+ 0,
1571
+ 0 /* NO_FEE */,
1572
+ {
1573
+ type: "close_partial_position" /* CLOSE_PARTIAL_POSITION */,
1574
+ data: payload
1575
+ },
1576
+ 2 /* TRANSACTION */
1577
+ );
1578
+ });
1579
+ }
1580
+ responseClosePartialPosition(memoId, accept, reason) {
1581
+ return __async(this, null, function* () {
1582
+ const memo = this.memos.find((m) => m.id === memoId);
1583
+ if ((memo == null ? void 0 : memo.nextPhase) !== 2 /* TRANSACTION */ || (memo == null ? void 0 : memo.type) !== 6 /* PAYABLE_REQUEST */) {
1584
+ throw new Error("No close position memo found");
1585
+ }
1586
+ const payload = tryParseJson(
1587
+ memo.content
1588
+ );
1589
+ if ((payload == null ? void 0 : payload.type) !== "close_partial_position" /* CLOSE_PARTIAL_POSITION */) {
1590
+ throw new Error("Invalid close position memo");
1591
+ }
1592
+ return yield this.acpClient.responseFundsRequest(
1034
1593
  memo.id,
1035
1594
  accept,
1595
+ payload.data.amount,
1036
1596
  reason
1037
1597
  );
1038
1598
  });
1039
1599
  }
1600
+ requestClosePosition(payload) {
1601
+ return __async(this, null, function* () {
1602
+ return yield this.acpClient.sendMessage(
1603
+ this.id,
1604
+ {
1605
+ type: "close_position" /* CLOSE_POSITION */,
1606
+ data: payload
1607
+ },
1608
+ 2 /* TRANSACTION */
1609
+ );
1610
+ });
1611
+ }
1612
+ responseRequestClosePosition(memoId, accept, payload, reason) {
1613
+ return __async(this, null, function* () {
1614
+ const memo = this.memos.find((m) => m.id === memoId);
1615
+ if ((memo == null ? void 0 : memo.nextPhase) !== 2 /* TRANSACTION */ || (memo == null ? void 0 : memo.type) !== 0 /* MESSAGE */) {
1616
+ throw new Error("No message memo found");
1617
+ }
1618
+ const messagePayload = tryParseJson(memo.content);
1619
+ if ((messagePayload == null ? void 0 : messagePayload.type) !== "close_position" /* CLOSE_POSITION */) {
1620
+ throw new Error("Invalid close position memo");
1621
+ }
1622
+ yield memo.sign(accept, reason);
1623
+ if (accept) {
1624
+ return yield this.acpClient.transferFunds(
1625
+ this.id,
1626
+ payload.amount,
1627
+ this.clientAddress,
1628
+ 0,
1629
+ 0 /* NO_FEE */,
1630
+ {
1631
+ type: "close_position" /* CLOSE_POSITION */,
1632
+ data: payload
1633
+ },
1634
+ 2 /* TRANSACTION */
1635
+ );
1636
+ }
1637
+ });
1638
+ }
1639
+ confirmClosePosition(memoId, accept, reason) {
1640
+ return __async(this, null, function* () {
1641
+ const memo = this.memos.find((m) => m.id === memoId);
1642
+ if ((memo == null ? void 0 : memo.nextPhase) !== 2 /* TRANSACTION */ || (memo == null ? void 0 : memo.type) !== 7 /* PAYABLE_TRANSFER */) {
1643
+ throw new Error("No payable transfer memo found");
1644
+ }
1645
+ const payload = tryParseJson(
1646
+ memo.content
1647
+ );
1648
+ if ((payload == null ? void 0 : payload.type) !== "close_position" /* CLOSE_POSITION */) {
1649
+ throw new Error("Invalid close position memo");
1650
+ }
1651
+ yield memo.sign(accept, reason);
1652
+ });
1653
+ }
1654
+ positionFulfilled(payload) {
1655
+ return __async(this, null, function* () {
1656
+ return yield this.acpClient.transferFunds(
1657
+ this.id,
1658
+ payload.amount,
1659
+ this.clientAddress,
1660
+ 0,
1661
+ 0 /* NO_FEE */,
1662
+ {
1663
+ type: "position_fulfilled" /* POSITION_FULFILLED */,
1664
+ data: payload
1665
+ },
1666
+ 2 /* TRANSACTION */
1667
+ );
1668
+ });
1669
+ }
1670
+ unfulfilledPosition(payload) {
1671
+ return __async(this, null, function* () {
1672
+ return yield this.acpClient.transferFunds(
1673
+ this.id,
1674
+ payload.amount,
1675
+ this.clientAddress,
1676
+ 0,
1677
+ 0 /* NO_FEE */,
1678
+ {
1679
+ type: "unfulfilled_position" /* UNFULFILLED_POSITION */,
1680
+ data: payload
1681
+ },
1682
+ 2 /* TRANSACTION */
1683
+ );
1684
+ });
1685
+ }
1686
+ responseUnfulfilledPosition(memoId, accept, reason) {
1687
+ return __async(this, null, function* () {
1688
+ const memo = this.memos.find((m) => m.id === memoId);
1689
+ if ((memo == null ? void 0 : memo.nextPhase) !== 2 /* TRANSACTION */ || (memo == null ? void 0 : memo.type) !== 7 /* PAYABLE_TRANSFER */) {
1690
+ throw new Error("No unfulfilled position memo found");
1691
+ }
1692
+ const payload = tryParseJson(
1693
+ memo.content
1694
+ );
1695
+ if ((payload == null ? void 0 : payload.type) !== "unfulfilled_position" /* UNFULFILLED_POSITION */) {
1696
+ throw new Error("Invalid unfulfilled position memo");
1697
+ }
1698
+ return yield this.acpClient.responseFundsTransfer(memo.id, accept, reason);
1699
+ });
1700
+ }
1701
+ responsePositionFulfilled(memoId, accept, reason) {
1702
+ return __async(this, null, function* () {
1703
+ const memo = this.memos.find((m) => m.id === memoId);
1704
+ if ((memo == null ? void 0 : memo.nextPhase) !== 2 /* TRANSACTION */ || (memo == null ? void 0 : memo.type) !== 7 /* PAYABLE_TRANSFER */) {
1705
+ throw new Error("No position fulfilled memo found");
1706
+ }
1707
+ const payload = tryParseJson(
1708
+ memo.content
1709
+ );
1710
+ if ((payload == null ? void 0 : payload.type) !== "position_fulfilled" /* POSITION_FULFILLED */) {
1711
+ throw new Error("Invalid position fulfilled memo");
1712
+ }
1713
+ return yield this.acpClient.responseFundsTransfer(memo.id, accept, reason);
1714
+ });
1715
+ }
1716
+ closeJob(message = "Close job and withdraw all") {
1717
+ return __async(this, null, function* () {
1718
+ return yield this.acpClient.sendMessage(
1719
+ this.id,
1720
+ {
1721
+ type: "close_job_and_withdraw" /* CLOSE_JOB_AND_WITHDRAW */,
1722
+ data: {
1723
+ message
1724
+ }
1725
+ },
1726
+ 2 /* TRANSACTION */
1727
+ );
1728
+ });
1729
+ }
1730
+ responseCloseJob(memoId, accept, fulfilledPositions, reason) {
1731
+ return __async(this, null, function* () {
1732
+ const memo = this.memos.find((m) => m.id === memoId);
1733
+ if ((memo == null ? void 0 : memo.nextPhase) !== 2 /* TRANSACTION */ || (memo == null ? void 0 : memo.type) !== 0 /* MESSAGE */) {
1734
+ throw new Error("No message memo found");
1735
+ }
1736
+ const payload = tryParseJson(
1737
+ memo.content
1738
+ );
1739
+ if ((payload == null ? void 0 : payload.type) !== "close_job_and_withdraw" /* CLOSE_JOB_AND_WITHDRAW */) {
1740
+ throw new Error("Invalid close job and withdraw memo");
1741
+ }
1742
+ yield memo.sign(accept, reason);
1743
+ if (!accept) {
1744
+ return;
1745
+ }
1746
+ const totalAmount = fulfilledPositions.reduce(
1747
+ (acc, curr) => acc + curr.amount,
1748
+ 0
1749
+ );
1750
+ if (totalAmount === 0) {
1751
+ return yield this.acpClient.sendMessage(
1752
+ this.id,
1753
+ {
1754
+ type: "close_job_and_withdraw" /* CLOSE_JOB_AND_WITHDRAW */,
1755
+ data: fulfilledPositions
1756
+ },
1757
+ 4 /* COMPLETED */
1758
+ );
1759
+ }
1760
+ return yield this.acpClient.transferFunds(
1761
+ this.id,
1762
+ fulfilledPositions.reduce((acc, curr) => acc + curr.amount, 0),
1763
+ this.clientAddress,
1764
+ 0,
1765
+ 0 /* NO_FEE */,
1766
+ {
1767
+ type: "close_job_and_withdraw" /* CLOSE_JOB_AND_WITHDRAW */,
1768
+ data: fulfilledPositions
1769
+ },
1770
+ 4 /* COMPLETED */
1771
+ );
1772
+ });
1773
+ }
1774
+ confirmJobClosure(memoId, accept, reason) {
1775
+ return __async(this, null, function* () {
1776
+ const memo = this.memos.find((m) => m.id === memoId);
1777
+ if (!memo) {
1778
+ throw new Error("Memo not found");
1779
+ }
1780
+ const payload = tryParseJson(
1781
+ memo.content
1782
+ );
1783
+ if ((payload == null ? void 0 : payload.type) !== "close_job_and_withdraw" /* CLOSE_JOB_AND_WITHDRAW */) {
1784
+ throw new Error("Invalid close job and withdraw memo");
1785
+ }
1786
+ yield memo.sign(accept, reason);
1787
+ });
1788
+ }
1040
1789
  };
1041
1790
  var acpJob_default = AcpJob;
1042
1791
 
1043
1792
  // src/acpMemo.ts
1044
1793
  var AcpMemo = class {
1045
- constructor(acpClient, id, type, content, nextPhase) {
1794
+ constructor(acpClient, id, type, content, nextPhase, expiry) {
1046
1795
  this.acpClient = acpClient;
1047
1796
  this.id = id;
1048
1797
  this.type = type;
1049
1798
  this.content = content;
1050
1799
  this.nextPhase = nextPhase;
1800
+ this.expiry = expiry;
1801
+ this.structuredContent = tryParseJson(this.content) || void 0;
1802
+ }
1803
+ get payloadType() {
1804
+ var _a;
1805
+ return (_a = this.structuredContent) == null ? void 0 : _a.type;
1806
+ }
1807
+ getStructuredContent() {
1808
+ return this.structuredContent;
1051
1809
  }
1052
1810
  create(jobId, isSecured = true) {
1053
1811
  return __async(this, null, function* () {
@@ -1105,6 +1863,7 @@ var AcpJobOffering = class {
1105
1863
  var acpJobOffering_default = AcpJobOffering;
1106
1864
 
1107
1865
  // src/acpClient.ts
1866
+ var { version } = require_package();
1108
1867
  var AcpClient = class {
1109
1868
  constructor(options) {
1110
1869
  this.acpContractClient = options.acpContractClient;
@@ -1121,11 +1880,16 @@ var AcpClient = class {
1121
1880
  init() {
1122
1881
  return __async(this, null, function* () {
1123
1882
  const socket = io(this.acpUrl, {
1124
- auth: __spreadValues(__spreadValues({}, this.onNewTask && {
1883
+ auth: __spreadValues({
1125
1884
  walletAddress: this.acpContractClient.walletAddress
1126
- }), this.onEvaluate !== this.defaultOnEvaluate && {
1885
+ }, this.onEvaluate !== this.defaultOnEvaluate && {
1127
1886
  evaluatorAddress: this.acpContractClient.walletAddress
1128
- })
1887
+ }),
1888
+ extraHeaders: {
1889
+ "x-sdk-version": version,
1890
+ "x-sdk-language": "node"
1891
+ },
1892
+ transports: ["websocket"]
1129
1893
  });
1130
1894
  socket.on("roomJoined" /* ROOM_JOINED */, (_, callback) => {
1131
1895
  console.log("Joined ACP Room");
@@ -1149,7 +1913,8 @@ var AcpClient = class {
1149
1913
  memo.id,
1150
1914
  memo.memoType,
1151
1915
  memo.content,
1152
- memo.nextPhase
1916
+ memo.nextPhase,
1917
+ memo.expiry ? new Date(parseInt(memo.expiry)) : null
1153
1918
  );
1154
1919
  }),
1155
1920
  data.phase,
@@ -1177,13 +1942,17 @@ var AcpClient = class {
1177
1942
  memo.id,
1178
1943
  memo.memoType,
1179
1944
  memo.content,
1180
- memo.nextPhase
1945
+ memo.nextPhase,
1946
+ memo.expiry ? new Date(parseInt(memo.expiry)) : null
1181
1947
  );
1182
1948
  }),
1183
1949
  data.phase,
1184
1950
  data.context
1185
1951
  );
1186
- this.onNewTask(job);
1952
+ this.onNewTask(
1953
+ job,
1954
+ job.memos.find((m) => m.id == data.memoToSign)
1955
+ );
1187
1956
  }
1188
1957
  })
1189
1958
  );
@@ -1199,28 +1968,26 @@ var AcpClient = class {
1199
1968
  }
1200
1969
  browseAgents(keyword, options) {
1201
1970
  return __async(this, null, function* () {
1202
- let { cluster, sort_by, rerank, top_k, graduated } = options;
1203
- rerank = rerank != null ? rerank : true;
1971
+ let { cluster, sort_by, top_k, graduationStatus, onlineStatus } = options;
1204
1972
  top_k = top_k != null ? top_k : 5;
1205
- graduated = graduated != null ? graduated : true;
1206
- let url = `${this.acpUrl}/api/agents?search=${keyword}`;
1973
+ let url = `${this.acpUrl}/api/agents/v2/search?search=${keyword}`;
1207
1974
  if (sort_by && sort_by.length > 0) {
1208
- url += `&sort=${sort_by.map((s) => s).join(",")}`;
1975
+ url += `&sortBy=${sort_by.map((s) => s).join(",")}`;
1209
1976
  }
1210
1977
  if (top_k) {
1211
1978
  url += `&top_k=${top_k}`;
1212
1979
  }
1213
- if (rerank) {
1214
- url += `&rerank=true`;
1215
- }
1216
1980
  if (this.acpContractClient.walletAddress) {
1217
- url += `&filters[walletAddress][$notIn]=${this.acpContractClient.walletAddress}`;
1981
+ url += `&walletAddressesToExclude=${this.acpContractClient.walletAddress}`;
1218
1982
  }
1219
1983
  if (cluster) {
1220
- url += `&filters[cluster]=${cluster}`;
1984
+ url += `&cluster=${cluster}`;
1985
+ }
1986
+ if (graduationStatus) {
1987
+ url += `&graduationStatus=${graduationStatus}`;
1221
1988
  }
1222
- if (graduated === false) {
1223
- url += `&filters[hasGraduated]=false`;
1989
+ if (onlineStatus) {
1990
+ url += `&onlineStatus=${onlineStatus}`;
1224
1991
  }
1225
1992
  const response = yield fetch(url);
1226
1993
  const data = yield response.json();
@@ -1252,10 +2019,12 @@ var AcpClient = class {
1252
2019
  evaluatorAddress || this.acpContractClient.walletAddress,
1253
2020
  expiredAt
1254
2021
  );
1255
- yield this.acpContractClient.setBudget(
1256
- jobId,
1257
- parseEther(amount.toString())
1258
- );
2022
+ if (amount > 0) {
2023
+ yield this.acpContractClient.setBudget(
2024
+ jobId,
2025
+ parseEther(amount.toString())
2026
+ );
2027
+ }
1259
2028
  yield this.acpContractClient.createMemo(
1260
2029
  jobId,
1261
2030
  typeof serviceRequirement === "string" ? serviceRequirement : JSON.stringify(serviceRequirement),
@@ -1266,7 +2035,7 @@ var AcpClient = class {
1266
2035
  return jobId;
1267
2036
  });
1268
2037
  }
1269
- respondJob(jobId, memoId, accept, reason) {
2038
+ respondJob(jobId, memoId, accept, content, reason) {
1270
2039
  return __async(this, null, function* () {
1271
2040
  yield this.acpContractClient.signMemo(memoId, accept, reason);
1272
2041
  if (!accept) {
@@ -1274,7 +2043,7 @@ var AcpClient = class {
1274
2043
  }
1275
2044
  return yield this.acpContractClient.createMemo(
1276
2045
  jobId,
1277
- `Job ${jobId} accepted. ${reason != null ? reason : ""}`,
2046
+ content != null ? content : `Job ${jobId} accepted. ${reason != null ? reason : ""}`,
1278
2047
  0 /* MESSAGE */,
1279
2048
  false,
1280
2049
  2 /* TRANSACTION */
@@ -1283,9 +2052,11 @@ var AcpClient = class {
1283
2052
  }
1284
2053
  payJob(jobId, amount, memoId, reason) {
1285
2054
  return __async(this, null, function* () {
1286
- yield this.acpContractClient.approveAllowance(
1287
- parseEther(amount.toString())
1288
- );
2055
+ if (amount > 0) {
2056
+ yield this.acpContractClient.approveAllowance(
2057
+ parseEther(amount.toString())
2058
+ );
2059
+ }
1289
2060
  yield this.acpContractClient.signMemo(memoId, true, reason);
1290
2061
  return yield this.acpContractClient.createMemo(
1291
2062
  jobId,
@@ -1296,11 +2067,75 @@ var AcpClient = class {
1296
2067
  );
1297
2068
  });
1298
2069
  }
2070
+ requestFunds(jobId, amount, recipient, feeAmount, feeType, reason, nextPhase) {
2071
+ return __async(this, null, function* () {
2072
+ return yield this.acpContractClient.createPayableMemo(
2073
+ jobId,
2074
+ JSON.stringify(reason),
2075
+ parseEther(amount.toString()),
2076
+ recipient,
2077
+ parseEther(feeAmount.toString()),
2078
+ feeType,
2079
+ nextPhase,
2080
+ 6 /* PAYABLE_REQUEST */
2081
+ );
2082
+ });
2083
+ }
2084
+ responseFundsRequest(memoId, accept, amount, reason) {
2085
+ return __async(this, null, function* () {
2086
+ if (!accept) {
2087
+ return yield this.acpContractClient.signMemo(memoId, accept, reason);
2088
+ }
2089
+ if (amount > 0) {
2090
+ yield this.acpContractClient.approveAllowance(
2091
+ parseEther(amount.toString())
2092
+ );
2093
+ }
2094
+ return yield this.acpContractClient.signMemo(memoId, true, reason);
2095
+ });
2096
+ }
2097
+ transferFunds(jobId, amount, recipient, feeAmount, feeType, reason, nextPhase, expiredAt) {
2098
+ return __async(this, null, function* () {
2099
+ const totalAmount = amount + feeAmount;
2100
+ if (totalAmount > 0) {
2101
+ yield this.acpContractClient.approveAllowance(
2102
+ parseEther(totalAmount.toString())
2103
+ );
2104
+ }
2105
+ return yield this.acpContractClient.createPayableMemo(
2106
+ jobId,
2107
+ JSON.stringify(reason),
2108
+ parseEther(amount.toString()),
2109
+ recipient,
2110
+ parseEther(feeAmount.toString()),
2111
+ feeType,
2112
+ nextPhase,
2113
+ 7 /* PAYABLE_TRANSFER */,
2114
+ expiredAt
2115
+ );
2116
+ });
2117
+ }
2118
+ sendMessage(jobId, message, nextPhase) {
2119
+ return __async(this, null, function* () {
2120
+ return yield this.acpContractClient.createMemo(
2121
+ jobId,
2122
+ JSON.stringify(message),
2123
+ 0 /* MESSAGE */,
2124
+ false,
2125
+ nextPhase
2126
+ );
2127
+ });
2128
+ }
2129
+ responseFundsTransfer(memoId, accept, reason) {
2130
+ return __async(this, null, function* () {
2131
+ return yield this.acpContractClient.signMemo(memoId, accept, reason);
2132
+ });
2133
+ }
1299
2134
  deliverJob(jobId, deliverable) {
1300
2135
  return __async(this, null, function* () {
1301
2136
  return yield this.acpContractClient.createMemo(
1302
2137
  jobId,
1303
- deliverable,
2138
+ JSON.stringify(deliverable),
1304
2139
  4 /* OBJECT_URL */,
1305
2140
  true,
1306
2141
  4 /* COMPLETED */
@@ -1334,7 +2169,8 @@ var AcpClient = class {
1334
2169
  memo.id,
1335
2170
  memo.memoType,
1336
2171
  memo.content,
1337
- memo.nextPhase
2172
+ memo.nextPhase,
2173
+ memo.expiry ? new Date(parseInt(memo.expiry)) : null
1338
2174
  );
1339
2175
  }),
1340
2176
  job.phase,
@@ -1373,7 +2209,8 @@ var AcpClient = class {
1373
2209
  memo.id,
1374
2210
  memo.memoType,
1375
2211
  memo.content,
1376
- memo.nextPhase
2212
+ memo.nextPhase,
2213
+ memo.expiry ? new Date(parseInt(memo.expiry)) : null
1377
2214
  );
1378
2215
  }),
1379
2216
  job.phase,
@@ -1412,7 +2249,8 @@ var AcpClient = class {
1412
2249
  memo.id,
1413
2250
  memo.memoType,
1414
2251
  memo.content,
1415
- memo.nextPhase
2252
+ memo.nextPhase,
2253
+ memo.expiry ? new Date(parseInt(memo.expiry)) : null
1416
2254
  );
1417
2255
  }),
1418
2256
  job.phase,
@@ -1454,7 +2292,8 @@ var AcpClient = class {
1454
2292
  memo.id,
1455
2293
  memo.memoType,
1456
2294
  memo.content,
1457
- memo.nextPhase
2295
+ memo.nextPhase,
2296
+ memo.expiry ? new Date(parseInt(memo.expiry)) : null
1458
2297
  );
1459
2298
  }),
1460
2299
  job.phase,
@@ -1487,7 +2326,8 @@ var AcpClient = class {
1487
2326
  memo.id,
1488
2327
  memo.memoType,
1489
2328
  memo.content,
1490
- memo.nextPhase
2329
+ memo.nextPhase,
2330
+ memo.expiry ? new Date(parseInt(memo.expiry)) : null
1491
2331
  );
1492
2332
  } catch (error) {
1493
2333
  throw error;
@@ -1509,26 +2349,19 @@ var AcpClient = class {
1509
2349
  };
1510
2350
  var acpClient_default = AcpClient;
1511
2351
 
1512
- // src/interfaces.ts
1513
- var AcpAgentSort = /* @__PURE__ */ ((AcpAgentSort2) => {
1514
- AcpAgentSort2["SUCCESSFUL_JOB_COUNT"] = "successfulJobCount";
1515
- AcpAgentSort2["SUCCESS_RATE"] = "successRate";
1516
- AcpAgentSort2["UNIQUE_BUYER_COUNT"] = "uniqueBuyerCount";
1517
- AcpAgentSort2["MINS_FROM_LAST_ONLINE"] = "minsFromLastOnlineTime";
1518
- AcpAgentSort2["IS_ONLINE"] = "isOnline";
1519
- return AcpAgentSort2;
1520
- })(AcpAgentSort || {});
1521
-
1522
2352
  // src/index.ts
1523
2353
  var index_default = acpClient_default;
1524
2354
  export {
1525
2355
  acpAbi_default as ACP_ABI,
1526
2356
  AcpAgentSort,
1527
2357
  acpContractClient_default as AcpContractClient,
2358
+ AcpGraduationStatus,
1528
2359
  acpJob_default as AcpJob,
1529
2360
  AcpJobPhases,
1530
2361
  acpMemo_default as AcpMemo,
2362
+ AcpOnlineStatus,
1531
2363
  MemoType,
2364
+ PayloadType,
1532
2365
  baseAcpConfig,
1533
2366
  baseSepoliaAcpConfig,
1534
2367
  index_default as default