I receive random undefined using date to block function for an array. If I try to make single request it is working. What’s the problem?
[removed because it had api key]
I receive random undefined using date to block function for an array. If I try to make single request it is working. What’s the problem?
[removed because it had api key]
Hi, can you post this part in code, like that list and how you iterate it to call Moralis.Web3API.native.getDateToBlock
?
async function priceGain() {
let adr = '0x34e942859469c9db9c22f4eaf866e2c2401bb795'
let d1 = Array(Number(30)).fill().map((e, i) => moment().subtract(i, "d").format("YYYY-MM-DD")).reverse()
console.log(d1)
let bl = await Promise.all(d1.map(async (e, i) =>
await Moralis.Cloud.run("dateToBlock", ({ date: e, chain: "bsc" }))
))
console.log(bl)
bl = await bl.filter(item => item !== undefined)
console.log(bl)
let pr = await Promise.all(bl.map(async (e, i) =>
await Moralis.Cloud.run("getPrice", { address: adr, to_block: e.block, chain: "bsc" })
))
console.log(pr)
pr = await pr.filter(item => item !== undefined)
console.log(pr)
pr = pr.map(e => e.usdPrice).filter(item => item !== 0)
console.log(pr)
const f1 = [pr.shift(), pr.pop()]
console.log(f1)
console.log(difference(f1[0], f1[1]))
}
function difference(a, b) {
if (a < b) {
return ((b - a) / a) * 100;
} else return ((a - b) / a) * -100;
}
Cloud Functions
Moralis.Cloud.define("getPrice", async (request) => {
let url = 'https://deep-index.moralis.io/api/v2/erc20/' + request.params.address + '/price'
let chain = request.params.chain
let chain_name = request.params.chain_name
let to_block = request.params.to_block
logger.info(url);
return Moralis.Cloud.httpRequest({
url: url,
params: {chain: chain, chain_name: chain_name, to_block: to_block},
headers: {
'accept': 'application/json',
'X-API-Key': 'key'
}
}).then( function(httpResponse) {
return httpResponse.data;
}, function (httpResponse){
logger.info("error");
logger.info(httpResponse);
return;
})
});
Moralis.Cloud.define("dateToBlock", async (request) => {
let url = 'https://deep-index.moralis.io/api/v2/dateToBlock/'
let date = request.params.date
let chain = request.params.chain
let chain_name = request.params.chain_name
logger.info(url)
return Moralis.Cloud.httpRequest({
url: url,
params: {date: date, chain: chain, chain_name: chain_name},
headers: {
'accept': 'application/json',
'X-API-Key': 'key'
}
}).then( function(httpResponse) {
return httpResponse.data;
}, function (httpResponse){
logger.info("error");
logger.info(httpResponse);
return;
})
});
you don’t have to call those cloud functions, you can use a syntax like this:
(await Moralis.Web3API.native.getDateToBlock({chain: “bsc”, date:“2021-10-27T00:00:00+00:00”})).block
=> 12120588x = await Moralis.Web3API.token.getTokenPrice({address: “0x34e942859469c9db9c22f4eaf866e2c2401bb795”, chain: “bsc”, to_block: 12120588})
I don’t understand. If I don’t use my cloud functions, then how I send my api key under header? And for what reason should I have those cloud functions?
When I use syntax like that, I also receive random undefined but they are not visible as I get red error instantly
async function priceGain() {
let adr = '0x34e942859469c9db9c22f4eaf866e2c2401bb795'
let d1 = Array(Number(30)).fill().map((e, i) => moment().subtract(i, "d").format("YYYY-MM-DD")).reverse()
console.log(d1)
let bl = await Promise.all(d1.map(async (e, i) =>
await Moralis.Web3API.native.getDateToBlock({ date: e, chain: "bsc" })
))
console.log(bl)
bl = await bl.filter(item => item !== undefined)
console.log(bl)
let pr = await Promise.all(bl.map(async (e, i) =>
await Moralis.Web3API.token.getTokenPrice({ address: adr, to_block: e.block, chain: "bsc" })
))
console.log(pr)
pr = await pr.filter(item => item !== undefined)
console.log(pr)
pr = pr.map(e => e.usdPrice).filter(item => item !== 0)
console.log(pr)
const f1 = [pr.shift(), pr.pop()]
console.log(f1)
console.log(difference(f1[0], f1[1]))
}
I looks like I also get some errors with that code. We will have to investigate. It may work fine if you don’t run 30 requests in parallel.
So how can’t I send 30 requests if I want to make a chart for 30 days?
you can send 30 requests, but not all at the same time for now, you can add a delay of 1/0.5/0.3 seconds for example between requests
can you try again now?
To be honest, I don’t know how to make a delay in async Promises with map inside.
But in general, I saw the guy from Moralis Youtube channel who made 30 requests without delays and it was working fine for his 30days price chart. For me it sends red errors with error Object Object randomly everytime.
I was able to send 30 requests in parallel yesterday without problems. for that 30 days price chart
It seems to be working with delays for now.
But it randomly gets zero or undefined values.
for example array with 30 dates, 1 random date can’t find block
I tested with this code inspired from previous forum post:
async function priceGain() {
let adr = '0xe9e7cea3dedca5984780bafc599bd69add087d56'
let d1 = Array(Number(30)).fill().map((e, i) => moment().subtract(i, "d").format("YYYY-MM-DD")).reverse()
console.log(d1)
let bl = await Promise.all(d1.map(async (e, i) =>
await Moralis.Web3API.native.getDateToBlock({ date: e, chain: "bsc" })
))
console.log(bl)
bl = await bl.filter(item => item !== undefined)
console.log(bl)
let pr = await Promise.all(bl.map(async (e, i) =>
await Moralis.Web3API.token.getTokenPrice({ address: adr, to_block: e.block, chain: "bsc" })
))
console.log(pr)
pr = await pr.filter(item => item !== undefined)
console.log(pr)
pr = pr.map(e => e.usdPrice).filter(item => item !== 0)
console.log(pr)
const f1 = [pr.shift(), pr.pop()]
console.log(f1)
}
x = await priceGain()