[solved] Hi, I have a question about get token price

I have this function to obtain the price of matic, but it didnā€™t work for me, and it worked for me on the server served by moralis, and now it doesnā€™t work on my own server. and I notice that the cloud returns this error:
Invalid function: "getTokenPrice" {"code":141,"stack":"Error: Invalid function: \"getTokenPrice\"\n at handler (C:\\Users\\david\\Documents\\Programacion\\react\\Koolinart\\backend\\node_modules\\parse-server\\src\\Routers\\FunctionsRouter.js:120:13)\n at C:\\Users\\david\\Documents\\Programacion\\react\\Koolinart\\backend\\node_modules\\parse-server\\src\\PromiseRouter.js:72:20"}

Does the function have anything to do with the cloud? or is this error a part?

const getMaticPrice = async () => {

    const options: any = {

      address: '0xCC42724C6683B7E57334c4E856f4c9965ED682bD',

      chain: "eth",

      exchange: "PancakeSwapv2",

      currencies: ["USD"]

    };

    const price = await Web3Api.token.getTokenPrice(options);

    console.log('priceMatic',  price);

    setPriceMatic(price.usdPrice);

  };

what would be the correct way to do it?

maybe it depends on what version of moralis SDK you are using, maybe the syntax changed over time on how to access getTokenPrice function with the SDK

i call it from:

import { useMoralis, useMoralisWeb3Api } from "react-moralis";
const Web3Api = useMoralisWeb3Api();

package.json:

"react-moralis": "^1.4.2",
 "moralis-v1": "^1.12.0",
 "moralis": "^2.18.4",

there should a cloud function with this name that is autogenerated

if there is, the template of the parse-server-migration repository had it, evmApi.ts is called the file, I notice that when I enter the amount of the token in the input it executes that cloud and I donā€™t call it, so I see const price = await Web3Api.token.getTokenPrice(options);
this call runs the cloud

you see this called in front end or on server side?

i call it in front end.

that doesnā€™t work like that in front end, you have to call a route in backend that calls the api in backend, that is required now mostly because you need the api key to make a call to the api and you donā€™t have the api key in front end

Could you tell me how it works now or where I can see how to do it? and this function also gives me the same error:
and Do not forget also the thread that I have of the authentication please.

const fetchTokenBalance = async (ethAddress: string) => {

    const options: any = {

      chain: "0x13881",

      address: ethAddress,

      symbol: "KNRT",

      token_address: contracts.token,

    };

    const balances = await Web3Api.account.getTokenBalances(options);

    console.log(

      "šŸš€ ~ file: UserState.tsx:39 ~ fetchTokenBalance ~ balances",

      balances

    );

    balances.forEach((value) => {

      console.log("valueEach", value);

      if (value.token_address === contracts.token.toLowerCase()) {

        const balanceConvert = Moralis.Units.FromWei(value.balance);

        console.log("balanceConvert", balanceConvert);

        dispatch({

          type: "SET_AMOUNT_KNRT_USER",

          payload: balanceConvert,

        });

      }

    });

    console.log("balance", balances);

  };

Thank you very much for the help, and the fast support, I love that about moralis and the community.

this is?

but, i get this error:

caught ReferenceError: global is not defined
    at node_modules/randombytes/browser.js (browser.js:16:14)
    at __require2 (chunk-JZQ37OGZ.js?v=b895fe44:18:50)
    at node_modules/ethereum-cryptography/random.js (random.ts:1:21)
    at __require2 (chunk-JZQ37OGZ.js?v=b895fe44:18:50)
    at node_modules/ethereum-cryptography/secp256k1.js (secp256k1.ts:2:1)
    at __require2 (chunk-JZQ37OGZ.js?v=b895fe44:18:50)
    at node_modules/@moralisweb3/streams/node_modules/ethereumjs-util/dist.browser/account.js (account.ts:3:1)
    at __require2 (chunk-JZQ37OGZ.js?v=b895fe44:18:50)
    at node_modules/@moralisweb3/streams/node_modules/ethereumjs-util/dist.browser/index.js (index.ts:9:1)
    at __require2 (chunk-JZQ37OGZ.js?v=b895fe44:18:50)
n
I was able to fix it by adding to vite.config.ts this:

define: {
  global: {},
}
now this error:

` caught (in promise) Moralis SDK Error: [C0009] Modules are started already. This method should be called only one time.`

ok problem solved thank you. for fetch token price

1 Like

why for some tokens moralis finds the reserves but doesnt fetch the price for it, returns a message no liquidity pools found. Just trying to understand whats causing this to happen and if there is anything you guys can do, because i cant use it if 5% of the tokens i cant fetch price for

What I noticed about this when doing the migration is that if you donā€™t put the correct token address with the correct exchange, it returns the price to 0.

no thats not the issue im facing, im facing an issue where moralis doesnt find liquidity pools to get the price, but moralis actually fetches the pools for those tokens

Can you give an exact example of token address and chain?

yes, on uniswap V2, im scanning new launched tokens and for about 5% of them the prices can not be retrieved due to no pools found but moralis actually provides the pools for those tokens. Here is an example of a couple that its not working for and been hours since they launched

0x721cdab61a45960291b1231d0b5b8fb126c48705
0x761eb61fb4481364b1766bd70542067a910a9439
0xdc83ea597838a6a49988dbf8485e9efa9544c2d2

could it be a caching issue if you try it exactly after the token is launched?

no, its been about 16hrs and still not getting price for these, even testing moralis doc api, doesnt get the price, so not an issue with my code. Could it be something to do with an ABI or certain contracts Moralis does not fetch price for?

it turns out that it is a caching issue on our side, it works with uniswapv2 and not with uniswap-v2

how can i fix that, or will you guys make an update to solve this issue?