Hey there i wanna convert ERC20 to ETH

web3Js.utils.fromWei(Moralis.Units.Token(token.balance, token.decimals), 'ether');
when i use this it doesnt work for some reason and im tryng to figure out why.
When i go to eth to usd it doesnt show the correct amount.
I want the correct balance. If i have 100 usdt i want 100usdt in eth amount to be shown

that function converts from or to WEI

can you give an example of input and what is the expected output?

for example.
from the moralis api i get a output of like balance 60000, deicmals 6.
I wanna convert thatz to eth. Like 0.02 ETH

Moralis.Units.FromWei(60000, 6)
=>
0.06
​
balance: 25305020
​
banner: "https://cdn.moralis.io/eth/0xdac17f958d2ee523a2206206994597c13d831ec7_thumb.png"
​
contractAddress: "0xdac17f958d2ee523a2206206994597c13d831ec7"
​
decimals: 6
​
name: "USDT"
​
price: "25.30502"
​
type: "ERC20"

output: 25.302. This is like the usd price
But i want it in ETH

You get the price of eth with getTokenPrice and then you compute that value

can you provide a example. That would help a lot)

An example for what in particular?
You use wrapped eth address to get the price.

with the object provided above. Convert the baalnce to eth. How shall i do it:(

I don’t know on what object you are referring

On this one see the balance key ?

You transform it in usd, you know how to do that.
The you get the price of eth, and convert it to eth
.

how do i get the price of eth with Moralis ?

You can search on forum, that question was asked before.
You can read the documentation also:
https://docs.moralis.io/moralis-dapp/web3-api/token#gettokenprice

I already told you that you need to use wrapped eth.

i am using the deep index api. What address to i have to out in ? User address or token contract ?

getting a bad request. I have put the x api key too
https://deep-index.moralis.io/api/v2/erc20/0xdac17f958d2ee523a2206206994597c13d831ec7/price?chain=Eth&exchange=PancakeSwapv2

You put the address of the erc20 token that you want to get the price for

Try to use the sdk, you don’t need an api key there

yeah but how is it done with api key. I dont wanna start a server with Moralis.start

if you want to run it server side, you don’t need a server, you can use Moralis.start with moralisSecret parameter

you can also find the api key in web3api page: https://admin.moralis.io/web3apis
you can also run the api endpoint there directly in that web3api page

1 Like