fix: распаковать источник из обёртки smart-updown в корень ветки

This commit is contained in:
2026-09-02 15:36:21 +03:00
parent a9d54feab6
commit 5cf3a7a8d1
35 changed files with 0 additions and 0 deletions
+136
View File
@@ -0,0 +1,136 @@
import { LAMPORTS_PER_SOL, PublicKey } from "@solana/web3.js";
import { Program } from "@anchor-lang/core";
import { loadConfig, loadKeypair, SIDE_UP } from "./config";
import { getPrice } from "./priceSource";
import { nextBetRequest } from "./frontStub";
import {
initialize,
createBet,
closeBet,
readGlobal,
ensureVaultFunded,
} from "./ops";
import { betPda } from "./pda";
import { makeConnection, makeProgram } from "./program";
import type { SmartUpdown } from "../idl/smart_updown";
const LAMPORTS_PER_SOL_NUM = Number(LAMPORTS_PER_SOL);
const sleep = (ms: number) => new Promise((r) => setTimeout(r, ms));
const STATUS_OPEN = 0;
async function fundBettor(connection: any, bettorPubkey: PublicKey): Promise<void> {
const bal = await connection.getBalance(bettorPubkey);
if (bal < 2 * LAMPORTS_PER_SOL_NUM) {
await connection.requestAirdrop(bettorPubkey, 5 * LAMPORTS_PER_SOL_NUM);
await sleep(800);
console.log(`bettor funded ${bettorPubkey.toBase58()} (+5 SOL)`);
}
}
/** Закрывает ставку, повторяя при «equal price» (exit==entry) пока не зафиналится. */
async function settleBet(
program: Program<SmartUpdown>,
id: any,
entryPrice: number,
relayer: any,
bettor: PublicKey
): Promise<void> {
for (let attempt = 1; ; attempt++) {
const exitPrice = await getPrice();
if (exitPrice === entryPrice) {
console.log(`attempt ${attempt}: exit==entry (${exitPrice}), жду движение...`);
await sleep(2000);
continue;
}
const sig = await closeBet(program, id, exitPrice, relayer, bettor);
console.log(`close_bet #${id} exit=${exitPrice} (attempt ${attempt}) tx=${sig}`);
const status = await getStatus(program, id);
if (status === STATUS_OPEN) {
console.log(`ставка ещё open (equal price), повторяю close...`);
await sleep(2000);
continue;
}
return;
}
}
async function getStatus(program: Program<SmartUpdown>, id: any): Promise<number> {
const bet = await program.account.bet.fetchNullable(betPda(program.programId, id));
return bet ? bet.status.toNumber() : -1;
}
async function main(): Promise<void> {
const cfg = loadConfig();
const admin = loadKeypair(cfg.adminKeypairPath);
const connection = makeConnection(cfg.rpcUrl);
const program = makeProgram(connection, admin);
console.log(`RPC: ${cfg.rpcUrl}`);
console.log(`Admin/relayer: ${admin.publicKey.toBase58()}`);
const global = await readGlobal(program, program.programId);
if (!global) {
if (!process.argv.includes("--init")) {
throw new Error(
"Контракт не инициализирован. Запусти с флагом --init (или init+run)."
);
}
await initialize(program, {
minAmount: cfg.minAmountLamports,
maxAmount: cfg.maxAmountLamports,
multiplierBps: cfg.multiplierBps,
expirySeconds: cfg.expirySeconds,
}, admin);
console.log("Контракт инициализирован.");
} else {
console.log(
`Global: counter=${global.counter} vault=${(global.lamports / LAMPORTS_PER_SOL_NUM).toFixed(2)} SOL ` +
`expiry=${global.expirySeconds}s paused=${global.paused}`
);
}
if (cfg.allowAirdrop) {
await ensureVaultFunded(connection, program, 10 * LAMPORTS_PER_SOL_NUM);
}
const loops = process.argv.includes("--once") ? 1 : Infinity;
let n = 0;
while (n < loops) {
n++;
const req = nextBetRequest(cfg.minAmountLamports, cfg.maxAmountLamports);
await fundBettor(connection, req.bettor.publicKey);
const entryPrice = await getPrice();
await createBet(program, req.bettor, {
side: req.side,
amount: req.amountLamports,
entryPrice,
});
console.log(
`ставка ${req.side === SIDE_UP ? "UP" : "DOWN"} на ${(req.amountLamports / LAMPORTS_PER_SOL_NUM).toFixed(3)} SOL, жду ${cfg.expirySeconds}s...`
);
await sleep((cfg.expirySeconds + 1) * 1000);
const g0 = await readGlobal(program, program.programId);
const id = g0!.counter.subn(1);
await settleBet(program, id, entryPrice, admin, req.bettor.publicKey);
const outcome = await program.account.bet.fetchNullable(betPda(program.programId, id));
console.log(
outcome!.status.toNumber() === 1
? `>> результат #${id}: ПОЛЬЗОВАТЕЛЬ ВЫИГРАЛ (payout 1.3x)`
: `>> результат #${id}: ВЫИГРАЛ ДОМ (user lost)`
);
}
console.log("Готово.");
}
main().catch((e) => {
console.error(e);
process.exit(1);
});