@suigar/mcp 0.1.0 → 0.2.0-beta.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 (77) hide show
  1. package/CHANGELOG.md +37 -0
  2. package/LICENSE +201 -0
  3. package/README.md +79 -110
  4. package/dist/app/index.html +181 -0
  5. package/dist/bin.mjs +3 -7
  6. package/dist/bin.mjs.map +1 -0
  7. package/dist/client.d.mts +41 -11
  8. package/dist/client.d.mts.map +1 -0
  9. package/dist/client.mjs +107 -34
  10. package/dist/client.mjs.map +1 -0
  11. package/dist/dry-run.mjs +163 -0
  12. package/dist/dry-run.mjs.map +1 -0
  13. package/dist/format.mjs +24 -0
  14. package/dist/format.mjs.map +1 -0
  15. package/dist/index.d.mts +5 -9
  16. package/dist/index.mjs +4 -8
  17. package/dist/schemas.d.mts +642 -0
  18. package/dist/schemas.d.mts.map +1 -0
  19. package/dist/schemas.mjs +113 -0
  20. package/dist/schemas.mjs.map +1 -0
  21. package/dist/server.d.mts +21 -69
  22. package/dist/server.d.mts.map +1 -0
  23. package/dist/server.mjs +172 -411
  24. package/dist/server.mjs.map +1 -0
  25. package/dist/tools.d.mts +14 -155
  26. package/dist/tools.d.mts.map +1 -0
  27. package/dist/tools.mjs +297 -553
  28. package/dist/tools.mjs.map +1 -0
  29. package/dist/types.d.mts +109 -83
  30. package/dist/types.d.mts.map +1 -0
  31. package/package.json +83 -61
  32. package/dist/bin.cjs +0 -11
  33. package/dist/bin.d.cts +0 -1
  34. package/dist/client.cjs +0 -46
  35. package/dist/client.d.cts +0 -17
  36. package/dist/coin.cjs +0 -86
  37. package/dist/coin.d.cts +0 -35
  38. package/dist/coin.d.mts +0 -35
  39. package/dist/coin.mjs +0 -86
  40. package/dist/config.cjs +0 -183
  41. package/dist/config.d.cts +0 -15
  42. package/dist/config.d.mts +0 -15
  43. package/dist/config.mjs +0 -174
  44. package/dist/index.cjs +0 -53
  45. package/dist/index.d.cts +0 -10
  46. package/dist/mcp-support.cjs +0 -62
  47. package/dist/mcp-support.d.cts +0 -16
  48. package/dist/mcp-support.d.mts +0 -16
  49. package/dist/mcp-support.mjs +0 -60
  50. package/dist/metadata.cjs +0 -51
  51. package/dist/metadata.d.cts +0 -52
  52. package/dist/metadata.d.mts +0 -52
  53. package/dist/metadata.mjs +0 -47
  54. package/dist/server.cjs +0 -433
  55. package/dist/server.d.cts +0 -73
  56. package/dist/tools.cjs +0 -617
  57. package/dist/tools.d.cts +0 -158
  58. package/dist/transactions.cjs +0 -294
  59. package/dist/transactions.d.cts +0 -40
  60. package/dist/transactions.d.mts +0 -40
  61. package/dist/transactions.mjs +0 -286
  62. package/dist/types.d.cts +0 -111
  63. package/node_modules/@suigar/currency-registry/dist/index.cjs +0 -121
  64. package/node_modules/@suigar/currency-registry/dist/index.d.cts +0 -50
  65. package/node_modules/@suigar/currency-registry/dist/index.d.mts +0 -50
  66. package/node_modules/@suigar/currency-registry/dist/index.mjs +0 -110
  67. package/node_modules/@suigar/currency-registry/package.json +0 -31
  68. package/node_modules/@suigar/game-registry/dist/index.cjs +0 -310
  69. package/node_modules/@suigar/game-registry/dist/index.d.cts +0 -65
  70. package/node_modules/@suigar/game-registry/dist/index.d.mts +0 -65
  71. package/node_modules/@suigar/game-registry/dist/index.mjs +0 -292
  72. package/node_modules/@suigar/game-registry/package.json +0 -31
  73. package/node_modules/@suigar/sui-rpc-pool/dist/index.cjs +0 -45590
  74. package/node_modules/@suigar/sui-rpc-pool/dist/index.d.cts +0 -465
  75. package/node_modules/@suigar/sui-rpc-pool/dist/index.d.mts +0 -465
  76. package/node_modules/@suigar/sui-rpc-pool/dist/index.mjs +0 -45570
  77. package/node_modules/@suigar/sui-rpc-pool/package.json +0 -31
@@ -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 };
package/dist/types.d.cts DELETED
@@ -1,111 +0,0 @@
1
- import { GameMcpSupport, SuigarMcpToolName } from "./mcp-support.cjs";
2
- import { ConfiguredCurrencyEntry } from "@suigar/currency-registry";
3
- import { GameDefinition } from "@suigar/game-registry";
4
- import { SuiCompatClient } from "@suigar/sui-rpc-pool";
5
-
6
- //#region src/types.d.ts
7
- type SuigarNetwork = 'mainnet' | 'testnet' | 'devnet' | 'localnet' | (string & {});
8
- type BetMetadataPrimitive = string | number | boolean | bigint;
9
- type BetMetadataInputValue = BetMetadataPrimitive | Uint8Array | number[];
10
- type BetMetadataInput = Record<string, BetMetadataInputValue>;
11
- type CoinSide = 'heads' | 'tails';
12
- type BuilderMode = 'build' | 'dry-run' | 'read-only';
13
- type CasinoBetGameId = 'coinflip' | 'limbo' | 'plinko' | 'wheel' | 'range';
14
- type SupportedGameId = CasinoBetGameId | 'pvp-coinflip';
15
- type SuigarConfig = {
16
- network: SuigarNetwork;
17
- providerUrl: string;
18
- graphqlUrl: string;
19
- siteUrl: string;
20
- suigarPackageId: string;
21
- coinflipPackageId: string;
22
- pvpCoinflipPackageId: string;
23
- plinkoPackageId: string;
24
- limboPackageId: string;
25
- rangePackageId: string;
26
- wheelPackageId: string;
27
- sweethouseId: string;
28
- suiCoinType: string;
29
- usdcCoinType: string;
30
- suiPythPriceInfoObjectId: string;
31
- usdcPythPriceInfoObjectId: string;
32
- coinflipSettingsId: string;
33
- pvpCoinflipSettingsId: string;
34
- plinkoSettingsId: string;
35
- limboSettingsId: string;
36
- rangeSettingsId: string;
37
- wheelSettingsId: string;
38
- pvpCoinflipRegistryId: string;
39
- };
40
- type SuigarConfigInput = Partial<SuigarConfig> & {
41
- network?: SuigarNetwork;
42
- providerUrl?: string;
43
- graphqlUrl?: string;
44
- config?: Partial<SuigarConfig>;
45
- };
46
- type ConfigInspection = {
47
- config: SuigarConfig;
48
- configuredCurrencies: ConfiguredCurrencyEntry[];
49
- availableGames: Array<GameDefinition & {
50
- mcpSupport: GameMcpSupport;
51
- }>;
52
- missingValues: Array<keyof SuigarConfig>;
53
- mcp: {
54
- serverName: string;
55
- supportedToolNames: SuigarMcpToolName[];
56
- };
57
- };
58
- type CoinReadClient = Pick<SuiCompatClient, 'getCoins' | 'getReferenceGasPrice'>;
59
- type BetCoinSource = {
60
- kind: 'gas';
61
- split?: boolean;
62
- } | {
63
- kind: 'object-ids';
64
- objectIds: string[];
65
- split?: boolean;
66
- };
67
- type SharedBuildOptions = {
68
- config?: SuigarConfigInput;
69
- client?: CoinReadClient;
70
- owner: string;
71
- coinType: string;
72
- stake: number | bigint;
73
- cashStake?: number | bigint;
74
- betCount?: number | bigint;
75
- metadata?: BetMetadataInput;
76
- partner?: string;
77
- gasBudget?: number | bigint;
78
- coinSource?: BetCoinSource;
79
- sender?: string;
80
- allowGasCoinShortcut?: boolean;
81
- };
82
- type TransactionSummary = {
83
- sender: string | null;
84
- gasBudget: string | null;
85
- gasPrice: string | null;
86
- commandCount: number;
87
- commands: Array<{
88
- kind: string;
89
- target?: string;
90
- typeArguments?: string[];
91
- }>;
92
- inputs: number;
93
- objectInputs: string[];
94
- game?: SupportedGameId;
95
- coinType?: string;
96
- stake?: string;
97
- };
98
- type ReadOnlyPlan = {
99
- mode: 'read-only';
100
- network: SuigarNetwork;
101
- game: SupportedGameId;
102
- config: ConfigInspection;
103
- plan: {
104
- target: string | null;
105
- typeArguments: string[];
106
- requiredConfigKeys: Array<keyof SuigarConfig>;
107
- notes: string[];
108
- };
109
- };
110
- //#endregion
111
- export { BetCoinSource, BetMetadataInput, BetMetadataInputValue, BetMetadataPrimitive, BuilderMode, CasinoBetGameId, CoinReadClient, CoinSide, ConfigInspection, ReadOnlyPlan, SharedBuildOptions, SuigarConfig, SuigarConfigInput, SuigarNetwork, SupportedGameId, TransactionSummary };
@@ -1,121 +0,0 @@
1
- Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
- //#region src/index.ts
3
- const CURRENCY_FAMILIES = [{
4
- id: "sui",
5
- symbol: "SUI",
6
- name: "Sui",
7
- decimals: 9,
8
- suffixes: ["::sui::sui", "::test_sui::test_sui"]
9
- }, {
10
- id: "usdc",
11
- symbol: "USDC",
12
- name: "USDC",
13
- decimals: 6,
14
- suffixes: ["::usdc::usdc", "::test_usdc::test_usdc"]
15
- }];
16
- const CONFIGURED_CURRENCY_SPECS = [{
17
- id: "sui",
18
- familyId: "sui",
19
- symbol: "SUI",
20
- name: "Sui",
21
- configKey: "suiCoinType"
22
- }, {
23
- id: "usdc",
24
- familyId: "usdc",
25
- symbol: "USDC",
26
- name: "USDC",
27
- configKey: "usdcCoinType"
28
- }];
29
- const DEFAULT_COIN_SYMBOL = "COIN";
30
- const DEFAULT_COIN_NAME = "Coin";
31
- const DEFAULT_COIN_DECIMALS = 9;
32
- const trimCoinType = (coinType) => typeof coinType === "string" ? coinType.trim() : "";
33
- const normalizeCoinType = (coinType) => {
34
- if (!coinType) return coinType;
35
- const parts = coinType.split("::");
36
- if (parts.length < 3) return `0x${((coinType.startsWith("0x") ? coinType.slice(2) : coinType).replace(/^0+/, "") || "0").toLowerCase()}`;
37
- const [rawAddress, module, ...rest] = parts;
38
- return [
39
- `0x${((rawAddress.startsWith("0x") ? rawAddress.slice(2) : rawAddress).replace(/^0+/, "") || "0").toLowerCase()}`,
40
- module,
41
- ...rest
42
- ].join("::");
43
- };
44
- const normalizeCoinTypeKey = (coinType) => {
45
- const trimmed = trimCoinType(coinType);
46
- return trimmed ? normalizeCoinType(trimmed).toLowerCase() : "";
47
- };
48
- const resolveFallbackSymbol = (coinType, fallback = DEFAULT_COIN_SYMBOL) => {
49
- const trimmed = trimCoinType(coinType);
50
- if (!trimmed) return fallback;
51
- const parts = trimmed.split("::").filter(Boolean);
52
- return (parts[parts.length - 1] ?? trimmed).replace(/\s+/g, "_").trim().toUpperCase() || fallback;
53
- };
54
- const dedupeCurrenciesByType = (currencies) => {
55
- const seen = /* @__PURE__ */ new Set();
56
- return currencies.filter((currency) => {
57
- const normalizedType = normalizeCoinTypeKey(currency.type);
58
- if (seen.has(normalizedType)) return false;
59
- seen.add(normalizedType);
60
- return true;
61
- });
62
- };
63
- const resolveCoinFamily = (coinType) => {
64
- const normalized = normalizeCoinTypeKey(coinType);
65
- if (!normalized) return null;
66
- for (const family of CURRENCY_FAMILIES) if (family.suffixes.some((suffix) => normalized.endsWith(suffix))) return family;
67
- return null;
68
- };
69
- const buildConfiguredCurrencies = (config) => dedupeCurrenciesByType(CONFIGURED_CURRENCY_SPECS.flatMap((spec) => {
70
- const type = trimCoinType(config[spec.configKey]);
71
- if (!type) return [];
72
- return [{
73
- id: spec.id,
74
- familyId: spec.familyId,
75
- symbol: spec.symbol,
76
- name: spec.name,
77
- type,
78
- normalizedType: normalizeCoinType(type),
79
- decimals: CURRENCY_FAMILIES.find((family) => family.id === spec.familyId)?.decimals ?? DEFAULT_COIN_DECIMALS
80
- }];
81
- }));
82
- const resolveConfiguredCurrency = (coinType, configuredCurrencies) => {
83
- const normalized = normalizeCoinTypeKey(coinType);
84
- if (!normalized) return null;
85
- return configuredCurrencies.find((currency) => normalizeCoinTypeKey(currency.type) === normalized) ?? null;
86
- };
87
- const resolveCurrencyMetadata = (coinType, configuredCurrencies = [], fallbacks = {}) => {
88
- const configuredCurrency = resolveConfiguredCurrency(coinType, configuredCurrencies);
89
- if (configuredCurrency) return {
90
- symbol: configuredCurrency.symbol,
91
- name: configuredCurrency.name,
92
- decimals: configuredCurrency.decimals
93
- };
94
- const family = resolveCoinFamily(coinType);
95
- if (family) return {
96
- symbol: family.symbol,
97
- name: family.name,
98
- decimals: family.decimals
99
- };
100
- const fallbackSymbol = resolveFallbackSymbol(coinType, fallbacks.symbol ?? DEFAULT_COIN_SYMBOL);
101
- return {
102
- symbol: fallbackSymbol,
103
- name: fallbacks.name ?? fallbackSymbol ?? DEFAULT_COIN_NAME,
104
- decimals: fallbacks.decimals ?? DEFAULT_COIN_DECIMALS
105
- };
106
- };
107
- const resolveCoinDecimals = (coinType, fallback = DEFAULT_COIN_DECIMALS) => resolveCurrencyMetadata(coinType, [], { decimals: fallback }).decimals;
108
- const resolveCoinSymbol = (coinType, fallback = DEFAULT_COIN_SYMBOL) => resolveCurrencyMetadata(coinType, [], { symbol: fallback }).symbol;
109
- const createCurrencyDecimalsMap = (currencies) => Object.fromEntries(currencies.map((currency) => [normalizeCoinTypeKey(currency.type), currency.decimals]).filter(([type]) => type.length > 0));
110
- //#endregion
111
- exports.CURRENCY_FAMILIES = CURRENCY_FAMILIES;
112
- exports.buildConfiguredCurrencies = buildConfiguredCurrencies;
113
- exports.createCurrencyDecimalsMap = createCurrencyDecimalsMap;
114
- exports.dedupeCurrenciesByType = dedupeCurrenciesByType;
115
- exports.normalizeCoinType = normalizeCoinType;
116
- exports.normalizeCoinTypeKey = normalizeCoinTypeKey;
117
- exports.resolveCoinDecimals = resolveCoinDecimals;
118
- exports.resolveCoinFamily = resolveCoinFamily;
119
- exports.resolveCoinSymbol = resolveCoinSymbol;
120
- exports.resolveConfiguredCurrency = resolveConfiguredCurrency;
121
- exports.resolveCurrencyMetadata = resolveCurrencyMetadata;
@@ -1,50 +0,0 @@
1
- //#region src/index.d.ts
2
- type CurrencyFamilyId = 'sui' | 'usdc';
3
- type CurrencyFamilyDefinition = {
4
- id: CurrencyFamilyId;
5
- symbol: string;
6
- name: string;
7
- decimals: number;
8
- suffixes: readonly string[];
9
- };
10
- type ConfiguredCurrencyId = 'sui' | 'usdc';
11
- type ConfiguredCurrencyEntry = {
12
- id: ConfiguredCurrencyId;
13
- familyId: CurrencyFamilyId;
14
- symbol: string;
15
- name: string;
16
- type: string;
17
- normalizedType: string;
18
- decimals: number;
19
- };
20
- type CurrencyConfigInput = {
21
- suiCoinType?: string | null;
22
- usdcCoinType?: string | null;
23
- };
24
- type CurrencyLike = {
25
- type: string;
26
- };
27
- type CurrencyMetadataLike = CurrencyLike & {
28
- symbol: string;
29
- name: string;
30
- decimals: number;
31
- };
32
- declare const CURRENCY_FAMILIES: readonly CurrencyFamilyDefinition[];
33
- declare const normalizeCoinType: (coinType: string) => string;
34
- declare const normalizeCoinTypeKey: (coinType?: string | null) => string;
35
- declare const dedupeCurrenciesByType: <T extends CurrencyLike>(currencies: readonly T[]) => T[];
36
- declare const resolveCoinFamily: (coinType?: string | null) => CurrencyFamilyDefinition | null;
37
- declare const buildConfiguredCurrencies: (config: CurrencyConfigInput) => ConfiguredCurrencyEntry[];
38
- declare const resolveConfiguredCurrency: <T extends CurrencyLike>(coinType: string | null | undefined, configuredCurrencies: readonly T[]) => T | null;
39
- declare const resolveCurrencyMetadata: <T extends CurrencyMetadataLike>(coinType?: string | null, configuredCurrencies?: readonly T[], fallbacks?: Partial<Omit<CurrencyMetadataLike, "type">>) => {
40
- symbol: string;
41
- name: string;
42
- decimals: number;
43
- };
44
- declare const resolveCoinDecimals: (coinType?: string | null, fallback?: number) => number;
45
- declare const resolveCoinSymbol: (coinType?: string | null, fallback?: string) => string;
46
- declare const createCurrencyDecimalsMap: <T extends Pick<CurrencyMetadataLike, "type" | "decimals">>(currencies: readonly T[]) => {
47
- [k: string]: number;
48
- };
49
- //#endregion
50
- export { CURRENCY_FAMILIES, ConfiguredCurrencyEntry, ConfiguredCurrencyId, CurrencyConfigInput, CurrencyFamilyDefinition, CurrencyFamilyId, CurrencyLike, CurrencyMetadataLike, buildConfiguredCurrencies, createCurrencyDecimalsMap, dedupeCurrenciesByType, normalizeCoinType, normalizeCoinTypeKey, resolveCoinDecimals, resolveCoinFamily, resolveCoinSymbol, resolveConfiguredCurrency, resolveCurrencyMetadata };
@@ -1,50 +0,0 @@
1
- //#region src/index.d.ts
2
- type CurrencyFamilyId = 'sui' | 'usdc';
3
- type CurrencyFamilyDefinition = {
4
- id: CurrencyFamilyId;
5
- symbol: string;
6
- name: string;
7
- decimals: number;
8
- suffixes: readonly string[];
9
- };
10
- type ConfiguredCurrencyId = 'sui' | 'usdc';
11
- type ConfiguredCurrencyEntry = {
12
- id: ConfiguredCurrencyId;
13
- familyId: CurrencyFamilyId;
14
- symbol: string;
15
- name: string;
16
- type: string;
17
- normalizedType: string;
18
- decimals: number;
19
- };
20
- type CurrencyConfigInput = {
21
- suiCoinType?: string | null;
22
- usdcCoinType?: string | null;
23
- };
24
- type CurrencyLike = {
25
- type: string;
26
- };
27
- type CurrencyMetadataLike = CurrencyLike & {
28
- symbol: string;
29
- name: string;
30
- decimals: number;
31
- };
32
- declare const CURRENCY_FAMILIES: readonly CurrencyFamilyDefinition[];
33
- declare const normalizeCoinType: (coinType: string) => string;
34
- declare const normalizeCoinTypeKey: (coinType?: string | null) => string;
35
- declare const dedupeCurrenciesByType: <T extends CurrencyLike>(currencies: readonly T[]) => T[];
36
- declare const resolveCoinFamily: (coinType?: string | null) => CurrencyFamilyDefinition | null;
37
- declare const buildConfiguredCurrencies: (config: CurrencyConfigInput) => ConfiguredCurrencyEntry[];
38
- declare const resolveConfiguredCurrency: <T extends CurrencyLike>(coinType: string | null | undefined, configuredCurrencies: readonly T[]) => T | null;
39
- declare const resolveCurrencyMetadata: <T extends CurrencyMetadataLike>(coinType?: string | null, configuredCurrencies?: readonly T[], fallbacks?: Partial<Omit<CurrencyMetadataLike, "type">>) => {
40
- symbol: string;
41
- name: string;
42
- decimals: number;
43
- };
44
- declare const resolveCoinDecimals: (coinType?: string | null, fallback?: number) => number;
45
- declare const resolveCoinSymbol: (coinType?: string | null, fallback?: string) => string;
46
- declare const createCurrencyDecimalsMap: <T extends Pick<CurrencyMetadataLike, "type" | "decimals">>(currencies: readonly T[]) => {
47
- [k: string]: number;
48
- };
49
- //#endregion
50
- export { CURRENCY_FAMILIES, ConfiguredCurrencyEntry, ConfiguredCurrencyId, CurrencyConfigInput, CurrencyFamilyDefinition, CurrencyFamilyId, CurrencyLike, CurrencyMetadataLike, buildConfiguredCurrencies, createCurrencyDecimalsMap, dedupeCurrenciesByType, normalizeCoinType, normalizeCoinTypeKey, resolveCoinDecimals, resolveCoinFamily, resolveCoinSymbol, resolveConfiguredCurrency, resolveCurrencyMetadata };