@0xobelisk/sui-client 1.0.6 → 1.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.mjs CHANGED
@@ -1,3 +1,10 @@
1
+ var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
2
+ get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
3
+ }) : x)(function(x) {
4
+ if (typeof require !== "undefined")
5
+ return require.apply(this, arguments);
6
+ throw Error('Dynamic require of "' + x + '" is not supported');
7
+ });
1
8
  var __accessCheck = (obj, member, msg) => {
2
9
  if (!member.has(obj))
3
10
  throw TypeError("Cannot " + msg);
@@ -28,7 +35,6 @@ import { bcs as bcs3, BcsType as BcsType2 } from "@mysten/bcs";
28
35
 
29
36
  // src/dubhe.ts
30
37
  import keccak256 from "keccak256";
31
- import { getFullnodeUrl } from "@mysten/sui/client";
32
38
  import { Transaction as Transaction2 } from "@mysten/sui/transactions";
33
39
  import { fromHex, toHex } from "@mysten/bcs";
34
40
 
@@ -858,7 +864,8 @@ var getDefaultURL = (networkType = "testnet") => {
858
864
  network: "localnet",
859
865
  txExplorer: "https://explorer.polymedia.app/txblock/:txHash?network=local",
860
866
  accountExplorer: "https://explorer.polymedia.app/address/:address?network=local",
861
- explorer: "https://explorer.polymedia.app?network=local"
867
+ explorer: "https://explorer.polymedia.app?network=local",
868
+ indexerUrl: "http://127.0.0.1:3001"
862
869
  };
863
870
  case "devnet":
864
871
  return {
@@ -866,7 +873,8 @@ var getDefaultURL = (networkType = "testnet") => {
866
873
  network: "devnet",
867
874
  txExplorer: "https://suiscan.xyz/devnet/tx/:txHash",
868
875
  accountExplorer: "https://suiscan.xyz/devnet/address/:address",
869
- explorer: "https://suiscan.xyz/devnet"
876
+ explorer: "https://suiscan.xyz/devnet",
877
+ indexerUrl: "http://127.0.0.1:3001"
870
878
  };
871
879
  case "testnet":
872
880
  return {
@@ -875,7 +883,8 @@ var getDefaultURL = (networkType = "testnet") => {
875
883
  network: "testnet",
876
884
  txExplorer: "https://suiscan.xyz/testnet/tx/:txHash",
877
885
  accountExplorer: "https://suiscan.xyz/testnet/address/:address",
878
- explorer: "https://suiscan.xyz/testnet"
886
+ explorer: "https://suiscan.xyz/testnet",
887
+ indexerUrl: "http://127.0.0.1:3001"
879
888
  };
880
889
  case "mainnet":
881
890
  return {
@@ -884,7 +893,8 @@ var getDefaultURL = (networkType = "testnet") => {
884
893
  network: "mainnet",
885
894
  txExplorer: "https://suiscan.xyz/mainnet/tx/:txHash",
886
895
  accountExplorer: "https://suiscan.xyz/mainnet/address/:address",
887
- explorer: "https://suiscan.xyz/mainnet"
896
+ explorer: "https://suiscan.xyz/mainnet",
897
+ indexerUrl: "http://127.0.0.1:3001"
888
898
  };
889
899
  default:
890
900
  return {
@@ -893,7 +903,8 @@ var getDefaultURL = (networkType = "testnet") => {
893
903
  network: "testnet",
894
904
  txExplorer: "https://suiscan.xyz/testnet/tx/:txHash",
895
905
  accountExplorer: "https://suiscan.xyz/testnet/address/:address",
896
- explorer: "https://suiscan.xyz/testnet"
906
+ explorer: "https://suiscan.xyz/testnet",
907
+ indexerUrl: "http://127.0.0.1:3001"
897
908
  };
898
909
  }
899
910
  };
@@ -999,6 +1010,14 @@ function normalizePackageId(input) {
999
1010
  const normalized = withoutPrefix.replace(/^0+/, "");
1000
1011
  return "0x" + normalized;
1001
1012
  }
1013
+ function convertHttpToWebSocket(url) {
1014
+ if (url.startsWith("https://")) {
1015
+ return url.replace("https://", "wss://");
1016
+ } else if (url.startsWith("http://")) {
1017
+ return url.replace("http://", "ws://");
1018
+ }
1019
+ return url;
1020
+ }
1002
1021
 
1003
1022
  // src/dubhe.ts
1004
1023
  import { bcs as bcs2, fromHEX as fromHEX2, toHEX } from "@mysten/bcs";
@@ -1026,6 +1045,283 @@ var ContractDataParsingError = class extends Error {
1026
1045
  }
1027
1046
  };
1028
1047
 
1048
+ // src/libs/suiIndexerClient/index.ts
1049
+ var SuiIndexerClient = class {
1050
+ constructor(http) {
1051
+ this.http = http;
1052
+ }
1053
+ async fetchGraphql(query, variables) {
1054
+ return this.http.fetchGraphql({ query, variables });
1055
+ }
1056
+ async getTransactions(params) {
1057
+ const query = `
1058
+ query GetTransactions($first: Int, $after: String, $last: Int, $before: String, $checkpoint: Int, $orderBy: TransactionOrderBy, $distinct: Boolean) {
1059
+ transactions(first: $first, after: $after, last: $last, before: $before, checkpoint: $checkpoint, orderBy: $orderBy, distinct: $distinct) {
1060
+ edges {
1061
+ cursor
1062
+ node {
1063
+ id
1064
+ checkpoint
1065
+ digest
1066
+ created_at
1067
+ }
1068
+ }
1069
+ pageInfo {
1070
+ hasNextPage
1071
+ hasPreviousPage
1072
+ startCursor
1073
+ endCursor
1074
+ }
1075
+ }
1076
+ }
1077
+ `;
1078
+ const response = await this.fetchGraphql(query, params);
1079
+ return response.transactions;
1080
+ }
1081
+ async getSchemas(params) {
1082
+ const query = `
1083
+ query GetSchemas($first: Int, $after: String, $last: Int, $before: String, $name: String, $key1: String, $key2: String, $orderBy: SchemaOrderBy, $distinct: Boolean) {
1084
+ schemas(first: $first, after: $after, last: $last, before: $before, name: $name, key1: $key1, key2: $key2, orderBy: $orderBy, distinct: $distinct) {
1085
+ edges {
1086
+ cursor
1087
+ node {
1088
+ id
1089
+ name
1090
+ key1
1091
+ key2
1092
+ value
1093
+ last_update_checkpoint
1094
+ last_update_digest
1095
+ is_removed
1096
+ created_at
1097
+ updated_at
1098
+ }
1099
+ }
1100
+ pageInfo {
1101
+ hasNextPage
1102
+ hasPreviousPage
1103
+ startCursor
1104
+ endCursor
1105
+ }
1106
+ }
1107
+ }
1108
+ `;
1109
+ const response = await this.fetchGraphql(query, params);
1110
+ return response.schemas;
1111
+ }
1112
+ async getEvents(params) {
1113
+ const query = `
1114
+ query GetEvents($first: Int, $after: String, $last: Int, $before: String, $name: String, $checkpoint: String, $orderBy: EventOrderBy, $distinct: Boolean) {
1115
+ events(first: $first, after: $after, last: $last, before: $before, name: $name, checkpoint: $checkpoint, orderBy: $orderBy, distinct: $distinct) {
1116
+ edges {
1117
+ cursor
1118
+ node {
1119
+ id
1120
+ checkpoint
1121
+ digest
1122
+ name
1123
+ value
1124
+ created_at
1125
+ }
1126
+ }
1127
+ pageInfo {
1128
+ hasNextPage
1129
+ hasPreviousPage
1130
+ startCursor
1131
+ endCursor
1132
+ }
1133
+ }
1134
+ }
1135
+ `;
1136
+ const response = await this.fetchGraphql(query, params);
1137
+ return response.events;
1138
+ }
1139
+ async getStorage({
1140
+ name,
1141
+ key1,
1142
+ key2,
1143
+ first,
1144
+ after,
1145
+ last,
1146
+ before,
1147
+ orderBy,
1148
+ distinct
1149
+ }) {
1150
+ const schemas = await this.getSchemas({
1151
+ name,
1152
+ key1,
1153
+ key2,
1154
+ first,
1155
+ after,
1156
+ last,
1157
+ before,
1158
+ orderBy,
1159
+ distinct
1160
+ });
1161
+ return schemas;
1162
+ }
1163
+ async subscribe(names, handleData) {
1164
+ return this.http.subscribe(names, handleData);
1165
+ }
1166
+ };
1167
+
1168
+ // src/libs/http/errors.ts
1169
+ var BaseError = class extends Error {
1170
+ constructor(message, code, type) {
1171
+ super(message);
1172
+ this.code = code;
1173
+ this.type = type;
1174
+ this.name = this.constructor.name;
1175
+ }
1176
+ };
1177
+ var HttpError = class extends BaseError {
1178
+ constructor(message, code) {
1179
+ super(message, code, "ERROR_HTTP");
1180
+ }
1181
+ };
1182
+ var GraphQLError = class extends BaseError {
1183
+ constructor(message) {
1184
+ super(message, 400, "ERROR_GRAPHQL");
1185
+ }
1186
+ };
1187
+ var ParseError = class extends BaseError {
1188
+ constructor(message) {
1189
+ super(message, 500, "ERROR_PARSE");
1190
+ }
1191
+ };
1192
+
1193
+ // src/libs/http/ws-adapter.ts
1194
+ function createWebSocketClient(url) {
1195
+ if (typeof window !== "undefined") {
1196
+ return new WebSocket(url);
1197
+ } else {
1198
+ try {
1199
+ __require.resolve("ws");
1200
+ const WebSocket2 = __require("ws");
1201
+ return new WebSocket2(url);
1202
+ } catch (e) {
1203
+ console.error("Failed to load WebSocket implementation:", e);
1204
+ throw new Error(
1205
+ 'WebSocket implementation not available. Please install the "ws" package.'
1206
+ );
1207
+ }
1208
+ }
1209
+ }
1210
+
1211
+ // src/libs/http/http.ts
1212
+ var Http = class {
1213
+ constructor(apiEndpoint, wsEndpoint, customFetch, defaultOptions) {
1214
+ this.customFetch = customFetch;
1215
+ this.apiEndpoint = apiEndpoint;
1216
+ this.graphqlEndpoint = apiEndpoint + "/graphql";
1217
+ this.wsEndpoint = wsEndpoint;
1218
+ this.defaultOptions = defaultOptions;
1219
+ }
1220
+ getFetch() {
1221
+ return this.customFetch || fetch;
1222
+ }
1223
+ async fetch(url) {
1224
+ try {
1225
+ const fetchFn = this.getFetch();
1226
+ const response = await fetchFn(url, {
1227
+ ...this.defaultOptions
1228
+ });
1229
+ if (!response.ok) {
1230
+ throw new HttpError(
1231
+ `HTTP error! status: ${response.status}`,
1232
+ response.status
1233
+ );
1234
+ }
1235
+ return response;
1236
+ } catch (error) {
1237
+ if (error instanceof HttpError) {
1238
+ throw error;
1239
+ }
1240
+ throw new HttpError(`Failed to fetch: ${error.message}`, 500);
1241
+ }
1242
+ }
1243
+ async fetchGraphql({
1244
+ query,
1245
+ variables
1246
+ }) {
1247
+ try {
1248
+ const isFirstPage = variables?.after === "first";
1249
+ const fetchFn = this.getFetch();
1250
+ const response = await fetchFn(this.graphqlEndpoint, {
1251
+ method: "POST",
1252
+ headers: {
1253
+ "Content-Type": "application/json",
1254
+ Accept: "application/json"
1255
+ },
1256
+ body: JSON.stringify({
1257
+ query,
1258
+ variables: {
1259
+ ...variables,
1260
+ after: isFirstPage ? void 0 : variables?.after
1261
+ }
1262
+ }),
1263
+ ...this.defaultOptions
1264
+ });
1265
+ if (!response.ok) {
1266
+ const errorData = await response.json();
1267
+ if (errorData.errors?.[0]?.message?.includes("Syntax Error")) {
1268
+ throw new GraphQLError(
1269
+ `GraphQL syntax error: ${errorData.errors[0].message}`
1270
+ );
1271
+ }
1272
+ if (errorData.errors?.length > 0) {
1273
+ throw new GraphQLError(
1274
+ errorData.errors[0].message || "Unknown GraphQL error"
1275
+ );
1276
+ }
1277
+ throw new HttpError(
1278
+ `HTTP error: ${JSON.stringify(errorData)}`,
1279
+ response.status
1280
+ );
1281
+ }
1282
+ const data = await response.json();
1283
+ if (data.errors) {
1284
+ throw new GraphQLError(
1285
+ data.errors[0]?.message || "GraphQL query failed"
1286
+ );
1287
+ }
1288
+ return data.data;
1289
+ } catch (error) {
1290
+ if (error instanceof BaseError) {
1291
+ throw error;
1292
+ }
1293
+ if (error instanceof SyntaxError) {
1294
+ throw new ParseError("Failed to parse JSON response");
1295
+ }
1296
+ throw new HttpError(
1297
+ `Failed to fetch GraphQL: ${error.message}`,
1298
+ 500
1299
+ );
1300
+ }
1301
+ }
1302
+ async subscribe(names, handleData) {
1303
+ const ws = createWebSocketClient(this.wsEndpoint);
1304
+ ws.onopen = () => {
1305
+ console.log("Connected to the WebSocket server");
1306
+ const subscribeMessage = JSON.stringify({
1307
+ type: "subscribe",
1308
+ names
1309
+ });
1310
+ ws.send(subscribeMessage);
1311
+ };
1312
+ ws.onmessage = (event) => {
1313
+ handleData(JSON.parse(event.data.toString()));
1314
+ };
1315
+ ws.onclose = () => {
1316
+ console.log("Disconnected from the WebSocket server");
1317
+ };
1318
+ ws.onerror = (error) => {
1319
+ console.error(`WebSocket error:`, error);
1320
+ };
1321
+ return ws;
1322
+ }
1323
+ };
1324
+
1029
1325
  // src/dubhe.ts
1030
1326
  function isUndefined(value) {
1031
1327
  return value === void 0;
@@ -1081,7 +1377,11 @@ var Dubhe = class {
1081
1377
  networkType,
1082
1378
  fullnodeUrls,
1083
1379
  packageId,
1084
- metadata
1380
+ metadata,
1381
+ customFetch,
1382
+ defaultOptions,
1383
+ indexerUrl,
1384
+ indexerWsUrl
1085
1385
  } = {}) {
1086
1386
  __privateAdd(this, _processKeyParameter);
1087
1387
  __privateAdd(this, _query, {});
@@ -1563,9 +1863,15 @@ var Dubhe = class {
1563
1863
  loopFlag
1564
1864
  };
1565
1865
  });
1866
+ networkType = networkType ?? "mainnet";
1867
+ const defaultParams = getDefaultURL(networkType);
1566
1868
  this.accountManager = new SuiAccountManager({ mnemonics, secretKey });
1567
- fullnodeUrls = fullnodeUrls || [getFullnodeUrl(networkType ?? "mainnet")];
1869
+ fullnodeUrls = fullnodeUrls || [defaultParams.fullNode];
1568
1870
  this.suiInteractor = new SuiInteractor(fullnodeUrls, networkType);
1871
+ indexerUrl = indexerUrl || defaultParams.indexerUrl;
1872
+ indexerWsUrl = indexerWsUrl || convertHttpToWebSocket(indexerUrl);
1873
+ this.http = new Http(indexerUrl, indexerWsUrl, customFetch, defaultOptions);
1874
+ this.suiIndexerClient = new SuiIndexerClient(this.http);
1569
1875
  this.packageId = packageId ? normalizePackageId(packageId) : void 0;
1570
1876
  if (metadata !== void 0) {
1571
1877
  this.metadata = metadata;
@@ -1843,6 +2149,32 @@ var Dubhe = class {
1843
2149
  params: processedParams
1844
2150
  });
1845
2151
  }
2152
+ async getStorage({
2153
+ name,
2154
+ key1,
2155
+ key2,
2156
+ first,
2157
+ after,
2158
+ last,
2159
+ before,
2160
+ orderBy,
2161
+ distinct
2162
+ }) {
2163
+ return await this.suiIndexerClient.getStorage({
2164
+ name,
2165
+ key1,
2166
+ key2,
2167
+ first,
2168
+ after,
2169
+ last,
2170
+ before,
2171
+ orderBy,
2172
+ distinct
2173
+ });
2174
+ }
2175
+ async subscribe(names, handleData) {
2176
+ return this.suiIndexerClient.subscribe(names, handleData);
2177
+ }
1846
2178
  /**
1847
2179
  * else:
1848
2180
  * it will generate signer from the mnemonic with the given derivePathParams.
@@ -1916,6 +2248,9 @@ var Dubhe = class {
1916
2248
  client() {
1917
2249
  return this.suiInteractor.currentClient;
1918
2250
  }
2251
+ indexerClient() {
2252
+ return this.suiIndexerClient;
2253
+ }
1919
2254
  async getObject(objectId) {
1920
2255
  return this.suiInteractor.getObject(objectId);
1921
2256
  }
@@ -2208,9 +2543,9 @@ var MultiSigClient = class _MultiSigClient {
2208
2543
  };
2209
2544
 
2210
2545
  // src/metadata/index.ts
2211
- import { getFullnodeUrl as getFullnodeUrl2 } from "@mysten/sui/client";
2546
+ import { getFullnodeUrl } from "@mysten/sui/client";
2212
2547
  async function loadMetadata(networkType, packageId, fullnodeUrls) {
2213
- fullnodeUrls = fullnodeUrls || [getFullnodeUrl2(networkType)];
2548
+ fullnodeUrls = fullnodeUrls || [getFullnodeUrl(networkType)];
2214
2549
  const suiInteractor = new SuiInteractor(fullnodeUrls);
2215
2550
  if (packageId !== void 0) {
2216
2551
  const jsonData = await suiInteractor.getNormalizedMoveModulesByPackage(