diff --git a/jrx/src/popup.ts b/jrx/src/popup.ts index 74d0efa..dc49e58 100644 --- a/jrx/src/popup.ts +++ b/jrx/src/popup.ts @@ -89,24 +89,58 @@ pi_repop if (akstrs_empty) document.getElementById('no-keypairs')!.hidden = false; // if the keypairs are not empty - else { + else + { // hide the "no keypairs" paragraph document.getElementById('no-keypairs')!.hidden = true; // for each keypair // each one of these is an ak_... string - for (let this_akstr of akstrs) { + for (let this_akstr of akstrs) + { // make an li let this_li = document.createElement('li'); // shorten akstr let this_shakstr = pi_shakstr(this_akstr); // make inner text the key this_li.innerHTML = this_shakstr; + + // // balance + // // add a br + // let br = document.createElement('br'); + // this_li.appendChild(br); + + // // add balance + // this_li.innerHTML += 'Balance: '; + // let maybe_balance = await pi_balance(this_akstr); + // // if there is a balance, show it in green + // if (maybe_balance.ok) + // { + // let greentext = document.createElement('span'); + // greentext.style.color = 'green'; + // // @ts-ignore fuck off i know what i'm doing + // greentext.innerHTML = '' + maybe_balance.balance; + + // this_li.appendChild(greentext); + // } + // // if not, show reason and make it red + // else + // { + // let redtext = document.createElement('span'); + // redtext.style.color = 'red'; + // // @ts-ignore fuck off i know what i'm doing + // redtext.innerHTML = '' + maybe_balance.reason; + + // this_li.appendChild(redtext); + // } + // add it to the list pubkeys_ul.appendChild(this_li); } } } + + /** * shorten the ak_... str */ @@ -118,3 +152,49 @@ pi_shakstr console.log('popup shakstr'); return akstr.slice(0, 10) + '...'; } + + + +/** + * get account balance of ak_... str + */ +async function +pi_balance + (akstr : string) + : Promise< {ok : true, + balance : number} + | {ok : false, + reason : string} + > +{ + let endpoint_init_url = 'http://demonet.qpq.swiss:3013'; + let this_endpoint = endpoint_init_url + '/accounts/' + akstr; + let input_params = {pubkey: akstr}; + // fetch: https://developer.mozilla.org/en-US/docs/Web/API/fetch + let response = await fetch(this_endpoint); + // 200 = ok + // 400 = pubkey malformed + // 404 = account dne + switch (response.status) { + case 200: + // unstringify the body + let return_data = response.json(); + // get the balance + // @ts-ignore talking to foreign api + let balance = return_data.balance; + return {ok : true, + balance : balance}; + // pubkey malformed + case 400: + return {ok : false, + reason : 'Pubkey Malformed'}; + // account dne + case 404: + return {ok : false, + reason : 'Account DNE'}; + // wtf + default: + return {ok : false, + reason : 'Unexpected response from node'}; + } +}