if you make this conversion by adding USD string at the end, then you will not be able to do other computations, you should keep price.usdPrice in a variable how it is and not convert it to string
sooo let me know if i m changing it to let cptprice = price.usdPrice + " USD" then why its not working ?
what i have to do to change this constant
this will generate a string, you can not do multiplication with strings after that
Soo what I have to do to define the value of tokens ? i also makes a constant
const tokenBalance = usercptBalance()
tokenBalance.then(
function (value) {
console.log(value);
let t =Moralis.Units.FromWei(value);
document.getElementById("cpt").innerHTML = t;}
);
let wet = price.usdprice ;
let l= t* wet;
console.log(l);
does this work fine for you?
Nooo sir it donāt work
what do you get if you print all those variables?
what exactly doesnāt work? what is the output for that console.log(l);?
``let see line 11 and 12 where i get marketcap by multiplying 10000 in usdprice so in the same way i m doing value but the different thing is value is should be in login function so when i m accessing the usdprice as varaible in login function then there is some error due to block scope variable i thinkā¦
async function getPrice() {
price = await Moralis.Web3API.token.getTokenPrice({address: ā0x28e4f32fa7e842ad0a6e530bb6b4ec03b36d5078ā, chain: ābscā})
console.log(price);
let ethPrice = (price.nativePrice.value / (8**price.nativePrice.decimals)) + " BNB"
console.log(ethPrice)
let usdPrice = price.usdPrice + " USD"
console.log(usdPrice)
document.getElementById(āBNB_priceā).innerHTML = ethPrice;
document.getElementById(āusd_priceā).innerHTML = usdPrice;
document.getElementById(āusd_pricā).innerHTML = usdPrice;
const pou = price.usdPrice.toFixed(7)
document.getElementById(āmarketcapā).innerHTML=10000 * pou + " USD";
}
getPrice();
you could write something like: const pou = (price.usdPrice * 10000 ).toFixed(7)
soo u can see my code here what i want to do just want to call the variable
usdprice
in loginfunction which is in under getprice function and do some computation to get value of my token ,
value means usd price of cpt token * balance of cpt of any user see last line of login function
async function login() {
let currentuser = Moralis.User.current();
if (!currentuser) {
currentuser = await Moralis.authenticate({ signingMessage: "welcome to crypto planet " })
}
console.log("logged in user:", currentuser);
console.log(currentuser.get('ethAddress'));
let bal=currentuser.get('ethAddress')
document.getElementById("btn-login").innerHTML=bal;
// document.getElementById("hi").innerHTML = 'ethaddress';
const tokenBalance = usercptBalance()
tokenBalance.then(
function (value) {
console.log(value);
const t =Moralis.Units.FromWei(value);
document.getElementById("cpt").innerHTML = t;}
document.getelementbyid("value).innerhtml = t * usdprice ;
);
// cpt price
async function getPrice() {
price= await Moralis.Web3API.token.getTokenPrice({address: "0x28e4f32fa7e842ad0a6e530bb6b4ec03b36d5078", chain: "bsc"})
console.log(price);
let ethPrice = (price.nativePrice.value / (8**price.nativePrice.decimals)) + "BNB"
console.log(ethPrice)
let usdPrice = price.usdPrice ; *this is price of token which i want to call in login function*
console.log(usdPrice)
document.getElementById("BNB_price").innerHTML = ethPrice;
document.getElementById("usd_price").innerHTML = usdPrice;
}
getPrice();
// bnb price
async function takePrice() {
price= await Moralis.Web3API.token.getTokenPrice({address: "0x3ee2200efb3400fabb9aacf31297cbdd1d435d47", chain: "bsc"})
console.log(price);
let usdPrice = price.usdPrice
console.log(usdPrice)
document.getElementById("usd1").innerHTML = usdPrice;
}
takePrice();
// cardano price
async function givePrice() {
price= await Moralis.Web3API.token.getTokenPrice({address: "0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c", chain: "bsc"})
console.log(price);
let usdPrice = price.usdPrice
console.log(usdPrice)
document.getElementById("usd2").innerHTML = usdPrice;
}
givePrice();
you could write only one function to get all the prices
but then also u see that usdprice variable used 3 times and how i call this variable in my login function which directs to my token price
document.getelementbyid("value).innerhtml = t * usdprice ;
you could use global variables or return data from functions (you may need to use await), it looks like you only write results with document.getelementbyid("value).innerhtml
soo i use
return usdprice in getprice function , and when i m trying to use this line
document.getelementbyid("value).innerhtml = t*usdprice
it starts showing nothing on my frontend ā¦ as i remove this line every function starts working ā¦ and where i have to use await ?
you can call this function something like x = await getPrice()
soo i have to call this function in my login function then i have to write
`document.getelementbyid("value).innerhtml = t*usdprice
?``
you donāt have to call it necessarily in the login function, but you could do it there too
actually this value will show after login so i m putting this āvalueā in login function and also i try this both will wrong and my all function stop working when i m doing this
const tokenBalance = usercptBalance()
tokenBalance.then(
function (value) {
console.log(value);
const t =Moralis.Units.FromWei(value);
document.getElementById("cpt").innerHTML = t;}
o= await getPrice()
let p= usdPrice *t ;
console.log(p)
or
const tokenBalance = usercptBalance()
tokenBalance.then(
function (value) {
console.log(value);
const t =Moralis.Units.FromWei(value);
document.getElementById("cpt").innerHTML = t;}
o= await getPrice()
document.getelementbyid("value").innerhtml = t*usdprice ;
after adding last 2 lines of code my code stop working
Here, can you use a syntax with balance = await usercptBalance() ?
It may be easier to understand the code, but is not enough only to write that line