0xOtto
0xOtto

Reputation: 11

get EthPrice with Moralis JavaScript

I want to get the Eth Price using the MoralisJS API. The problem is that Moralis doesnt only return the ETH-Price but also a lot of other data. I only want the usdPrice of Eth, what would be the right RegEx to get it?

`

const Moralis      = require('moralis').default;
const config       = require("./config.json");
const { EvmChain } = require("@moralisweb3/evm-utils");
const fs           = require('fs');

const runApp = async () => {
  await Moralis.start({
    apiKey: config.API_KEY
  });
  
  const address = "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2";

  const chain = EvmChain.ETHEREUM;

  const response = await Moralis.EvmApi.token.getTokenPrice({
        address,
        chain,
    });
  
    
    console.log(response)
    
}

runApp()

// this is the return:

ApiResultAdapter {
  data: {
    nativePrice: {
      value: '1000000000000000000',
      decimals: 18,
      name: 'Ether',
      symbol: 'ETH'
    },
    usdPrice: 1210.4502931832342,
    exchangeAddress: '0x1f98431c8ad98523631ae4a59f267346ea31f984',
    exchangeName: 'Uniswap v3'
  },
  adapter: [Function: apiToResult],
  jsonAdapter: [Function: resultToJson],
  params: {
    address: '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2',
    chain: EvmChain {
      config: [Config],
      _value: '0x1',
      _chainlistData: [Object]
    }
  }
}


`

Upvotes: 0

Views: 106

Answers (1)

Alex
Alex

Reputation: 358

You can use response.result.usdPrice e.g. console.log(response.result.usdPrice);. Or you can use response.raw for just the data.

Upvotes: 1

Related Questions