SHA256
Перевести донаты на Stars и обновить Devnet
This commit is contained in:
@@ -4,8 +4,9 @@ import { readShineUserPda, supportShineUserOnSolana, supportShineDaoOnSolana } f
|
||||
import { loadEncryptedUserSecrets } from '../services/key-vault.js';
|
||||
import { getWalletFromStoredBlockchainKey, transferSolLamports, solanaAddressFromPublicKeyBase64 } from '../services/solana-wallet-service.js';
|
||||
import { bytesToBase58 } from '../services/crypto-utils.js';
|
||||
import { parseSolLamports, formatLamportsSol } from '../services/shine-support-amount.js';
|
||||
import { parseStarCents, formatStars, splitStarCents, starCentsToLamports, formatLamportsSol } from '../services/shine-support-amount.js';
|
||||
import { toUserMessage } from '../services/ui-error-texts.js';
|
||||
import { getStarsQuoteFromRpc } from '../services/shine-stars-pyth.js';
|
||||
|
||||
/** One shared payment dialog for profiles, posts and donations to the SHiNE economy. */
|
||||
export function openShineSupportDialog({ recipientLogin = '', target = { type: 0 }, onSuccess = () => {} } = {}) {
|
||||
@@ -33,7 +34,7 @@ export function openShineSupportDialog({ recipientLogin = '', target = { type: 0
|
||||
input.type = 'text';
|
||||
input.inputMode = 'decimal';
|
||||
input.autocomplete = 'off';
|
||||
input.placeholder = '0.01';
|
||||
input.placeholder = '0.10';
|
||||
const details = document.createElement('p');
|
||||
details.className = 'meta-muted';
|
||||
const status = document.createElement('p');
|
||||
@@ -52,38 +53,45 @@ export function openShineSupportDialog({ recipientLogin = '', target = { type: 0
|
||||
cancel.className = 'text-btn';
|
||||
cancel.textContent = t('screens.support_phase2.close');
|
||||
panel.append(header, description, amountLabel, input, details, status, donate);
|
||||
if (!dao) panel.append(direct);
|
||||
// Direct transfers bypass Stars accounting and must not appear in the support dialog.
|
||||
panel.append(cancel);
|
||||
overlay.append(panel);
|
||||
document.body.append(overlay);
|
||||
let quote = null;
|
||||
let busy = false;
|
||||
let closed = false;
|
||||
const close = () => { if (busy) return; closed = true; overlay.remove(); };
|
||||
cancel.addEventListener('click', close);
|
||||
overlay.addEventListener('click', (event) => { if (event.target === overlay) close(); });
|
||||
const update = () => {
|
||||
if (dao) { details.textContent = t('screens.support_phase2.dao_info'); return; }
|
||||
try {
|
||||
const amount = parseSolLamports(input.value);
|
||||
details.textContent = amount % 10n === 0n
|
||||
? `${t('screens.support_phase2.recipient')}: ${formatLamportsSol(amount * 9n / 10n)} SOL · ${t('screens.support_phase2.fund')}: ${formatLamportsSol(amount / 10n)} SOL`
|
||||
: t('screens.support_phase2.divisible');
|
||||
} catch { details.textContent = t('screens.support_phase2.explanation'); }
|
||||
const cents = parseStarCents(input.value);
|
||||
const split = splitStarCents(cents);
|
||||
const sol = quote ? ` · ≈ ${formatLamportsSol(starCentsToLamports(cents, quote.solUsdE8, quote.usdtUsdE8))} SOL` : '';
|
||||
details.textContent = dao ? `${formatStars(cents)}${sol}` :
|
||||
`${t('screens.support_phase2.recipient')}: ${formatStars(split.author)} · ${t('screens.support_phase2.fund')}: ${formatStars(split.dao)}${sol}`;
|
||||
} catch { details.textContent = 'Минимум 0,10 ⭐'; }
|
||||
};
|
||||
input.addEventListener('input', update);
|
||||
amountLabel.textContent = 'Сумма поддержки, ⭐';
|
||||
update();
|
||||
void getStarsQuoteFromRpc({ solanaEndpoint: state.entrySettings.solanaServer }).then((value) => { quote = value; if (!closed) update(); })
|
||||
.catch(() => { if (!closed) details.textContent = 'Курс недоступен. Повторите попытку позднее.'; });
|
||||
const send = async (isDirect) => {
|
||||
if (busy || closed) return;
|
||||
let amount;
|
||||
let amount, cents;
|
||||
try {
|
||||
amount = parseSolLamports(input.value);
|
||||
if (!dao && !isDirect && amount % 10n !== 0n) throw new Error(t('screens.support_phase2.divisible'));
|
||||
cents = parseStarCents(input.value);
|
||||
if (isDirect) throw new Error('Прямой перевод не учитывается в Stars');
|
||||
} catch (error) { status.textContent = error.message; return; }
|
||||
if (isDirect && !window.confirm(t('screens.support_phase2.direct_warning'))) return;
|
||||
busy = true;
|
||||
donate.disabled = direct.disabled = cancel.disabled = input.disabled = true;
|
||||
status.textContent = t('screens.support_phase2.sending');
|
||||
try {
|
||||
// Fetch a fresh quote immediately before signing; Pyth on-chain check is authoritative.
|
||||
quote = await getStarsQuoteFromRpc({ solanaEndpoint: state.entrySettings.solanaServer });
|
||||
amount = starCentsToLamports(cents, quote.solUsdE8, quote.usdtUsdE8);
|
||||
const { storagePwdInMemory: password } = state.session;
|
||||
const endpoint = state.entrySettings.solanaServer;
|
||||
let result;
|
||||
@@ -102,7 +110,9 @@ export function openShineSupportDialog({ recipientLogin = '', target = { type: 0
|
||||
} else {
|
||||
const secrets = await loadEncryptedUserSecrets(login, password);
|
||||
if (!secrets?.blockchainKey || !secrets?.clientKey) throw new Error(t('screens.support_phase2.keys_required'));
|
||||
const args = { senderLogin: login, amountLamports: amount, solanaEndpoint: endpoint, blockchainPrivatePkcs8B64: secrets.blockchainKey, clientPrivatePkcs8B64: secrets.clientKey };
|
||||
const args = { senderLogin: login, amountLamports: amount, starCents: cents,
|
||||
oracleAccounts: { solUsd: quote.solOracleAccount, usdtUsd: quote.usdtOracleAccount },
|
||||
solanaEndpoint: endpoint, blockchainPrivatePkcs8B64: secrets.blockchainKey, clientPrivatePkcs8B64: secrets.clientKey };
|
||||
result = dao ? await supportShineDaoOnSolana(args) : await supportShineUserOnSolana({ ...args, recipientLogin, target });
|
||||
}
|
||||
status.textContent = `${t('screens.support_phase2.success')} ${result.signature || ''}`;
|
||||
|
||||
@@ -1,12 +1,13 @@
|
||||
import { t } from '../i18n/index.js';
|
||||
import { authService } from '../state.js';
|
||||
import { formatLamportsSol } from '../services/shine-support-amount.js';
|
||||
import { getSolanaCluster } from '../services/solana-rpc-gateway.js';
|
||||
import { formatStars } from '../services/shine-support-amount.js';
|
||||
|
||||
export function supportStatsValues(stats = {}) {
|
||||
const sent = BigInt(stats.supportSent ?? 0);
|
||||
const received = BigInt(stats.supportReceived ?? 0);
|
||||
const daoDirect = BigInt(stats.daoDirect ?? 0);
|
||||
return [sent, received, sent / 10n + daoDirect];
|
||||
return [sent, received, daoDirect];
|
||||
}
|
||||
|
||||
/** Adds an independent asynchronous block; failure/Free accounts do not get fictitious statistics. */
|
||||
@@ -20,7 +21,87 @@ export function attachSupportStats(parent, { login, isActive = () => true }) {
|
||||
content.className = 'meta-muted';
|
||||
content.textContent = t('screens.support_phase2.loading');
|
||||
section.append(title, content);
|
||||
const history = document.createElement('details');
|
||||
history.className = 'profile-support-history';
|
||||
const historySummary = document.createElement('summary');
|
||||
historySummary.textContent = t('screens.support_phase2.history');
|
||||
const historyRows = document.createElement('div');
|
||||
historyRows.className = 'stack';
|
||||
historyRows.style.cssText = 'gap:8px;margin-top:10px;';
|
||||
history.append(historySummary, historyRows);
|
||||
section.append(history);
|
||||
parent.append(section);
|
||||
let historyOffset = 0;
|
||||
let loadingHistory = false;
|
||||
let historyLoaded = false;
|
||||
const historyLogin = String(login || '').trim().toLowerCase();
|
||||
const loadHistory = async () => {
|
||||
if (loadingHistory || !isActive() || !section.isConnected) return;
|
||||
loadingHistory = true;
|
||||
const note = document.createElement('div');
|
||||
note.className = 'meta-muted';
|
||||
note.textContent = t('screens.support_phase2.loading');
|
||||
historyRows.append(note);
|
||||
try {
|
||||
const result = await authService.getSupportHistory(login, 20, historyOffset);
|
||||
if (!isActive() || !section.isConnected) return;
|
||||
note.remove();
|
||||
if (!result.ready) {
|
||||
historyRows.textContent = t('screens.support_phase2.loading');
|
||||
return;
|
||||
}
|
||||
if (!historyOffset && !result.transfers?.length) {
|
||||
historyRows.textContent = t('screens.support_phase2.no_history');
|
||||
}
|
||||
const more = historyRows.querySelector('[data-stars-more]');
|
||||
more?.remove();
|
||||
for (const transfer of result.transfers || []) {
|
||||
const outgoing = transfer.senderLogin?.trim().toLowerCase() === historyLogin;
|
||||
const cents = transfer.kind === 'DAO' || outgoing
|
||||
? transfer.amountStarCents : transfer.recipientStarCents;
|
||||
const row = document.createElement('div');
|
||||
row.className = 'row';
|
||||
row.style.cssText = 'display:flex;flex-wrap:wrap;align-items:center;justify-content:space-between;gap:6px;';
|
||||
const info = document.createElement('span');
|
||||
info.textContent = transfer.kind === 'DAO'
|
||||
? t('screens.support_phase2.history_dao')
|
||||
: `${outgoing ? '→' : '←'} ${outgoing ? transfer.recipientLogin : transfer.senderLogin}`;
|
||||
const value = document.createElement('strong');
|
||||
value.textContent = `${outgoing ? '−' : '+'}${formatStars(cents)}`;
|
||||
const detail = document.createElement('small');
|
||||
detail.className = 'meta-muted';
|
||||
const date = transfer.blockTime ? new Date(Number(transfer.blockTime) * 1000).toLocaleString() : '';
|
||||
const lamports = BigInt(transfer.amountLamports || '0');
|
||||
const sol = `${lamports / 1_000_000_000n}.${String(lamports % 1_000_000_000n).padStart(9, '0').replace(/0+$/, '') || '0'} SOL`;
|
||||
detail.textContent = `${date} · ${sol}`;
|
||||
const explorer = document.createElement('a');
|
||||
const sig = String(transfer.signature || '');
|
||||
if (/^[1-9A-HJ-NP-Za-km-z]{64,90}$/.test(sig)) {
|
||||
const network = getSolanaCluster();
|
||||
explorer.href = `https://explorer.solana.com/tx/${sig}${network && network !== 'mainnet-beta' ? `?cluster=${encodeURIComponent(network)}` : ''}`;
|
||||
explorer.target = '_blank'; explorer.rel = 'noopener noreferrer';
|
||||
explorer.textContent = t('screens.support_phase2.transaction');
|
||||
row.append(info, value, detail, explorer);
|
||||
} else row.append(info, value, detail);
|
||||
historyRows.append(row);
|
||||
}
|
||||
historyOffset += (result.transfers || []).length;
|
||||
if (result.hasMore) {
|
||||
const btn = document.createElement('button');
|
||||
btn.className = 'text-btn'; btn.type = 'button';
|
||||
btn.dataset.starsMore = '1';
|
||||
btn.textContent = t('screens.support_phase2.more');
|
||||
btn.addEventListener('click', () => void loadHistory());
|
||||
historyRows.append(btn);
|
||||
}
|
||||
historyLoaded = true;
|
||||
} catch {
|
||||
if (isActive() && section.isConnected) note.textContent = t('screens.support_phase2.history_unavailable');
|
||||
} finally { loadingHistory = false; }
|
||||
};
|
||||
history.addEventListener('toggle', () => {
|
||||
if (history.open && !historyLoaded) void loadHistory();
|
||||
});
|
||||
let pendingRefresh = null;
|
||||
const scheduleRetry = () => {
|
||||
if (pendingRefresh !== null) return;
|
||||
@@ -45,9 +126,9 @@ export function attachSupportStats(parent, { login, isActive = () => true }) {
|
||||
return;
|
||||
}
|
||||
const values = supportStatsValues({
|
||||
supportSent: stats.supportSentLamports,
|
||||
supportReceived: stats.supportReceivedLamports,
|
||||
daoDirect: stats.daoDirectLamports,
|
||||
supportSent: stats.supportSentStarCents,
|
||||
supportReceived: stats.supportReceivedStarCents,
|
||||
daoDirect: stats.daoDirectStarCents,
|
||||
});
|
||||
content.replaceChildren();
|
||||
content.className = 'stack';
|
||||
@@ -58,7 +139,7 @@ export function attachSupportStats(parent, { login, isActive = () => true }) {
|
||||
const name = document.createElement('span');
|
||||
const amount = document.createElement('strong');
|
||||
name.textContent = label;
|
||||
amount.textContent = `${formatLamportsSol(values[idx])} SOL`;
|
||||
amount.textContent = formatStars(values[idx]);
|
||||
row.append(name, amount);
|
||||
content.append(row);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user