简体   繁体   English

使用 Moralis 获取 EthPrice JavaScript

[英]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.我想使用 MoralisJS API 获取 Eth 价格。问题是 Moralis 不仅返回 ETH 价格,还返回很多其他数据。 I only want the usdPrice of Eth, what would be the right RegEx to get it?我只想要 Eth 的 usdPrice,获得它的正确 RegEx 是什么?

` `

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]
    }
  }
}


` `

You can use response.result.usdPrice eg console.log(response.result.usdPrice);您可以使用response.result.usdPrice例如console.log(response.result.usdPrice); . . Or you can use response.raw for just the data.或者您可以只对数据使用response.raw

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM