@actalink/commonlib 0.0.6 → 0.0.8

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
@@ -688,7 +688,142 @@ import {
688
688
  toTimestampPolicy
689
689
  } from "@zerodev/permissions/policies";
690
690
  import { coerce, gt } from "semver";
691
+
692
+ // src/api.ts
693
+ import { toHex } from "viem";
694
+ var HttpMethod = /* @__PURE__ */ ((HttpMethod2) => {
695
+ HttpMethod2["Get"] = "get";
696
+ HttpMethod2["Post"] = "post";
697
+ HttpMethod2["Delete"] = "delete";
698
+ HttpMethod2["Update"] = "update";
699
+ return HttpMethod2;
700
+ })(HttpMethod || {});
701
+ function sendRequest(_0) {
702
+ return __async(this, arguments, function* ({
703
+ url,
704
+ method,
705
+ body,
706
+ headers = {}
707
+ }) {
708
+ var _a;
709
+ const response = yield fetch(url, {
710
+ method,
711
+ headers: __spreadProps(__spreadValues({}, headers), {
712
+ Accept: "application/json",
713
+ "Content-Type": "application/json"
714
+ }),
715
+ body: JSON.stringify(body)
716
+ });
717
+ let jsonResponse;
718
+ const res = yield response.json();
719
+ if (!response.ok) {
720
+ jsonResponse = {
721
+ error: {
722
+ code: response.status,
723
+ message: response.statusText
724
+ },
725
+ data: void 0
726
+ };
727
+ return jsonResponse;
728
+ }
729
+ if (!res) {
730
+ jsonResponse = {
731
+ error: {
732
+ code: 400,
733
+ message: "No response received."
734
+ },
735
+ data: void 0
736
+ };
737
+ return jsonResponse;
738
+ }
739
+ if (res == null ? void 0 : res.error) {
740
+ jsonResponse = {
741
+ error: {
742
+ code: response.status,
743
+ message: res.error
744
+ },
745
+ data: void 0
746
+ };
747
+ return jsonResponse;
748
+ }
749
+ jsonResponse = {
750
+ data: res.data,
751
+ message: (_a = res.message) != null ? _a : "Success",
752
+ error: void 0
753
+ };
754
+ return jsonResponse;
755
+ });
756
+ }
757
+ function createSessionAPICall(url, sessionBodyParams) {
758
+ return __async(this, null, function* () {
759
+ const response = yield sendRequest({
760
+ url,
761
+ method: "post" /* Post */,
762
+ body: sessionBodyParams
763
+ });
764
+ return response.data;
765
+ });
766
+ }
767
+ function verifySessionAPICall(url, sessionId) {
768
+ return __async(this, null, function* () {
769
+ const response = yield sendRequest({
770
+ url,
771
+ method: "get" /* Get */
772
+ });
773
+ return response.data;
774
+ });
775
+ }
776
+ function scheduleRecurringPaymentsAPICall(url, params) {
777
+ return __async(this, null, function* () {
778
+ const parsedParams = __spreadProps(__spreadValues({}, params), {
779
+ amount: toHex(params.amount)
780
+ });
781
+ const response = yield sendRequest({
782
+ url,
783
+ method: "post" /* Post */,
784
+ body: parsedParams
785
+ });
786
+ return response.data;
787
+ });
788
+ }
789
+ function createBillingSessionAPICall(url, sessionBodyParams) {
790
+ return __async(this, null, function* () {
791
+ const response = yield sendRequest({
792
+ url,
793
+ method: "post" /* Post */,
794
+ body: sessionBodyParams
795
+ });
796
+ return response.data;
797
+ });
798
+ }
799
+ function fetchBillingSessionDetails(url) {
800
+ return __async(this, null, function* () {
801
+ const response = yield sendRequest({
802
+ url,
803
+ method: "get" /* Get */
804
+ });
805
+ return response.data;
806
+ });
807
+ }
808
+ function executeSinglePaymentAPICall(url, userOperation, paymentParams, serviceParams) {
809
+ return __async(this, null, function* () {
810
+ const params = {
811
+ userOperation,
812
+ paymentParams,
813
+ serviceParams
814
+ };
815
+ const response = yield sendRequest({
816
+ url,
817
+ method: "post" /* Post */,
818
+ body: params
819
+ });
820
+ return response.data;
821
+ });
822
+ }
823
+
824
+ // src/utils.ts
691
825
  var ECDSA_SIGNER_CONTRACT = "0x6A6F069E2a08c2468e7724Ab3250CdBFBA14D4FF";
826
+ var billingServiceUrl = "https://api.acta.link/billing/v1/";
692
827
  var toSignerId = (signer) => {
693
828
  return encodeAbiParameters(
694
829
  [{ name: "signerData", type: "bytes" }],
@@ -1049,6 +1184,33 @@ var getPeriodInterval = (periodUnit) => {
1049
1184
  throw new Error("Invalid period unit");
1050
1185
  }
1051
1186
  };
1187
+ var createBillingCheckoutSession = (serviceSessionparams) => __async(null, null, function* () {
1188
+ try {
1189
+ const sessionData = yield createBillingSessionAPICall(
1190
+ `${billingServiceUrl}paysession/create`,
1191
+ {
1192
+ paylinkId: serviceSessionparams.paylinkId
1193
+ }
1194
+ );
1195
+ if (sessionData) {
1196
+ return sessionData.checkoutSessionId;
1197
+ }
1198
+ } catch (error) {
1199
+ if (error instanceof Error) {
1200
+ throw new Error(error.message);
1201
+ }
1202
+ throw new Error("Failed to create deposit session.");
1203
+ }
1204
+ });
1205
+ var getBillingPaymentSessionDetails = (serviceSessionparams) => __async(null, null, function* () {
1206
+ if (!serviceSessionparams.checkoutSessionId || serviceSessionparams.checkoutSessionId === "") {
1207
+ throw new Error("checkout session ID is required.");
1208
+ }
1209
+ const sessionData = yield fetchBillingSessionDetails(
1210
+ `${billingServiceUrl}paysession?checkoutSessionId=${serviceSessionparams.checkoutSessionId}`
1211
+ );
1212
+ return sessionData;
1213
+ });
1052
1214
 
1053
1215
  // src/account.ts
1054
1216
  import {
@@ -1143,7 +1305,7 @@ var ActaAccount = class {
1143
1305
  functionName: "transferFrom",
1144
1306
  args: [
1145
1307
  fromAddress,
1146
- "0x061BA68bc8208F4AddBeE86F74F17D77129cCF70",
1308
+ "0xC4910E5ec82Da0A41aF9C6360b7A1f531e1e37B0",
1147
1309
  BigInt(0)
1148
1310
  ]
1149
1311
  },
@@ -1389,119 +1551,6 @@ var ActaAccount = class {
1389
1551
  }
1390
1552
  };
1391
1553
 
1392
- // src/api.ts
1393
- import { toHex } from "viem";
1394
- var HttpMethod = /* @__PURE__ */ ((HttpMethod2) => {
1395
- HttpMethod2["Get"] = "get";
1396
- HttpMethod2["Post"] = "post";
1397
- HttpMethod2["Delete"] = "delete";
1398
- HttpMethod2["Update"] = "update";
1399
- return HttpMethod2;
1400
- })(HttpMethod || {});
1401
- function sendRequest(_0) {
1402
- return __async(this, arguments, function* ({
1403
- url,
1404
- method,
1405
- body,
1406
- headers = {}
1407
- }) {
1408
- var _a;
1409
- const response = yield fetch(url, {
1410
- method,
1411
- headers: __spreadProps(__spreadValues({}, headers), {
1412
- Accept: "application/json",
1413
- "Content-Type": "application/json"
1414
- }),
1415
- body: JSON.stringify(body)
1416
- });
1417
- let jsonResponse;
1418
- const res = yield response.json();
1419
- if (!response.ok) {
1420
- jsonResponse = {
1421
- error: {
1422
- code: response.status,
1423
- message: response.statusText
1424
- },
1425
- data: void 0
1426
- };
1427
- return jsonResponse;
1428
- }
1429
- if (!res) {
1430
- jsonResponse = {
1431
- error: {
1432
- code: 400,
1433
- message: "No response received."
1434
- },
1435
- data: void 0
1436
- };
1437
- return jsonResponse;
1438
- }
1439
- if (res == null ? void 0 : res.error) {
1440
- jsonResponse = {
1441
- error: {
1442
- code: response.status,
1443
- message: res.error
1444
- },
1445
- data: void 0
1446
- };
1447
- return jsonResponse;
1448
- }
1449
- jsonResponse = {
1450
- data: res.data,
1451
- message: (_a = res.message) != null ? _a : "Success",
1452
- error: void 0
1453
- };
1454
- return jsonResponse;
1455
- });
1456
- }
1457
- function createSessionAPICall(url, sessionBodyParams) {
1458
- return __async(this, null, function* () {
1459
- const response = yield sendRequest({
1460
- url,
1461
- method: "post" /* Post */,
1462
- body: sessionBodyParams
1463
- });
1464
- return response.data;
1465
- });
1466
- }
1467
- function verifySessionAPICall(url, sessionId) {
1468
- return __async(this, null, function* () {
1469
- const response = yield sendRequest({
1470
- url,
1471
- method: "get" /* Get */
1472
- });
1473
- return response.data;
1474
- });
1475
- }
1476
- function scheduleRecurringPaymentsAPICall(url, params) {
1477
- return __async(this, null, function* () {
1478
- const parsedParams = __spreadProps(__spreadValues({}, params), {
1479
- amount: toHex(params.amount)
1480
- });
1481
- const response = yield sendRequest({
1482
- url,
1483
- method: "post" /* Post */,
1484
- body: parsedParams
1485
- });
1486
- return response.data;
1487
- });
1488
- }
1489
- function executeSinglePaymentAPICall(url, userOperation, paymentParams, serviceParams) {
1490
- return __async(this, null, function* () {
1491
- const params = {
1492
- userOperation,
1493
- paymentParams,
1494
- serviceParams
1495
- };
1496
- const response = yield sendRequest({
1497
- url,
1498
- method: "post" /* Post */,
1499
- body: params
1500
- });
1501
- return response.data;
1502
- });
1503
- }
1504
-
1505
1554
  // src/deposit.ts
1506
1555
  var transactionServiceUrl = "https://api.acta.link/transaction/v1/";
1507
1556
  var depositServiceUrl = "https://api.acta.link/deposit/v1/";
@@ -1845,8 +1894,226 @@ var ActaDeposit = class {
1845
1894
  });
1846
1895
  }
1847
1896
  };
1897
+
1898
+ // src/billing.ts
1899
+ import { toHex as toHex3 } from "viem";
1900
+ var transactionServiceUrl2 = "https://api.acta.link/transaction/v1/";
1901
+ var ActaBilling = class {
1902
+ constructor(parameters) {
1903
+ this.count = 0;
1904
+ this.intervalUnit = void 0;
1905
+ this.startDate = void 0;
1906
+ this.endDate = void 0;
1907
+ this.serviceSessionParams = void 0;
1908
+ this.status = "not_started";
1909
+ this.serviceType = "deposit";
1910
+ this.allowMaxTokenApproval = false;
1911
+ var _a;
1912
+ this.connectorType = parameters.connectorType;
1913
+ this.walletClient = parameters.walletClient;
1914
+ this.signerAddress = parameters.signerAddress;
1915
+ this.chainId = parameters.chainId;
1916
+ this.token = parameters.token;
1917
+ this.amount = parameters.amount;
1918
+ this.receiver = parameters.receiver;
1919
+ this.feeInclusive = parameters.feeInclusive;
1920
+ this.paymentType = parameters.paymentType;
1921
+ this.count = parameters.count;
1922
+ this.intervalUnit = parameters.intervalUnit;
1923
+ this.startDate = parameters.startDate;
1924
+ this.endDate = parameters.endDate;
1925
+ this.allowMaxTokenApproval = (_a = parameters.allowMaxTokenApproval) != null ? _a : false;
1926
+ this.serviceType = parameters.serviceType;
1927
+ this.viemClient = new ViemClient(this.chainId, this.walletClient);
1928
+ this.account = new ActaAccount(
1929
+ this.chainId,
1930
+ this.viemClient.publicClient(),
1931
+ this.walletClient
1932
+ );
1933
+ }
1934
+ estimateSinglePaymentGas(parameters) {
1935
+ return __async(this, null, function* () {
1936
+ try {
1937
+ if (this.connectorType !== "Self-Custody" /* SELF_CUSTODY */) {
1938
+ throw new Error("Only self custody payments are supported.");
1939
+ }
1940
+ const {
1941
+ estimatedGasCostInToken,
1942
+ ActalinkFeesInToken,
1943
+ feeInclusiveAmountInToken,
1944
+ feeExclusiveAmountInToken,
1945
+ estimatedTotalFeesInToken,
1946
+ paymaster,
1947
+ userOperation
1948
+ } = yield this.account.estimateSinglePaymentGas(parameters);
1949
+ return {
1950
+ estimatedGasCostInToken,
1951
+ ActalinkFeesInToken,
1952
+ feeInclusiveAmountInToken,
1953
+ feeExclusiveAmountInToken,
1954
+ estimatedTotalFeesInToken,
1955
+ paymaster,
1956
+ userOperation
1957
+ };
1958
+ } catch (error) {
1959
+ if (error instanceof Error) {
1960
+ throw new Error(error.message);
1961
+ }
1962
+ throw new Error("Failed to estimate single payment gas.");
1963
+ }
1964
+ });
1965
+ }
1966
+ createSinglePayment(servicePaymentParams) {
1967
+ return __async(this, null, function* () {
1968
+ try {
1969
+ if (this.connectorType !== "Self-Custody" /* SELF_CUSTODY */) {
1970
+ throw new Error("Only self custody payments are supported.");
1971
+ }
1972
+ if (!servicePaymentParams.checkoutSessionId || servicePaymentParams.checkoutSessionId === "") {
1973
+ throw new Error("checkout session ID is required.");
1974
+ }
1975
+ const signerAddress = this.signerAddress;
1976
+ const chainId = this.chainId;
1977
+ const tokenSymbol = this.token;
1978
+ const amount = this.amount;
1979
+ const receiver = this.receiver;
1980
+ const feeInclusive = this.feeInclusive;
1981
+ const serviceType = this.serviceType;
1982
+ const token2 = getTokenByChainIdAndSymbol(chainId, tokenSymbol);
1983
+ if (!token2) {
1984
+ throw new Error("Token not supported.");
1985
+ }
1986
+ const rpcParameters = yield this.account.signSinglePaymentOperation({
1987
+ signerAddress,
1988
+ chainId,
1989
+ token: tokenSymbol,
1990
+ amount,
1991
+ receiver,
1992
+ feeInclusive,
1993
+ allowMaxTokenApproval: this.allowMaxTokenApproval
1994
+ });
1995
+ const txn = yield executeSinglePaymentAPICall(
1996
+ `${transactionServiceUrl2}execute/single`,
1997
+ rpcParameters,
1998
+ {
1999
+ senderAddress: signerAddress,
2000
+ receiverAddress: receiver,
2001
+ chainId,
2002
+ tokenAddress: token2.address,
2003
+ amount: toHex3(amount),
2004
+ feeInclusive,
2005
+ serviceType
2006
+ },
2007
+ servicePaymentParams
2008
+ );
2009
+ return txn.transaction.id;
2010
+ } catch (error) {
2011
+ if (error instanceof Error) {
2012
+ throw new Error(error.message);
2013
+ }
2014
+ throw new Error("Failed to create payment.");
2015
+ }
2016
+ });
2017
+ }
2018
+ createRecurringPayments(servicePaymentParams) {
2019
+ return __async(this, null, function* () {
2020
+ if (this.connectorType !== "Self-Custody" /* SELF_CUSTODY */) {
2021
+ throw new Error("Only self custody payments are supported.");
2022
+ }
2023
+ if (!servicePaymentParams.checkoutSessionId || servicePaymentParams.checkoutSessionId === "") {
2024
+ throw new Error("checkout session ID is required.");
2025
+ }
2026
+ if (!this.walletClient) {
2027
+ throw new Error("Signer is required for self custody payments.");
2028
+ }
2029
+ const signerAddress = this.signerAddress;
2030
+ const chainId = this.chainId;
2031
+ const tokenSymbol = this.token;
2032
+ const amount = this.amount;
2033
+ const receiver = this.receiver;
2034
+ const feeInclusive = this.feeInclusive;
2035
+ const count = this.count;
2036
+ const intervalUnit = this.intervalUnit;
2037
+ const startDate = this.startDate;
2038
+ const endDate = this.endDate;
2039
+ const serviceType = this.serviceType;
2040
+ const token2 = getTokenByChainIdAndSymbol(chainId, tokenSymbol);
2041
+ if (!token2) {
2042
+ throw new Error("Token not supported.");
2043
+ }
2044
+ if (!chainId || !count || !intervalUnit || !startDate || !endDate) {
2045
+ throw new Error("Invalid parameters.");
2046
+ }
2047
+ const approval = yield this.account.signRecurringPayments({
2048
+ signerAddress,
2049
+ chainId,
2050
+ token: tokenSymbol,
2051
+ amount,
2052
+ feeInclusive,
2053
+ count,
2054
+ intervalUnit,
2055
+ startDate,
2056
+ endDate,
2057
+ receiver,
2058
+ allowMaxTokenApproval: this.allowMaxTokenApproval
2059
+ });
2060
+ const txn = yield scheduleRecurringPaymentsAPICall(
2061
+ `${transactionServiceUrl2}schedule/recurring`,
2062
+ {
2063
+ amount,
2064
+ chainId,
2065
+ feeInclusive,
2066
+ intervalCount: count,
2067
+ intervalUnit,
2068
+ senderAddress: signerAddress,
2069
+ receiverAddress: receiver,
2070
+ tokenAddress: token2.address,
2071
+ startAt: startDate,
2072
+ endAt: endDate,
2073
+ approval,
2074
+ serviceParams: servicePaymentParams,
2075
+ serviceType
2076
+ }
2077
+ );
2078
+ return txn.recurringTransaction.id;
2079
+ });
2080
+ }
2081
+ getPaymentSteps() {
2082
+ return __async(this, null, function* () {
2083
+ const signerAddress = this.signerAddress;
2084
+ const chainId = this.chainId;
2085
+ const tokenSymbol = this.token;
2086
+ const amount = this.amount;
2087
+ const receiver = this.receiver;
2088
+ const feeInclusive = this.feeInclusive;
2089
+ const count = this.count;
2090
+ if (!signerAddress) return ["allowance", "confirm"];
2091
+ const token2 = getTokenByChainIdAndSymbol(chainId, tokenSymbol);
2092
+ if (!token2) return ["allowance", "confirm"];
2093
+ const smartAccount = yield this.account.createAccount();
2094
+ const { estimatedGasCostInToken, feeExclusiveAmountInToken } = yield this.account.estimateSinglePaymentGas({
2095
+ chainId,
2096
+ amount,
2097
+ feeInclusive,
2098
+ receiver,
2099
+ signerAddress,
2100
+ token: tokenSymbol
2101
+ });
2102
+ const amountToTransfer = feeInclusive ? amount : feeExclusiveAmountInToken;
2103
+ let paymentAmount = (amountToTransfer + estimatedGasCostInToken * BigInt(2)) * BigInt(count != null ? count : 1);
2104
+ const allowance = yield this.viemClient.checkTokenAllowance(
2105
+ token2,
2106
+ signerAddress,
2107
+ smartAccount.address
2108
+ );
2109
+ if (paymentAmount <= allowance) return ["confirm"];
2110
+ return ["allowance", "confirm"];
2111
+ });
2112
+ }
2113
+ };
1848
2114
  export {
1849
2115
  ActaAccount,
2116
+ ActaBilling,
1850
2117
  ActaDeposit,
1851
2118
  ConnectorType,
1852
2119
  HttpMethod,
@@ -1872,6 +2139,8 @@ export {
1872
2139
  bscUSDT,
1873
2140
  bscWBNB,
1874
2141
  bytesToBase64,
2142
+ createBillingCheckoutSession,
2143
+ createBillingSessionAPICall,
1875
2144
  createPolicyFromParams,
1876
2145
  createSessionAPICall,
1877
2146
  decodeParamsFromInitCode,
@@ -1884,6 +2153,8 @@ export {
1884
2153
  ethereumUSDT,
1885
2154
  ethereumWETH,
1886
2155
  executeSinglePaymentAPICall,
2156
+ fetchBillingSessionDetails,
2157
+ getBillingPaymentSessionDetails,
1887
2158
  getChainById,
1888
2159
  getChainExplorerByChainId,
1889
2160
  getPeriodInterval,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@actalink/commonlib",
3
- "version": "0.0.6",
3
+ "version": "0.0.8",
4
4
  "author": "Actalink",
5
5
  "license": "MIT license",
6
6
  "publishConfig": {