Hello everybody.
I know html and css but I’m a beginner for the rest.
I spent 2 days watching the tutorials and videos to help me but I can’t do what I want.
My request is probably going to be very easy for a lot of people here, but I think my question will be of interest to a lot of newbies like me in the future.
My goal is simple, I would like to have the values of a token in USD on my site but I cannot display it on the page.
Here are my codes and thank you in advance to those who will be kind enough to help me.
Mainnet/Cloud fonctions:
Moralis.Cloud.define("getPrice", async (request) => {
let url = 'https://deep-index.moralis.io/api/v2/erc20/0xd448068f8c73a7f64bfddbc17204afbbd7b3f80a/price?chain=bsc&providerUrl=https%3A%2F%2Fspeedy-nodes-nyc.moralis.io%2F6bf2df218b520e7ea408af3b%2Fbsc%2Fmainnet&exchange=pancakeswap'
logger.info(url);
return Moralis.Cloud.httpRequest({
url: url,
params: {chain: "bsc", chain_name: "mainnet"},
headers: {
'accept': 'application/json',
'X-API-key': 'LqjTTWPKzCz.......Nw9pH'
}
}).then( function(httpResponse){
return httpResponse.data;
}, function(httpResponse){
logger.info("error");
logger.info(httpResponse);
})
});
main.js
Moralis.initialize("5JNM9X1Nq8ciDngst2pNBUrSoThG5a7jBPYUvrZM"); // Application id from moralis.io
Moralis.serverURL = "https://xsmb9wqekxnl.moralishost.com:2053/server"; //Server url from moralis.io
async function getPrice() {
let address = document.getElementById("address").value;
console.log(adresse);
let price = await Moralis.cloud.run("getPrice", {address: address});
let ethPrice = (price.nativePrice.value / (8**price.nativePrice.decimals)) + "BNB"
let usdPrice = price.usdPrice + " USD"
document.getElementById("BNB_price").innerHTML = BNBPrice;
document.getElementById("usd_price").innerHTML = usdPrice;
console.log(price);
}
document.getElementById("text").addEventListener(getPrice);
Html
<head>
<script src="https://cdn.jsdelivr.net/npm/web3@latest/dist/web3.min.js"></script>
<script src="https://npmcdn.com/moralis@latest/dist/moralis.js"></script>
<script src="https://unpkg.com/moralis/dist/moralis.js"></script>
</head>
<body>
<div class="pricess">
<h1>Price</h1>
<div id="price">
<div id="BNB_price">bnb</div>
<div id="usd_price">usd</div>
</div>
</div>
<script type="text/javascript" src="./main.js"></script>
</body>
I would just like the price of the token to be visible on the page and update regularly for example every minute or with every transaction, it doesn’t matter.
Thanks again.