I am trying to pull price data from the API for cryptowatch, when I go to the URL with my API key it works fine, but my program isn't successfully pulling it so I am getting my error of Could not set price feed for cryptowatch:" + cryptowatchMarketId
I'm pretty stuck on where to go from here.
// Set initial prices
const cryptowatchApiKey = process.env.CRYPTOWATCH_API_KEY || MM_CONFIG.cryptowatchApiKey;
const cryptowatchMarkets = await fetch("https://api.cryptowat.ch/markets?apikey=" + cryptowatchApiKey).then(r => r.json());
const cryptowatchMarketPrices = await fetch("https://api.cryptowat.ch/markets/prices?apikey=" + cryptowatchApiKey).then(r => r.json());
for (let i in cryptowatchMarketIds) {
const cryptowatchMarketId = cryptowatchMarketIds[i];
try {
const cryptowatchMarket = cryptowatchMarkets.result.find(row => row.id == cryptowatchMarketId);
const exchange = cryptowatchMarket.exchange;
const pair = cryptowatchMarket.pair;
const key = `market:${exchange}:${pair}`;
PRICE_FEEDS['cryptowatch:'+cryptowatchMarketIds[i]] = cryptowatchMarketPrices.result[key];
} catch (e) {
console.error("Could not set price feed for cryptowatch:" + cryptowatchMarketId);
}
}
const subscriptionMsg = {
"subscribe": {
"subscriptions": []
}
}
for (let i in cryptowatchMarketIds) {
const cryptowatchMarketId = cryptowatchMarketIds[i];
// first get initial price info
subscriptionMsg.subscribe.subscriptions.push({
"streamSubscription": {
"resource": `markets:${cryptowatchMarketId}:book:spread`
}
})
}
let cryptowatch_ws = new WebSocket("wss://stream.cryptowat.ch/connect?apikey=" + cryptowatchApiKey);
cryptowatch_ws.on('open', onopen);
cryptowatch_ws.on('message', onmessage);
cryptowatch_ws.on('close', onclose);
cryptowatch_ws.on('error', console.error);
function onopen() {
cryptowatch_ws.send(JSON.stringify(subscriptionMsg));
}
function onmessage (data) {
const msg = JSON.parse(data);
if (!msg.marketUpdate) return;
const marketId = "cryptowatch:" + msg.marketUpdate.market.marketId;
let ask = msg.marketUpdate.orderBookSpreadUpdate.ask.priceStr;
let bid = msg.marketUpdate.orderBookSpreadUpdate.bid.priceStr;
let price = ask / 2 + bid / 2;
PRICE_FEEDS[marketId] = price;
}
function onclose () {
setTimeout(cryptowatchWsSetup, 5000, cryptowatchMarketIds);
}
}
Related
I'm trying to query the token balance history of significant user with web3's batchRequest.
But the function can't guarantee the order of functions, so I get the balance datas not in order.
So I want to know other multicall functions which can get response with blockNumber to organaize the order after querying datas.
This is my current code
// this succeeded to query, and I'm using this
const Web3 = require('web3');
const { getTokens } = require('./utils/getTokens.js');
const { convertToNumber } = require('./utils/convertToNumber.js')
const { abi, rpcEndpoint, walletAddress, wsEndpoint } = require('./constant.js');
const { formatUnits } = require('ethers/lib/utils');
const web3 = new Web3(new Web3.providers.HttpProvider(rpcEndpoint));
const queryHistoryBalance = async (userAddress, tokenAddress, startBlock, endBlock, decimal) => {
const batch = new web3.BatchRequest(); // https://stackoverflow.com/questions/69788660/web3-batchrequest-always-returning-undefined-what-am-i-doing-wrong
const contract = new web3.eth.Contract(abi, tokenAddress);
/*
let step = Math.floor((endBlock - startBlock) / eachNumber);
if (step < 1) {
step = 1;
}
for (let n = 0; n < step; n++) {
}*/
let data = { [userAddress]: [] }
for (let n = startBlock; n <= endBlock; n++) {
batch.add(
contract.methods.balanceOf(userAddress).call.request({}, n, (err, res) => {
if (res) {
data[userAddress].push({ 'blockNumber': n, 'balance': res })
}
})
)
/*
batch.add(
contract.methods.balanceOf(userAddress).call.request({}, n, async (err, res) => {
const timestamp = await web3.eth.getBlock(n).timestamp;
data[userAddress].push({ 'blockNumber': n, 'blockTime': timestamp, 'balance': res })
//data[userAddress].push({ 'blockNumber': n,'balance': res })
})
)*/
}
await batch.execute();
//console.log(data);
let data2 = { [userAddress]: [] }
let formarNumber
await Promise.all(data[userAddress].map((element, i) => {
//console.log(element['balance'] / 1)
//data[userAddress][i]['balance'] = convertToNumber(element['balance'], 6)
let formatBalance = element['balance'] / (10 ** decimal);
data[userAddress][i]['balance'] = formatBalance;
if (formarNumber !== element['balance']) {
data2[userAddress].push({ 'blockNumber': element['blockNumber'], 'balance': formatBalance });
formarNumber = element['balance'];
}
if (i === 0) formarNumber = element['balance'];
}))
//console.log("data", data)
return data2
}
const main = async () => {
const data = await queryHistoryBalance('0x8d9e0f65569b3cc2f602a7158d02ab52976a4535', '0xdAC17F958D2ee523a2206206994597C13D831ec7', 14954947, 14958046, 6)
console.log(data)
}
main()
module.exports.queryHistoryBalance = queryHistoryBalance;
I appreciate any help and advice
I came up with this question on Stack Overflow because I could not solve a small problem that I had been dealing with for a long time.
One problem I do not understand is:
I want a function that automatically sets the data from the API and automatically saves it directly to Local Storage or Database by specific local time zone using with pure javascript.
I want the data to be automatically saved to LocalStorage or Database at 12:01 local time and 4:30 pm then display it to HTML DOM.
I want a API get the update data in every seconds.
Please take a look at some of the codes I wrote myself.
/* ---------------------------------------------------------------------- */
/* -------------------------- Retrieve API ------------------------- */
/* ---------------------------------------------------------------------- */
// Fetch Meow Data Main();
const pjk_Meow = async () => {
const base = 'aHR0cHM6Ly90d29';
const cors = atob('aHR0cHM6Ly9jb3JzLmNo');
const pjk_meow = atob(base);
try {
const res = await fetch(cors + pjk_meow);
const data = await res.json();
return data;
} catch (error) {
return error.message;
}
};
/* ---------------------------------------------------------------------- */
/* -------------------------- Schedule and Save to Local Storage ------------------------- */
/* ---------------------------------------------------------------------- */
// // 12:00 PM Request Data
const pjk_Meow_4 = async () => {
const base = 'aHR0cHM6Ly90d29';
const cors = atob('aHR0cHM6Ly9jb3JzLmNoY');
const pjk_meow = atob(base);
// Set default time what I want it to.
let d_time = '12:03:12 PM';
let d_time_2 = '12:10:12 PM';
let current_time = new Date().toLocaleTimeString();
try {
if (current_time === d_time || current_time <= 13) {
try {
const res = await fetch(cors + pjk_meow);
const data = await res.json();
// Working with LS
let data_serialized = JSON.stringify(data);
localStorage.setItem('afternoon', data_serialized);
return data_serialized;
} catch (error) {
return false;
}
}
} catch (error) {
return false;
}
};
pjk_Meow_4();
// 12:00 PM Callback and output to html
const twelve_pm = () => {
setInterval(() => {
try {
let third_Data = JSON.parse(localStorage.getItem('afternoon'));
const setThird = document.getElementById('setThird');
const valueThird = document.getElementById('valueThird');
const resultThird = document.getElementById('resultThird');
// If there is no any data in LS, this condition will run
if (localStorage.length <= 0 || localStorage === undefined) {
setThird.innerText = '------';
valueThird.innerText = '-------';
resultThird.innerText = '--';
}
// Destructuring 12:00 PM Result
const { set, val, result } = third_Data;
// Check time and get data
setThird.innerText = set;
valueThird.innerText = val;
resultThird.innerText = result;
} catch (error) {
return false;
}
}, 3000);
};
twelve_pm();
// // 4:30 PM Request Data
const pjk_Meow_3 = async () => {
const base = 'aHR0cHM6Ly90d29kLml0';
const cors = atob('aHR0cHM6Ly9jb3J');
const pjk_meow = atob(base);
// Set default time what I want it to.
let d_time_1 = '04:35:00 PM';
let d_time_2 = '16:35:00 PM';
let current_time = new Date().toLocaleTimeString();
// let current_time = new Date().getHours();
try {
if (current_time === d_time_1 || current_time >= d_time_2) {
try {
const res = await fetch(cors + pjk_meow);
const data = await res.json();
// Working with LS
let data_serialized = JSON.stringify(data);
localStorage.setItem('evening', data_serialized);
return data_serialized;
} catch (error) {
console.log(error.message);
}
}
} catch (error) {
return false;
}
};
pjk_Meow_3();
// 4:30 PM Callback and output to html
const four_pm = () => {
setInterval(() => {
try {
let second_Data = JSON.parse(localStorage.getItem('evening'));
const setSec = document.getElementById('setSec');
const valueSec = document.getElementById('valueSec');
const resultSec = document.getElementById('resultSec');
// If there is no any data in LS, this condition will run
if (localStorage.length <= 0 || localStorage === undefined) {
setSec.innerText = '------';
valueSec.innerText = '-------';
resultSec.innerText = '--';
}
// Destructuring 12:00 PM Result
const { set, val, result } = second_Data;
// Check time and get data
setSec.innerText = set;
valueSec.innerText = val;
resultSec.innerText = result;
} catch (error) {
return false;
}
}, 3000);
};
four_pm();
OR
Please take a look at some of the codes I wrote updated.
// Set the default time what I want to save to the localStorage
let d = new Date();
let h = d.getHours();
let m = d.getMinutes();
let compile = `${h}:${m}`;
try {
if (compile === '4:30' || compile >= '16:30') {
try {
const res = await fetch(cors + pjk_meow);
const data = await res.json();
// Working with LS
let data_serialized = JSON.stringify(data);
localStorage.setItem('evening', data_serialized);
return data_serialized;
} catch (error) {
console.log(error.message);
}
}
} catch (error) {
return false;
}
I am trying to get a user's input from a search bar, and use it to get the drink information from a cocktail API, but I am not getting the results back.
If I use a real value for example, Margarita, instead of the placeholder ${drinkName} in my query URL I do get the data I'm looking for in the object form I'm looking for, but something about how I'm getting/using the user input is wrong. I do get the user's input from the search in the console when I console log drinkName, but it doesn't seem to pass that into the searchByName function, and console logging cocktail after the searchByName function returns my empty cocktail object.
let cocktail = {};
const searchByName = (drinkName) => {
fetch(`https://www.thecocktaildb.com/api/json/v1/1/search.php?s=${drinkName}`)
.then(
function(response) {
if (response.status !== 200) {
console.log('Looks like there was a problem. Status Code: ' +
response.status);
return data;
}
response.json().then(function(data) {
console.log(data);
getDrinkName(data);
getIngredients(data);
getDirections(data);
getImage(data);
buildCocktail(data);
console.log(cocktail);
});
}
)
.catch(function(err) {
console.log('Fetch Error :-S', err);
});
}
const getImage = (data) => {
const imageUrl = (data.drinks[0].strDrinkThumb);
return imageUrl;
}
const getDrinkName = (data) => {
const name = (data.drinks[0].strDrink);
return name;
}
const getIngredients = (data) => {
let ingredientList = [];
for (let i = 1; i < 16; i++) {
if (data.drinks[0][`strIngredient${i}`] == null){
break;
} else {
const ingredients = ((data.drinks[0][`strMeasure${i}`]) + ': ' + data.drinks[0][`strIngredient${i}`]);
ingredientList.push(ingredients);
}
}
return ingredientList;
}
const getDirections = (data) => {
const directions = (data.drinks[0].strInstructions);
return directions;
}
const buildCocktail = (data) => {
cocktail.image = getImage(data);
cocktail.name = getDrinkName(data);
cocktail.ingredients = getIngredients(data);
cocktail.directions = getDirections(data);
console.log(cocktail);
}
$("#search").keypress(function(event) {
if (event.which === 13) {
const drinkName = $('#search').val();
console.log(drinkName);
searchByName(drinkName);
console.log(cocktail);
}
});
The code blow can detect faces, write scores, with no errors and warnings but it's working without recognition >(label name) and I want detect and recognition face if is in label.
How can I do that?
<script>
let forwardTimes = []
let withFaceLandmarks = false
let withBoxes = true
function onChangeWithFaceLandmarks(e) {
withFaceLandmarks = $(e.target).prop('checked')
}
function onChangeHideBoundingBoxes(e) {
withBoxes = !$(e.target).prop('checked')
}
function updateTimeStats(timeInMs) {
forwardTimes = [timeInMs].concat(forwardTimes).slice(0, 30)
const avgTimeInMs = forwardTimes.reduce((total, t) => total + t) / forwardTimes.length
$('#time').val(`${Math.round(avgTimeInMs)} ms`)
$('#fps').val(`${faceapi.utils.round(1000 / avgTimeInMs)}`)
}
async function onPlay(videoEl) {
if(!videoEl.currentTime || videoEl.paused || videoEl.ended || !isFaceDetectionModelLoaded())
return setTimeout(() => onPlay(videoEl))
const options = getFaceDetectorOptions()
const ts = Date.now()
const drawBoxes = withBoxes
const drawLandmarks = withFaceLandmarks
let task = faceapi.detectAllFaces(videoEl, options)
task = withFaceLandmarks ? task.withFaceLandmarks() : task
const results = await task
updateTimeStats(Date.now() - ts)
const canvas = $('#overlay').get(0)
const dims = faceapi.matchDimensions(canvas, videoEl, true)
const resizedResults = faceapi.resizeResults(results, dims)
if (drawBoxes) {
faceapi.draw.drawDetections(canvas, resizedResults)
}
if (drawLandmarks) {
faceapi.draw.drawFaceLandmarks(canvas, resizedResults)
}
setTimeout(() => onPlay(videoEl))
}
async function run() {
// load face detection and face landmark models
await changeFaceDetector(TINY_FACE_DETECTOR)
await faceapi.loadSsdMobilenetv1Model('/')
await faceapi.loadFaceRecognitionModel('/')
await faceapi.loadFaceLandmarkModel('/')
changeInputSize(416)
// start processing frames
const labels = ['1','2']
const labeledFaceDescriptors = await Promise.all(
labels.map(async label => {
// fetch image data from urls and convert blob to HTMLImage element
const imgUrl = `picture/${label}.JPG`
const img = await faceapi.fetchImage(imgUrl)
// detect the face with the highest score in the image and compute it's landmarks and face descriptor
const fullFaceDescription = await faceapi.detectSingleFace(img).withFaceLandmarks().withFaceDescriptor()
if (!fullFaceDescription) {
throw new Error(`no faces detected for ${label}`)
}
const faceDescriptors = [fullFaceDescription.descriptor]
console.log(label)
return new faceapi.LabeledFaceDescriptors(label, faceDescriptors)
})
)
const input = document.getElementById('inputVideo')
const fullFaceDescriptions = await faceapi.detectAllFaces(input).withFaceLandmarks().withFaceDescriptors()
// 0.6 is a good distance threshold value to judge
// whether the descriptors match or not
const maxDescriptorDistance = 0.6
const faceMatcher = new faceapi.FaceMatcher(labeledFaceDescriptors, maxDescriptorDistance)
console.log("face matcher"+faceMatcher)
const results = fullFaceDescriptions.map(fd => faceMatcher.findBestMatch(fd.descriptor))
results.forEach((bestMatch, i) => {
const box = fullFaceDescriptions[i].detection.box
const text = bestMatch.toString()
const drawBox = new faceapi.draw.DrawBox(box, { label: text })
console.log("last")
})
// results
onPlay($('#inputVideo').get(0))
}
function updateResults() {}
$(document).ready(function() {
renderNavBar('#navbar', 'video_face_tracking')
initFaceDetectionControls()
run()
})
</script>
I have a firebase cloud function that is unable to finish executing. I suspect my code can be dramatically improved but I'm not quite sure how.
I've made the query as specific as possible to try and reduce the number of documents required to iterate through but that didn't solve the issue.
I get a Deadline Exceeded error which I suspect is due to the fact that I'm iterating through so many documents and trying to update them.
I increased the timeout (9 minutes)and memory allocation (2GB) in Google cloud console but that didn't help either.
exports.updatePollWinner = functions.runWith(runtimeOpts).firestore.document('triggerAccuracyCalculation/{id}').onCreate(trigger => {
const week = trigger.get('week');
const scoringTags = ["STD", "0.25PPR", "0.5PPR", "PPR", "0.10PPC", "0.25PPC", "0.5PPC", "4PTPASS", "5PTPASS", "6PTPASS", "-2INT", "TEPREMIUM"]
let winningChoiceIds = [];
let totalPollIds = [];
return db.collection("polls").where("sport", "==", 1).where("week", "==", week).where("pollType", "==", "WDIS").get()
.then((querySnapshot) => {
console.log("A");
querySnapshot.forEach((doc) => {
totalPollIds.push(doc.id);
let pollData = doc.data();
// extract relevant scoring tags
let tags = pollData.tags.filter(tag => scoringTags.includes(tag.code)).map(tag => tag.code);
// if no scoring setting is tagged, then use STD - determine what STD is
// extract player from each option
let winner = {score: 0, choice: {}, choiceId: null};
let cnt = 0;
pollData.choices.forEach((choice) => {
let choiceId = choice.id
let mappedChoices = choice.players.map(player => {
return { displayName: player.displayName, playerId: player.playerId, position: player.position, team: player.team }
});
// ToDo: What happens if someone posts a poll with two players in one option? This poll should be ignoree from accuracy calculation
// ignmore if option has more than one player
// if (mappedChoices.length > 1) return;
const player = mappedChoices[0]
// We can't score defense
if (player.position === "DEF") {
return;
}
const playerId = player.playerId;
// Make FFN API call to retrieve stats for that player in that weekconst statsEndpoint = `https://www.fantasyfootballnerd.com/service/player/json/${functions.config().ffnerd.key}${req.url}`;
const statsEndpoint = `https://www.fantasyfootballnerd.com/service/player/json/${functions.config().ffnerd.key}/${playerId}`;
const json = {"Stats": {"2019": ""}, "Player": {}};
https.get(statsEndpoint, (resp) => {
let data = '';
resp.on('data', (chunk) => {
data += chunk;
});
resp.on('end', () => {
const weekString = week.toString();
const fetchedStats = JSON.parse(data).Stats
if (!fetchedStats) return;
const response = fetchedStats["2019"]
if (!response) return;
// TODO SCORE KICKERS AND DEFENSES
const stats = response[weekString];
let score = 0;
stats["recYards"] ? score += parseInt(stats["recYards"]) / 10 : false
stats["recTD"] ? score += parseInt(stats["recTD"]) * 6 : false
stats["rushYards"] ? score += parseInt(stats["rushYards"]) / 10 : false
stats["rushTD"] ? score += parseInt(stats["rushTD"]) * 6 : false
stats["xpMade"] ? score += parseInt(stats["xpMade"]) : false
stats["fgMade"] ? score += parseInt(stats["fgMade"]) * 3 : false
stats["kickoffRet"] ? score += parseInt(stats["kickoffRet"]) / 10 : false
stats["SackYards"] ? score -= parseInt(stats["SackYards"]) / 10 : false
stats["fumbleLost"] ? score -= parseInt(stats["fumbleLost"]) * 2 : false
// Determine winner
// ToDo: handle ties
if (score > winner.score) {
winner.score = score;
winner.choiceId = choiceId;
winner.choice = choice;
}
if (cnt>=pollData.choices.length-1){
// Save player object on the poll Document (include choice ID)
winningChoiceIds.push(winner.choiceId);
const pollDoc = db.doc(`polls/${doc.id}`);
pollDoc.update({winner: winner});
}
cnt++;
});
}).on("error", (err) => {
console.log("Error: ", err.message);
});
});
});
console.log("B");
return false;
}).then(() => {
console.log("C");
let dateToQueryAfter = new Date(new Date("08/22/19").setHours(0,0,0,0))
return db.collection("users").where("recentVote", ">", dateToQueryAfter).get()
})
.then((querySnapshot) => {
console.log("D");
const promises = [];
querySnapshot.forEach((doc) => {
const p = db.collection("votes").where("uid", "==", doc.id).where("week", "==", week).where("pollType", "==", "WDIS").get()
promises.push(p)
});
return Promise.all(promises)
})
.then((querySnapshots) => {
console.log("E");
querySnapshots.forEach((querySnapshot) => {
if (querySnapshot.docs.length <= 0) return;
const uid = querySnapshot.docs[0].data().uid
const retrieveUserDoc = db.doc(`users/${uid}`);
let correctVotes = 0;
let cnt = 0;
let totalVotes = 0;
let pollVoteIds = [];
let pollVoteIdsCorrect = [];
querySnapshot.docs.forEach((doc) => {
const voteData = doc.data();
if (totalPollIds.includes(voteData.poll)) {
pollVoteIds.push(voteData.poll)
totalVotes++;
if (winningChoiceIds.includes(voteData.choice)) {
pollVoteIdsCorrect.push(voteData.poll)
correctVotes++;
}
}
if (cnt>=querySnapshot.size-1){
console.log("Updating user ID: ", uid);
retrieveUserDoc.update({
['accuracyWeeks.week'+week]: true,
['accuracy.week'+week]: {
totalVotes: totalVotes,
correct: correctVotes,
accuracy: correctVotes/totalVotes,
correctVoteIds: pollVoteIdsCorrect,
totalVoteIds: pollVoteIds
}
});
}
cnt++;
})
});
console.log("F");
return false;
})
.catch((error) => {
console.log("Error getting documents: ", error);
});
});