@paxoslabs/amplify-sdk 0.1.1 → 0.2.0-alpha.0

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.
Files changed (53) hide show
  1. package/CHANGELOG.md +0 -2
  2. package/README.md +42 -0
  3. package/dist/{chunk-DTFLXAZJ.js → chunk-4NQCOMCZ.js} +147 -5
  4. package/dist/chunk-4NQCOMCZ.js.map +1 -0
  5. package/dist/{chunk-KYR6BFAE.js → chunk-5VOI4WPL.js} +38 -33
  6. package/dist/chunk-5VOI4WPL.js.map +1 -0
  7. package/dist/{chunk-ZILA73XN.mjs → chunk-5Y7M43XN.mjs} +144 -4
  8. package/dist/chunk-5Y7M43XN.mjs.map +1 -0
  9. package/dist/{chunk-VIULRHK6.mjs → chunk-7PCUTZF6.mjs} +3 -3
  10. package/dist/{chunk-VIULRHK6.mjs.map → chunk-7PCUTZF6.mjs.map} +1 -1
  11. package/dist/{chunk-XXHRCCZS.mjs → chunk-AQFFMJX7.mjs} +3 -3
  12. package/dist/chunk-AQFFMJX7.mjs.map +1 -0
  13. package/dist/{chunk-REKEQLQA.mjs → chunk-BOW2QPQC.mjs} +3 -3
  14. package/dist/{chunk-REKEQLQA.mjs.map → chunk-BOW2QPQC.mjs.map} +1 -1
  15. package/dist/{chunk-HI44AMLC.mjs → chunk-E7R7TL7J.mjs} +24 -19
  16. package/dist/chunk-E7R7TL7J.mjs.map +1 -0
  17. package/dist/{chunk-CUGK4ZBJ.mjs → chunk-IMS2HBJ7.mjs} +305 -30
  18. package/dist/chunk-IMS2HBJ7.mjs.map +1 -0
  19. package/dist/{chunk-LSNRG5Z2.js → chunk-KB4EPCK2.js} +4 -4
  20. package/dist/{chunk-LSNRG5Z2.js.map → chunk-KB4EPCK2.js.map} +1 -1
  21. package/dist/{chunk-ITB7FXG4.js → chunk-OJOKPBK7.js} +3 -3
  22. package/dist/chunk-OJOKPBK7.js.map +1 -0
  23. package/dist/{chunk-B2QCI6ET.js → chunk-UMRTU75Q.js} +309 -30
  24. package/dist/chunk-UMRTU75Q.js.map +1 -0
  25. package/dist/{chunk-GS7TDQXA.js → chunk-XUS6KI3G.js} +8 -8
  26. package/dist/{chunk-GS7TDQXA.js.map → chunk-XUS6KI3G.js.map} +1 -1
  27. package/dist/core.js +16 -16
  28. package/dist/core.js.map +1 -1
  29. package/dist/core.mjs +6 -6
  30. package/dist/core.mjs.map +1 -1
  31. package/dist/display.d.mts +1 -1
  32. package/dist/display.d.ts +1 -1
  33. package/dist/display.js +12 -12
  34. package/dist/display.mjs +4 -4
  35. package/dist/{exchange-rate-BfPH_fQt.d.mts → exchange-rate-DCB2CY9v.d.mts} +5 -0
  36. package/dist/{exchange-rate-Cp1ddpw4.d.ts → exchange-rate-DOmoqut9.d.ts} +5 -0
  37. package/dist/index.d.mts +455 -8
  38. package/dist/index.d.ts +455 -8
  39. package/dist/index.js +402 -215
  40. package/dist/index.js.map +1 -1
  41. package/dist/index.mjs +292 -123
  42. package/dist/index.mjs.map +1 -1
  43. package/dist/utils.js +5 -5
  44. package/dist/utils.mjs +2 -2
  45. package/package.json +2 -1
  46. package/dist/chunk-B2QCI6ET.js.map +0 -1
  47. package/dist/chunk-CUGK4ZBJ.mjs.map +0 -1
  48. package/dist/chunk-DTFLXAZJ.js.map +0 -1
  49. package/dist/chunk-HI44AMLC.mjs.map +0 -1
  50. package/dist/chunk-ITB7FXG4.js.map +0 -1
  51. package/dist/chunk-KYR6BFAE.js.map +0 -1
  52. package/dist/chunk-XXHRCCZS.mjs.map +0 -1
  53. package/dist/chunk-ZILA73XN.mjs.map +0 -1
@@ -3,29 +3,92 @@
3
3
  var viem = require('viem');
4
4
  var chains = require('viem/chains');
5
5
 
6
- // src/types/amplify-sdk-api.ts
7
- var APIError = class _APIError extends Error {
8
- constructor(message, options) {
9
- super(message);
10
- this.name = "APIError";
11
- this.statusCode = options?.statusCode;
12
- this.endpoint = options?.endpoint;
13
- this.cause = options?.cause;
14
- if (Error.captureStackTrace) {
15
- Error.captureStackTrace(this, _APIError);
6
+ // src/types/telemetry.ts
7
+ var LogLevel = /* @__PURE__ */ ((LogLevel2) => {
8
+ LogLevel2[LogLevel2["DEBUG"] = 0] = "DEBUG";
9
+ LogLevel2[LogLevel2["INFO"] = 1] = "INFO";
10
+ LogLevel2[LogLevel2["WARN"] = 2] = "WARN";
11
+ LogLevel2[LogLevel2["ERROR"] = 3] = "ERROR";
12
+ LogLevel2[LogLevel2["NONE"] = 4] = "NONE";
13
+ return LogLevel2;
14
+ })(LogLevel || {});
15
+
16
+ // src/lib/logger.ts
17
+ var ConsoleLogger = class {
18
+ constructor(level = 3 /* ERROR */) {
19
+ this.level = level;
20
+ }
21
+ /**
22
+ * Update log level
23
+ */
24
+ setLevel(level) {
25
+ this.level = level;
26
+ }
27
+ /**
28
+ * Get current log level
29
+ */
30
+ getLevel() {
31
+ return this.level;
32
+ }
33
+ debug(message, context) {
34
+ if (this.level <= 0 /* DEBUG */) {
35
+ if (context) {
36
+ console.debug("[Amplify SDK]", message, context);
37
+ } else {
38
+ console.debug("[Amplify SDK]", message);
39
+ }
40
+ }
41
+ }
42
+ info(message, context) {
43
+ if (this.level <= 1 /* INFO */) {
44
+ if (context) {
45
+ console.info("[Amplify SDK]", message, context);
46
+ } else {
47
+ console.info("[Amplify SDK]", message);
48
+ }
49
+ }
50
+ }
51
+ warn(message, context) {
52
+ if (this.level <= 2 /* WARN */) {
53
+ if (context) {
54
+ console.warn("[Amplify SDK]", message, context);
55
+ } else {
56
+ console.warn("[Amplify SDK]", message);
57
+ }
58
+ }
59
+ }
60
+ error(message, context) {
61
+ if (this.level <= 3 /* ERROR */) {
62
+ if (context) {
63
+ console.error("[Amplify SDK]", message, context);
64
+ } else {
65
+ console.error("[Amplify SDK]", message);
66
+ }
16
67
  }
17
68
  }
18
69
  };
19
- function isValidYieldType(value) {
20
- return typeof value === "string" && (value === "PRIME" || value === "TBILL" || value === "LENDING");
70
+ var currentLogger = new ConsoleLogger(3 /* ERROR */);
71
+ var defaultLogger = currentLogger;
72
+ function setLogger(logger) {
73
+ currentLogger = logger;
21
74
  }
22
- function isValidAddress(value) {
23
- return typeof value === "string" && /^0x[a-fA-F0-9]{40}$/.test(value);
75
+ function getLogger() {
76
+ return currentLogger;
24
77
  }
25
- function isValidChainId(value) {
26
- return typeof value === "number" && value > 0 && Number.isInteger(value);
78
+ function setLogLevel(level) {
79
+ if (defaultLogger && typeof defaultLogger.setLevel === "function") {
80
+ defaultLogger.setLevel(level);
81
+ }
27
82
  }
28
83
 
84
+ // src/constants/index.ts
85
+ var ATOMIC_QUEUE_CONTRACT_ADDRESS = "0x228c44bb4885c6633f4b6c83f14622f37d5112e5";
86
+ var DEFAULT_DEADLINE = 3;
87
+ var NATIVE_TOKEN_FOR_BRIDGE_FEE = "0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee";
88
+ var CHAINLINK_ADDRESS = "0x5f4eC3Df9cbd43714FE2740f5E3616155c5b8419";
89
+ var DEFAULT_APPROVAL_AMOUNT = BigInt(2) ** BigInt(256) - BigInt(1);
90
+ var API_BASE_URL = "https://api.paxoslabs.com";
91
+
29
92
  // src/lib/vault-cache.ts
30
93
  var DEFAULT_TTL = 6e5;
31
94
  var VaultCache = class {
@@ -193,7 +256,6 @@ var VaultCache = class {
193
256
  };
194
257
 
195
258
  // src/api/amplify-sdk-client.ts
196
- var API_BASE_URL = "https://api.paxoslabs.com";
197
259
  var DEFAULT_TIMEOUT = 1e4;
198
260
  function createTimeoutSignal(timeoutMs) {
199
261
  const controller = new AbortController();
@@ -619,7 +681,8 @@ var sdkConfig = {
619
681
  isInitialized: false,
620
682
  initializedAt: null,
621
683
  isInitializing: false,
622
- initPromise: null
684
+ initPromise: null,
685
+ telemetryEnabled: true
623
686
  };
624
687
  var ERROR_MESSAGES = {
625
688
  INVALID_API_KEY: "Invalid API key format. Expected format: pxl_<type>_<string>",
@@ -652,6 +715,8 @@ function clearConfig() {
652
715
  sdkConfig.initializedAt = null;
653
716
  sdkConfig.isInitializing = false;
654
717
  sdkConfig.initPromise = null;
718
+ sdkConfig.telemetryEnabled = true;
719
+ resetTelemetry();
655
720
  }
656
721
  function getRequestHeaders() {
657
722
  const apiKey = getAPIKey();
@@ -663,7 +728,7 @@ function getRequestHeaders() {
663
728
  }
664
729
  return headers;
665
730
  }
666
- async function initAmplifySDK(apiKey) {
731
+ async function initAmplifySDK(apiKey, options) {
667
732
  validateAPIKey(apiKey);
668
733
  if (sdkConfig.isInitialized && sdkConfig.apiKey === apiKey) {
669
734
  return;
@@ -676,6 +741,13 @@ async function initAmplifySDK(apiKey) {
676
741
  clearConfig();
677
742
  clearCache();
678
743
  }
744
+ if (options?.logger) {
745
+ setLogger(options.logger);
746
+ }
747
+ if (options?.logLevel !== void 0) {
748
+ setLogLevel(options.logLevel);
749
+ }
750
+ sdkConfig.telemetryEnabled = options?.telemetry !== false;
679
751
  sdkConfig.isInitializing = true;
680
752
  sdkConfig.initPromise = (async () => {
681
753
  try {
@@ -684,6 +756,10 @@ async function initAmplifySDK(apiKey) {
684
756
  await cache.refresh();
685
757
  sdkConfig.isInitialized = true;
686
758
  sdkConfig.initializedAt = /* @__PURE__ */ new Date();
759
+ if (sdkConfig.telemetryEnabled) {
760
+ initTelemetry(apiKey).catch(() => {
761
+ });
762
+ }
687
763
  } catch (error) {
688
764
  clearConfig();
689
765
  clearCache();
@@ -701,6 +777,213 @@ async function initAmplifySDK(apiKey) {
701
777
  })();
702
778
  await sdkConfig.initPromise;
703
779
  }
780
+
781
+ // src/lib/telemetry.ts
782
+ var SDK_VERSION = "0.1.1-beta.1";
783
+ var MAX_ERROR_BUFFER_SIZE = 10;
784
+ var CONFIG_FETCH_TIMEOUT = 5e3;
785
+ var telemetryState = {
786
+ enabled: false,
787
+ isInitialized: false,
788
+ isInitializing: false,
789
+ initPromise: null,
790
+ config: null,
791
+ apiKey: null
792
+ };
793
+ var posthogClient = null;
794
+ var errorBuffer = [];
795
+ var bufferFullWarningLogged = false;
796
+ function createTimeoutSignal2(timeoutMs) {
797
+ const controller = new AbortController();
798
+ setTimeout(() => controller.abort(), timeoutMs);
799
+ return controller.signal;
800
+ }
801
+ function hashApiKey(apiKey) {
802
+ const FNV_OFFSET = BigInt("0xcbf29ce484222325");
803
+ const FNV_PRIME = BigInt("0x100000001b3");
804
+ let hash = FNV_OFFSET;
805
+ for (let i = 0; i < apiKey.length; i++) {
806
+ hash ^= BigInt(apiKey.charCodeAt(i));
807
+ hash = hash * FNV_PRIME & BigInt("0xffffffffffffffff");
808
+ }
809
+ return `sdk_${hash.toString(16).slice(0, 12)}`;
810
+ }
811
+ async function fetchTelemetryConfig(apiKey) {
812
+ const endpoint = "/v1/earn-sdk/config";
813
+ const url = `${API_BASE_URL}${endpoint}`;
814
+ try {
815
+ const headers = getRequestHeaders();
816
+ if (!headers["x-api-key"]) {
817
+ headers["x-api-key"] = apiKey;
818
+ }
819
+ const response = await fetch(url, {
820
+ method: "GET",
821
+ headers,
822
+ signal: createTimeoutSignal2(CONFIG_FETCH_TIMEOUT)
823
+ });
824
+ if (!response.ok) {
825
+ return null;
826
+ }
827
+ const data = await response.json();
828
+ if (!data || typeof data.posthogApiKey !== "string") {
829
+ return null;
830
+ }
831
+ return {
832
+ posthogApiKey: data.posthogApiKey,
833
+ posthogHost: data.posthogHost || "https://us.i.posthog.com"
834
+ };
835
+ } catch {
836
+ return null;
837
+ }
838
+ }
839
+ async function flushErrorBuffer() {
840
+ if (errorBuffer.length === 0 || !posthogClient) {
841
+ return;
842
+ }
843
+ for (const bufferedError of errorBuffer) {
844
+ const payload = {
845
+ error_type: bufferedError.name,
846
+ error_message: bufferedError.message,
847
+ error_code: bufferedError.code,
848
+ sdk_version: SDK_VERSION,
849
+ buffered: true,
850
+ ...bufferedError.context
851
+ };
852
+ posthogClient.capture("sdk_error", payload);
853
+ }
854
+ errorBuffer.length = 0;
855
+ }
856
+ async function initTelemetry(apiKey) {
857
+ if (telemetryState.isInitialized) {
858
+ return;
859
+ }
860
+ if (telemetryState.isInitializing && telemetryState.initPromise) {
861
+ await telemetryState.initPromise;
862
+ return;
863
+ }
864
+ telemetryState.isInitializing = true;
865
+ telemetryState.apiKey = apiKey;
866
+ telemetryState.initPromise = (async () => {
867
+ try {
868
+ const config = await fetchTelemetryConfig(apiKey);
869
+ if (!config) {
870
+ telemetryState.enabled = false;
871
+ telemetryState.isInitialized = true;
872
+ return;
873
+ }
874
+ telemetryState.config = config;
875
+ const posthogModule = await import('posthog-js');
876
+ const posthog = posthogModule.default;
877
+ posthog.init(config.posthogApiKey, {
878
+ api_host: config.posthogHost,
879
+ autocapture: false,
880
+ capture_pageview: false,
881
+ capture_pageleave: false,
882
+ disable_session_recording: true,
883
+ persistence: "memory",
884
+ // Don't persist to localStorage
885
+ loaded: (ph) => {
886
+ ph.identify(hashApiKey(apiKey));
887
+ }
888
+ });
889
+ posthogClient = posthog;
890
+ telemetryState.enabled = true;
891
+ telemetryState.isInitialized = true;
892
+ await flushErrorBuffer();
893
+ posthog.capture("sdk_initialized", {
894
+ sdk_version: SDK_VERSION
895
+ });
896
+ } catch (error) {
897
+ const logger = getLogger();
898
+ logger.warn("Telemetry initialization failed", {
899
+ error: error instanceof Error ? error.message : String(error)
900
+ });
901
+ telemetryState.enabled = false;
902
+ telemetryState.isInitialized = true;
903
+ } finally {
904
+ telemetryState.isInitializing = false;
905
+ telemetryState.initPromise = null;
906
+ }
907
+ })();
908
+ await telemetryState.initPromise;
909
+ }
910
+ function trackError(error, context) {
911
+ const logger = getLogger();
912
+ logger.error(error.message, {
913
+ name: error.name,
914
+ stack: error.stack,
915
+ ...context
916
+ });
917
+ const errorDetails = {
918
+ name: error.name,
919
+ message: error.message,
920
+ code: error.code,
921
+ context: context || {},
922
+ timestamp: Date.now()
923
+ };
924
+ if (!posthogClient || !telemetryState.enabled) {
925
+ if (errorBuffer.length < MAX_ERROR_BUFFER_SIZE) {
926
+ errorBuffer.push(errorDetails);
927
+ } else if (!bufferFullWarningLogged) {
928
+ logger.warn(
929
+ `Telemetry error buffer full (${MAX_ERROR_BUFFER_SIZE} errors). Additional errors will not be tracked until telemetry initializes.`
930
+ );
931
+ bufferFullWarningLogged = true;
932
+ }
933
+ return;
934
+ }
935
+ const payload = {
936
+ error_type: error.name,
937
+ error_message: error.message,
938
+ error_code: error.code,
939
+ endpoint: error.endpoint,
940
+ status_code: error.statusCode,
941
+ sdk_version: SDK_VERSION,
942
+ ...context
943
+ };
944
+ posthogClient.capture("sdk_error", payload);
945
+ }
946
+ function resetTelemetry() {
947
+ telemetryState.enabled = false;
948
+ telemetryState.isInitialized = false;
949
+ telemetryState.isInitializing = false;
950
+ telemetryState.initPromise = null;
951
+ telemetryState.config = null;
952
+ telemetryState.apiKey = null;
953
+ posthogClient = null;
954
+ errorBuffer.length = 0;
955
+ bufferFullWarningLogged = false;
956
+ }
957
+
958
+ // src/types/amplify-sdk-api.ts
959
+ var APIError = class _APIError extends Error {
960
+ constructor(message, options) {
961
+ super(message);
962
+ this.name = "APIError";
963
+ this.statusCode = options?.statusCode;
964
+ this.endpoint = options?.endpoint;
965
+ this.cause = options?.cause;
966
+ if (Error.captureStackTrace) {
967
+ Error.captureStackTrace(this, _APIError);
968
+ }
969
+ try {
970
+ trackError(this, {
971
+ endpoint: this.endpoint,
972
+ statusCode: this.statusCode
973
+ });
974
+ } catch {
975
+ }
976
+ }
977
+ };
978
+ function isValidYieldType(value) {
979
+ return typeof value === "string" && (value === "PRIME" || value === "TBILL" || value === "LENDING");
980
+ }
981
+ function isValidAddress(value) {
982
+ return typeof value === "string" && /^0x[a-fA-F0-9]{40}$/.test(value);
983
+ }
984
+ function isValidChainId(value) {
985
+ return typeof value === "number" && value > 0 && Number.isInteger(value);
986
+ }
704
987
  var rari = viem.defineChain({
705
988
  id: 1380012617,
706
989
  name: "Rari Chain",
@@ -803,21 +1086,14 @@ function clearChainsCache() {
803
1086
  chainsCache = null;
804
1087
  }
805
1088
 
806
- // src/constants/index.ts
807
- var ATOMIC_QUEUE_CONTRACT_ADDRESS = "0x228c44bb4885c6633f4b6c83f14622f37d5112e5";
808
- var DEFAULT_DEADLINE = 3;
809
- var NATIVE_TOKEN_FOR_BRIDGE_FEE = "0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee";
810
- var CHAINLINK_ADDRESS = "0x5f4eC3Df9cbd43714FE2740f5E3616155c5b8419";
811
- var DEFAULT_APPROVAL_AMOUNT = BigInt(2) ** BigInt(256) - BigInt(1);
812
- var NUCLEUS_BASE_URL = "https://backend.nucleusearn.io/v1/protocol";
813
-
814
1089
  exports.APIError = APIError;
1090
+ exports.API_BASE_URL = API_BASE_URL;
815
1091
  exports.ATOMIC_QUEUE_CONTRACT_ADDRESS = ATOMIC_QUEUE_CONTRACT_ADDRESS;
816
1092
  exports.CHAINLINK_ADDRESS = CHAINLINK_ADDRESS;
817
1093
  exports.DEFAULT_APPROVAL_AMOUNT = DEFAULT_APPROVAL_AMOUNT;
818
1094
  exports.DEFAULT_DEADLINE = DEFAULT_DEADLINE;
1095
+ exports.LogLevel = LogLevel;
819
1096
  exports.NATIVE_TOKEN_FOR_BRIDGE_FEE = NATIVE_TOKEN_FOR_BRIDGE_FEE;
820
- exports.NUCLEUS_BASE_URL = NUCLEUS_BASE_URL;
821
1097
  exports.clearChainsCache = clearChainsCache;
822
1098
  exports.fetchSupportedAssets = fetchSupportedAssets;
823
1099
  exports.fetchVaults = fetchVaults;
@@ -825,9 +1101,12 @@ exports.findVaultByConfig = findVaultByConfig;
825
1101
  exports.getAssetsFromCache = getAssetsFromCache;
826
1102
  exports.getCache = getCache;
827
1103
  exports.getChainFromConfig = getChainFromConfig;
1104
+ exports.getLogger = getLogger;
828
1105
  exports.getWithdrawSupportedAssets = getWithdrawSupportedAssets;
829
1106
  exports.initAmplifySDK = initAmplifySDK;
830
1107
  exports.initializeCache = initializeCache;
831
1108
  exports.refreshVaultCache = refreshVaultCache;
832
- //# sourceMappingURL=chunk-B2QCI6ET.js.map
833
- //# sourceMappingURL=chunk-B2QCI6ET.js.map
1109
+ exports.setLogLevel = setLogLevel;
1110
+ exports.setLogger = setLogger;
1111
+ //# sourceMappingURL=chunk-UMRTU75Q.js.map
1112
+ //# sourceMappingURL=chunk-UMRTU75Q.js.map