@virtuals-protocol/acp-node 0.1.0-beta.13 → 0.1.0-beta.15

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.1.0-beta.15",
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" },
@@ -734,18 +773,31 @@ var baseSepoliaAcpConfig = {
734
773
  contractAddress: "0xD1D196Ac27Fd386dDc04879a86C074aBF3AD9d0f",
735
774
  virtualsTokenAddress: "0xbfAB80ccc15DF6fb7185f9498d6039317331846a",
736
775
  alchemyRpcUrl: "https://alchemy-proxy.virtuals.io/api/proxy/rpc",
737
- acpUrl: "https://acpx-staging.virtuals.io"
776
+ acpUrl: "https://acpx-staging.virtuals.io",
777
+ priorityFeeMultiplier: 2,
778
+ maxFeePerGas: 2e7,
779
+ maxPriorityFeePerGas: 21e6
738
780
  };
739
781
  var baseAcpConfig = {
740
782
  chain: base,
741
783
  contractAddress: "0x6a1FE26D54ab0d3E1e3168f2e0c0cDa5cC0A0A4A",
742
784
  virtualsTokenAddress: "0x0b3e328455c4059EEb9e3f84b5543F74E24e7E1b",
743
785
  alchemyRpcUrl: "https://alchemy-proxy-prod.virtuals.io/api/proxy/rpc",
744
- acpUrl: "https://acpx.virtuals.io"
786
+ acpUrl: "https://acpx.virtuals.io",
787
+ priorityFeeMultiplier: 2,
788
+ maxFeePerGas: 2e7,
789
+ maxPriorityFeePerGas: 21e6
745
790
  };
746
791
 
747
792
  // src/acpContractClient.ts
748
- import { encodeFunctionData, erc20Abi, fromHex } from "viem";
793
+ import {
794
+ createPublicClient,
795
+ encodeFunctionData,
796
+ erc20Abi,
797
+ fromHex,
798
+ http
799
+ } from "viem";
800
+ import { publicActionsL2 } from "viem/op-stack";
749
801
  var MemoType = /* @__PURE__ */ ((MemoType2) => {
750
802
  MemoType2[MemoType2["MESSAGE"] = 0] = "MESSAGE";
751
803
  MemoType2[MemoType2["CONTEXT_URL"] = 1] = "CONTEXT_URL";
@@ -766,22 +818,30 @@ var AcpJobPhases = /* @__PURE__ */ ((AcpJobPhases2) => {
766
818
  return AcpJobPhases2;
767
819
  })(AcpJobPhases || {});
768
820
  var AcpContractClient = class _AcpContractClient {
769
- constructor(walletPrivateKey, sessionEntityKeyId, agentWalletAddress, config = baseAcpConfig) {
821
+ constructor(walletPrivateKey, sessionEntityKeyId, agentWalletAddress, config = baseAcpConfig, customRpcUrl) {
770
822
  this.walletPrivateKey = walletPrivateKey;
771
823
  this.sessionEntityKeyId = sessionEntityKeyId;
772
824
  this.agentWalletAddress = agentWalletAddress;
773
825
  this.config = config;
826
+ this.customRpcUrl = customRpcUrl;
827
+ this.MAX_RETRIES = 3;
774
828
  this.chain = config.chain;
775
829
  this.contractAddress = config.contractAddress;
776
830
  this.virtualsTokenAddress = config.virtualsTokenAddress;
831
+ this.customRpcUrl = customRpcUrl;
832
+ this.customRpcClient = createPublicClient({
833
+ chain: this.chain,
834
+ transport: this.customRpcUrl ? http(this.customRpcUrl) : http()
835
+ }).extend(publicActionsL2());
777
836
  }
778
- static build(_0, _1, _2) {
779
- return __async(this, arguments, function* (walletPrivateKey, sessionEntityKeyId, agentWalletAddress, config = baseAcpConfig) {
837
+ static build(_0, _1, _2, _3) {
838
+ return __async(this, arguments, function* (walletPrivateKey, sessionEntityKeyId, agentWalletAddress, customRpcUrl, config = baseAcpConfig) {
780
839
  const acpContractClient = new _AcpContractClient(
781
840
  walletPrivateKey,
782
841
  sessionEntityKeyId,
783
842
  agentWalletAddress,
784
- config
843
+ config,
844
+ customRpcUrl
785
845
  );
786
846
  yield acpContractClient.init();
787
847
  return acpContractClient;
@@ -814,6 +874,54 @@ var AcpContractClient = class _AcpContractClient {
814
874
  get walletAddress() {
815
875
  return this.sessionKeyClient.account.address;
816
876
  }
877
+ calculateGasFees() {
878
+ return __async(this, null, function* () {
879
+ const { maxFeePerGas, maxPriorityFeePerGas } = yield this.customRpcClient.estimateFeesPerGas();
880
+ let finalMaxFeePerGas = maxFeePerGas;
881
+ let priorityFeeMultiplier = Number(this.config.priorityFeeMultiplier) || 2;
882
+ const overrideMaxFeePerGas = this.config.maxFeePerGas || maxFeePerGas;
883
+ const overrideMaxPriorityFeePerGas = this.config.maxPriorityFeePerGas || maxPriorityFeePerGas;
884
+ finalMaxFeePerGas = BigInt(overrideMaxFeePerGas) + BigInt(overrideMaxPriorityFeePerGas) * BigInt(Math.max(0, priorityFeeMultiplier - 1));
885
+ return finalMaxFeePerGas;
886
+ });
887
+ }
888
+ handleSendUserOperation(_0) {
889
+ return __async(this, arguments, function* (data, contractAddress = this.contractAddress) {
890
+ const payload = {
891
+ uo: {
892
+ target: contractAddress,
893
+ data
894
+ },
895
+ overrides: {}
896
+ };
897
+ let retries = this.MAX_RETRIES;
898
+ let finalError;
899
+ while (retries > 0) {
900
+ try {
901
+ if (this.MAX_RETRIES > retries) {
902
+ const gasFees = yield this.calculateGasFees();
903
+ payload["overrides"] = {
904
+ maxFeePerGas: `0x${gasFees.toString(16)}`
905
+ };
906
+ }
907
+ const { hash } = yield this.sessionKeyClient.sendUserOperation(payload);
908
+ yield this.sessionKeyClient.waitForUserOperationTransaction({
909
+ hash
910
+ });
911
+ return hash;
912
+ } catch (error) {
913
+ console.debug("Failed to send user operation", error);
914
+ retries -= 1;
915
+ if (retries === 0) {
916
+ finalError = error;
917
+ break;
918
+ }
919
+ yield new Promise((resolve) => setTimeout(resolve, 2e3 * retries));
920
+ }
921
+ }
922
+ throw new Error(`Failed to send user operation ${finalError}`);
923
+ });
924
+ }
817
925
  getJobId(hash) {
818
926
  return __async(this, null, function* () {
819
927
  const result = yield this.sessionKeyClient.getUserOperationReceipt(hash);
@@ -841,98 +949,61 @@ var AcpContractClient = class _AcpContractClient {
841
949
  Math.floor(expireAt.getTime() / 1e3)
842
950
  ]
843
951
  });
844
- const { hash } = yield this.sessionKeyClient.sendUserOperation({
845
- uo: {
846
- target: this.contractAddress,
847
- data
848
- }
849
- });
850
- yield this.sessionKeyClient.waitForUserOperationTransaction({
851
- hash
852
- });
952
+ const hash = yield this.handleSendUserOperation(data);
853
953
  const jobId = yield this.getJobId(hash);
854
954
  return { txHash: hash, jobId };
855
955
  } catch (error) {
856
- console.error(error);
956
+ console.error(`Failed to create job ${error}`);
857
957
  throw new Error("Failed to create job");
858
958
  }
859
959
  });
860
960
  }
861
961
  approveAllowance(priceInWei) {
862
962
  return __async(this, null, function* () {
863
- const data = encodeFunctionData({
864
- abi: erc20Abi,
865
- functionName: "approve",
866
- args: [this.contractAddress, priceInWei]
867
- });
868
- const { hash } = yield this.sessionKeyClient.sendUserOperation({
869
- uo: {
870
- target: this.virtualsTokenAddress,
871
- data
872
- }
873
- });
874
- yield this.sessionKeyClient.waitForUserOperationTransaction({
875
- hash
876
- });
877
- return hash;
963
+ try {
964
+ const data = encodeFunctionData({
965
+ abi: erc20Abi,
966
+ functionName: "approve",
967
+ args: [this.contractAddress, priceInWei]
968
+ });
969
+ return yield this.handleSendUserOperation(
970
+ data,
971
+ this.virtualsTokenAddress
972
+ );
973
+ } catch (error) {
974
+ console.error(`Failed to approve allowance ${error}`);
975
+ throw new Error("Failed to approve allowance");
976
+ }
878
977
  });
879
978
  }
880
979
  createMemo(jobId, content, type, isSecured, nextPhase) {
881
980
  return __async(this, null, function* () {
882
- let retries = 3;
883
- while (retries > 0) {
884
- try {
885
- const data = encodeFunctionData({
886
- abi: acpAbi_default,
887
- functionName: "createMemo",
888
- args: [jobId, content, type, isSecured, nextPhase]
889
- });
890
- const { hash } = yield this.sessionKeyClient.sendUserOperation({
891
- uo: {
892
- target: this.contractAddress,
893
- data
894
- }
895
- });
896
- yield this.sessionKeyClient.waitForUserOperationTransaction({
897
- hash
898
- });
899
- return hash;
900
- } catch (error) {
901
- console.error(`failed to create memo ${jobId} ${content} ${error}`);
902
- retries -= 1;
903
- yield new Promise((resolve) => setTimeout(resolve, 2e3 * retries));
904
- }
981
+ try {
982
+ const data = encodeFunctionData({
983
+ abi: acpAbi_default,
984
+ functionName: "createMemo",
985
+ args: [jobId, content, type, isSecured, nextPhase]
986
+ });
987
+ return yield this.handleSendUserOperation(data);
988
+ } catch (error) {
989
+ console.error(`Failed to create memo ${jobId} ${content} ${error}`);
990
+ throw new Error("Failed to create memo");
905
991
  }
906
- throw new Error("Failed to create memo");
907
992
  });
908
993
  }
909
994
  signMemo(memoId, isApproved, reason) {
910
995
  return __async(this, null, function* () {
911
- let retries = 3;
912
- while (retries > 0) {
913
- try {
914
- const data = encodeFunctionData({
915
- abi: acpAbi_default,
916
- functionName: "signMemo",
917
- args: [memoId, isApproved, reason]
918
- });
919
- const { hash } = yield this.sessionKeyClient.sendUserOperation({
920
- uo: {
921
- target: this.contractAddress,
922
- data
923
- }
924
- });
925
- yield this.sessionKeyClient.waitForUserOperationTransaction({
926
- hash
927
- });
928
- return hash;
929
- } catch (error) {
930
- console.error(`failed to sign memo ${error}`);
931
- retries -= 1;
932
- yield new Promise((resolve) => setTimeout(resolve, 2e3 * retries));
933
- }
996
+ try {
997
+ const data = encodeFunctionData({
998
+ abi: acpAbi_default,
999
+ functionName: "signMemo",
1000
+ args: [memoId, isApproved, reason]
1001
+ });
1002
+ return yield this.handleSendUserOperation(data);
1003
+ } catch (error) {
1004
+ console.error(`Failed to sign memo ${error}`);
1005
+ throw new Error("Failed to sign memo");
934
1006
  }
935
- throw new Error("Failed to sign memo");
936
1007
  });
937
1008
  }
938
1009
  setBudget(jobId, budget) {
@@ -943,18 +1014,9 @@ var AcpContractClient = class _AcpContractClient {
943
1014
  functionName: "setBudget",
944
1015
  args: [jobId, budget]
945
1016
  });
946
- const { hash } = yield this.sessionKeyClient.sendUserOperation({
947
- uo: {
948
- target: this.contractAddress,
949
- data
950
- }
951
- });
952
- yield this.sessionKeyClient.waitForUserOperationTransaction({
953
- hash
954
- });
955
- return hash;
1017
+ return yield this.handleSendUserOperation(data);
956
1018
  } catch (error) {
957
- console.error(error);
1019
+ console.error(`Failed to set budget ${error}`);
958
1020
  throw new Error("Failed to set budget");
959
1021
  }
960
1022
  });
@@ -1106,6 +1168,7 @@ var AcpJobOffering = class {
1106
1168
  var acpJobOffering_default = AcpJobOffering;
1107
1169
 
1108
1170
  // src/acpClient.ts
1171
+ var { version } = require_package();
1109
1172
  var AcpClient = class {
1110
1173
  constructor(options) {
1111
1174
  this.acpContractClient = options.acpContractClient;
@@ -1122,11 +1185,15 @@ var AcpClient = class {
1122
1185
  init() {
1123
1186
  return __async(this, null, function* () {
1124
1187
  const socket = io(this.acpUrl, {
1125
- auth: __spreadValues(__spreadValues({}, this.onNewTask && {
1188
+ auth: __spreadValues({
1126
1189
  walletAddress: this.acpContractClient.walletAddress
1127
- }), this.onEvaluate !== this.defaultOnEvaluate && {
1190
+ }, this.onEvaluate !== this.defaultOnEvaluate && {
1128
1191
  evaluatorAddress: this.acpContractClient.walletAddress
1129
1192
  }),
1193
+ extraHeaders: {
1194
+ "x-sdk-version": version,
1195
+ "x-sdk-language": "node"
1196
+ },
1130
1197
  transports: ["websocket"]
1131
1198
  });
1132
1199
  socket.on("roomJoined" /* ROOM_JOINED */, (_, callback) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@virtuals-protocol/acp-node",
3
- "version": "0.1.0-beta.13",
3
+ "version": "0.1.0-beta.15",
4
4
  "main": "./dist/index.js",
5
5
  "module": "./dist/index.mjs",
6
6
  "types": "./dist/index.d.ts",