File size: 5,525 Bytes
e14bacb |
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 |
const axios = require("axios");
const crypto = require("crypto");
const Asset = require("../models/Asset");
const User = require("../models/User");
const Transaction = require("../models/Transaction");
const StrikeHistory = require("../models/StrikeHistory");
const { sendGlobal, sendToUser } = require('../utils/sendPush');
const WIN_PCT = {
Silver: [0.30, 0.15, 0.08],
Gold: [0.40, 0.20, 0.10],
Platinum: [0.50, 0.25, 0.125]
};
const circularDiff = (a, b, mod) => {
const diff = Math.abs(a - b);
return Math.min(diff, mod - diff);
};
const secureShuffle = (arr) => {
for (let i = arr.length - 1; i > 0; i--) {
const j = crypto.randomInt(0, i + 1);
[arr[i], arr[j]] = [arr[j], arr[i]];
}
};
const sha256 = (payload) =>
crypto.createHash("sha256").update(JSON.stringify(payload)).digest("hex");
async function runStrike({ triggeredBy = "CRON" } = {}) {
if (global.isStrikeRunning) return;
global.isStrikeRunning = true;
global.lastStrikeStatus = "RUNNING";
global.lastStrikeResult = null;
try {
// β prevent double strike per day
const startOfDay = new Date();
startOfDay.setUTCHours(0, 0, 0, 0);
const already = await StrikeHistory.findOne({
date: { $gte: startOfDay }
});
if (already) {
global.lastStrikeStatus = "COMPLETED";
return;
}
// π START EVENT
await sendGlobal(
"β‘ Strike Started!",
triggeredBy === "ADMIN"
? "Admin manually triggered today's strike."
: "The 8 PM Strike is LIVE.",
"/"
);
// 1οΈβ£ BTC PRICE
const { data } = await axios.get(
"https://api.binance.com/api/v3/ticker/price?symbol=BTCUSDT"
);
const price = parseFloat(data.price).toFixed(2);
const btcInt = price.split(".")[0];
const btcLast3 = parseInt(btcInt.slice(-3));
const btcLast6 = parseInt(btcInt.slice(-6));
global.liveStrikeData = {
price,
btcLast3,
btcLast6
};
// 2οΈβ£ POOL
const txs = await Transaction.find({
timestamp: { $gte: startOfDay },
status: "Verified"
});
const totalPool = txs.reduce((s, t) => s + t.amount, 0);
if (!totalPool) {
global.lastStrikeStatus = "CANCELLED";
return;
}
// 3οΈβ£ ASSETS
const assets = await Asset.find({
purchase_date: { $gte: startOfDay },
status: "Held"
}).populate("owner_id");
// 4οΈβ£ LOW PARTICIPATION β CANCEL
if (assets.length <= 3) {
const prices = { Silver: 500, Gold: 1000, Platinum: 2500 };
for (const a of assets) {
await User.findByIdAndUpdate(a.owner_id._id, {
$inc: { wallet_balance: prices[a.tier] }
});
a.status = "Burned";
await a.save();
}
global.lastStrikeStatus = "CANCELLED";
global.lastStrikeResult = { cancelled: true };
await sendGlobal(
"π« Strike Cancelled",
"Low participation. Refunds issued.",
"/profile"
);
return;
}
// 5οΈβ£ RANKING
let ranked = assets.map(asset => {
const kusLast6 = parseInt(asset.kus_id.slice(-6));
const kusLast3 = kusLast6 % 1000;
return {
asset,
diff3: circularDiff(kusLast3, btcLast3, 1000),
diff6: circularDiff(kusLast6, btcLast6, 1_000_000)
};
});
ranked.sort((a, b) => {
if (a.diff3 !== b.diff3) return a.diff3 - b.diff3;
if (a.diff6 !== b.diff6) return a.diff6 - b.diff6;
return 0;
});
secureShuffle(ranked.slice(0, 3));
// 6οΈβ£ WINNERS
const historyWinners = [];
for (let i = 0; i < 3; i++) {
const { asset } = ranked[i];
const pct = WIN_PCT[asset.tier][i];
if (!pct) continue;
const gross = totalPool * pct;
const net = gross * (1 - 0.312);
await User.findByIdAndUpdate(asset.owner_id._id, {
$inc: { wallet_balance: net }
});
await Transaction.create({
user_id: asset.owner_id._id,
amount: net,
status: "Verified",
tier: asset.tier,
utr: `STRIKE-${Date.now()}`
});
historyWinners.push({
tier: asset.tier,
user_id: asset.owner_id._id,
amount: net,
rank: i + 1,
kus_id: asset.kus_id
});
await sendToUser(
asset.owner_id._id,
"π YOU WON!",
`Rank #${i + 1} in today's strike.`,
"/profile"
);
}
// 7οΈβ£ AUDIT
const auditPayload = {
price,
btcLast3,
btcLast6,
pool: totalPool,
participants: assets.map(a => a.kus_id),
winners: historyWinners,
triggeredBy
};
const auditHash = sha256(auditPayload);
await StrikeHistory.create({
target_price: price,
winning_digits: btcLast3,
total_pool: totalPool,
audit_hash: auditHash,
audit_payload: auditPayload,
winners: historyWinners
});
// β
FINAL GLOBAL RESULT (frontend reads this)
global.lastStrikeStatus = "COMPLETED";
global.lastStrikeResult = {
cancelled: false,
winningDigits: btcLast3,
winners: historyWinners
};
await sendGlobal(
"π Results Announced",
"Strike completed successfully.",
"/"
);
} catch (err) {
console.error("β Strike failed:", err);
global.lastStrikeStatus = "FAILED";
} finally {
global.isStrikeRunning = false;
global.liveStrikeData = null;
console.log("β‘ STRIKE ENDED β‘");
}
}
module.exports = { runStrike };
|