@0xobelisk/sui-client 0.5.19 → 0.5.21

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
@@ -974,7 +974,7 @@ function createTx(meta, fn) {
974
974
  }
975
975
  );
976
976
  }
977
- var _query, _tx, _struct, _exec, _read;
977
+ var _query, _tx, _object, _exec, _read, _bcs;
978
978
  var Obelisk = class {
979
979
  /**
980
980
  * Support the following ways to init the ObeliskClient:
@@ -998,7 +998,49 @@ var Obelisk = class {
998
998
  } = {}) {
999
999
  __privateAdd(this, _query, {});
1000
1000
  __privateAdd(this, _tx, {});
1001
- __privateAdd(this, _struct, {});
1001
+ __privateAdd(this, _object, {
1002
+ address: import_bcs2.bcs.bytes(32).transform({
1003
+ // To change the input type, you need to provide a type definition for the input
1004
+ input: (val) => (0, import_bcs2.fromHEX)(val),
1005
+ output: (val) => (0, import_bcs2.toHEX)(val)
1006
+ }),
1007
+ u8: import_bcs2.bcs.u8(),
1008
+ u16: import_bcs2.bcs.u16(),
1009
+ u32: import_bcs2.bcs.u32(),
1010
+ u64: import_bcs2.bcs.u64(),
1011
+ u128: import_bcs2.bcs.u128(),
1012
+ u256: import_bcs2.bcs.u256(),
1013
+ bool: import_bcs2.bcs.bool(),
1014
+ "0x1::ascii::String": import_bcs2.bcs.string(),
1015
+ "0x1::option::Option<address>": import_bcs2.bcs.option(
1016
+ import_bcs2.bcs.bytes(32).transform({
1017
+ // To change the input type, you need to provide a type definition for the input
1018
+ input: (val) => (0, import_bcs2.fromHEX)(val),
1019
+ output: (val) => (0, import_bcs2.toHEX)(val)
1020
+ })
1021
+ ),
1022
+ "0x1::option::Option<u8>": import_bcs2.bcs.option(import_bcs2.bcs.u8()),
1023
+ "0x1::option::Option<u16>": import_bcs2.bcs.option(import_bcs2.bcs.u16()),
1024
+ "0x1::option::Option<u32>": import_bcs2.bcs.option(import_bcs2.bcs.u32()),
1025
+ "0x1::option::Option<u64>": import_bcs2.bcs.option(import_bcs2.bcs.u64()),
1026
+ "0x1::option::Option<u128>": import_bcs2.bcs.option(import_bcs2.bcs.u128()),
1027
+ "0x1::option::Option<u256>": import_bcs2.bcs.option(import_bcs2.bcs.u256()),
1028
+ "0x1::option::Option<bool>": import_bcs2.bcs.option(import_bcs2.bcs.bool()),
1029
+ "vector<address>": import_bcs2.bcs.vector(
1030
+ import_bcs2.bcs.bytes(32).transform({
1031
+ // To change the input type, you need to provide a type definition for the input
1032
+ input: (val) => (0, import_bcs2.fromHEX)(val),
1033
+ output: (val) => (0, import_bcs2.toHEX)(val)
1034
+ })
1035
+ ),
1036
+ "vector<u8>": import_bcs2.bcs.vector(import_bcs2.bcs.u8()),
1037
+ "vector<u16>": import_bcs2.bcs.vector(import_bcs2.bcs.u16()),
1038
+ "vector<u32>": import_bcs2.bcs.vector(import_bcs2.bcs.u32()),
1039
+ "vector<u64>": import_bcs2.bcs.vector(import_bcs2.bcs.u64()),
1040
+ "vector<u128>": import_bcs2.bcs.vector(import_bcs2.bcs.u128()),
1041
+ "vector<u256>": import_bcs2.bcs.vector(import_bcs2.bcs.u256()),
1042
+ "vector<bool>": import_bcs2.bcs.vector(import_bcs2.bcs.bool())
1043
+ });
1002
1044
  __privateAdd(this, _exec, async (meta, tx, params, typeArguments, isRaw) => {
1003
1045
  if (isRaw === true) {
1004
1046
  return tx.moveCall({
@@ -1029,43 +1071,224 @@ var Obelisk = class {
1029
1071
  });
1030
1072
  return await this.inspectTxn(tx);
1031
1073
  });
1074
+ __privateAdd(this, _bcs, (bcsmeta) => {
1075
+ let loopFlag = false;
1076
+ const bcsJson = {};
1077
+ Object.entries(bcsmeta.objectType.fields).forEach(([index, type]) => {
1078
+ const objName = type.name;
1079
+ const objType = type.type;
1080
+ switch (typeof objType) {
1081
+ case "object":
1082
+ for (const [key, value] of Object.entries(objType)) {
1083
+ switch (key) {
1084
+ case "Struct":
1085
+ const structType = value;
1086
+ if (structType.address === "0x1" && structType.module === "ascii" && structType.name === "String") {
1087
+ bcsJson[objName] = import_bcs2.bcs.string();
1088
+ return;
1089
+ } else if (structType.address === "0x2" && structType.module === "object" && structType.name === "UID") {
1090
+ bcsJson[objName] = import_bcs2.bcs.fixedArray(32, import_bcs2.bcs.u8()).transform({
1091
+ input: (id) => (0, import_bcs2.fromHEX)(id),
1092
+ output: (id) => (0, import_bcs2.toHEX)(Uint8Array.from(id))
1093
+ });
1094
+ return;
1095
+ } else if (structType.address === "0x2" && structType.module === "object" && structType.name === "ID") {
1096
+ bcsJson[objName] = import_bcs2.bcs.fixedArray(32, import_bcs2.bcs.u8()).transform({
1097
+ input: (id) => (0, import_bcs2.fromHEX)(id),
1098
+ output: (id) => (0, import_bcs2.toHEX)(Uint8Array.from(id))
1099
+ });
1100
+ return;
1101
+ } else if (structType.address === "0x2" && structType.module === "bag" && structType.name === "Bag") {
1102
+ bcsJson[objName] = import_bcs2.bcs.fixedArray(32, import_bcs2.bcs.u8()).transform({
1103
+ input: (id) => (0, import_bcs2.fromHEX)(id),
1104
+ output: (id) => (0, import_bcs2.toHEX)(Uint8Array.from(id))
1105
+ });
1106
+ return;
1107
+ } else if (structType.address === "0x1" && structType.module === "option" && structType.name === "Option") {
1108
+ switch (structType.typeArguments[0]) {
1109
+ case "U8":
1110
+ bcsJson[objName] = import_bcs2.bcs.option(import_bcs2.bcs.u8());
1111
+ return;
1112
+ case "U16":
1113
+ bcsJson[objName] = import_bcs2.bcs.option(import_bcs2.bcs.u16());
1114
+ return;
1115
+ case "U32":
1116
+ bcsJson[objName] = import_bcs2.bcs.option(import_bcs2.bcs.u32());
1117
+ return;
1118
+ case "U64":
1119
+ bcsJson[objName] = import_bcs2.bcs.option(import_bcs2.bcs.u64());
1120
+ return;
1121
+ case "U128":
1122
+ bcsJson[objName] = import_bcs2.bcs.option(import_bcs2.bcs.u128());
1123
+ return;
1124
+ case "U256":
1125
+ bcsJson[objName] = import_bcs2.bcs.option(import_bcs2.bcs.u256());
1126
+ return;
1127
+ case "Bool":
1128
+ bcsJson[objName] = import_bcs2.bcs.option(import_bcs2.bcs.bool());
1129
+ return;
1130
+ case "Address":
1131
+ bcsJson[objName] = import_bcs2.bcs.option(
1132
+ import_bcs2.bcs.bytes(32).transform({
1133
+ // To change the input type, you need to provide a type definition for the input
1134
+ input: (val) => (0, import_bcs2.fromHEX)(val),
1135
+ output: (val) => (0, import_bcs2.toHEX)(val)
1136
+ })
1137
+ );
1138
+ return;
1139
+ default:
1140
+ }
1141
+ } else {
1142
+ if (this.object[`${structType.address}::${structType.module}::${structType.name}`] === void 0) {
1143
+ loopFlag = true;
1144
+ } else {
1145
+ bcsJson[objName] = this.object[`${structType.address}::${structType.module}::${structType.name}`];
1146
+ return;
1147
+ }
1148
+ }
1149
+ return;
1150
+ case "Vector":
1151
+ switch (value) {
1152
+ case "U8":
1153
+ bcsJson[objName] = import_bcs2.bcs.vector(import_bcs2.bcs.u8());
1154
+ return;
1155
+ case "U16":
1156
+ bcsJson[objName] = import_bcs2.bcs.vector(import_bcs2.bcs.u16());
1157
+ return;
1158
+ case "U32":
1159
+ bcsJson[objName] = import_bcs2.bcs.vector(import_bcs2.bcs.u32());
1160
+ return;
1161
+ case "U64":
1162
+ bcsJson[objName] = import_bcs2.bcs.vector(import_bcs2.bcs.u64());
1163
+ return;
1164
+ case "U128":
1165
+ bcsJson[objName] = import_bcs2.bcs.vector(import_bcs2.bcs.u128());
1166
+ return;
1167
+ case "U256":
1168
+ bcsJson[objName] = import_bcs2.bcs.vector(import_bcs2.bcs.u256());
1169
+ return;
1170
+ case "Bool":
1171
+ bcsJson[objName] = import_bcs2.bcs.vector(import_bcs2.bcs.bool());
1172
+ return;
1173
+ case "Address":
1174
+ bcsJson[objName] = import_bcs2.bcs.vector(
1175
+ import_bcs2.bcs.bytes(32).transform({
1176
+ // To change the input type, you need to provide a type definition for the input
1177
+ input: (val) => (0, import_bcs2.fromHEX)(val),
1178
+ output: (val) => (0, import_bcs2.toHEX)(val)
1179
+ })
1180
+ );
1181
+ return;
1182
+ default:
1183
+ }
1184
+ case "TypeParameter":
1185
+ bcsJson[objName] = import_bcs2.bcs.u128();
1186
+ return;
1187
+ default:
1188
+ throw new Error("Unsupported type");
1189
+ }
1190
+ }
1191
+ return;
1192
+ case "string":
1193
+ switch (objType) {
1194
+ case "U8":
1195
+ bcsJson[objName] = import_bcs2.bcs.u8();
1196
+ return;
1197
+ case "U16":
1198
+ bcsJson[objName] = import_bcs2.bcs.u16();
1199
+ return;
1200
+ case "U32":
1201
+ bcsJson[objName] = import_bcs2.bcs.u32();
1202
+ return;
1203
+ case "U64":
1204
+ bcsJson[objName] = import_bcs2.bcs.u64();
1205
+ return;
1206
+ case "U128":
1207
+ bcsJson[objName] = import_bcs2.bcs.u128();
1208
+ return;
1209
+ case "U256":
1210
+ bcsJson[objName] = import_bcs2.bcs.u256();
1211
+ return;
1212
+ case "Bool":
1213
+ bcsJson[objName] = import_bcs2.bcs.bool();
1214
+ return;
1215
+ case "Address":
1216
+ bcsJson[objName] = import_bcs2.bcs.bytes(32).transform({
1217
+ // To change the input type, you need to provide a type definition for the input
1218
+ input: (val) => (0, import_bcs2.fromHEX)(val),
1219
+ output: (val) => (0, import_bcs2.toHEX)(val)
1220
+ });
1221
+ return;
1222
+ default:
1223
+ return;
1224
+ }
1225
+ default:
1226
+ throw new Error("Unsupported type");
1227
+ }
1228
+ });
1229
+ return {
1230
+ bcs: import_bcs2.bcs.struct(bcsmeta.objectName, bcsJson),
1231
+ loopFlag
1232
+ };
1233
+ });
1032
1234
  this.accountManager = new SuiAccountManager({ mnemonics, secretKey });
1033
1235
  fullnodeUrls = fullnodeUrls || [(0, import_client2.getFullnodeUrl)(networkType ?? "mainnet")];
1034
1236
  this.suiInteractor = new SuiInteractor(fullnodeUrls, networkType);
1035
1237
  this.packageId = packageId;
1036
1238
  if (metadata !== void 0) {
1037
1239
  this.metadata = metadata;
1038
- Object.values(metadata).forEach(
1039
- (moudlevalue) => {
1040
- const data = moudlevalue;
1041
- const moduleName = data.name;
1042
- Object.entries(data.exposedFunctions).forEach(
1043
- ([funcName, funcvalue]) => {
1044
- const meta = funcvalue;
1045
- meta.moduleName = moduleName;
1046
- meta.funcName = funcName;
1047
- if (isUndefined(__privateGet(this, _query)[moduleName])) {
1048
- __privateGet(this, _query)[moduleName] = {};
1049
- }
1050
- if (isUndefined(__privateGet(this, _query)[moduleName][funcName])) {
1051
- __privateGet(this, _query)[moduleName][funcName] = createQuery(
1052
- meta,
1053
- (tx, p, typeArguments, isRaw) => __privateGet(this, _read).call(this, meta, tx, p, typeArguments, isRaw)
1054
- );
1055
- }
1056
- if (isUndefined(__privateGet(this, _tx)[moduleName])) {
1057
- __privateGet(this, _tx)[moduleName] = {};
1240
+ const maxLoopNum = 5;
1241
+ let loopNum = 0;
1242
+ let stillNeedFormat = true;
1243
+ while (stillNeedFormat === true && loopNum <= maxLoopNum) {
1244
+ let loopFlag = false;
1245
+ Object.values(metadata).forEach(
1246
+ (moudlevalue) => {
1247
+ const data = moudlevalue;
1248
+ const moduleName = data.name;
1249
+ const objMoudleId = `${packageId}::${moduleName}`;
1250
+ Object.entries(data.structs).forEach(([objectName, objectType]) => {
1251
+ const objId = `${objMoudleId}::${objectName}`;
1252
+ const bcsmeta = {
1253
+ objectName,
1254
+ objectType
1255
+ };
1256
+ let bcsObj = __privateGet(this, _bcs).call(this, bcsmeta);
1257
+ if (bcsObj.loopFlag === true) {
1258
+ loopFlag = bcsObj.loopFlag;
1058
1259
  }
1059
- if (isUndefined(__privateGet(this, _tx)[moduleName][funcName])) {
1060
- __privateGet(this, _tx)[moduleName][funcName] = createTx(
1061
- meta,
1062
- (tx, p, typeArguments, isRaw) => __privateGet(this, _exec).call(this, meta, tx, p, typeArguments, isRaw)
1063
- );
1260
+ __privateGet(this, _object)[objId] = bcsObj.bcs;
1261
+ });
1262
+ Object.entries(data.exposedFunctions).forEach(
1263
+ ([funcName, funcvalue]) => {
1264
+ const meta = funcvalue;
1265
+ meta.moduleName = moduleName;
1266
+ meta.funcName = funcName;
1267
+ if (isUndefined(__privateGet(this, _query)[moduleName])) {
1268
+ __privateGet(this, _query)[moduleName] = {};
1269
+ }
1270
+ if (isUndefined(__privateGet(this, _query)[moduleName][funcName])) {
1271
+ __privateGet(this, _query)[moduleName][funcName] = createQuery(
1272
+ meta,
1273
+ (tx, p, typeArguments, isRaw) => __privateGet(this, _read).call(this, meta, tx, p, typeArguments, isRaw)
1274
+ );
1275
+ }
1276
+ if (isUndefined(__privateGet(this, _tx)[moduleName])) {
1277
+ __privateGet(this, _tx)[moduleName] = {};
1278
+ }
1279
+ if (isUndefined(__privateGet(this, _tx)[moduleName][funcName])) {
1280
+ __privateGet(this, _tx)[moduleName][funcName] = createTx(
1281
+ meta,
1282
+ (tx, p, typeArguments, isRaw) => __privateGet(this, _exec).call(this, meta, tx, p, typeArguments, isRaw)
1283
+ );
1284
+ }
1064
1285
  }
1065
- }
1066
- );
1067
- }
1068
- );
1286
+ );
1287
+ }
1288
+ );
1289
+ stillNeedFormat = loopFlag;
1290
+ loopNum++;
1291
+ }
1069
1292
  }
1070
1293
  this.contractFactory = new SuiContractFactory({
1071
1294
  packageId,
@@ -1078,8 +1301,23 @@ var Obelisk = class {
1078
1301
  get tx() {
1079
1302
  return __privateGet(this, _tx);
1080
1303
  }
1081
- get struct() {
1082
- return __privateGet(this, _struct);
1304
+ get object() {
1305
+ return __privateGet(this, _object);
1306
+ }
1307
+ view(dryResult) {
1308
+ let returnValues = [];
1309
+ if (dryResult.effects.status.status === "success") {
1310
+ const resultList = dryResult.results[0].returnValues;
1311
+ for (const res of resultList) {
1312
+ let baseValue = res[0];
1313
+ let baseType = res[1];
1314
+ const value = Uint8Array.from(baseValue);
1315
+ returnValues.push(this.object[baseType].parse(value));
1316
+ }
1317
+ return returnValues;
1318
+ } else {
1319
+ return void 0;
1320
+ }
1083
1321
  }
1084
1322
  /**
1085
1323
  * if derivePathParams is not provided or mnemonics is empty, it will return the keypair.
@@ -1297,50 +1535,11 @@ var Obelisk = class {
1297
1535
  if (entityId !== void 0) {
1298
1536
  params.push(tx.pure.address(entityId));
1299
1537
  }
1300
- const getResult = await this.query[schemaModuleName].get(
1538
+ const dryResult = await this.query[schemaModuleName].get(
1301
1539
  tx,
1302
1540
  params
1303
1541
  );
1304
- let returnValues = [];
1305
- if (getResult.effects.status.status === "success") {
1306
- const resultList = getResult.results[0].returnValues;
1307
- for (const res of resultList) {
1308
- let baseValue = res[0];
1309
- let baseType = res[1];
1310
- const value = Uint8Array.from(baseValue);
1311
- if (baseType === "address") {
1312
- const Address = import_bcs2.bcs.bytes(32).transform({
1313
- // To change the input type, you need to provide a type definition for the input
1314
- input: (val) => (0, import_bcs2.fromHEX)(val),
1315
- output: (val) => (0, import_bcs2.toHEX)(val)
1316
- });
1317
- returnValues.push(Address.parse(value));
1318
- } else if (baseType === "u8") {
1319
- returnValues.push(import_bcs2.bcs.u8().parse(value));
1320
- } else if (baseType === "u16") {
1321
- returnValues.push(import_bcs2.bcs.u16().parse(value));
1322
- } else if (baseType === "u32") {
1323
- returnValues.push(import_bcs2.bcs.u32().parse(value));
1324
- } else if (baseType === "u64") {
1325
- returnValues.push(import_bcs2.bcs.u64().parse(value));
1326
- } else if (baseType === "u128") {
1327
- returnValues.push(import_bcs2.bcs.u128().parse(value));
1328
- } else if (baseType === "u256") {
1329
- returnValues.push(import_bcs2.bcs.u256().parse(value));
1330
- } else if (baseType === "bool") {
1331
- returnValues.push(import_bcs2.bcs.bool().parse(value));
1332
- } else if (baseType === "0x1::ascii::String") {
1333
- returnValues.push(import_bcs2.bcs.string().parse(value));
1334
- } else if (baseType === "vector<u8>") {
1335
- returnValues.push(import_bcs2.bcs.vector(import_bcs2.bcs.u8()).parse(value));
1336
- } else if (baseType === "0x1::option::Option<u8>") {
1337
- returnValues.push(import_bcs2.bcs.option(import_bcs2.bcs.u8()).parse(value));
1338
- }
1339
- }
1340
- return returnValues;
1341
- } else {
1342
- return void 0;
1343
- }
1542
+ return this.view(dryResult);
1344
1543
  }
1345
1544
  async containEntity(worldId, schemaName, entityId) {
1346
1545
  const schemaModuleName = `${schemaName}_schema`;
@@ -1349,18 +1548,11 @@ var Obelisk = class {
1349
1548
  if (entityId !== void 0) {
1350
1549
  params.push(tx.pure.address(entityId));
1351
1550
  }
1352
- const getResult = await this.query[schemaModuleName].contains(
1551
+ const dryResult = await this.query[schemaModuleName].contains(
1353
1552
  tx,
1354
1553
  params
1355
1554
  );
1356
- if (getResult.effects.status.status === "success") {
1357
- const res = getResult.results[0].returnValues[0];
1358
- let baseValue = res[0];
1359
- const value = Uint8Array.from(baseValue);
1360
- return import_bcs2.bcs.bool().parse(value);
1361
- } else {
1362
- return void 0;
1363
- }
1555
+ return this.view(dryResult);
1364
1556
  }
1365
1557
  // async getEntities(
1366
1558
  // worldId: string,
@@ -1457,54 +1649,13 @@ var Obelisk = class {
1457
1649
  // const u8Value = Uint8Array.from(value);
1458
1650
  // return bcs.de(type, u8Value);
1459
1651
  // }
1460
- async autoFormatDryValue(value) {
1461
- let returnValues = [];
1462
- if (value.effects.status.status === "success") {
1463
- const resultList = value.results[0].returnValues;
1464
- for (const res of resultList) {
1465
- let baseValue = res[0];
1466
- let baseType = res[1];
1467
- const value2 = Uint8Array.from(baseValue);
1468
- if (baseType === "address") {
1469
- const Address = import_bcs2.bcs.bytes(32).transform({
1470
- // To change the input type, you need to provide a type definition for the input
1471
- input: (val) => (0, import_bcs2.fromHEX)(val),
1472
- output: (val) => (0, import_bcs2.toHEX)(val)
1473
- });
1474
- returnValues.push(Address.parse(value2));
1475
- } else if (baseType === "u8") {
1476
- returnValues.push(import_bcs2.bcs.u8().parse(value2));
1477
- } else if (baseType === "u16") {
1478
- returnValues.push(import_bcs2.bcs.u16().parse(value2));
1479
- } else if (baseType === "u32") {
1480
- returnValues.push(import_bcs2.bcs.u32().parse(value2));
1481
- } else if (baseType === "u64") {
1482
- returnValues.push(import_bcs2.bcs.u64().parse(value2));
1483
- } else if (baseType === "u128") {
1484
- returnValues.push(import_bcs2.bcs.u128().parse(value2));
1485
- } else if (baseType === "u256") {
1486
- returnValues.push(import_bcs2.bcs.u256().parse(value2));
1487
- } else if (baseType === "bool") {
1488
- returnValues.push(import_bcs2.bcs.bool().parse(value2));
1489
- } else if (baseType === "0x1::ascii::String") {
1490
- returnValues.push(import_bcs2.bcs.string().parse(value2));
1491
- } else if (baseType === "vector<u8>") {
1492
- returnValues.push(import_bcs2.bcs.vector(import_bcs2.bcs.u8()).parse(value2));
1493
- } else if (baseType === "0x1::option::Option<u8>") {
1494
- returnValues.push(import_bcs2.bcs.option(import_bcs2.bcs.u8()).parse(value2));
1495
- }
1496
- }
1497
- return returnValues;
1498
- } else {
1499
- return void 0;
1500
- }
1501
- }
1502
1652
  };
1503
1653
  _query = new WeakMap();
1504
1654
  _tx = new WeakMap();
1505
- _struct = new WeakMap();
1655
+ _object = new WeakMap();
1506
1656
  _exec = new WeakMap();
1507
1657
  _read = new WeakMap();
1658
+ _bcs = new WeakMap();
1508
1659
 
1509
1660
  // src/libs/multiSig/client.ts
1510
1661
  var import_multisig = require("@mysten/sui/multisig");