dice-roll-sdk 1.0.2 → 1.0.4

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.
@@ -0,0 +1,17 @@
1
+ import type { DiceRollConfig, LeaderEntry, ReadOnlyResponse, RollCall } from "./types";
2
+ export declare const DEFAULT_CONFIG: Required<DiceRollConfig>;
3
+ export declare function callReadOnly(functionName: string, args?: string[], config?: DiceRollConfig): Promise<ReadOnlyResponse>;
4
+ export declare function getTotalRolls(config?: DiceRollConfig): Promise<number>;
5
+ export declare function getUserRolls(userAddress: string, config?: DiceRollConfig): Promise<number>;
6
+ export declare function getUserLastResult(userAddress: string, config?: DiceRollConfig): Promise<number>;
7
+ export declare function getLeaderboard(config?: DiceRollConfig): Promise<LeaderEntry[]>;
8
+ export declare function createRollCall(config?: DiceRollConfig): RollCall;
9
+ export declare class DiceRollClient {
10
+ private readonly config;
11
+ constructor(config?: DiceRollConfig);
12
+ getTotalRolls(): Promise<number>;
13
+ getUserRolls(userAddress: string): Promise<number>;
14
+ getUserLastResult(userAddress: string): Promise<number>;
15
+ getLeaderboard(): Promise<LeaderEntry[]>;
16
+ createRollCall(): RollCall;
17
+ }
package/dist/client.js ADDED
@@ -0,0 +1,136 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DiceRollClient = exports.DEFAULT_CONFIG = void 0;
4
+ exports.callReadOnly = callReadOnly;
5
+ exports.getTotalRolls = getTotalRolls;
6
+ exports.getUserRolls = getUserRolls;
7
+ exports.getUserLastResult = getUserLastResult;
8
+ exports.getLeaderboard = getLeaderboard;
9
+ exports.createRollCall = createRollCall;
10
+ const network_1 = require("@stacks/network");
11
+ const transactions_1 = require("@stacks/transactions");
12
+ exports.DEFAULT_CONFIG = {
13
+ contractAddress: "SP1Q7YR67R6WGP28NXDJD1WZ11REPAAXRJJ3V6RKM",
14
+ contractName: "dice-roll",
15
+ apiBase: "https://api.mainnet.hiro.so",
16
+ network: network_1.STACKS_MAINNET,
17
+ };
18
+ function resolveConfig(overrides = {}) {
19
+ return { ...exports.DEFAULT_CONFIG, ...overrides };
20
+ }
21
+ function serializeCvToHex(cv) {
22
+ const serialized = (0, transactions_1.serializeCV)(cv);
23
+ if (typeof serialized === "string") {
24
+ return serialized.startsWith("0x") ? serialized : `0x${serialized}`;
25
+ }
26
+ return `0x${Buffer.from(serialized).toString("hex")}`;
27
+ }
28
+ async function callReadOnly(functionName, args = [], config = {}) {
29
+ const resolved = resolveConfig(config);
30
+ const response = await fetch(`${resolved.apiBase}/v2/contracts/call-read/${resolved.contractAddress}/${resolved.contractName}/${functionName}`, {
31
+ method: "POST",
32
+ headers: { "Content-Type": "application/json" },
33
+ body: JSON.stringify({
34
+ sender: resolved.contractAddress,
35
+ arguments: args,
36
+ }),
37
+ });
38
+ if (!response.ok) {
39
+ throw new Error(`Read-only call failed with status ${response.status}`);
40
+ }
41
+ return response.json();
42
+ }
43
+ function normalizeLeaderboardValue(raw) {
44
+ const entries = Array.isArray(raw) ? raw : [];
45
+ return entries
46
+ .map(item => {
47
+ const entry = item && typeof item === "object" && "value" in item
48
+ ? item.value
49
+ : item;
50
+ const record = entry;
51
+ return {
52
+ who: String(record?.who && typeof record.who === "object" ? record.who.value ?? "" : record?.who ?? ""),
53
+ rolls: Number(record?.rolls && typeof record.rolls === "object"
54
+ ? record.rolls.value ?? 0
55
+ : record?.rolls ?? 0),
56
+ };
57
+ })
58
+ .filter(entry => entry.who && entry.rolls > 0);
59
+ }
60
+ async function getTotalRolls(config = {}) {
61
+ const data = await callReadOnly("get-total-rolls", [], config);
62
+ if (!data.okay || !data.result) {
63
+ return 0;
64
+ }
65
+ const clarityValue = (0, transactions_1.hexToCV)(data.result);
66
+ const parsed = (0, transactions_1.cvToValue)(clarityValue, true);
67
+ return Number(parsed && typeof parsed === "object" && "value" in parsed
68
+ ? parsed.value ?? 0
69
+ : parsed ?? 0);
70
+ }
71
+ async function getUserRolls(userAddress, config = {}) {
72
+ const principalArg = serializeCvToHex((0, transactions_1.principalCV)(userAddress));
73
+ const data = await callReadOnly("get-user-rolls", [principalArg], config);
74
+ if (!data.okay || !data.result) {
75
+ return 0;
76
+ }
77
+ const clarityValue = (0, transactions_1.hexToCV)(data.result);
78
+ const parsed = (0, transactions_1.cvToValue)(clarityValue, true);
79
+ return Number(parsed && typeof parsed === "object" && "value" in parsed
80
+ ? parsed.value ?? 0
81
+ : parsed ?? 0);
82
+ }
83
+ async function getUserLastResult(userAddress, config = {}) {
84
+ const principalArg = serializeCvToHex((0, transactions_1.principalCV)(userAddress));
85
+ const data = await callReadOnly("get-user-last-result", [principalArg], config);
86
+ if (!data.okay || !data.result) {
87
+ return 0;
88
+ }
89
+ const clarityValue = (0, transactions_1.hexToCV)(data.result);
90
+ const parsed = (0, transactions_1.cvToValue)(clarityValue, true);
91
+ return Number(parsed && typeof parsed === "object" && "value" in parsed
92
+ ? parsed.value ?? 0
93
+ : parsed ?? 0);
94
+ }
95
+ async function getLeaderboard(config = {}) {
96
+ const data = await callReadOnly("get-leaderboard", [], config);
97
+ if (!data.okay || !data.result) {
98
+ return [];
99
+ }
100
+ const clarityValue = (0, transactions_1.hexToCV)(data.result);
101
+ const parsed = (0, transactions_1.cvToValue)(clarityValue, true);
102
+ return normalizeLeaderboardValue(parsed);
103
+ }
104
+ function createRollCall(config = {}) {
105
+ const resolved = resolveConfig(config);
106
+ return {
107
+ contractAddress: resolved.contractAddress,
108
+ contractName: resolved.contractName,
109
+ functionName: "roll",
110
+ functionArgs: [],
111
+ postConditionMode: transactions_1.PostConditionMode.Deny,
112
+ postConditions: [],
113
+ network: resolved.network,
114
+ };
115
+ }
116
+ class DiceRollClient {
117
+ constructor(config = {}) {
118
+ this.config = resolveConfig(config);
119
+ }
120
+ getTotalRolls() {
121
+ return getTotalRolls(this.config);
122
+ }
123
+ getUserRolls(userAddress) {
124
+ return getUserRolls(userAddress, this.config);
125
+ }
126
+ getUserLastResult(userAddress) {
127
+ return getUserLastResult(userAddress, this.config);
128
+ }
129
+ getLeaderboard() {
130
+ return getLeaderboard(this.config);
131
+ }
132
+ createRollCall() {
133
+ return createRollCall(this.config);
134
+ }
135
+ }
136
+ exports.DiceRollClient = DiceRollClient;
@@ -0,0 +1,4 @@
1
+ import type { DiceRollClient } from "./client";
2
+ export { DiceRollClient, DEFAULT_CONFIG, callReadOnly, createRollCall, getLeaderboard, getTotalRolls, getUserRolls, getUserLastResult, } from "./client";
3
+ export type { DiceRollConfig, LeaderEntry, ReadOnlyResponse, RollCall, } from "./types";
4
+ export type TotalRollsResult = Awaited<ReturnType<DiceRollClient["getTotalRolls"]>>;
package/dist/index.js ADDED
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getUserLastResult = exports.getUserRolls = exports.getTotalRolls = exports.getLeaderboard = exports.createRollCall = exports.callReadOnly = exports.DEFAULT_CONFIG = exports.DiceRollClient = void 0;
4
+ var client_1 = require("./client");
5
+ Object.defineProperty(exports, "DiceRollClient", { enumerable: true, get: function () { return client_1.DiceRollClient; } });
6
+ Object.defineProperty(exports, "DEFAULT_CONFIG", { enumerable: true, get: function () { return client_1.DEFAULT_CONFIG; } });
7
+ Object.defineProperty(exports, "callReadOnly", { enumerable: true, get: function () { return client_1.callReadOnly; } });
8
+ Object.defineProperty(exports, "createRollCall", { enumerable: true, get: function () { return client_1.createRollCall; } });
9
+ Object.defineProperty(exports, "getLeaderboard", { enumerable: true, get: function () { return client_1.getLeaderboard; } });
10
+ Object.defineProperty(exports, "getTotalRolls", { enumerable: true, get: function () { return client_1.getTotalRolls; } });
11
+ Object.defineProperty(exports, "getUserRolls", { enumerable: true, get: function () { return client_1.getUserRolls; } });
12
+ Object.defineProperty(exports, "getUserLastResult", { enumerable: true, get: function () { return client_1.getUserLastResult; } });
@@ -0,0 +1,26 @@
1
+ import type { StacksNetwork } from "@stacks/network";
2
+ import type { PostConditionMode } from "@stacks/transactions";
3
+ export interface DiceRollConfig {
4
+ contractAddress?: string;
5
+ contractName?: string;
6
+ apiBase?: string;
7
+ network?: StacksNetwork;
8
+ }
9
+ export interface LeaderEntry {
10
+ who: string;
11
+ rolls: number;
12
+ }
13
+ export interface ReadOnlyResponse {
14
+ okay?: boolean;
15
+ result?: string;
16
+ cause?: string;
17
+ }
18
+ export interface RollCall {
19
+ contractAddress: string;
20
+ contractName: string;
21
+ functionName: "roll";
22
+ functionArgs: [];
23
+ postConditionMode: PostConditionMode;
24
+ postConditions: [];
25
+ network: StacksNetwork;
26
+ }
package/dist/types.js ADDED
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dice-roll-sdk",
3
- "version": "1.0.2",
3
+ "version": "1.0.4",
4
4
  "description": "TypeScript SDK for interacting with the Dice Roll smart contract on Stacks blockchain",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",