@suigar/mcp 0.1.0 → 0.1.1

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 (55) hide show
  1. package/CHANGELOG.md +19 -0
  2. package/dist/bin.cjs +1 -1
  3. package/dist/bin.mjs +1 -1
  4. package/dist/index.cjs +43 -50
  5. package/dist/index.d.cts +818 -8
  6. package/dist/index.d.mts +818 -8
  7. package/dist/index.mjs +1 -8
  8. package/{node_modules/@suigar/sui-rpc-pool/dist/index.cjs → dist/server-BD-123-u.mjs} +2084 -671
  9. package/{node_modules/@suigar/sui-rpc-pool/dist/index.mjs → dist/server-Cmo8UaHE.cjs} +2341 -645
  10. package/dist/server.cjs +3 -432
  11. package/dist/server.mjs +1 -430
  12. package/package.json +75 -61
  13. package/dist/client.cjs +0 -46
  14. package/dist/client.d.cts +0 -17
  15. package/dist/client.d.mts +0 -17
  16. package/dist/client.mjs +0 -43
  17. package/dist/coin.cjs +0 -86
  18. package/dist/coin.d.cts +0 -35
  19. package/dist/coin.d.mts +0 -35
  20. package/dist/coin.mjs +0 -86
  21. package/dist/config.cjs +0 -183
  22. package/dist/config.d.cts +0 -15
  23. package/dist/config.d.mts +0 -15
  24. package/dist/config.mjs +0 -174
  25. package/dist/mcp-support.cjs +0 -62
  26. package/dist/mcp-support.d.cts +0 -16
  27. package/dist/mcp-support.d.mts +0 -16
  28. package/dist/mcp-support.mjs +0 -60
  29. package/dist/metadata.cjs +0 -51
  30. package/dist/metadata.d.cts +0 -52
  31. package/dist/metadata.d.mts +0 -52
  32. package/dist/metadata.mjs +0 -47
  33. package/dist/tools.cjs +0 -617
  34. package/dist/tools.d.cts +0 -158
  35. package/dist/tools.d.mts +0 -158
  36. package/dist/tools.mjs +0 -608
  37. package/dist/transactions.cjs +0 -294
  38. package/dist/transactions.d.cts +0 -40
  39. package/dist/transactions.d.mts +0 -40
  40. package/dist/transactions.mjs +0 -286
  41. package/dist/types.d.cts +0 -111
  42. package/dist/types.d.mts +0 -111
  43. package/node_modules/@suigar/currency-registry/dist/index.cjs +0 -121
  44. package/node_modules/@suigar/currency-registry/dist/index.d.cts +0 -50
  45. package/node_modules/@suigar/currency-registry/dist/index.d.mts +0 -50
  46. package/node_modules/@suigar/currency-registry/dist/index.mjs +0 -110
  47. package/node_modules/@suigar/currency-registry/package.json +0 -31
  48. package/node_modules/@suigar/game-registry/dist/index.cjs +0 -310
  49. package/node_modules/@suigar/game-registry/dist/index.d.cts +0 -65
  50. package/node_modules/@suigar/game-registry/dist/index.d.mts +0 -65
  51. package/node_modules/@suigar/game-registry/dist/index.mjs +0 -292
  52. package/node_modules/@suigar/game-registry/package.json +0 -31
  53. package/node_modules/@suigar/sui-rpc-pool/dist/index.d.cts +0 -465
  54. package/node_modules/@suigar/sui-rpc-pool/dist/index.d.mts +0 -465
  55. package/node_modules/@suigar/sui-rpc-pool/package.json +0 -31
@@ -1,294 +0,0 @@
1
- const require_config = require("./config.cjs");
2
- const require_coin = require("./coin.cjs");
3
- let _suigar_currency_registry = require("@suigar/currency-registry");
4
- let _mysten_sui_utils = require("@mysten/sui/utils");
5
- let _suigar_sdk_utils = require("@suigar/sdk/utils");
6
- let _mysten_sui_transactions = require("@mysten/sui/transactions");
7
- //#region src/transactions.ts
8
- const ADDRESS_METADATA_KEYS = /* @__PURE__ */ new Set(["referrer", "partner"]);
9
- const RESERVED_METADATA_KEYS = /* @__PURE__ */ new Set(["referrer", "partner"]);
10
- const textEncoder = new TextEncoder();
11
- const normalizeOwner = (value) => (0, _mysten_sui_utils.normalizeSuiAddress)(value);
12
- const normalizePartner = (value) => (0, _mysten_sui_utils.normalizeSuiAddress)(value);
13
- const resolveBetMetadata = (metadata, partner) => {
14
- const resolvedEntries = Object.entries(metadata ?? {}).filter(([key, value]) => {
15
- if (value === void 0 || value === null) return false;
16
- return !RESERVED_METADATA_KEYS.has(key);
17
- });
18
- if (partner?.trim()) resolvedEntries.unshift(["partner", normalizePartner(partner.trim())]);
19
- if (resolvedEntries.length === 0) return;
20
- return Object.fromEntries(resolvedEntries);
21
- };
22
- const parseHexAddress = (value) => {
23
- const trimmed = value.trim();
24
- if (!trimmed) return null;
25
- const hexBody = trimmed.startsWith("0x") || trimmed.startsWith("0X") ? trimmed.slice(2) : trimmed;
26
- if (!/^[0-9a-fA-F]+$/.test(hexBody) || hexBody.length > 64) return null;
27
- const padded = (hexBody.length % 2 === 1 ? `0${hexBody}` : hexBody).padStart(64, "0");
28
- const bytes = new Uint8Array(padded.length / 2);
29
- for (let index = 0; index < padded.length; index += 2) bytes[index / 2] = Number.parseInt(padded.slice(index, index + 2), 16);
30
- return bytes;
31
- };
32
- const encodeBetMetadata = (metadata) => {
33
- const keys = [];
34
- const values = [];
35
- for (const [key, value] of Object.entries(metadata ?? {})) {
36
- if (value === void 0 || value === null) continue;
37
- let encodedValue;
38
- if (value instanceof Uint8Array) encodedValue = Array.from(value);
39
- else if (Array.isArray(value)) encodedValue = value;
40
- else if (typeof value === "string" && ADDRESS_METADATA_KEYS.has(key)) encodedValue = Array.from(parseHexAddress(value) ?? textEncoder.encode(String(value)));
41
- else encodedValue = Array.from(textEncoder.encode(String(value)));
42
- keys.push(key);
43
- values.push(encodedValue);
44
- }
45
- return {
46
- keys,
47
- values
48
- };
49
- };
50
- const getGameGasBudget = (_game) => _suigar_sdk_utils.DEFAULT_GAS_BUDGET_MIST;
51
- const applyCommonTransactionDefaults = ({ transaction, owner, gasBudget, game }) => {
52
- transaction.setSenderIfNotSet(normalizeOwner(owner));
53
- transaction.setGasBudget(Number(gasBudget ?? getGameGasBudget(game)));
54
- return transaction;
55
- };
56
- const buildGenericBetTransaction = async ({ game, owner, coinType, stake, cashStake, betCount, metadata, config: configInput, client, coinSource, gasBudget, sender, partner, allowGasCoinShortcut = true, target, extraArgs }) => {
57
- const config = require_config.assertRequiredConfig(game, configInput);
58
- const tx = new _mysten_sui_transactions.Transaction();
59
- const normalizedOwner = normalizeOwner(sender ?? owner);
60
- const normalizedCoinType = (0, _suigar_currency_registry.normalizeCoinType)(coinType);
61
- const resolvedStake = (0, _suigar_sdk_utils.toBigInt)(stake);
62
- const resolvedCashStake = (0, _suigar_sdk_utils.toBigInt)(cashStake ?? stake);
63
- const resolvedBetCount = (0, _suigar_sdk_utils.toBigInt)(betCount ?? 1);
64
- const encodedMetadata = encodeBetMetadata(resolveBetMetadata(metadata, partner));
65
- const pythPriceInfoObjectId = require_config.resolvePythPriceInfoId(normalizedCoinType, config);
66
- tx.setSenderIfNotSet(normalizedOwner);
67
- const betCoin = await require_coin.resolveBetCoin({
68
- tx,
69
- coinType: normalizedCoinType,
70
- amount: resolvedCashStake,
71
- suiCoinType: config.suiCoinType,
72
- coinSource,
73
- client,
74
- owner: normalizedOwner,
75
- allowGasCoinShortcut
76
- });
77
- const rewardCoin = tx.moveCall({
78
- target,
79
- typeArguments: [normalizedCoinType],
80
- arguments: [
81
- tx.object(config.sweethouseId),
82
- tx.pure.u64(resolvedStake),
83
- betCoin,
84
- tx.pure.u64(resolvedBetCount),
85
- ...extraArgs(tx),
86
- tx.pure.vector("string", encodedMetadata.keys),
87
- tx.pure.vector("vector<u8>", encodedMetadata.values),
88
- tx.object(pythPriceInfoObjectId),
89
- tx.object(_mysten_sui_utils.SUI_CLOCK_OBJECT_ID),
90
- tx.object(_mysten_sui_utils.SUI_RANDOM_OBJECT_ID)
91
- ]
92
- });
93
- tx.transferObjects([rewardCoin], tx.pure.address(normalizedOwner));
94
- applyCommonTransactionDefaults({
95
- transaction: tx,
96
- owner: normalizedOwner,
97
- gasBudget,
98
- game
99
- });
100
- return tx;
101
- };
102
- const buildCoinflipTransaction = (args) => {
103
- const config = require_config.resolveSuigarConfig(args.config);
104
- return buildGenericBetTransaction({
105
- ...args,
106
- config,
107
- game: "coinflip",
108
- target: `${config.coinflipPackageId}::coinflip::play`,
109
- extraArgs: (tx) => [tx.pure.bool(args.side === "tails")]
110
- });
111
- };
112
- const buildLimboTransaction = (args) => {
113
- const config = require_config.resolveSuigarConfig(args.config);
114
- const numerator = Math.round(args.targetMultiplier * _suigar_sdk_utils.DEFAULT_LIMBO_MULTIPLIER_SCALE);
115
- const denominator = _suigar_sdk_utils.DEFAULT_LIMBO_MULTIPLIER_SCALE;
116
- return buildGenericBetTransaction({
117
- ...args,
118
- config,
119
- game: "limbo",
120
- target: `${config.limboPackageId}::limbo::play`,
121
- extraArgs: (tx) => [tx.pure.u64(BigInt(numerator)), tx.pure.u64(BigInt(denominator))]
122
- });
123
- };
124
- const buildPlinkoTransaction = (args) => {
125
- const config = require_config.resolveSuigarConfig(args.config);
126
- const configId = (0, _suigar_sdk_utils.toU8)(args.configId);
127
- return buildGenericBetTransaction({
128
- ...args,
129
- config,
130
- game: "plinko",
131
- target: `${config.plinkoPackageId}::plinko::play`,
132
- extraArgs: (tx) => [tx.pure.u8(configId)]
133
- });
134
- };
135
- const buildWheelTransaction = (args) => {
136
- const config = require_config.resolveSuigarConfig(args.config);
137
- const configId = (0, _suigar_sdk_utils.toU8)(args.configId);
138
- return buildGenericBetTransaction({
139
- ...args,
140
- config,
141
- game: "wheel",
142
- target: `${config.wheelPackageId}::wheel::play`,
143
- extraArgs: (tx) => [tx.pure.u8(configId)]
144
- });
145
- };
146
- const buildRangeTransaction = (args) => {
147
- const config = require_config.resolveSuigarConfig(args.config);
148
- const scale = args.scale ?? _suigar_sdk_utils.DEFAULT_RANGE_SCALE;
149
- const leftPoint = Math.round(args.leftPoint * scale);
150
- const rightPoint = Math.round(args.rightPoint * scale);
151
- return buildGenericBetTransaction({
152
- ...args,
153
- config,
154
- game: "range",
155
- target: `${config.rangePackageId}::range::play`,
156
- extraArgs: (tx) => [
157
- tx.pure.u64(BigInt(leftPoint)),
158
- tx.pure.u64(BigInt(rightPoint)),
159
- tx.pure.bool(Boolean(args.outOfRange))
160
- ]
161
- });
162
- };
163
- const buildPvpCoinflipCreateTransaction = async (args) => {
164
- const config = require_config.assertRequiredConfig("pvp-coinflip", args.config);
165
- const tx = new _mysten_sui_transactions.Transaction();
166
- const normalizedOwner = normalizeOwner(args.sender ?? args.owner);
167
- const normalizedCoinType = (0, _suigar_currency_registry.normalizeCoinType)(args.coinType);
168
- const resolvedStake = (0, _suigar_sdk_utils.toBigInt)(args.stake);
169
- const encodedMetadata = encodeBetMetadata(resolveBetMetadata(args.metadata, args.partner));
170
- tx.setSenderIfNotSet(normalizedOwner);
171
- const betCoin = await require_coin.resolveBetCoin({
172
- tx,
173
- coinType: normalizedCoinType,
174
- amount: resolvedStake,
175
- suiCoinType: config.suiCoinType,
176
- coinSource: args.coinSource,
177
- client: args.client,
178
- owner: normalizedOwner,
179
- allowGasCoinShortcut: args.allowGasCoinShortcut
180
- });
181
- tx.moveCall({
182
- target: `${config.pvpCoinflipPackageId}::pvp_coinflip::create_game`,
183
- typeArguments: [normalizedCoinType],
184
- arguments: [
185
- tx.object(config.sweethouseId),
186
- betCoin,
187
- tx.pure.bool(args.creatorSide === "tails"),
188
- tx.pure.bool(Boolean(args.isPrivate)),
189
- tx.pure.vector("string", encodedMetadata.keys),
190
- tx.pure.vector("vector<u8>", encodedMetadata.values)
191
- ]
192
- });
193
- applyCommonTransactionDefaults({
194
- transaction: tx,
195
- owner: normalizedOwner,
196
- gasBudget: args.gasBudget,
197
- game: "pvp-coinflip"
198
- });
199
- return tx;
200
- };
201
- const buildPvpCoinflipJoinTransaction = async (args) => {
202
- const config = require_config.assertRequiredConfig("pvp-coinflip", args.config);
203
- const tx = new _mysten_sui_transactions.Transaction();
204
- const normalizedOwner = normalizeOwner(args.sender ?? args.owner);
205
- const normalizedCoinType = (0, _suigar_currency_registry.normalizeCoinType)(args.coinType);
206
- const resolvedStake = (0, _suigar_sdk_utils.toBigInt)(args.stake);
207
- const encodedMetadata = encodeBetMetadata(resolveBetMetadata(args.metadata, args.partner));
208
- const pythPriceInfoObjectId = require_config.resolvePythPriceInfoId(normalizedCoinType, config);
209
- tx.setSenderIfNotSet(normalizedOwner);
210
- const betCoin = await require_coin.resolveBetCoin({
211
- tx,
212
- coinType: normalizedCoinType,
213
- amount: resolvedStake,
214
- suiCoinType: config.suiCoinType,
215
- coinSource: args.coinSource,
216
- client: args.client,
217
- owner: normalizedOwner,
218
- allowGasCoinShortcut: args.allowGasCoinShortcut
219
- });
220
- tx.moveCall({
221
- target: `${config.pvpCoinflipPackageId}::pvp_coinflip::join_game`,
222
- typeArguments: [normalizedCoinType],
223
- arguments: [
224
- tx.pure.id(args.gameId),
225
- tx.object(config.sweethouseId),
226
- betCoin,
227
- tx.pure.vector("string", encodedMetadata.keys),
228
- tx.pure.vector("vector<u8>", encodedMetadata.values),
229
- tx.object(pythPriceInfoObjectId),
230
- tx.object(_mysten_sui_utils.SUI_CLOCK_OBJECT_ID),
231
- tx.object(_mysten_sui_utils.SUI_RANDOM_OBJECT_ID)
232
- ]
233
- });
234
- applyCommonTransactionDefaults({
235
- transaction: tx,
236
- owner: normalizedOwner,
237
- gasBudget: args.gasBudget,
238
- game: "pvp-coinflip"
239
- });
240
- return tx;
241
- };
242
- const buildPvpCoinflipCancelTransaction = (args) => {
243
- const config = require_config.assertRequiredConfig("pvp-coinflip", args.config);
244
- const tx = new _mysten_sui_transactions.Transaction();
245
- const normalizedOwner = normalizeOwner(args.sender ?? args.owner);
246
- tx.setSenderIfNotSet(normalizedOwner);
247
- tx.moveCall({
248
- target: `${config.pvpCoinflipPackageId}::pvp_coinflip::cancel_game`,
249
- typeArguments: [(0, _suigar_currency_registry.normalizeCoinType)(args.coinType)],
250
- arguments: [tx.pure.id(args.gameId), tx.object(config.sweethouseId)]
251
- });
252
- applyCommonTransactionDefaults({
253
- transaction: tx,
254
- owner: normalizedOwner,
255
- gasBudget: args.gasBudget,
256
- game: "pvp-coinflip"
257
- });
258
- return tx;
259
- };
260
- const summarizeTransaction = (transaction, context = {}) => {
261
- const data = transaction.getData();
262
- const commands = (data.commands ?? []).map((command) => {
263
- const kind = String(command.$kind ?? Object.keys(command)[0] ?? "Unknown");
264
- const moveCall = command.MoveCall;
265
- return {
266
- kind,
267
- target: moveCall?.package && moveCall?.module && moveCall?.function ? `${moveCall.package}::${moveCall.module}::${moveCall.function}` : void 0,
268
- typeArguments: moveCall?.typeArguments
269
- };
270
- });
271
- const objectInputs = (data.inputs ?? []).flatMap((input) => input.$kind === "UnresolvedObject" && input.UnresolvedObject?.objectId ? [input.UnresolvedObject.objectId] : []);
272
- return {
273
- sender: data.sender ?? null,
274
- gasBudget: data.gasData?.budget != null ? String(data.gasData.budget) : null,
275
- gasPrice: data.gasData?.price != null ? String(data.gasData.price) : null,
276
- commandCount: commands.length,
277
- commands,
278
- inputs: (data.inputs ?? []).length,
279
- objectInputs,
280
- game: context.game,
281
- coinType: context.coinType ? (0, _suigar_currency_registry.normalizeCoinTypeKey)(context.coinType) : void 0,
282
- stake: context.stake != null ? String((0, _suigar_sdk_utils.toBigInt)(context.stake)) : void 0
283
- };
284
- };
285
- //#endregion
286
- exports.buildCoinflipTransaction = buildCoinflipTransaction;
287
- exports.buildLimboTransaction = buildLimboTransaction;
288
- exports.buildPlinkoTransaction = buildPlinkoTransaction;
289
- exports.buildPvpCoinflipCancelTransaction = buildPvpCoinflipCancelTransaction;
290
- exports.buildPvpCoinflipCreateTransaction = buildPvpCoinflipCreateTransaction;
291
- exports.buildPvpCoinflipJoinTransaction = buildPvpCoinflipJoinTransaction;
292
- exports.buildRangeTransaction = buildRangeTransaction;
293
- exports.buildWheelTransaction = buildWheelTransaction;
294
- exports.summarizeTransaction = summarizeTransaction;
@@ -1,40 +0,0 @@
1
- import { CoinSide, SharedBuildOptions, SupportedGameId, TransactionSummary } from "./types.cjs";
2
- import { Transaction } from "@mysten/sui/transactions";
3
-
4
- //#region src/transactions.d.ts
5
- declare const buildCoinflipTransaction: (args: SharedBuildOptions & {
6
- side: CoinSide;
7
- }) => Promise<Transaction>;
8
- declare const buildLimboTransaction: (args: SharedBuildOptions & {
9
- targetMultiplier: number;
10
- }) => Promise<Transaction>;
11
- declare const buildPlinkoTransaction: (args: SharedBuildOptions & {
12
- configId: number;
13
- }) => Promise<Transaction>;
14
- declare const buildWheelTransaction: (args: SharedBuildOptions & {
15
- configId: number;
16
- }) => Promise<Transaction>;
17
- declare const buildRangeTransaction: (args: SharedBuildOptions & {
18
- leftPoint: number;
19
- rightPoint: number;
20
- outOfRange?: boolean;
21
- scale?: number;
22
- }) => Promise<Transaction>;
23
- declare const buildPvpCoinflipCreateTransaction: (args: SharedBuildOptions & {
24
- creatorSide: CoinSide;
25
- isPrivate?: boolean;
26
- }) => Promise<Transaction>;
27
- declare const buildPvpCoinflipJoinTransaction: (args: SharedBuildOptions & {
28
- gameId: string;
29
- }) => Promise<Transaction>;
30
- declare const buildPvpCoinflipCancelTransaction: (args: Pick<SharedBuildOptions, "config" | "owner" | "sender" | "gasBudget"> & {
31
- gameId: string;
32
- coinType: string;
33
- }) => Transaction;
34
- declare const summarizeTransaction: (transaction: Transaction, context?: {
35
- game?: SupportedGameId;
36
- coinType?: string;
37
- stake?: bigint | number;
38
- }) => TransactionSummary;
39
- //#endregion
40
- export { buildCoinflipTransaction, buildLimboTransaction, buildPlinkoTransaction, buildPvpCoinflipCancelTransaction, buildPvpCoinflipCreateTransaction, buildPvpCoinflipJoinTransaction, buildRangeTransaction, buildWheelTransaction, summarizeTransaction };
@@ -1,40 +0,0 @@
1
- import { CoinSide, SharedBuildOptions, SupportedGameId, TransactionSummary } from "./types.mjs";
2
- import { Transaction } from "@mysten/sui/transactions";
3
-
4
- //#region src/transactions.d.ts
5
- declare const buildCoinflipTransaction: (args: SharedBuildOptions & {
6
- side: CoinSide;
7
- }) => Promise<Transaction>;
8
- declare const buildLimboTransaction: (args: SharedBuildOptions & {
9
- targetMultiplier: number;
10
- }) => Promise<Transaction>;
11
- declare const buildPlinkoTransaction: (args: SharedBuildOptions & {
12
- configId: number;
13
- }) => Promise<Transaction>;
14
- declare const buildWheelTransaction: (args: SharedBuildOptions & {
15
- configId: number;
16
- }) => Promise<Transaction>;
17
- declare const buildRangeTransaction: (args: SharedBuildOptions & {
18
- leftPoint: number;
19
- rightPoint: number;
20
- outOfRange?: boolean;
21
- scale?: number;
22
- }) => Promise<Transaction>;
23
- declare const buildPvpCoinflipCreateTransaction: (args: SharedBuildOptions & {
24
- creatorSide: CoinSide;
25
- isPrivate?: boolean;
26
- }) => Promise<Transaction>;
27
- declare const buildPvpCoinflipJoinTransaction: (args: SharedBuildOptions & {
28
- gameId: string;
29
- }) => Promise<Transaction>;
30
- declare const buildPvpCoinflipCancelTransaction: (args: Pick<SharedBuildOptions, "config" | "owner" | "sender" | "gasBudget"> & {
31
- gameId: string;
32
- coinType: string;
33
- }) => Transaction;
34
- declare const summarizeTransaction: (transaction: Transaction, context?: {
35
- game?: SupportedGameId;
36
- coinType?: string;
37
- stake?: bigint | number;
38
- }) => TransactionSummary;
39
- //#endregion
40
- export { buildCoinflipTransaction, buildLimboTransaction, buildPlinkoTransaction, buildPvpCoinflipCancelTransaction, buildPvpCoinflipCreateTransaction, buildPvpCoinflipJoinTransaction, buildRangeTransaction, buildWheelTransaction, summarizeTransaction };
@@ -1,286 +0,0 @@
1
- import { assertRequiredConfig, resolvePythPriceInfoId, resolveSuigarConfig } from "./config.mjs";
2
- import { resolveBetCoin } from "./coin.mjs";
3
- import { normalizeCoinType, normalizeCoinTypeKey } from "@suigar/currency-registry";
4
- import { SUI_CLOCK_OBJECT_ID, SUI_RANDOM_OBJECT_ID, normalizeSuiAddress } from "@mysten/sui/utils";
5
- import { DEFAULT_GAS_BUDGET_MIST, DEFAULT_LIMBO_MULTIPLIER_SCALE, DEFAULT_RANGE_SCALE, toBigInt, toU8 } from "@suigar/sdk/utils";
6
- import { Transaction } from "@mysten/sui/transactions";
7
- //#region src/transactions.ts
8
- const ADDRESS_METADATA_KEYS = /* @__PURE__ */ new Set(["referrer", "partner"]);
9
- const RESERVED_METADATA_KEYS = /* @__PURE__ */ new Set(["referrer", "partner"]);
10
- const textEncoder = new TextEncoder();
11
- const normalizeOwner = (value) => normalizeSuiAddress(value);
12
- const normalizePartner = (value) => normalizeSuiAddress(value);
13
- const resolveBetMetadata = (metadata, partner) => {
14
- const resolvedEntries = Object.entries(metadata ?? {}).filter(([key, value]) => {
15
- if (value === void 0 || value === null) return false;
16
- return !RESERVED_METADATA_KEYS.has(key);
17
- });
18
- if (partner?.trim()) resolvedEntries.unshift(["partner", normalizePartner(partner.trim())]);
19
- if (resolvedEntries.length === 0) return;
20
- return Object.fromEntries(resolvedEntries);
21
- };
22
- const parseHexAddress = (value) => {
23
- const trimmed = value.trim();
24
- if (!trimmed) return null;
25
- const hexBody = trimmed.startsWith("0x") || trimmed.startsWith("0X") ? trimmed.slice(2) : trimmed;
26
- if (!/^[0-9a-fA-F]+$/.test(hexBody) || hexBody.length > 64) return null;
27
- const padded = (hexBody.length % 2 === 1 ? `0${hexBody}` : hexBody).padStart(64, "0");
28
- const bytes = new Uint8Array(padded.length / 2);
29
- for (let index = 0; index < padded.length; index += 2) bytes[index / 2] = Number.parseInt(padded.slice(index, index + 2), 16);
30
- return bytes;
31
- };
32
- const encodeBetMetadata = (metadata) => {
33
- const keys = [];
34
- const values = [];
35
- for (const [key, value] of Object.entries(metadata ?? {})) {
36
- if (value === void 0 || value === null) continue;
37
- let encodedValue;
38
- if (value instanceof Uint8Array) encodedValue = Array.from(value);
39
- else if (Array.isArray(value)) encodedValue = value;
40
- else if (typeof value === "string" && ADDRESS_METADATA_KEYS.has(key)) encodedValue = Array.from(parseHexAddress(value) ?? textEncoder.encode(String(value)));
41
- else encodedValue = Array.from(textEncoder.encode(String(value)));
42
- keys.push(key);
43
- values.push(encodedValue);
44
- }
45
- return {
46
- keys,
47
- values
48
- };
49
- };
50
- const getGameGasBudget = (_game) => DEFAULT_GAS_BUDGET_MIST;
51
- const applyCommonTransactionDefaults = ({ transaction, owner, gasBudget, game }) => {
52
- transaction.setSenderIfNotSet(normalizeOwner(owner));
53
- transaction.setGasBudget(Number(gasBudget ?? getGameGasBudget(game)));
54
- return transaction;
55
- };
56
- const buildGenericBetTransaction = async ({ game, owner, coinType, stake, cashStake, betCount, metadata, config: configInput, client, coinSource, gasBudget, sender, partner, allowGasCoinShortcut = true, target, extraArgs }) => {
57
- const config = assertRequiredConfig(game, configInput);
58
- const tx = new Transaction();
59
- const normalizedOwner = normalizeOwner(sender ?? owner);
60
- const normalizedCoinType = normalizeCoinType(coinType);
61
- const resolvedStake = toBigInt(stake);
62
- const resolvedCashStake = toBigInt(cashStake ?? stake);
63
- const resolvedBetCount = toBigInt(betCount ?? 1);
64
- const encodedMetadata = encodeBetMetadata(resolveBetMetadata(metadata, partner));
65
- const pythPriceInfoObjectId = resolvePythPriceInfoId(normalizedCoinType, config);
66
- tx.setSenderIfNotSet(normalizedOwner);
67
- const betCoin = await resolveBetCoin({
68
- tx,
69
- coinType: normalizedCoinType,
70
- amount: resolvedCashStake,
71
- suiCoinType: config.suiCoinType,
72
- coinSource,
73
- client,
74
- owner: normalizedOwner,
75
- allowGasCoinShortcut
76
- });
77
- const rewardCoin = tx.moveCall({
78
- target,
79
- typeArguments: [normalizedCoinType],
80
- arguments: [
81
- tx.object(config.sweethouseId),
82
- tx.pure.u64(resolvedStake),
83
- betCoin,
84
- tx.pure.u64(resolvedBetCount),
85
- ...extraArgs(tx),
86
- tx.pure.vector("string", encodedMetadata.keys),
87
- tx.pure.vector("vector<u8>", encodedMetadata.values),
88
- tx.object(pythPriceInfoObjectId),
89
- tx.object(SUI_CLOCK_OBJECT_ID),
90
- tx.object(SUI_RANDOM_OBJECT_ID)
91
- ]
92
- });
93
- tx.transferObjects([rewardCoin], tx.pure.address(normalizedOwner));
94
- applyCommonTransactionDefaults({
95
- transaction: tx,
96
- owner: normalizedOwner,
97
- gasBudget,
98
- game
99
- });
100
- return tx;
101
- };
102
- const buildCoinflipTransaction = (args) => {
103
- const config = resolveSuigarConfig(args.config);
104
- return buildGenericBetTransaction({
105
- ...args,
106
- config,
107
- game: "coinflip",
108
- target: `${config.coinflipPackageId}::coinflip::play`,
109
- extraArgs: (tx) => [tx.pure.bool(args.side === "tails")]
110
- });
111
- };
112
- const buildLimboTransaction = (args) => {
113
- const config = resolveSuigarConfig(args.config);
114
- const numerator = Math.round(args.targetMultiplier * DEFAULT_LIMBO_MULTIPLIER_SCALE);
115
- const denominator = DEFAULT_LIMBO_MULTIPLIER_SCALE;
116
- return buildGenericBetTransaction({
117
- ...args,
118
- config,
119
- game: "limbo",
120
- target: `${config.limboPackageId}::limbo::play`,
121
- extraArgs: (tx) => [tx.pure.u64(BigInt(numerator)), tx.pure.u64(BigInt(denominator))]
122
- });
123
- };
124
- const buildPlinkoTransaction = (args) => {
125
- const config = resolveSuigarConfig(args.config);
126
- const configId = toU8(args.configId);
127
- return buildGenericBetTransaction({
128
- ...args,
129
- config,
130
- game: "plinko",
131
- target: `${config.plinkoPackageId}::plinko::play`,
132
- extraArgs: (tx) => [tx.pure.u8(configId)]
133
- });
134
- };
135
- const buildWheelTransaction = (args) => {
136
- const config = resolveSuigarConfig(args.config);
137
- const configId = toU8(args.configId);
138
- return buildGenericBetTransaction({
139
- ...args,
140
- config,
141
- game: "wheel",
142
- target: `${config.wheelPackageId}::wheel::play`,
143
- extraArgs: (tx) => [tx.pure.u8(configId)]
144
- });
145
- };
146
- const buildRangeTransaction = (args) => {
147
- const config = resolveSuigarConfig(args.config);
148
- const scale = args.scale ?? DEFAULT_RANGE_SCALE;
149
- const leftPoint = Math.round(args.leftPoint * scale);
150
- const rightPoint = Math.round(args.rightPoint * scale);
151
- return buildGenericBetTransaction({
152
- ...args,
153
- config,
154
- game: "range",
155
- target: `${config.rangePackageId}::range::play`,
156
- extraArgs: (tx) => [
157
- tx.pure.u64(BigInt(leftPoint)),
158
- tx.pure.u64(BigInt(rightPoint)),
159
- tx.pure.bool(Boolean(args.outOfRange))
160
- ]
161
- });
162
- };
163
- const buildPvpCoinflipCreateTransaction = async (args) => {
164
- const config = assertRequiredConfig("pvp-coinflip", args.config);
165
- const tx = new Transaction();
166
- const normalizedOwner = normalizeOwner(args.sender ?? args.owner);
167
- const normalizedCoinType = normalizeCoinType(args.coinType);
168
- const resolvedStake = toBigInt(args.stake);
169
- const encodedMetadata = encodeBetMetadata(resolveBetMetadata(args.metadata, args.partner));
170
- tx.setSenderIfNotSet(normalizedOwner);
171
- const betCoin = await resolveBetCoin({
172
- tx,
173
- coinType: normalizedCoinType,
174
- amount: resolvedStake,
175
- suiCoinType: config.suiCoinType,
176
- coinSource: args.coinSource,
177
- client: args.client,
178
- owner: normalizedOwner,
179
- allowGasCoinShortcut: args.allowGasCoinShortcut
180
- });
181
- tx.moveCall({
182
- target: `${config.pvpCoinflipPackageId}::pvp_coinflip::create_game`,
183
- typeArguments: [normalizedCoinType],
184
- arguments: [
185
- tx.object(config.sweethouseId),
186
- betCoin,
187
- tx.pure.bool(args.creatorSide === "tails"),
188
- tx.pure.bool(Boolean(args.isPrivate)),
189
- tx.pure.vector("string", encodedMetadata.keys),
190
- tx.pure.vector("vector<u8>", encodedMetadata.values)
191
- ]
192
- });
193
- applyCommonTransactionDefaults({
194
- transaction: tx,
195
- owner: normalizedOwner,
196
- gasBudget: args.gasBudget,
197
- game: "pvp-coinflip"
198
- });
199
- return tx;
200
- };
201
- const buildPvpCoinflipJoinTransaction = async (args) => {
202
- const config = assertRequiredConfig("pvp-coinflip", args.config);
203
- const tx = new Transaction();
204
- const normalizedOwner = normalizeOwner(args.sender ?? args.owner);
205
- const normalizedCoinType = normalizeCoinType(args.coinType);
206
- const resolvedStake = toBigInt(args.stake);
207
- const encodedMetadata = encodeBetMetadata(resolveBetMetadata(args.metadata, args.partner));
208
- const pythPriceInfoObjectId = resolvePythPriceInfoId(normalizedCoinType, config);
209
- tx.setSenderIfNotSet(normalizedOwner);
210
- const betCoin = await resolveBetCoin({
211
- tx,
212
- coinType: normalizedCoinType,
213
- amount: resolvedStake,
214
- suiCoinType: config.suiCoinType,
215
- coinSource: args.coinSource,
216
- client: args.client,
217
- owner: normalizedOwner,
218
- allowGasCoinShortcut: args.allowGasCoinShortcut
219
- });
220
- tx.moveCall({
221
- target: `${config.pvpCoinflipPackageId}::pvp_coinflip::join_game`,
222
- typeArguments: [normalizedCoinType],
223
- arguments: [
224
- tx.pure.id(args.gameId),
225
- tx.object(config.sweethouseId),
226
- betCoin,
227
- tx.pure.vector("string", encodedMetadata.keys),
228
- tx.pure.vector("vector<u8>", encodedMetadata.values),
229
- tx.object(pythPriceInfoObjectId),
230
- tx.object(SUI_CLOCK_OBJECT_ID),
231
- tx.object(SUI_RANDOM_OBJECT_ID)
232
- ]
233
- });
234
- applyCommonTransactionDefaults({
235
- transaction: tx,
236
- owner: normalizedOwner,
237
- gasBudget: args.gasBudget,
238
- game: "pvp-coinflip"
239
- });
240
- return tx;
241
- };
242
- const buildPvpCoinflipCancelTransaction = (args) => {
243
- const config = assertRequiredConfig("pvp-coinflip", args.config);
244
- const tx = new Transaction();
245
- const normalizedOwner = normalizeOwner(args.sender ?? args.owner);
246
- tx.setSenderIfNotSet(normalizedOwner);
247
- tx.moveCall({
248
- target: `${config.pvpCoinflipPackageId}::pvp_coinflip::cancel_game`,
249
- typeArguments: [normalizeCoinType(args.coinType)],
250
- arguments: [tx.pure.id(args.gameId), tx.object(config.sweethouseId)]
251
- });
252
- applyCommonTransactionDefaults({
253
- transaction: tx,
254
- owner: normalizedOwner,
255
- gasBudget: args.gasBudget,
256
- game: "pvp-coinflip"
257
- });
258
- return tx;
259
- };
260
- const summarizeTransaction = (transaction, context = {}) => {
261
- const data = transaction.getData();
262
- const commands = (data.commands ?? []).map((command) => {
263
- const kind = String(command.$kind ?? Object.keys(command)[0] ?? "Unknown");
264
- const moveCall = command.MoveCall;
265
- return {
266
- kind,
267
- target: moveCall?.package && moveCall?.module && moveCall?.function ? `${moveCall.package}::${moveCall.module}::${moveCall.function}` : void 0,
268
- typeArguments: moveCall?.typeArguments
269
- };
270
- });
271
- const objectInputs = (data.inputs ?? []).flatMap((input) => input.$kind === "UnresolvedObject" && input.UnresolvedObject?.objectId ? [input.UnresolvedObject.objectId] : []);
272
- return {
273
- sender: data.sender ?? null,
274
- gasBudget: data.gasData?.budget != null ? String(data.gasData.budget) : null,
275
- gasPrice: data.gasData?.price != null ? String(data.gasData.price) : null,
276
- commandCount: commands.length,
277
- commands,
278
- inputs: (data.inputs ?? []).length,
279
- objectInputs,
280
- game: context.game,
281
- coinType: context.coinType ? normalizeCoinTypeKey(context.coinType) : void 0,
282
- stake: context.stake != null ? String(toBigInt(context.stake)) : void 0
283
- };
284
- };
285
- //#endregion
286
- export { buildCoinflipTransaction, buildLimboTransaction, buildPlinkoTransaction, buildPvpCoinflipCancelTransaction, buildPvpCoinflipCreateTransaction, buildPvpCoinflipJoinTransaction, buildRangeTransaction, buildWheelTransaction, summarizeTransaction };