@suigar/sdk 2.0.0-beta.10 → 2.0.0-beta.11

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
@@ -1,7 +1,8 @@
1
- import { MoveStruct, Float, parseCoinType, toBigInt, toU8, DEFAULT_GAS_BUDGET_MIST, normalizeMoveArguments, DEFAULT_LIMBO_MULTIPLIER_SCALE, DEFAULT_RANGE_SCALE } from './chunk-PPUDLRHA.js';
2
- import { toBase64, normalizeStructTag, normalizeSuiAddress, fromHex } from '@mysten/sui/utils';
1
+ import { MoveStruct, Float, parseCoinType, toBigInt, toU8, DEFAULT_GAS_BUDGET_MIST, normalizeMoveArguments, DEFAULT_LIMBO_MULTIPLIER_SCALE, DEFAULT_RANGE_SCALE } from './chunk-TBJ5TYYE.js';
2
+ import { toBase64, normalizeStructTag, parseStructTag, normalizeSuiAddress, fromHex } from '@mysten/sui/utils';
3
3
  import { bcs } from '@mysten/sui/bcs';
4
4
  import { Transaction } from '@mysten/sui/transactions';
5
+ import { ClientCache } from '@mysten/sui/client';
5
6
 
6
7
  var $moduleName = "0x0000000000000000000000000000000000000000000000000000000000000001::type_name";
7
8
  var TypeName = new MoveStruct({
@@ -79,6 +80,12 @@ function VecMap2(...typeParameters) {
79
80
 
80
81
  // src/contracts/pvp-coinflip/pvp_coinflip.ts
81
82
  var $moduleName7 = "0xb43cf6583c0c15315c7e66f173af4be79ac40c38aad1fd92ec08638ab2026202::pvp_coinflip";
83
+ var PvpCoinflipSettingsKey = new MoveStruct({
84
+ name: `${$moduleName7}::PvpCoinflipSettingsKey`,
85
+ fields: {
86
+ dummy_field: bcs.bool()
87
+ }
88
+ });
82
89
  var Game = new MoveStruct({
83
90
  name: `${$moduleName7}::Game<phantom T0>`,
84
91
  fields: {
@@ -135,6 +142,14 @@ var GameCancelledEvent = new MoveStruct({
135
142
  coin_type: TypeName2
136
143
  }
137
144
  });
145
+ var Parameters = new MoveStruct({
146
+ name: `${$moduleName7}::Parameters<phantom T0>`,
147
+ fields: {
148
+ id: bcs.Address,
149
+ house_edge_bps: bcs.u64(),
150
+ min_stake: bcs.u64()
151
+ }
152
+ });
138
153
  function createGame(options) {
139
154
  const packageAddress = options.package ?? "0xb43cf6583c0c15315c7e66f173af4be79ac40c38aad1fd92ec08638ab2026202";
140
155
  const argumentsTypes = [
@@ -256,6 +271,7 @@ var REGISTRY_IDS = {
256
271
  };
257
272
 
258
273
  // src/helpers/config.ts
274
+ var DEFAULT_CACHE_TTL_MS = 30 * 60 * 1e3;
259
275
  function resolveSuigarConfig(network) {
260
276
  const packageIds = PACKAGE_IDS[network];
261
277
  const registryIds = REGISTRY_IDS[network];
@@ -317,6 +333,22 @@ function resolveSupportedCoin(config, coinType) {
317
333
  }
318
334
  return supportedCoin;
319
335
  }
336
+ function replaceStructTagAddress(structName, address) {
337
+ return normalizeStructTag({
338
+ ...parseStructTag(structName),
339
+ address
340
+ });
341
+ }
342
+ function resolveGameSettingsKeyType(structName, packageId) {
343
+ return replaceStructTagAddress(structName, packageId);
344
+ }
345
+ function resolveCoinTypeNameForTypeNameKey(structName) {
346
+ const { address } = parseStructTag(structName);
347
+ return replaceStructTagAddress(
348
+ structName,
349
+ normalizeSuiAddress(address).replace(/^0x/, "")
350
+ );
351
+ }
320
352
  var PARTNER_METADATA_KEY = "partner";
321
353
  var RESERVED_METADATA_KEYS = /* @__PURE__ */ new Set([PARTNER_METADATA_KEY, "referrer"]);
322
354
  var textEncoder = new TextEncoder();
@@ -361,8 +393,22 @@ function encodeBetMetadata(metadata, partner) {
361
393
  values
362
394
  };
363
395
  }
364
-
365
- // src/contracts/coinflip/coinflip.ts
396
+ var $moduleName8 = "0xb35c5f286c443752afc8ccb40125a578a4f32df35617170ccfa17fe180ab80ea::coinflip";
397
+ var CoinFlipSettingsKey = new MoveStruct({
398
+ name: `${$moduleName8}::CoinFlipSettingsKey`,
399
+ fields: {
400
+ dummy_field: bcs.bool()
401
+ }
402
+ });
403
+ var Parameters2 = new MoveStruct({
404
+ name: `${$moduleName8}::Parameters<phantom T0>`,
405
+ fields: {
406
+ id: bcs.Address,
407
+ house_edge: bcs.u64(),
408
+ min_stake: bcs.u64(),
409
+ max_stake: bcs.u64()
410
+ }
411
+ });
366
412
  function play(options) {
367
413
  const packageAddress = options.package ?? "0xb35c5f286c443752afc8ccb40125a578a4f32df35617170ccfa17fe180ab80ea";
368
414
  const argumentsTypes = [
@@ -485,8 +531,47 @@ function buildCoinflipTransaction(options) {
485
531
  })(tx)
486
532
  });
487
533
  }
534
+ var $moduleName9 = "0xf391858d2a08473e8d4defcc8df89976bd7b123d3865c6b9341b237f7853dbbc::i64";
535
+ var I64 = new MoveStruct({
536
+ name: `${$moduleName9}::I64`,
537
+ fields: {
538
+ bits: bcs.u64()
539
+ }
540
+ });
541
+
542
+ // src/contracts/limbo/deps/0xf391858d2a08473e8d4defcc8df89976bd7b123d3865c6b9341b237f7853dbbc/float.ts
543
+ var $moduleName10 = "0xf391858d2a08473e8d4defcc8df89976bd7b123d3865c6b9341b237f7853dbbc::float";
544
+ var Float2 = new MoveStruct({
545
+ name: `${$moduleName10}::Float`,
546
+ fields: {
547
+ is_negative: bcs.bool(),
548
+ exp: I64,
549
+ mant: bcs.u64()
550
+ }
551
+ });
488
552
 
489
553
  // src/contracts/limbo/limbo.ts
554
+ var $moduleName11 = "0x96c7841b9b32c59a219760fd656f1c3aceb53cc74a68ec9844a3a696374309f4::limbo";
555
+ var LimboSettingsKey = new MoveStruct({
556
+ name: `${$moduleName11}::LimboSettingsKey`,
557
+ fields: {
558
+ dummy_field: bcs.bool()
559
+ }
560
+ });
561
+ var Parameters3 = new MoveStruct({
562
+ name: `${$moduleName11}::Parameters<phantom T0>`,
563
+ fields: {
564
+ id: bcs.Address,
565
+ min_stake: bcs.u64(),
566
+ max_stake: bcs.u64(),
567
+ max_payout: bcs.u64(),
568
+ min_target_multiplier: Float2,
569
+ max_target_multiplier: Float2,
570
+ max_number_of_games: bcs.u64(),
571
+ min_rtp: Float2,
572
+ max_rtp: Float2
573
+ }
574
+ });
490
575
  function play2(options) {
491
576
  const packageAddress = options.package ?? "0x96c7841b9b32c59a219760fd656f1c3aceb53cc74a68ec9844a3a696374309f4";
492
577
  const argumentsTypes = [
@@ -544,8 +629,71 @@ function buildLimboTransaction(options) {
544
629
  })(tx)
545
630
  });
546
631
  }
632
+ var $moduleName12 = "0x2::vec_map";
633
+ function Entry3(...typeParameters) {
634
+ return new MoveStruct({
635
+ name: `${$moduleName12}::Entry<${typeParameters[0].name}, ${typeParameters[1].name}>`,
636
+ fields: {
637
+ key: typeParameters[0],
638
+ value: typeParameters[1]
639
+ }
640
+ });
641
+ }
642
+ function VecMap3(...typeParameters) {
643
+ return new MoveStruct({
644
+ name: `${$moduleName12}::VecMap<${typeParameters[0].name}, ${typeParameters[1].name}>`,
645
+ fields: {
646
+ contents: bcs.vector(Entry3(typeParameters[0], typeParameters[1]))
647
+ }
648
+ });
649
+ }
650
+ var $moduleName13 = "0xf391858d2a08473e8d4defcc8df89976bd7b123d3865c6b9341b237f7853dbbc::i64";
651
+ var I642 = new MoveStruct({
652
+ name: `${$moduleName13}::I64`,
653
+ fields: {
654
+ bits: bcs.u64()
655
+ }
656
+ });
657
+
658
+ // src/contracts/plinko/deps/0xf391858d2a08473e8d4defcc8df89976bd7b123d3865c6b9341b237f7853dbbc/float.ts
659
+ var $moduleName14 = "0xf391858d2a08473e8d4defcc8df89976bd7b123d3865c6b9341b237f7853dbbc::float";
660
+ var Float3 = new MoveStruct({
661
+ name: `${$moduleName14}::Float`,
662
+ fields: {
663
+ is_negative: bcs.bool(),
664
+ exp: I642,
665
+ mant: bcs.u64()
666
+ }
667
+ });
547
668
 
548
669
  // src/contracts/plinko/plinko.ts
670
+ var $moduleName15 = "0xd3dd2200883af10811724f0bed97591ad155a02efd6332d471ff8b346030dfb7::plinko";
671
+ var PlinkoSettingsKey = new MoveStruct({
672
+ name: `${$moduleName15}::PlinkoSettingsKey`,
673
+ fields: {
674
+ dummy_field: bcs.bool()
675
+ }
676
+ });
677
+ var PlinkoConfig = new MoveStruct({
678
+ name: `${$moduleName15}::PlinkoConfig`,
679
+ fields: {
680
+ num_rows: bcs.u8(),
681
+ multipliers: bcs.vector(Float3),
682
+ min_stake: bcs.u64(),
683
+ max_stake: bcs.u64(),
684
+ is_playable: bcs.bool()
685
+ }
686
+ });
687
+ var Parameters4 = new MoveStruct({
688
+ name: `${$moduleName15}::Parameters<phantom T0>`,
689
+ fields: {
690
+ id: bcs.Address,
691
+ min_stake: bcs.u64(),
692
+ max_stake: bcs.u64(),
693
+ max_number_of_balls: bcs.u64(),
694
+ configs: VecMap3(bcs.u8(), PlinkoConfig)
695
+ }
696
+ });
549
697
  function play3(options) {
550
698
  const packageAddress = options.package ?? "0xd3dd2200883af10811724f0bed97591ad155a02efd6332d471ff8b346030dfb7";
551
699
  const argumentsTypes = [
@@ -672,8 +820,46 @@ function buildPvPCoinflipTransaction(action, options) {
672
820
  throw new Error(`Unsupported PvP coinflip action: ${action}`);
673
821
  }
674
822
  }
823
+ var $moduleName16 = "0xf391858d2a08473e8d4defcc8df89976bd7b123d3865c6b9341b237f7853dbbc::i64";
824
+ var I643 = new MoveStruct({
825
+ name: `${$moduleName16}::I64`,
826
+ fields: {
827
+ bits: bcs.u64()
828
+ }
829
+ });
830
+
831
+ // src/contracts/range/deps/0xf391858d2a08473e8d4defcc8df89976bd7b123d3865c6b9341b237f7853dbbc/float.ts
832
+ var $moduleName17 = "0xf391858d2a08473e8d4defcc8df89976bd7b123d3865c6b9341b237f7853dbbc::float";
833
+ var Float4 = new MoveStruct({
834
+ name: `${$moduleName17}::Float`,
835
+ fields: {
836
+ is_negative: bcs.bool(),
837
+ exp: I643,
838
+ mant: bcs.u64()
839
+ }
840
+ });
675
841
 
676
842
  // src/contracts/range/range.ts
843
+ var $moduleName18 = "0x096a4cf18b3661e76b2c62b90785418345d52f45b272448794f123a4cb6b6416::range";
844
+ var RangeSettingsKey = new MoveStruct({
845
+ name: `${$moduleName18}::RangeSettingsKey`,
846
+ fields: {
847
+ dummy_field: bcs.bool()
848
+ }
849
+ });
850
+ var Parameters5 = new MoveStruct({
851
+ name: `${$moduleName18}::Parameters<phantom T0>`,
852
+ fields: {
853
+ id: bcs.Address,
854
+ min_stake: bcs.u64(),
855
+ max_stake: bcs.u64(),
856
+ min_zone_size: bcs.u64(),
857
+ max_zone_size: bcs.u64(),
858
+ max_number_of_games: bcs.u64(),
859
+ min_rtp: Float4,
860
+ max_rtp: Float4
861
+ }
862
+ });
677
863
  function play4(options) {
678
864
  const packageAddress = options.package ?? "0x096a4cf18b3661e76b2c62b90785418345d52f45b272448794f123a4cb6b6416";
679
865
  const argumentsTypes = [
@@ -734,8 +920,71 @@ function buildRangeTransaction(options) {
734
920
  })(tx)
735
921
  });
736
922
  }
923
+ var $moduleName19 = "0x2::vec_map";
924
+ function Entry4(...typeParameters) {
925
+ return new MoveStruct({
926
+ name: `${$moduleName19}::Entry<${typeParameters[0].name}, ${typeParameters[1].name}>`,
927
+ fields: {
928
+ key: typeParameters[0],
929
+ value: typeParameters[1]
930
+ }
931
+ });
932
+ }
933
+ function VecMap4(...typeParameters) {
934
+ return new MoveStruct({
935
+ name: `${$moduleName19}::VecMap<${typeParameters[0].name}, ${typeParameters[1].name}>`,
936
+ fields: {
937
+ contents: bcs.vector(Entry4(typeParameters[0], typeParameters[1]))
938
+ }
939
+ });
940
+ }
941
+ var $moduleName20 = "0xf391858d2a08473e8d4defcc8df89976bd7b123d3865c6b9341b237f7853dbbc::i64";
942
+ var I644 = new MoveStruct({
943
+ name: `${$moduleName20}::I64`,
944
+ fields: {
945
+ bits: bcs.u64()
946
+ }
947
+ });
948
+
949
+ // src/contracts/wheel/deps/0xf391858d2a08473e8d4defcc8df89976bd7b123d3865c6b9341b237f7853dbbc/float.ts
950
+ var $moduleName21 = "0xf391858d2a08473e8d4defcc8df89976bd7b123d3865c6b9341b237f7853dbbc::float";
951
+ var Float5 = new MoveStruct({
952
+ name: `${$moduleName21}::Float`,
953
+ fields: {
954
+ is_negative: bcs.bool(),
955
+ exp: I644,
956
+ mant: bcs.u64()
957
+ }
958
+ });
737
959
 
738
960
  // src/contracts/wheel/wheel.ts
961
+ var $moduleName22 = "0x0997852ded7e13301c42317004bc49704a893aa82997c5706cebee59053a31b7::wheel";
962
+ var WheelSettingsKey = new MoveStruct({
963
+ name: `${$moduleName22}::WheelSettingsKey`,
964
+ fields: {
965
+ dummy_field: bcs.bool()
966
+ }
967
+ });
968
+ var WheelConfig = new MoveStruct({
969
+ name: `${$moduleName22}::WheelConfig`,
970
+ fields: {
971
+ num_cases: bcs.u8(),
972
+ multipliers: bcs.vector(Float5),
973
+ min_stake: bcs.u64(),
974
+ max_stake: bcs.u64(),
975
+ is_playable: bcs.bool()
976
+ }
977
+ });
978
+ var Parameters6 = new MoveStruct({
979
+ name: `${$moduleName22}::Parameters<phantom T0>`,
980
+ fields: {
981
+ id: bcs.Address,
982
+ min_stake: bcs.u64(),
983
+ max_stake: bcs.u64(),
984
+ max_number_of_spins: bcs.u64(),
985
+ configs: VecMap4(bcs.u8(), WheelConfig)
986
+ }
987
+ });
739
988
  function play5(options) {
740
989
  const packageAddress = options.package ?? "0x0997852ded7e13301c42317004bc49704a893aa82997c5706cebee59053a31b7";
741
990
  const argumentsTypes = [
@@ -790,16 +1039,127 @@ function buildWheelTransaction(options) {
790
1039
  })(tx)
791
1040
  });
792
1041
  }
1042
+ var TtlClientCache = class extends ClientCache {
1043
+ #ttlMs;
1044
+ constructor({ ttlMs, ...options }) {
1045
+ super({
1046
+ ...options,
1047
+ prefix: options.prefix ?? ["ttl"]
1048
+ });
1049
+ this.#ttlMs = ttlMs;
1050
+ }
1051
+ read(key, load, options) {
1052
+ if (options?.ignoreCache) {
1053
+ super.clear(key);
1054
+ }
1055
+ if (this.#ttlMs <= 0) {
1056
+ return load();
1057
+ }
1058
+ const cached = super.read(
1059
+ key,
1060
+ () => this.#loadEntry(key, load)
1061
+ );
1062
+ if (cached && cached.expiresAt > Date.now()) {
1063
+ return cached.value;
1064
+ }
1065
+ super.clear(key);
1066
+ return super.read(
1067
+ key,
1068
+ () => this.#loadEntry(key, load)
1069
+ ).value;
1070
+ }
1071
+ readSync(key, load, options) {
1072
+ if (options?.ignoreCache) {
1073
+ super.clear(key);
1074
+ }
1075
+ if (this.#ttlMs <= 0) {
1076
+ return load();
1077
+ }
1078
+ const cached = super.readSync(
1079
+ key,
1080
+ () => this.#loadSyncEntry(load)
1081
+ );
1082
+ if (cached.expiresAt > Date.now()) {
1083
+ return cached.value;
1084
+ }
1085
+ super.clear(key);
1086
+ return super.readSync(key, () => this.#loadSyncEntry(load)).value;
1087
+ }
1088
+ #loadEntry(key, load) {
1089
+ const value = load();
1090
+ const entry = {
1091
+ value,
1092
+ expiresAt: Date.now() + this.#ttlMs
1093
+ };
1094
+ if (isPromiseLike(value)) {
1095
+ entry.value = Promise.resolve(value).then((resolved) => {
1096
+ super.clear(key);
1097
+ super.read(key, () => ({
1098
+ value: resolved,
1099
+ expiresAt: Date.now() + this.#ttlMs
1100
+ }));
1101
+ return resolved;
1102
+ }).catch((error) => {
1103
+ super.clear(key);
1104
+ throw error;
1105
+ });
1106
+ }
1107
+ return entry;
1108
+ }
1109
+ #loadSyncEntry(load) {
1110
+ return {
1111
+ value: load(),
1112
+ expiresAt: Date.now() + this.#ttlMs
1113
+ };
1114
+ }
1115
+ };
1116
+ function isPromiseLike(value) {
1117
+ return (typeof value === "object" || typeof value === "function") && value !== null && "then" in value;
1118
+ }
1119
+
1120
+ // src/types/game-settings.type.ts
1121
+ var GAME_SETTINGS = {
1122
+ coinflip: {
1123
+ settingsKey: CoinFlipSettingsKey,
1124
+ parameters: Parameters2
1125
+ },
1126
+ limbo: {
1127
+ settingsKey: LimboSettingsKey,
1128
+ parameters: Parameters3
1129
+ },
1130
+ plinko: {
1131
+ settingsKey: PlinkoSettingsKey,
1132
+ parameters: Parameters4
1133
+ },
1134
+ "pvp-coinflip": {
1135
+ settingsKey: PvpCoinflipSettingsKey,
1136
+ parameters: Parameters
1137
+ },
1138
+ range: {
1139
+ settingsKey: RangeSettingsKey,
1140
+ parameters: Parameters5
1141
+ },
1142
+ wheel: {
1143
+ settingsKey: WheelSettingsKey,
1144
+ parameters: Parameters6
1145
+ }
1146
+ };
793
1147
 
794
1148
  // src/client.ts
795
1149
  function suigar({
796
1150
  name = "suigar",
797
- partner
1151
+ partner,
1152
+ cacheTtl
798
1153
  } = {}) {
799
1154
  return {
800
1155
  name,
801
1156
  register: (client) => {
802
- return new SuigarClient({ client, partner });
1157
+ return new SuigarClient({
1158
+ client,
1159
+ name: String(name),
1160
+ partner,
1161
+ cacheTtl
1162
+ });
803
1163
  }
804
1164
  };
805
1165
  }
@@ -807,12 +1167,20 @@ var SuigarClient = class {
807
1167
  #client;
808
1168
  #config;
809
1169
  #partner;
1170
+ #cache;
810
1171
  constructor({
811
1172
  client,
812
- partner
1173
+ name,
1174
+ partner,
1175
+ cacheTtl
813
1176
  }) {
814
1177
  this.#client = client;
815
1178
  this.#partner = partner;
1179
+ this.#cache = client.cache.scope("@suigar/sdk").readSync([name, "ttl-cache"], () => {
1180
+ return new TtlClientCache({
1181
+ ttlMs: cacheTtl ?? DEFAULT_CACHE_TTL_MS
1182
+ });
1183
+ });
816
1184
  const network = this.#client.network;
817
1185
  if (!SUPPORTED_SUI_NETWORKS.includes(network)) {
818
1186
  throw new Error(`Unsupported network: ${network}`);
@@ -823,8 +1191,8 @@ var SuigarClient = class {
823
1191
  * Returns the resolved SDK configuration for the connected network.
824
1192
  *
825
1193
  * This is primarily useful for debugging or inspecting which package ids,
826
- * supported coin types, and price info object ids the SDK resolved for the
827
- * current client network.
1194
+ * registry ids, supported coin types, and price info object ids the SDK
1195
+ * resolved for the current client network.
828
1196
  *
829
1197
  * @returns Network-resolved Suigar configuration.
830
1198
  */
@@ -846,6 +1214,29 @@ var SuigarClient = class {
846
1214
  const bytes = await transaction.build({ ...options, client: this.#client });
847
1215
  return toBase64(bytes);
848
1216
  }
1217
+ /**
1218
+ * Reads on-chain game parameters for the requested game.
1219
+ *
1220
+ * The SDK first reads the selected game's settings object from SweetHouse,
1221
+ * then reads that game's coin-specific `Parameters<T>` object. Results are
1222
+ * cached according to the extension `cacheTtl` option. Pass
1223
+ * `ignoreCache: true` to refresh the onchain read and replace the cached
1224
+ * value.
1225
+ *
1226
+ * @param game Game whose parameters should be loaded.
1227
+ * @param options Optional coin type, cache override, and abort signal.
1228
+ * @returns Parsed game parameters typed for the requested game.
1229
+ */
1230
+ async getGameParameters(game, options = {}) {
1231
+ const coinType = normalizeStructTag(
1232
+ options.coinType ?? this.#config.coinTypes.sui
1233
+ );
1234
+ return this.#cache.read(
1235
+ ["parameters", this.#client.network, game, coinType],
1236
+ () => this.#fetchGameParameters(game, coinType, options.signal),
1237
+ { ignoreCache: options.ignoreCache }
1238
+ );
1239
+ }
849
1240
  /**
850
1241
  * Lists unresolved PvP coinflip games from the configured registry and resolves
851
1242
  * each entry into parsed onchain game state.
@@ -910,6 +1301,41 @@ var SuigarClient = class {
910
1301
  (game) => game instanceof Error ? [] : [game]
911
1302
  );
912
1303
  }
1304
+ async #fetchGameParameters(game, coinType, signal) {
1305
+ const gameDefinition = GAME_SETTINGS[game];
1306
+ const { object: settingsObject } = await this.#client.core.getDynamicObjectField({
1307
+ parentId: this.#config.packageIds.sweetHouse,
1308
+ name: {
1309
+ type: resolveGameSettingsKeyType(
1310
+ gameDefinition.settingsKey.name,
1311
+ resolveGamePackageId(this.#config, game)
1312
+ ),
1313
+ bcs: gameDefinition.settingsKey.serialize({ dummy_field: false }).toBytes()
1314
+ },
1315
+ signal
1316
+ });
1317
+ const { object } = await this.#client.core.getDynamicObjectField({
1318
+ parentId: settingsObject.objectId,
1319
+ name: {
1320
+ type: TypeName.name,
1321
+ bcs: TypeName.serialize({
1322
+ name: resolveCoinTypeNameForTypeNameKey(coinType)
1323
+ }).toBytes()
1324
+ },
1325
+ include: {
1326
+ content: true
1327
+ },
1328
+ signal
1329
+ });
1330
+ if (!object?.content) {
1331
+ throw new Error(
1332
+ `Missing parameters object content for ${game} and coin type ${coinType}`
1333
+ );
1334
+ }
1335
+ return gameDefinition.parameters.parse(
1336
+ object.content
1337
+ );
1338
+ }
913
1339
  /**
914
1340
  * BCS struct constructors for decoding on-chain objects and events related to Suigar games.
915
1341
  *
package/dist/utils.cjs CHANGED
@@ -11,36 +11,46 @@ var RANGE_POINT_LIMIT = DEFAULT_RANGE_SCALE * 100;
11
11
  var DEFAULT_LIMBO_MULTIPLIER_SCALE = 100;
12
12
 
13
13
  // src/utils/numeric.ts
14
- function isFiniteNumber(value, message) {
15
- if (typeof value !== "number") {
16
- throw new Error(`${message}: ${String(value)}`);
17
- }
18
- if (!Number.isFinite(value)) {
19
- throw new Error(`Value must be a finite number: ${value}`);
14
+ function assertFiniteNumber(value, errorMessage) {
15
+ if (typeof value !== "number" || !Number.isFinite(value)) {
16
+ throw new TypeError(`${errorMessage}: ${String(value)}`);
20
17
  }
21
18
  }
22
19
  function toBigInt(value) {
23
- if (typeof value === "bigint") {
24
- if (value < 0n) {
25
- throw new Error(`Value must be non-negative: ${value}`);
20
+ let result;
21
+ try {
22
+ if (typeof value === "bigint" || typeof value === "string" || typeof value === "boolean") {
23
+ result = BigInt(value);
24
+ } else {
25
+ assertFiniteNumber(
26
+ value,
27
+ "Value must be a bigint, number, integer string, or boolean"
28
+ );
29
+ result = BigInt(Math.trunc(value));
26
30
  }
27
- return value;
31
+ } catch {
32
+ throw new TypeError(
33
+ `Value must be a bigint, number, integer string, or boolean: ${value}`
34
+ );
28
35
  }
29
- isFiniteNumber(value, "Value must be a bigint or number");
30
- if (value < 0) {
31
- throw new Error(`Value must be a finite non-negative number: ${value}`);
36
+ if (result < 0n) {
37
+ throw new RangeError(`Value must be non-negative: ${value}`);
32
38
  }
33
- return BigInt(Math.trunc(value));
39
+ return result;
34
40
  }
35
- function toU8(value) {
36
- isFiniteNumber(value, "Value must be a number");
37
- if (!Number.isInteger(value)) {
38
- throw new Error(`Value must be an integer: ${value}`);
41
+ function toBoundedInt(value, max, typeName) {
42
+ const num = typeof value === "string" && value.trim() === "" ? NaN : Number(value);
43
+ assertFiniteNumber(num, "Value must be a finite number or integer string");
44
+ if (typeof value === "boolean" || value == null || !Number.isInteger(num) || num < 0 || num > max) {
45
+ throw new Error(`Value must be a ${typeName} integer (0-${max}): ${value}`);
39
46
  }
40
- if (value < 0 || value > 255) {
41
- throw new Error(`Value must be an integer between 0 and 255: ${value}`);
42
- }
43
- return value;
47
+ return num;
48
+ }
49
+ function toU8(value) {
50
+ return toBoundedInt(value, 255, "u8");
51
+ }
52
+ function toU16(value) {
53
+ return toBoundedInt(value, 65535, "u16");
44
54
  }
45
55
  utils.normalizeSuiAddress("0x1");
46
56
  utils.normalizeSuiAddress("0x2");
@@ -227,4 +237,5 @@ exports.fromMoveI64 = fromMoveI64;
227
237
  exports.parseCoinType = parseCoinType;
228
238
  exports.parseGameDetails = parseGameDetails;
229
239
  exports.toBigInt = toBigInt;
240
+ exports.toU16 = toU16;
230
241
  exports.toU8 = toU8;