Files
SHiNE-server/shine-UI/js/pages/topup-view.js
T

137 lines
5.2 KiB
JavaScript

import { t, th } from '../i18n/index.js';
import { createTopBar } from '../components/topbar.js';
import { showToast } from '../services/channels-ux.js';
import { state } from '../state.js';
import {
formatSol,
getBalanceSol,
getTopupSiteUrl,
} from '../services/solana-wallet-service.js';
import { loadSolanaWeb3 } from '../vendor/solana-web3-loader.js';
import { actionErrorText } from '../services/ui-error-texts.js';
export const pageMeta = { id: 'topup-view', get title() { return t('screens.topup_view.text_001'); }, showAppChrome: false };
const DEVNET_ENDPOINT = 'https://api.devnet.solana.com';
const REGISTRATION_TOPUP_AMOUNT_SOL = 0.02;
// Канонический Solana-кошелёк пользователя = текущий blockchain key.
// Тот же адрес используется как fee payer для регистрации и обычных PDA update.
async function blockchainWalletAddressFromBundle() {
const keyBundle = state.registrationDraft.preGeneratedKeyBundle;
if (!keyBundle || !keyBundle.blockchainPair) {
throw new Error(t('screens.topup_view.text_013'));
}
const raw = atob(keyBundle.blockchainPair.publicKeyB64);
const bytes = new Uint8Array(raw.length);
for (let i = 0; i < raw.length; i += 1) bytes[i] = raw.charCodeAt(i);
const { PublicKey } = await loadSolanaWeb3();
return new PublicKey(bytes).toBase58();
}
export function render({ navigate }) {
const screen = document.createElement('section');
screen.className = 'stack';
const walletValue = document.createElement('input');
walletValue.className = 'input';
walletValue.type = 'text';
walletValue.value = state.registrationPayment.walletAddress || '';
walletValue.readOnly = true;
walletValue.style.fontSize = '13px';
const status = document.createElement('p');
status.className = 'meta-muted';
status.textContent = t('screens.topup_view.text_002');
status.style.whiteSpace = 'pre-wrap';
status.style.overflowWrap = 'anywhere';
status.style.wordBreak = 'break-word';
status.style.fontSize = '13px';
const copyButton = document.createElement('button');
copyButton.className = 'ghost-btn';
copyButton.type = 'button';
copyButton.textContent = t('screens.topup_view.text_003');
copyButton.addEventListener('click', async () => {
try {
await navigator.clipboard.writeText(walletValue.value);
copyButton.textContent = t('screens.topup_view.text_004');
window.setTimeout(() => {
copyButton.textContent = t('screens.topup_view.text_005');
}, 1500);
} catch {
showToast(t('screens.topup_view.text_011'), { kind: 'error' });
}
});
const walletRow = document.createElement('div');
walletRow.className = 'inline-input-row';
walletRow.append(walletValue, copyButton);
const card = document.createElement('div');
card.className = 'card stack';
card.innerHTML = `
<p class="auth-copy">${th('screens.topup_view.text_015')}</p>
<div class="card stack" style="padding:12px; max-width:320px;">
<div class="field-label" style="margin-bottom:6px;">${th('screens.topup_view.text_016')}</div>
</div>
<div class="stack" style="gap:6px;">
<p class="meta-muted">${th('screens.topup_view.text_017')}</p>
<a class="link-card" id="topup-site-link" href="${getTopupSiteUrl(state.registrationPayment.walletAddress || '')}" target="_blank" rel="noreferrer">${th('screens.topup_view.text_018')}</a>
</div>
`;
card.children[1].append(walletRow);
const testButton = document.createElement('button');
testButton.className = 'ghost-btn';
testButton.type = 'button';
testButton.textContent = t('screens.topup_view.text_006');
testButton.textContent = t('screens.topup_view.text_007');
testButton.addEventListener('click', () => {
status.textContent = t('screens.topup_view.text_008');
});
const backButton = document.createElement('button');
backButton.className = 'primary-btn';
backButton.type = 'button';
backButton.textContent = t('screens.topup_view.text_009');
backButton.addEventListener('click', () => navigate('registration-payment-view'));
const actions = document.createElement('div');
actions.className = 'auth-footer-actions';
actions.append(testButton, backButton);
(async () => {
try {
if (!walletValue.value) {
const address = await blockchainWalletAddressFromBundle();
state.registrationPayment.walletAddress = address;
walletValue.value = address;
}
const topupSiteLink = card.querySelector('#topup-site-link');
if (topupSiteLink instanceof HTMLAnchorElement) {
topupSiteLink.href = getTopupSiteUrl(walletValue.value);
}
const balance = await getBalanceSol({
endpoint: state.entrySettings.solanaServer,
address: walletValue.value,
});
state.registrationPayment.balanceSOL = String(balance.sol);
status.textContent = t('screens.topup_view.text_014', { value1: formatSol(balance.sol, 6) });
} catch (error) {
status.textContent = actionErrorText(t('screens.topup_view.text_012'), error);
}
})();
screen.append(
createTopBar({
title: t('screens.topup_view.text_010'),
back: { label: '←', onClick: () => navigate('registration-payment-view') },
}),
card,
status,
actions,
);
return screen;
}