Token Metrics: Market Data
const Web3Data = require("web3data-js@0.6.1")
// Create the Web3Data instances passing in your API Key (Get one! -> amberdata.io/onboarding)
// If you're signed in and have an api key it will get autopopulated here!
const w3d = new Web3Data(process.env.API_KEY)
// Contract we'll use as an example but you would but the address of your dApp here
const BAT_CONTRACT = '0x0d8775f648430679a709e98d2b0cb6250d2887ef'
// Get time from 1 day ago
let startDate = new Date()
startDate.setDate(startDate.getDate() - 1)
startDate = startDate.getTime()
// Get current price data for token contract
const currentPriceData = await w3d.market.getTokenPrices(BAT_CONTRACT)
console.log({currentPriceData})
// We can adjust the window using the 'startDate' & 'endDate' parameters
// for now we want historical prices over last 24 hours
const histPriceData = await w3d.market.getTokenPrices(BAT_CONTRACT, {startDate})
console.log({histPriceData})
// Watch for token price changes
w3d.connect()
w3d.on({eventName: 'market:prices', filters: { "pair": "bat_usd"}}, price => {
const time = (new Date(price.timestamp)).toLocaleString()
console.log(`Token price update ${price.price} at ${time}`)
})
// Watch for tokens being traded on an exchange
w3d.on({eventName: 'market:trades', filters: { "pair": "bat_usd"}}, ([trade]) => {
const time = (new Date(trade[2])).toLocaleString() // Human readable timestamp
const price = trade[5]
const volume = trade[6]
const type = trade[7] ? 'bought' : 'sold'
console.log(`${volume} BAT ${type} for ${price} at ${time}`)
})
no comments