I donāt understand, can you give a 2-3 lines of code example of what you want to do, what is the expected output and what is the current output?
const tokenBalance = usercptBalance()
tokenBalance.then(
function (value) {
console.log(value);
let t =Moralis.Units.FromWei(value);
document.getElementById("cpt").innerHTML = t;}
for taking value i have to multiply t with usdprice of cpt .
let value = t * usdprice ;
document.getelementbyid("").innerhtml = value ;
);
expected output will be userbalance * cpt price in usd
current is userbalance * cpt price *cardano price * bnb price
bcz of constant usd price bcz if i m changing usd price constant then code is not working
you have price for coin_1 = $10, and price for coin_2 = $20;
what do you want to do?
somehow I am not able to follow
i have three coins bnb , caradano , cpt
in all three prices there is common constant used let usdprice
so when i m trying to call usdprice for cpt token to multiply with usersbalance to get value of tokenā¦ then all three tokens price are multiplied it is showing some big amount ā¦
somehow i m changing the usdprice constant soo then nothing works!
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()