Toke price with chain as bsc

I followed your video(https://youtu.be/90dMpTlPNZ0) to generate the token price chart and it work only when we use to pass chain as β€œbsc”, with eth it give same block for every different date which results in same price for every date too

2nd issue
I am not getting the reaponse when i am trying to get the blocks or price for the below token
token name=NFTArt.Finance
address=0xf7844cb890f4c339c497aeab599abdc3c874b67a

token name= CleanOcean
and many other
i am using the same method explained in the video and it work for the coin mentioned in etherscan but not for the coin of bscscan

  let blocks1=await Promise.all(dates1.map(async (e,i)=>
    await Moralis.Web3API.native.getDateToBlock({chain: "bsc",date:e})
    ))

 let prices1=await Promise.all(blocks1.map(async (e,i)=>
         await Moralis.Web3API.token.getTokenPrice({address: addrs,to_block: e.block})
    ))

i tried with both chain eth and bsc but not getting response for many token and i mentioned the two token already

PLease assist me on in this ASAP

2 Likes

It looks like this works fine for me:

x = await Moralis.Web3API.token.getTokenPrice({address: β€œ0xf7844cb890f4c339c497aeab599abdc3c874b67a”, chain: β€œbsc”})

Can you give examples of exact queries that don’t work for you?

1 Like

I really appreciate you repsonse
yes this worked but my concern is i need the price of the same toke for the last 7 days and i tried to follow this video of moralis https://docs.moralis.io/tutorials/token-price-charts
in which we need to pass the date and from that we use to get the the blocks and from block we use to get the token price
and i implemented the same and it worked for all the coin of ether scan but not for bsc scan
what you did is you got the price directly but i need to specific date and for date we need block and i am not getting that block so i am not able to passs the the block number to token price api and it throws error

addrs=β€œ0xf7844cb890f4c339c497aeab599abdc3c874b67a”

  let dates1=Array(Number(days)).fill().map((e,i)=> 
    moment().subtract(i,"d").format("YYYY-MM-DD")
    ).reverse()
  console.log(dates1);//['2021-10-27', '2021-10-28', '2021-10-29', '2021-10-30', '2021-10-31', '2021-11-01', '2021-11-02']
  
  let dates1_first=dates1[0];
  let dates1_LAST=dates1[dates1.length-1];

  let blocks1=await Promise.all(dates1.map(async (e,i)=>
    await Moralis.Web3API.native.getDateToBlock({chain: "bsc",date:e})
    ))

console.log(blocks1);
response of blocks1
0: {date: β€˜2021-10-27T00:00:00+00:00’, block: 12120588, timestamp: 1635292802}
1: {date: β€˜2021-10-28T00:00:00+00:00’, block: 12148891, timestamp: 1635379202}
2: {date: β€˜2021-10-29T00:00:00+00:00’, block: 12177266, timestamp: 1635465602}
3: {date: β€˜2021-10-30T00:00:00+00:00’, block: 12205037, timestamp: 1635552000}
4: {date: β€˜2021-10-31T00:00:00+00:00’, block: 12233506, timestamp: 1635638400}
5: {date: β€˜2021-11-01T00:00:00+00:00’, block: 12262159, timestamp: 1635724800}
6: {date: β€˜2021-11-02T00:00:00+00:00’, block: 12285773, timestamp: 1635797973}

  let prices1=await Promise.all(blocks1.map(async (e,i)=>
   // await Moralis.Web3API.native.getDateToBlock({date:e})
   await Moralis.Web3API.token.getTokenPrice({address: addrs,to_block: e.block})
    ))


    its workinf for other etherscan coin but nont for bscscan as i am not getting block number for bsccscan coin
1 Like

(await Moralis.Web3API.native.getDateToBlock({chain: β€œbsc”, date:β€œ2021-10-27T00:00:00+00:00”})).block

=> 12120588

x = await Moralis.Web3API.token.getTokenPrice({address: β€œ0xf7844cb890f4c339c497aeab599abdc3c874b67a”, chain: β€œbsc”, to_block: 12120588})

works too

I think that you forgot to add chain: "bsc" in that line

1 Like

thank you so much friend
it works
actually we need to pass the chain as per the token name in token price api, does not matter you passed the chain in block api or not

BTW i have one more point do we have any api or way by which we can get the top gainer and top loossers from moralis api like its here

I think that you can sort them by price difference in percentage for last 24 hours to get the top

1 Like

ok thank you and from which api i can get the list of all the token with their price difference
like to get the block we used this one getDateToBlock
similarly for price pf specific token we get used this getTokenPrice
similarly which method we have for this

I don’t think that it is a method for this now, you keep a list of tokens and you can check them all once a day or every hour and then you can compute that statistic

1 Like

you mean suppose i have 20 token and than i should store the price of all the 20 token in my database and do the computation with the current price and show the result, like that.

And how other website like coinmarket.com, coingecko.com how are they showing the same thing like top gainers and losoers

I think that they keep the data in database and make queries in database so they can compare easily the current price with the price that was 24 hours ago.

1 Like