diff --git a/qortal-ui-plugins/plugins/core/become-minter/become-minter.src.js b/qortal-ui-plugins/plugins/core/become-minter/become-minter.src.js new file mode 100644 index 00000000..5ebc646c --- /dev/null +++ b/qortal-ui-plugins/plugins/core/become-minter/become-minter.src.js @@ -0,0 +1,355 @@ +import { LitElement, html } from 'lit'; +import { Epml } from '../../../epml.js'; +import '../components/ButtonIconCopy.js'; +import { use, translate, registerTranslateConfig } from 'lit-translate'; + +registerTranslateConfig({ + loader: (lang) => fetch(`/language/${lang}.json`).then((res) => res.json()), +}); +import '@polymer/paper-spinner/paper-spinner-lite.js'; + +import '@material/mwc-button'; +import '@material/mwc-textfield'; +import '@vaadin/button'; +import { _blocksNeed } from './utils/blocks-needed.js'; +import { pageStyles } from './become-minter-css.js'; +import './components/not-sponsored'; +const parentEpml = new Epml({ type: 'WINDOW', source: window.parent }); + +class BecomeMinter extends LitElement { + static get properties() { + return { + theme: { type: String, reflect: true }, + isLoadingSponsorshipKeySubmit: { type: Boolean }, + sponsorshipKeyValue: { type: String }, + addMintingAccountMessage: { type: String }, + + nodeInfo: { type: Object }, + isPageLoading: { type: Boolean }, + addressInfo: { type: Object }, + rewardSharePublicKey: { type: String }, + }; + } + + static styles = [pageStyles]; + + constructor() { + super(); + this.theme = localStorage.getItem('qortalTheme') + ? localStorage.getItem('qortalTheme') + : 'light'; + this.isLoadingSponsorshipKeySubmit = false; + this.sponsorshipKeyValue = ''; + this.addMintingAccountMessage = ''; + this.isPageLoading = true; + this.nodeInfo = {}; + this.addressInfo = {}; + this.rewardSharePublicKey = ''; + } + + changeLanguage() { + const checkLanguage = localStorage.getItem('qortalLanguage'); + + if (checkLanguage === null || checkLanguage.length === 0) { + localStorage.setItem('qortalLanguage', 'us'); + use('us'); + } else { + use(checkLanguage); + } + } + + _handleStorage() { + const checkLanguage = localStorage.getItem('qortalLanguage'); + const checkTheme = localStorage.getItem('qortalTheme'); + + use(checkLanguage); + + if (checkTheme === 'dark') { + this.theme = 'dark'; + } else { + this.theme = 'light'; + } + document.querySelector('html').setAttribute('theme', this.theme); + } + + connectedCallback() { + super.connectedCallback(); + window.addEventListener('storage', this._handleStorage); + } + + disconnectedCallback() { + window.removeEventListener('storage', this._handleStorage); + super.disconnectedCallback(); + } + + getNodeInfo() { + parentEpml + .request('apiCall', { url: `/admin/status` }) + .then((res) => { + this.nodeInfo = res; + this.isPageLoading = false; + }) + .catch(() => { + this.isPageLoading = false; + }); + } + + async firstUpdated() { + console.log({ change: 6 }); + + this.changeLanguage(); + this.getNodeInfo(); + this.addressInfo = + window.parent.reduxStore.getState().app.accountInfo.addressInfo; + + await this.getRewardShareRelationship( + window.parent.reduxStore.getState().app?.selectedAddress?.address + ); + } + + updateMintingAccounts() { + parentEpml + .request('apiCall', { + url: `/admin/mintingaccounts`, + }) + .then((res) => { + setTimeout(() => (this.mintingAccounts = res), 1); + }); + } + + getApiKey() { + const myNode = + window.parent.reduxStore.getState().app.nodeConfig.knownNodes[ + window.parent.reduxStore.getState().app.nodeConfig.node + ]; + let apiKey = myNode.apiKey; + return apiKey; + } + + renderErr1Text() { + return html`${translate('nodepage.nchange27')}`; + } + + renderErr2Text() { + return html`${translate('nodepage.nchange28')}`; + } + + addMintingAccount(e) { + this.isLoadingSponsorshipKeySubmit = true; + this.addMintingAccountMessage = 'Loading...'; + + parentEpml + .request('apiCall', { + url: `/admin/mintingaccounts?apiKey=${this.getApiKey()}`, + method: 'POST', + body: this.sponsorshipKeyValue, + }) + .then((res) => { + if (res === true) { + this.updateMintingAccounts(); + this.sponsorshipKeyValue = ''; + this.addMintingAccountMessage = this.renderErr1Text(); + this.isLoadingSponsorshipKeySubmit = false; + } else { + this.sponsorshipKeyValue = ''; + this.addMintingAccountMessage = this.renderErr2Text(); + this.isLoadingSponsorshipKeySubmit = false; + } + }); + } + + inputHandler(e) { + this.sponsorshipKeyValue = e.target.value; + } + + async getRewardShareRelationship(recipientAddress) { + let myRewardShareArray = await parentEpml.request('apiCall', { + type: 'api', + url: `/addresses/rewardshares?minters=${recipientAddress}&recipients=${recipientAddress}`, + }); + + this.rewardSharePublicKey = myRewardShareArray[0]?.rewardSharePublicKey; + } + + _levelUpBlocks() { + let countBlocksString = ( + _blocksNeed(0) - + (this.addressInfo?.blocksMinted + + this.addressInfo?.blocksMintedAdjustment) + ).toString(); + return countBlocksString; + } + + render() { + const isAlreadySponsored = + this.addressInfo?.error !== 124 && + this.addressInfo?.level === 0 && + this.rewardSharePublicKey; + + return html` +
Loading....
` : ''} + ${isAlreadySponsored + ? '' + : html` ++ ${this.rewardSharePublicKey} +
+