-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathstackspot.ts
More file actions
362 lines (324 loc) · 10.8 KB
/
stackspot.ts
File metadata and controls
362 lines (324 loc) · 10.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
#!/usr/bin/env bun
/**
* Stackspot skill CLI
* Stacking lottery pots on stackspot.app — pool STX into pots that stack via PoX,
* VRF picks a random winner for sBTC rewards, all participants get their STX back.
*
* Usage: bun run stackspot/stackspot.ts <subcommand> [options]
*/
import { Command } from "commander";
import { NETWORK, getExplorerTxUrl } from "../src/lib/config/networks.js";
import { getAccount } from "../src/lib/services/x402.service.js";
import { callContract } from "../src/lib/transactions/builder.js";
import { printJson, handleError } from "../src/lib/utils/cli.js";
import {
uintCV,
contractPrincipalCV,
PostConditionMode,
} from "@stacks/transactions";
import {
PLATFORM_ADDRESS,
PLATFORM_CONTRACT,
KNOWN_POTS,
parseContractName,
callPotReadOnly,
} from "../src/lib/utils/stackspot-shared.js";
const SKILL_NAME = "stackspot";
const program = new Command();
program
.name(SKILL_NAME)
.description(
"Stacking lottery pots on stackspot.app — pool STX into pots that stack via PoX, " +
"VRF picks a random winner for sBTC rewards, all participants get their STX back. Mainnet-only."
)
.version("0.1.0");
// ---------------------------------------------------------------------------
// list-pots
// ---------------------------------------------------------------------------
program
.command("list-pots")
.description(
"List all known stackspot pot contracts with their current on-chain value and lock status."
)
.action(async () => {
try {
if (NETWORK !== "mainnet") {
throw new Error(
`${SKILL_NAME} skill is mainnet-only. Set NETWORK=mainnet to use this skill.`
);
}
const pots = await Promise.all(
KNOWN_POTS.map(async (pot) => {
let currentValueUstx: unknown = null;
let isLocked: unknown = null;
try {
currentValueUstx = await callPotReadOnly(
pot.contractName,
"get-pot-value",
[]
);
} catch {
// pot may not be deployed on current network — skip gracefully
}
try {
isLocked = await callPotReadOnly(pot.contractName, "is-locked", []);
} catch {
// same
}
return {
name: pot.name,
contract: `${pot.deployer}.${pot.contractName}`,
maxParticipants: pot.maxParticipants,
minAmountStx: pot.minAmountStx,
currentValueUstx,
isLocked,
};
})
);
printJson({
network: NETWORK,
potCount: pots.length,
pots,
});
} catch (error) {
handleError(error);
}
});
// ---------------------------------------------------------------------------
// get-pot-state
// ---------------------------------------------------------------------------
program
.command("get-pot-state")
.description(
"Get full on-chain state for a pot: value, lock status, configs, pool config, and details."
)
.requiredOption(
"--contract-name <name>",
"Pot contract name or full identifier (e.g., SPT4SQP5RC1BFAJEQKBHZMXQ8NQ7G118F335BD85.STXLFG or STXLFG)"
)
.action(async (opts: { contractName: string }) => {
try {
if (NETWORK !== "mainnet") {
throw new Error(
`${SKILL_NAME} skill is mainnet-only. Set NETWORK=mainnet to use this skill.`
);
}
const parsed = parseContractName(opts.contractName);
const contractId = `${parsed.deployer}.${parsed.contractName}`;
const [potValue, isLocked, configs, poolConfig, details] =
await Promise.all([
callPotReadOnly(opts.contractName, "get-pot-value", []),
callPotReadOnly(opts.contractName, "is-locked", []),
callPotReadOnly(opts.contractName, "get-configs", []),
callPotReadOnly(opts.contractName, "get-pool-config", []),
callPotReadOnly(opts.contractName, "get-pot-details", []),
]);
printJson({
network: NETWORK,
contractName: parsed.contractName,
contractId,
state: {
potValueUstx: potValue,
isLocked,
configs,
poolConfig,
details,
},
});
} catch (error) {
handleError(error);
}
});
// ---------------------------------------------------------------------------
// join-pot
// ---------------------------------------------------------------------------
program
.command("join-pot")
.description(
"Contribute STX to a pot. STX is locked until the stacking cycle completes. " +
"Requires an unlocked wallet. Mainnet-only."
)
.requiredOption(
"--contract-name <name>",
"Pot name or full identifier (e.g., SPT4SQP5RC1BFAJEQKBHZMXQ8NQ7G118F335BD85.STXLFG or STXLFG)"
)
.requiredOption(
"--amount <microStx>",
"Amount to contribute in micro-STX (1 STX = 1,000,000 micro-STX)"
)
.action(async (opts: { contractName: string; amount: string }) => {
try {
if (NETWORK !== "mainnet") {
throw new Error(
`${SKILL_NAME} skill is mainnet-only. Set NETWORK=mainnet to use this skill.`
);
}
const amount = BigInt(opts.amount);
if (amount <= 0n) {
throw new Error("--amount must be a positive integer in micro-STX");
}
const parsed = parseContractName(opts.contractName);
const knownPot = KNOWN_POTS.find(
(p) => p.contractName === parsed.contractName
);
if (knownPot) {
const minUstx = BigInt(knownPot.minAmountStx) * 1_000_000n;
if (amount < minUstx) {
throw new Error(
`--amount ${opts.amount} is below the minimum for ${parsed.contractName}: ` +
`${minUstx} micro-STX (${knownPot.minAmountStx} STX)`
);
}
}
const account = await getAccount();
const result = await callContract(account, {
contractAddress: parsed.deployer,
contractName: parsed.contractName,
functionName: "join-pot",
functionArgs: [uintCV(amount)],
postConditionMode: PostConditionMode.Allow,
});
printJson({
success: true,
txid: result.txid,
network: NETWORK,
explorerUrl: getExplorerTxUrl(result.txid, NETWORK),
pot: {
contractName: parsed.contractName,
amountUstx: opts.amount,
},
});
} catch (error) {
handleError(error);
}
});
// ---------------------------------------------------------------------------
// start-pot
// ---------------------------------------------------------------------------
program
.command("start-pot")
.description(
"Trigger a full pot to begin stacking via the platform contract. " +
"Must be called during the PoX prepare phase. Requires an unlocked wallet. Mainnet-only."
)
.requiredOption(
"--contract-name <name>",
"Pot name or full identifier to start stacking"
)
.action(async (opts: { contractName: string }) => {
try {
if (NETWORK !== "mainnet") {
throw new Error(
`${SKILL_NAME} skill is mainnet-only. Set NETWORK=mainnet to use this skill.`
);
}
const parsed = parseContractName(opts.contractName);
const account = await getAccount();
const result = await callContract(account, {
contractAddress: PLATFORM_ADDRESS,
contractName: PLATFORM_CONTRACT,
functionName: "start-stackspot-jackpot",
functionArgs: [contractPrincipalCV(parsed.deployer, parsed.contractName)],
postConditionMode: PostConditionMode.Allow,
});
printJson({
success: true,
txid: result.txid,
network: NETWORK,
explorerUrl: getExplorerTxUrl(result.txid, NETWORK),
pot: {
contractName: parsed.contractName,
},
});
} catch (error) {
handleError(error);
}
});
// ---------------------------------------------------------------------------
// claim-rewards
// ---------------------------------------------------------------------------
program
.command("claim-rewards")
.description(
"Claim sBTC rewards from a completed pot. Only the VRF-selected winner receives sBTC; " +
"all participants recover their STX. Requires an unlocked wallet. Mainnet-only."
)
.requiredOption(
"--contract-name <name>",
"Pot name or full identifier to claim rewards from"
)
.action(async (opts: { contractName: string }) => {
try {
if (NETWORK !== "mainnet") {
throw new Error(
`${SKILL_NAME} skill is mainnet-only. Set NETWORK=mainnet to use this skill.`
);
}
const parsed = parseContractName(opts.contractName);
const account = await getAccount();
const result = await callContract(account, {
contractAddress: parsed.deployer,
contractName: parsed.contractName,
functionName: "claim-pot-reward",
functionArgs: [contractPrincipalCV(parsed.deployer, parsed.contractName)],
postConditionMode: PostConditionMode.Allow,
});
printJson({
success: true,
txid: result.txid,
network: NETWORK,
explorerUrl: getExplorerTxUrl(result.txid, NETWORK),
pot: {
contractName: parsed.contractName,
},
});
} catch (error) {
handleError(error);
}
});
// ---------------------------------------------------------------------------
// cancel-pot
// ---------------------------------------------------------------------------
program
.command("cancel-pot")
.description(
"Cancel a pot before stacking begins to recover contributed STX. " +
"The pot must not be locked. Requires an unlocked wallet. Mainnet-only."
)
.requiredOption(
"--contract-name <name>",
"Pot name or full identifier to cancel"
)
.action(async (opts: { contractName: string }) => {
try {
if (NETWORK !== "mainnet") {
throw new Error(
`${SKILL_NAME} skill is mainnet-only. Set NETWORK=mainnet to use this skill.`
);
}
const parsed = parseContractName(opts.contractName);
const account = await getAccount();
const result = await callContract(account, {
contractAddress: parsed.deployer,
contractName: parsed.contractName,
functionName: "cancel-pot",
functionArgs: [contractPrincipalCV(parsed.deployer, parsed.contractName)],
postConditionMode: PostConditionMode.Allow,
});
printJson({
success: true,
txid: result.txid,
network: NETWORK,
explorerUrl: getExplorerTxUrl(result.txid, NETWORK),
pot: {
contractName: parsed.contractName,
},
});
} catch (error) {
handleError(error);
}
});
// ---------------------------------------------------------------------------
// Parse
// ---------------------------------------------------------------------------
program.parse(process.argv);