@ -13,14 +13,27 @@ import "@polymer/paper-spinner/paper-spinner-lite.js"
import "@material/mwc-button"
import "@material/mwc-textfield"
import "@vaadin/button"
import "@material/mwc-button"
import "@polymer/paper-spinner/paper-spinner-lite.js"
import '@material/mwc-dialog'
import { asyncReplace } from 'lit/directives/async-replace.js' ;
import { pageStyles } from "./sponsorship-list-css.src.js"
const parentEpml = new Epml ( { type : "WINDOW" , source : window . parent } )
async function * countDown ( count , callback ) {
while ( count > 0 ) {
yield count -- ;
await new Promise ( ( r ) => setTimeout ( r , 1000 ) ) ;
if ( count === 0 ) {
callback ( )
}
}
}
class SponsorshipList extends LitElement {
static get properties ( ) {
return {
@ -32,11 +45,14 @@ class SponsorshipList extends LitElement {
mintingAccountData : { type : Array } ,
sponsorships : { type : Array } ,
removeRewardShareLoading : { type : Array } ,
createSponsorship Message: { type : String } ,
error Message: { type : String } ,
isLoadingCreateSponsorship : { type : Array } ,
publicKeyValue : { type : String } ,
error : { type : Boolean } ,
isOpenModal : { type : Boolean }
isOpenModal : { type : Boolean } ,
status : { type : Number } ,
privateRewardShareKey : { type : String } ,
timer : { type : Number } ,
openDialogRewardShare : { type : Boolean }
}
}
@ -54,11 +70,14 @@ class SponsorshipList extends LitElement {
this . mintingAccountData = null
this . sponsorships = [ ]
this . removeRewardShareLoading = false
this . error = false
this . createSponsorship Message = ""
this . error Message = ""
this . isLoadingCreateSponsorship = false
this . publicKeyValue = ""
this . isOpenModal = false
this . status = 0
this . privateRewardShareKey = ""
this . openDialogRewardShare = false
}
inputHandler ( e ) {
@ -108,9 +127,27 @@ class SponsorshipList extends LitElement {
return nodeInfo
}
async saveToClipboard ( text ) {
try {
await navigator . clipboard . writeText ( text )
parentEpml . request ( 'showSnackBar' , this . onSuccessMessage )
} catch ( err ) {
parentEpml . request ( 'showSnackBar' , this . onErrorMessage )
console . error ( 'Copy to clipboard error:' , err )
}
}
changeStatus ( value ) {
this . status = value
this . saveToClipboard ( translate (
"walletpage.wchange4"
) )
}
async atMount ( ) {
this . changeLanguage ( )
this . addressInfo =
window . parent . reduxStore . getState ( ) . app . accountInfo . addressInfo
this . isPageLoading = true
@ -119,6 +156,8 @@ class SponsorshipList extends LitElement {
const address =
window . parent . reduxStore . getState ( ) . app ? . selectedAddress
? . address
let rewardShares = await this . getRewardShareRelationship (
address
)
@ -130,7 +169,6 @@ class SponsorshipList extends LitElement {
type : "api" ,
url : ` /addresses/ ${ rs . recipient } ` ,
} )
let blocksRemaining = this . _levelUpBlocks ( addressInfo )
blocksRemaining = + blocksRemaining > 0 ? + blocksRemaining : 0
return {
@ -152,6 +190,7 @@ class SponsorshipList extends LitElement {
if ( openModal ) {
this . shadowRoot . querySelector ( '#showDialog' ) . show ( )
}
} catch ( error ) {
@ -250,18 +289,23 @@ class SponsorshipList extends LitElement {
removeReceiver ( )
}
async createRewardShare ( e ) {
this . error = false
this . createSponsorshipMessage = ""
const recipientPublicKey = this . publicKeyValue
async createRewardShare ( publicKeyValue ) {
this . openDialogRewardShare = true
if ( ! publicKeyValue ) {
this . errorMessage = "unable to pull public key from the chain, account has no outgoing transactions"
return
}
this . privateRewardShareKey = ""
this . errorMessage = ""
const recipientPublicKey = publicKeyValue
const percentageShare = 0
const selectedAddress =
window . parent . reduxStore . getState ( ) . app ? . selectedAddress
// Check for valid...
this . isLoadingCreateSponsorship = true
let recipientAddress =
window . parent . base58PublicKeyToAddress ( recipientPublicKey )
// Get Last Ref
const getLastRef = async ( ) => {
@ -281,96 +325,39 @@ class SponsorshipList extends LitElement {
return myAccountDetails
}
// Get Reward Relationship if it already exists
const getRewardShareRelationship = async ( minterAddr ) => {
let isRewardShareExisting = false
let myRewardShareArray = await parentEpml . request ( "apiCall" , {
type : "api" ,
url : ` /addresses/rewardshares?minters= ${ minterAddr } &recipients= ${ recipientAddress } ` ,
} )
isRewardShareExisting =
myRewardShareArray . length !== 0 ? true : false
return isRewardShareExisting
}
// Validate Reward Share by Level
const validateReceiver = async ( ) => {
let accountDetails = await getAccountDetails ( )
let accountDetails
try {
accountDetails = await getAccountDetails ( )
} catch ( error ) {
this . errorMessage = "Couldn't fetch account details"
}
let lastRef = await getLastRef ( )
let isExisting = await getRewardShareRelationship (
selectedAddress . address
)
// Check for creating self share at different levels (also adding check for flags...)
if ( accountDetails . flags === 1 ) {
this . error = false
this . createSponsorshipMessage = ""
let myTransaction = await makeTransactionRequest ( lastRef )
if ( isExisting === true ) {
this . error = true
this . createSponsorshipMessage = ` Cannot Create Multiple Reward Shares! `
} else {
// Send the transaction for confirmation by the user
this . error = false
this . createSponsorshipMessage = ""
getTxnRequestResponse ( myTransaction )
}
} else if ( accountDetails . address === recipientAddress ) {
if ( accountDetails . level >= 1 && accountDetails . level <= 4 ) {
this . error = false
this . createSponsorshipMessage = ""
let myTransaction = await makeTransactionRequest ( lastRef )
if ( isExisting === true ) {
let err1string = get ( "rewardsharepage.rchange18" )
this . error = true
this . createSponsorshipMessage = ` ${ err1string } `
} else {
// Send the transaction for confirmation by the user
this . error = false
this . createSponsorshipMessage = ""
getTxnRequestResponse ( myTransaction )
}
} else if ( accountDetails . level >= 5 ) {
this . error = false
this . createSponsorshipMessage = ""
let myTransaction = await makeTransactionRequest ( lastRef )
if ( isExisting === true ) {
let err2string = get ( "rewardsharepage.rchange19" )
this . error = true
this . createSponsorshipMessage = ` ${ err2string } `
} else {
// Send the transaction for confirmation by the user
this . error = false
this . createSponsorshipMessage = ""
getTxnRequestResponse ( myTransaction )
}
} else {
let err3string = get ( "rewardsharepage.rchange20" )
this . error = true
this . createSponsorshipMessage = ` ${ err3string } ${ accountDetails . level } `
}
} else {
//Check for creating reward shares
if ( accountDetails . level >= 5 ) {
this . error = false
this . createSponsorshipMessage = ""
let myTransaction = await makeTransactionRequest ( lastRef )
if ( isExisting === true ) {
let err4string = get ( "rewardsharepage.rchange18" )
this . error = true
this . createSponsorshipMessage = ` ${ err4string } `
} else {
// Send the transaction for confirmation by the user
this . error = false
this . createSponsorshipMessage = ""
this . status = 1
this . errorMessage = ""
try {
const myTransaction = await makeTransactionRequest ( lastRef )
getTxnRequestResponse ( myTransaction )
} catch ( error ) {
this . errorMessage = error
}
} else {
this . error = true
let err5string = get ( "rewardsharepage.rchange20" )
this . createSponsorship Message = ` ${ err5string } ${ accountDetails . level } `
this . errorMessage = ` ${ err5string } ${ accountDetails . level } `
}
}
}
// Make Transaction Request
@ -392,35 +379,86 @@ class SponsorshipList extends LitElement {
rewarddialog3 : rewarddialog3 ,
rewarddialog4 : rewarddialog4 ,
} ,
disableModal : true
} )
return myTxnrequest
}
const getTxnRequestResponse = ( txnResponse ) => {
if ( txnResponse . success === false && txnResponse . message ) {
this . error = true
this . createSponsorshipMessage = txnResponse . message
throw new Error ( txnResponse )
if ( txnResponse ? . extraData ? . rewardSharePrivateKey && ( txnResponse ? . data ? . message . includes ( 'multiple' ) || txnResponse ? . data ? . message . includes ( 'SELF_SHARE_EXISTS' ) ) ) {
this . privateRewardShareKey = txnResponse ? . extraData ? . rewardSharePrivateKey
this . confirmRelationship ( publicKeyValue )
} else if ( txnResponse . success === false && txnResponse ? . message ) {
this . errorMessage = txnResponse ? . message
this . isLoadingCreateSponsorship = false
throw ( txnResponse ? . message )
} else if (
txnResponse . success === true &&
! txnResponse . data . error
) {
let err6string = get ( "rewardsharepage.rchange21" )
this . createSponsorshipMessage = err6string
this . error = false
this . privateRewardShareKey = txnResponse ? . extraData ? . rewardSharePrivateKey
this . confirmRelationship ( publicKeyValue )
} else {
this . error = true
this . createSponsorshipMessage = txnResponse . data . message
throw new Error ( txnResponse )
this . errorMessage = txnResponse ? . data ? . message || txnResponse ? . message
this . isLoadingCreateSponsorship = false
throw ( txnResponse ? . data ? . message || txnResponse ? . message )
}
}
validateReceiver ( )
this . isLoadingCreateSponsorship = false
}
render ( ) {
async confirmRelationship ( recipientPublicKey ) {
this . status = 2
let interval = null
let stop = false
const getAnswer = async ( ) => {
if ( ! stop ) {
stop = true ;
try {
const recipientAddress =
window . parent . base58PublicKeyToAddress ( recipientPublicKey )
const minterAddress = window . parent . reduxStore . getState ( ) . app ? . selectedAddress . address
const myRewardShareArray = await parentEpml . request ( "apiCall" , {
type : "api" ,
url : ` /addresses/rewardshares?minters= ${ minterAddress } &recipients= ${ recipientAddress } ` ,
} )
if ( myRewardShareArray . length > 0 ) {
clearInterval ( interval )
this . status = 3
this . timer = countDown ( 180 , ( ) => this . changeStatus ( 4 ) ) ;
}
} catch ( error ) {
console . error ( error )
}
stop = false
}
} ;
interval = setInterval ( getAnswer , 5000 ) ;
}
render ( ) {
return html `
$ {
this . isPageLoading
@ -452,17 +490,17 @@ class SponsorshipList extends LitElement {
< p > $ { translate ( "sponsorshipspage.schange1" ) } < / p >
< / d i v >
< div class = "tableGrid table-header" >
< div class = "grid-item" >
< div class = "grid-item header " >
< p > $ { translate ( "sponsorshipspage.schange2" ) } < / p >
< / d i v >
< div class = "grid-item" >
< div class = "grid-item header " >
< p > $ { translate ( "walletprofile.blocksminted" ) } < / p >
< / d i v >
< div class = "grid-item" >
< div class = "grid-item header " >
< p > $ { translate ( "becomeMinterPage.bchange17" ) } < / p >
< / d i v >
< div class = "grid-item" >
< div class = "grid-item header " >
< / d i v >
< / d i v >
@ -472,33 +510,29 @@ class SponsorshipList extends LitElement {
( sponsorship ) => html `
< ul class = "tableGrid" >
< li class = "grid-item" >
< p class = "grid-item-text" >
Account Address
< / p >
$ { sponsorship . address }
< / l i >
< li class = "grid-item" >
< p class = "grid-item-text" >
Blocks Minted
< / p >
$ { + sponsorship . blocksMinted +
+ sponsorship . blocksMintedAdjustment }
< / l i >
< li class = "grid-item" >
< p class = "grid-item-text" >
Copy Sponsorship Key
< / p >
< mwc - button @ click = $ { ( ) => {
< button - icon - copy
title = " $ { translate (
"becomeMinterPage.bchange17"
) } "
onSuccessMessage = " $ { translate (
"walletpage.wchange4"
) } "
onErrorMessage = " $ { translate (
"walletpage.wchange39"
) } "
textToCopy = $ { sponsorship . rewardSharePublicKey }
buttonSize = "28px"
iconSize = "16px"
color = "var(--copybutton)"
offsetLeft = "4px"
> < / b u t t o n - i c o n - c o p y >
this . createRewardShare ( sponsorship ? . publicKey )
} } > copy < / m w c - b u t t o n >
< / l i >
< li class = "grid-item grid-item-button" >
< mwc - button
@ -540,7 +574,7 @@ class SponsorshipList extends LitElement {
< / d i v >
`
: '' }
< p class = "message" > $ { this . createSponsorship Message} < / p >
< p class = "message-error " > $ { this . error Message} < / p >
< div class = "form-wrapper" >
< div class = "sponsor-minter-wrapper" >
< p class = "sponsor-minter-text" > $ { translate ( "sponsorshipspage.schange5" ) } < / p >
@ -559,17 +593,11 @@ class SponsorshipList extends LitElement {
< div class = "form-item form-item--button" >
< vaadin - button
? disabled = "${this.isLoadingCreateSponsorship || !this.publicKeyValue}"
@ click = "${this.createRewardShare}"
@ click = "${()=> this.createRewardShare(this.publicKeyValue) }"
>
$ {
this . isLoadingCreateSponsorship === false
? html ` ${ translate (
$ { translate (
"puzzlepage.pchange15"
) } `
: html ` <paper-spinner-lite
active
> < / p a p e r - s p i n n e r - l i t e > `
}
) }
< / v a a d i n - b u t t o n >
< / d i v >
< / d i v >
@ -598,6 +626,85 @@ class SponsorshipList extends LitElement {
$ { translate ( "general.close" ) }
< / m w c - b u t t o n >
< / m w c - d i a l o g >
< mwc - dialog escapeKeyAction = "" scrimClickAction = "" id = "showDialogRewardShareCreationStatus" ? hideActions = $ { this . errorMessage ? false : this . status < 4 ? true : false } ? open = $ { this . openDialogRewardShare } >
< div class = "dialog-header" >
< div class = "row" >
< h1 > In progress < / h 1 > < d i v c l a s s = $ { ` s m a l l L o a d i n g m a r g i n L o a d e r $ { t h i s . s t a t u s > 3 & & ' h i d e ' } ` } > < / d i v >
< / d i v >
< hr / >
< / d i v >
< div class = "dialog-container" >
< ul >
< li class = "row between" > 1. Creating relationship < div class = $ { ` smallLoading marginLoader ${ this . status !== 1 && 'hide' } ` } > < / d i v > < / l i >
< li class = $ { ` row between ${ this . status < 2 && 'inactiveText' } ` } >
< p >
2. Awaiting confirmation on blockchain
< / p >
< div class = $ { ` smallLoading marginLoader ${ this . status !== 2 && 'hide' } ` } > < / d i v >
< / l i >
< li class = $ { ` row between ${ this . status < 3 && 'inactiveText' } ` } >
< p >
3. Finishing up
< / p >
< div class = "row no-width" >
< div class = $ { ` smallLoading marginLoader marginRight ${ this . status !== 3 && 'hide' } ` } > < / d i v > $ { a s y n c R e p l a c e ( t h i s . t i m e r ) }
< / d i v >
< / l i >
< li class = $ { ` row between ${ this . status < 4 && 'inactiveText' } ` } >
< p >
4. Complete
< / p >
< / l i >
$ { this . privateRewardShareKey && this . status === 4 ? html `
< li class = $ { ` column word-break ${ this . status < 4 && 'inactiveText' } ` } >
< p > Copy the key below and share it with your sponsored person . < / p >
< div style = "background: #eee; padding: 8px; margin: 8px 0; border-radius: 5px;" >
< span style = "color: #000;" > $ { this . privateRewardShareKey } < / s p a n >
< / d i v >
< / l i >
` : ''}
< / u l >
< div class = "warning column" >
< p >
Warning : do not close the Qortal UI until completion !
< / p >
< p class = "message-error" > $ { this . errorMessage } < / p >
< / d i v >
< / d i v >
< mwc - button
slot = "primaryAction"
@ click = $ { ( ) => {
this . openDialogRewardShare = false
this . errorMessage = ''
this . isLoadingCreateSponsorship = false
this . privateRewardShareKey = ""
this . atMount ( )
} }
class = "red"
>
$ { translate ( "general.close" ) }
< / m w c - b u t t o n >
< / m w c - d i a l o g >
< / d i v >
`