@actalink/commonlib 0.0.11 → 0.0.13

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.js CHANGED
@@ -807,6 +807,83 @@ var ActaAccount = class {
807
807
  };
808
808
  });
809
809
  }
810
+ estimateSingleBatchPaymentGas(parameters) {
811
+ return __async(this, null, function* () {
812
+ const {
813
+ signerAddress,
814
+ chainId,
815
+ token: tokenSymbol,
816
+ receivers,
817
+ feeInclusive,
818
+ walletClient,
819
+ totalAmount
820
+ } = parameters;
821
+ if (totalAmount <= BigInt(0)) {
822
+ throw new Error("Amount must be greater than 0.");
823
+ }
824
+ if (receivers.length === 0) {
825
+ throw new Error("Receivers not found for batch payment");
826
+ }
827
+ const account = yield this.createAccount();
828
+ const { accountClient, pimlicoClient } = yield this.createAccountHelpers();
829
+ const fromAddress = signerAddress;
830
+ const smartAccountAddress = account.address;
831
+ const token2 = getTokenByChainIdAndSymbol(chainId, tokenSymbol);
832
+ if (!token2) {
833
+ throw new Error("Token not found.");
834
+ }
835
+ console.log(`fromAddress: ${fromAddress}`);
836
+ const quotes = yield pimlicoClient.getTokenQuotes({
837
+ tokens: [token2.address],
838
+ chain: getChainById(chainId)
839
+ });
840
+ const { postOpGas, exchangeRate, paymaster } = quotes[0];
841
+ console.log("preparing");
842
+ console.log(token2.address);
843
+ const receiversData = receivers.map((r) => {
844
+ return {
845
+ to: getAddress2(token2.address),
846
+ abi: parseAbi2(["function transferFrom(address,address,uint)"]),
847
+ functionName: "transferFrom",
848
+ args: [fromAddress, r.address, BigInt(0)]
849
+ };
850
+ });
851
+ console.log(receiversData);
852
+ const userOperation = yield accountClient.prepareUserOperation({
853
+ calls: [
854
+ ...receiversData,
855
+ {
856
+ to: getAddress2(token2.address),
857
+ abi: parseAbi2(["function transferFrom(address,address,uint)"]),
858
+ functionName: "transferFrom",
859
+ args: [
860
+ fromAddress,
861
+ "0xC4910E5ec82Da0A41aF9C6360b7A1f531e1e37B0",
862
+ BigInt(0)
863
+ ]
864
+ }
865
+ ]
866
+ });
867
+ const userOperationMaxGas = userOperation.preVerificationGas + userOperation.callGasLimit + userOperation.verificationGasLimit + (userOperation.paymasterPostOpGasLimit || BigInt(0)) + (userOperation.paymasterVerificationGasLimit || BigInt(0));
868
+ const userOperationMaxCost = BigInt(
869
+ userOperationMaxGas * userOperation.maxFeePerGas
870
+ );
871
+ const estimatedGasCostInToken = (userOperationMaxCost + postOpGas * userOperation.maxFeePerGas) * exchangeRate / BigInt(1e18);
872
+ const ActalinkFeesInToken = totalAmount * BigInt(20) / BigInt(1e4);
873
+ const estimatedTotalFeesInToken = estimatedGasCostInToken + ActalinkFeesInToken;
874
+ const feeInclusiveAmountInToken = totalAmount - estimatedTotalFeesInToken;
875
+ const feeExclusiveAmountInToken = totalAmount + estimatedTotalFeesInToken;
876
+ return {
877
+ estimatedGasCostInToken,
878
+ ActalinkFeesInToken,
879
+ estimatedTotalFeesInToken,
880
+ feeInclusiveAmountInToken,
881
+ feeExclusiveAmountInToken,
882
+ paymaster,
883
+ userOperation
884
+ };
885
+ });
886
+ }
810
887
  signSinglePaymentOperation(singlePaymentParams) {
811
888
  return __async(this, null, function* () {
812
889
  try {
@@ -895,6 +972,102 @@ var ActaAccount = class {
895
972
  }
896
973
  });
897
974
  }
975
+ signSingleBatchOperation(singlePaymentParams) {
976
+ return __async(this, null, function* () {
977
+ try {
978
+ if (!this.signer) {
979
+ throw new Error("Signer is required for self custody payments.");
980
+ }
981
+ const {
982
+ signerAddress,
983
+ chainId,
984
+ token: tokenSymbol,
985
+ receivers,
986
+ feeInclusive,
987
+ allowMaxTokenApproval,
988
+ totalAmount,
989
+ walletClient
990
+ } = singlePaymentParams;
991
+ if (totalAmount <= BigInt(0)) {
992
+ throw new Error("Amount must be greater than 0.");
993
+ }
994
+ if (receivers.length === 0) {
995
+ throw new Error("Receivers not found for batch payment");
996
+ }
997
+ const token2 = getTokenByChainIdAndSymbol(chainId, tokenSymbol);
998
+ if (!token2) {
999
+ throw new Error("Token not found.");
1000
+ }
1001
+ const viemClient = new ViemClient(this.chainId, this.signer);
1002
+ console.log("validating gas");
1003
+ console.log(receivers);
1004
+ const {
1005
+ estimatedGasCostInToken,
1006
+ ActalinkFeesInToken,
1007
+ feeInclusiveAmountInToken,
1008
+ feeExclusiveAmountInToken,
1009
+ estimatedTotalFeesInToken,
1010
+ paymaster
1011
+ } = yield this.estimateSingleBatchPaymentGas({
1012
+ signerAddress,
1013
+ chainId,
1014
+ token: tokenSymbol,
1015
+ receivers,
1016
+ feeInclusive,
1017
+ totalAmount,
1018
+ walletClient
1019
+ });
1020
+ const account = yield this.createAccount();
1021
+ const { accountClient } = yield this.createAccountHelpers();
1022
+ const fromAddress = signerAddress;
1023
+ const smartAccountAddress = account.address;
1024
+ const amountToTransfer = feeInclusive ? totalAmount : feeExclusiveAmountInToken;
1025
+ const receiverAmount = feeInclusive ? feeInclusiveAmountInToken : totalAmount;
1026
+ console.log("checking approval");
1027
+ yield viemClient.checkAndApproveToken(
1028
+ token2,
1029
+ smartAccountAddress,
1030
+ amountToTransfer,
1031
+ allowMaxTokenApproval != null ? allowMaxTokenApproval : false
1032
+ );
1033
+ const receiversData = receivers.map((r) => {
1034
+ return {
1035
+ to: getAddress2(token2.address),
1036
+ abi: parseAbi2(["function transferFrom(address,address,uint)"]),
1037
+ functionName: "transferFrom",
1038
+ args: [fromAddress, r.address, r.amount]
1039
+ };
1040
+ });
1041
+ const userOperation = yield accountClient.prepareUserOperation({
1042
+ calls: [
1043
+ ...receiversData,
1044
+ {
1045
+ to: getAddress2(token2.address),
1046
+ abi: parseAbi2(["function transferFrom(address,address,uint)"]),
1047
+ functionName: "transferFrom",
1048
+ args: [
1049
+ fromAddress,
1050
+ "0xC4910E5ec82Da0A41aF9C6360b7A1f531e1e37B0",
1051
+ estimatedGasCostInToken
1052
+ ]
1053
+ }
1054
+ ]
1055
+ });
1056
+ const signature = yield account.signUserOperation(__spreadProps(__spreadValues({}, userOperation), {
1057
+ chainId: this.chainId
1058
+ }));
1059
+ const rpcParameters = formatUserOperationRequest(__spreadProps(__spreadValues({}, userOperation), {
1060
+ signature
1061
+ }));
1062
+ return rpcParameters;
1063
+ } catch (error) {
1064
+ if (error instanceof Error) {
1065
+ throw new Error(error.message);
1066
+ }
1067
+ throw new Error("Failed to sign single payment operation.");
1068
+ }
1069
+ });
1070
+ }
898
1071
  signRecurringPayments(recurringPaymentParams) {
899
1072
  return __async(this, null, function* () {
900
1073
  if (!this.signer) {
@@ -1255,6 +1428,24 @@ function executeSinglePaymentAPICall(url, userOperation, paymentParams, serviceP
1255
1428
  return response.data;
1256
1429
  });
1257
1430
  }
1431
+ function executeSingleBatchPaymentAPICall(url, APIKey, userOperation, paymentParams, serviceParams) {
1432
+ return __async(this, null, function* () {
1433
+ const params = {
1434
+ userOperation,
1435
+ paymentParams,
1436
+ serviceParams
1437
+ };
1438
+ const response = yield sendRequest({
1439
+ url,
1440
+ method: "post" /* Post */,
1441
+ body: params,
1442
+ headers: {
1443
+ "x-api-key": APIKey
1444
+ }
1445
+ });
1446
+ return response.data;
1447
+ });
1448
+ }
1258
1449
  function fetchRecurringTransactionWithId(url) {
1259
1450
  return __async(this, null, function* () {
1260
1451
  const response = yield sendRequest({ url, method: "get" /* Get */ });
@@ -1276,6 +1467,31 @@ function cancelRecurringPaymentAPICall(url, userOperation, paymentParams, servic
1276
1467
  return response.data;
1277
1468
  });
1278
1469
  }
1470
+ function createBatchSessionAPICall(url, APIKey, params) {
1471
+ return __async(this, null, function* () {
1472
+ const response = yield sendRequest({
1473
+ url,
1474
+ method: "post" /* Post */,
1475
+ body: params,
1476
+ headers: {
1477
+ "x-api-key": APIKey
1478
+ }
1479
+ });
1480
+ return response.data;
1481
+ });
1482
+ }
1483
+ function fetchBatchInstructionDetails(url, APIKey) {
1484
+ return __async(this, null, function* () {
1485
+ const response = yield sendRequest({
1486
+ url,
1487
+ method: "get" /* Get */,
1488
+ headers: {
1489
+ "x-api-key": APIKey
1490
+ }
1491
+ });
1492
+ return response.data;
1493
+ });
1494
+ }
1279
1495
 
1280
1496
  // src/deposit.ts
1281
1497
  var transactionServiceUrl = "https://api.acta.link/transaction/v1/";
@@ -1840,6 +2056,213 @@ var ActaBilling = class {
1840
2056
  }
1841
2057
  };
1842
2058
 
2059
+ // src/batch.ts
2060
+ import {
2061
+ getAddress as getAddress3,
2062
+ parseUnits,
2063
+ toHex as toHex4,
2064
+ zeroAddress as zeroAddress5
2065
+ } from "viem";
2066
+ var batchServiceUrl = "https://api.acta.link/batch/api/v1/";
2067
+ var ActaBatch = class {
2068
+ constructor(parameters) {
2069
+ this.APIkey = parameters.APIKey;
2070
+ }
2071
+ createSession(params) {
2072
+ return __async(this, null, function* () {
2073
+ try {
2074
+ const { instructionId, name, signerAddress, token: token2, chainId } = params;
2075
+ if (!signerAddress || signerAddress === zeroAddress5) {
2076
+ throw new Error("Signer address is required.");
2077
+ }
2078
+ if (!name || name === "") {
2079
+ throw new Error("Name of Batch payment is required");
2080
+ }
2081
+ if (!token2) {
2082
+ throw new Error("Token symbol is required");
2083
+ }
2084
+ if (!instructionId || instructionId === "") {
2085
+ throw new Error("Instruction id is required");
2086
+ }
2087
+ const session = yield createBatchSessionAPICall(
2088
+ `${batchServiceUrl}create/session`,
2089
+ this.APIkey,
2090
+ {
2091
+ name,
2092
+ chainId,
2093
+ instructionId,
2094
+ signerAddress,
2095
+ token: token2
2096
+ }
2097
+ );
2098
+ console.log(session);
2099
+ const sessionId = session.id;
2100
+ return sessionId;
2101
+ } catch (error) {
2102
+ if (error instanceof Error) {
2103
+ throw new Error(error.message);
2104
+ }
2105
+ throw new Error("Failed to create session.");
2106
+ }
2107
+ });
2108
+ }
2109
+ estimateSinglePaymentGas(parameters) {
2110
+ return __async(this, null, function* () {
2111
+ try {
2112
+ const {
2113
+ chainId,
2114
+ feeInclusive,
2115
+ signerAddress,
2116
+ token: token2,
2117
+ walletClient,
2118
+ instructionId
2119
+ } = parameters;
2120
+ const instuctionData = yield fetchBatchInstructionDetails(
2121
+ `${batchServiceUrl}instruction/${instructionId}`,
2122
+ this.APIkey
2123
+ );
2124
+ if (instuctionData.receivers.length === 0 || instuctionData.receivers.length === void 0) {
2125
+ throw new Error("Instruction not found");
2126
+ }
2127
+ const tokenData = getTokenByChainIdAndSymbol(chainId, token2);
2128
+ if (!tokenData) {
2129
+ throw new Error("Token not supported");
2130
+ }
2131
+ const totalAmount = instuctionData.totalAmount;
2132
+ const totalAmountParsed = BigInt(
2133
+ parseUnits(totalAmount, tokenData.decimals)
2134
+ );
2135
+ const receivers = instuctionData.receivers.map(
2136
+ (i) => {
2137
+ const receiver = getAddress3(i.address);
2138
+ const tokenAmount = BigInt(parseUnits(i.amount, tokenData.decimals));
2139
+ return {
2140
+ address: receiver,
2141
+ amount: tokenAmount
2142
+ };
2143
+ }
2144
+ );
2145
+ const viemClient = new ViemClient(chainId, walletClient);
2146
+ const account = new ActaAccount(
2147
+ chainId,
2148
+ viemClient.publicClient(),
2149
+ walletClient
2150
+ );
2151
+ const {
2152
+ estimatedGasCostInToken,
2153
+ ActalinkFeesInToken,
2154
+ feeInclusiveAmountInToken,
2155
+ feeExclusiveAmountInToken,
2156
+ estimatedTotalFeesInToken,
2157
+ paymaster,
2158
+ userOperation
2159
+ } = yield account.estimateSingleBatchPaymentGas({
2160
+ chainId,
2161
+ feeInclusive,
2162
+ signerAddress,
2163
+ token: token2,
2164
+ walletClient,
2165
+ receivers,
2166
+ totalAmount: totalAmountParsed
2167
+ });
2168
+ return {
2169
+ estimatedGasCostInToken,
2170
+ ActalinkFeesInToken,
2171
+ feeInclusiveAmountInToken,
2172
+ feeExclusiveAmountInToken,
2173
+ estimatedTotalFeesInToken,
2174
+ paymaster,
2175
+ userOperation
2176
+ };
2177
+ } catch (error) {
2178
+ if (error instanceof Error) {
2179
+ throw new Error(error.message);
2180
+ }
2181
+ throw new Error("Failed to estimate single payment gas.");
2182
+ }
2183
+ });
2184
+ }
2185
+ createSingleBatchPayment(params, serviceParams) {
2186
+ return __async(this, null, function* () {
2187
+ try {
2188
+ const {
2189
+ chainId,
2190
+ feeInclusive,
2191
+ signerAddress,
2192
+ token: tokenSymbol,
2193
+ walletClient,
2194
+ allowMaxTokenApproval,
2195
+ instructionId
2196
+ } = params;
2197
+ const instuctionData = yield fetchBatchInstructionDetails(
2198
+ `${batchServiceUrl}instruction/${instructionId}`,
2199
+ this.APIkey
2200
+ );
2201
+ if (instuctionData.receivers.length === 0 || instuctionData.receivers.length === void 0) {
2202
+ throw new Error("Instruction not found");
2203
+ }
2204
+ console.log(instuctionData);
2205
+ const tokenData = getTokenByChainIdAndSymbol(chainId, tokenSymbol);
2206
+ if (!tokenData) {
2207
+ throw new Error("Token not supported");
2208
+ }
2209
+ const totalAmount = instuctionData.totalAmount;
2210
+ const totalAmountParsed = BigInt(
2211
+ parseUnits(totalAmount, tokenData.decimals)
2212
+ );
2213
+ const receivers = instuctionData.receivers.map(
2214
+ (i) => {
2215
+ const receiver = getAddress3(i.address);
2216
+ const tokenAmount = BigInt(parseUnits(i.amount, tokenData.decimals));
2217
+ return {
2218
+ address: receiver,
2219
+ amount: tokenAmount
2220
+ };
2221
+ }
2222
+ );
2223
+ const viemClient = new ViemClient(chainId, walletClient);
2224
+ const account = new ActaAccount(
2225
+ chainId,
2226
+ viemClient.publicClient(),
2227
+ walletClient
2228
+ );
2229
+ console.log("signing");
2230
+ const rpcParameters = yield account.signSingleBatchOperation({
2231
+ signerAddress,
2232
+ chainId,
2233
+ token: tokenSymbol,
2234
+ feeInclusive,
2235
+ receivers,
2236
+ totalAmount: totalAmountParsed,
2237
+ walletClient,
2238
+ allowMaxTokenApproval
2239
+ });
2240
+ const txn = yield executeSingleBatchPaymentAPICall(
2241
+ `${batchServiceUrl}execute/single/batch`,
2242
+ this.APIkey,
2243
+ rpcParameters,
2244
+ {
2245
+ senderAddress: signerAddress,
2246
+ chainId,
2247
+ tokenAddress: tokenData.address,
2248
+ amount: toHex4(totalAmountParsed),
2249
+ feeInclusive,
2250
+ serviceType: "batch"
2251
+ },
2252
+ serviceParams
2253
+ );
2254
+ console.log(txn);
2255
+ return txn.transaction.id;
2256
+ } catch (error) {
2257
+ if (error instanceof Error) {
2258
+ throw new Error(error.message);
2259
+ }
2260
+ throw new Error("Failed to create payment.");
2261
+ }
2262
+ });
2263
+ }
2264
+ };
2265
+
1843
2266
  // src/utils.ts
1844
2267
  import {
1845
2268
  constants,
@@ -1859,7 +2282,7 @@ import {
1859
2282
  encodeAbiParameters,
1860
2283
  keccak256,
1861
2284
  slice,
1862
- zeroAddress as zeroAddress5,
2285
+ zeroAddress as zeroAddress6,
1863
2286
  decodeFunctionData,
1864
2287
  hexToBigInt as hexToBigInt2
1865
2288
  } from "viem";
@@ -1970,7 +2393,7 @@ function toPermissionValidator2(_0, _1) {
1970
2393
  return __spreadProps(__spreadValues({}, signer.account), {
1971
2394
  supportedKernelVersions: ">=0.3.0",
1972
2395
  validatorType: "PERMISSION",
1973
- address: zeroAddress5,
2396
+ address: zeroAddress6,
1974
2397
  source: "PermissionValidator",
1975
2398
  getEnableData,
1976
2399
  getIdentifier: getPermissionId,
@@ -2299,6 +2722,7 @@ var cancelRecurringTransaction = (_0) => __async(null, [_0], function* ({
2299
2722
  });
2300
2723
  export {
2301
2724
  ActaAccount,
2725
+ ActaBatch,
2302
2726
  ActaBilling,
2303
2727
  ActaDeposit,
2304
2728
  ConnectorType,
@@ -2327,6 +2751,7 @@ export {
2327
2751
  bytesToBase64,
2328
2752
  cancelRecurringPaymentAPICall,
2329
2753
  cancelRecurringTransaction,
2754
+ createBatchSessionAPICall,
2330
2755
  createBillingCheckoutSession,
2331
2756
  createBillingSessionAPICall,
2332
2757
  createPolicyFromParams,
@@ -2340,7 +2765,9 @@ export {
2340
2765
  ethereumUSDC,
2341
2766
  ethereumUSDT,
2342
2767
  ethereumWETH,
2768
+ executeSingleBatchPaymentAPICall,
2343
2769
  executeSinglePaymentAPICall,
2770
+ fetchBatchInstructionDetails,
2344
2771
  fetchBillingSessionDetails,
2345
2772
  fetchRecurringTransactionWithId,
2346
2773
  getBillingPaymentSessionDetails,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@actalink/commonlib",
3
- "version": "0.0.11",
3
+ "version": "0.0.13",
4
4
  "author": "Actalink",
5
5
  "license": "MIT license",
6
6
  "publishConfig": {