Continuing the discussion from [SOLVED] Build A Web3 DEX | FULL COURSE | React, Node, Moralis, 1Inch:
Im trying to use the API for prices in Binance smart chain tokens not ethereum.
This is the code i used.
const express = require(“express”);
const Moralis = require(“moralis”).default;
const { EvmChain } = require(“moralis/node”);
const app = express();
const cors = require(“cors”);
require(“dotenv”).config();
const port = 3001;
app.use(cors());
app.use(express.json());
const chain = EvmChain.BSC;
app.get("/tokenPrice", async (req, res) => {
const { query } = req;
const responseOne = await Moralis.EvmApi.token.getTokenPrice({
chain,
address: query.addressOne
});
const responseTwo = await Moralis.EvmApi.token.getTokenPrice({
chain,
address: query.addressTwo
});
console.log(responseOne.raw);
console.log(responseTwo.raw);
return res.status(200).json({});
});
Moralis.start({
apiKey: process.env.MORALIS_KEY,
serverUrl: process.env.MORALIS_SERVER_URL,
chain,
}).then(() => {
app.listen(port, () => {
console.log(`Listening for API Calls`);
});
});
This code is specific to a server url, you are using Moralis v1 sdk or Moralis v2 sdk?
I’m case that you are using Moralis v1 sdk then you have a server url?
Can you please look at this code?
const express = require(“express”);
const Moralis = require(“moralis”).default;
const app = express();
const cors = require(“cors”);
require(“dotenv”).config();
const port = 3001;
app.use(cors());
app.use(express.json());
const chain = EvmChain.BSC;
app.get("/tokenPrice", async (req, res) => {
const {query} = req;
const responseOne = await Moralis.EvmApi.token.getTokenPrice({
chain,
address: query.addressOne
})
const responseTwo = await Moralis.EvmApi.token.getTokenPrice({
chain,
address: query.addressTwo
})
console.log(responseOne.raw);
console.log(responseTwo.raw);
return res.status(200).json({});
});
Moralis.start({
apiKey: process.env.MORALIS_KEY,
}).then(() => {
app.listen(port, () => {
console.log(`Listening for API Calls`);
});
});
Can you look on how to paste the code on forum? There is a forum thread about it.
What version of the sdk you are using?
What happens? You get an error, something else?
yes, i get an error.
What is the error that you get?
PS C:\Users\cable\Desktop\BLUEPRINTS\dexstarter\dexBack> node index.js
C:\Users\cable\Desktop\BLUEPRINTS\dexstarter\dexBack\index.js:10
const chain = EvmChain.BSC;
^
ReferenceError: EvmChain is not defined
at Object. (C:\Users\cable\Desktop\BLUEPRINTS\dexstarter\dexBack\index.js:10:15)
at Module._compile (node:internal/modules/cjs/loader:1226:14)
at Module._extensions…js (node:internal/modules/cjs/loader:1280:10)
at Module.load (node:internal/modules/cjs/loader:1089:32)
at Module._load (node:internal/modules/cjs/loader:930:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
at node:internal/main/run_main_module:23:47
Node.js v18.14.0
Try to hardcode the chain id for BSC directly
how do i do that is there a tutorial?
What do you want to achieve? You can make a sinple http rest request to get that info too. You have to candles on docs interface at docs.moralis.io where you can try the endpoint directly and see the curl command
I’m following a moralis youtube tutorial for a DEX.
https://www.youtube.com/watch?v=t8U7GRrlYW8&t=333s
in minute 41 of the video talks about what i need which is to use specific chain.
The code works fine for Ethereum but i need it to work for BSC.
That part of the code helps you get token current prices.
How it works for eth? What do you use for eth as a chain? Try 0x38 as chain for bsc or look in the docs interface for the chain number
The following code works fine for ethereum chain:
const express = require(“express”);
const Moralis = require(“moralis”).default;
const app = express();
const cors = require(“cors”);
require(“dotenv”).config();
const port = 3001;
app.use(cors());
app.use(express.json());
app.get("/tokenPrice", async (req, res) => {
const {query} = req;
const responseOne = await Moralis.EvmApi.token.getTokenPrice({
address: query.addressOne
})
const responseTwo = await Moralis.EvmApi.token.getTokenPrice({
address: query.addressTwo
})
console.log(responseOne.raw);
console.log(responseTwo.raw);
return res.status(200).json({});
});
Moralis.start({
apiKey: process.env.MORALIS_KEY,
}).then(() => {
app.listen(port, () => {
console.log(Listening for API Calls
);
});
});
Ok, it should be easy, use “0x38” for chain for bsc or try to find what is the chain id for bsc in hex
You mean replace with this part of the code?
const chain = EvmChain.BSC; to const chain = EvmChain.0x38;