Skip to content

Instantly share code, notes, and snippets.

@jotto
Last active January 31, 2022 08:36
Show Gist options
  • Save jotto/b8ba22841d3a4d4bba2142aeae3b763c to your computer and use it in GitHub Desktop.
Save jotto/b8ba22841d3a4d4bba2142aeae3b763c to your computer and use it in GitHub Desktop.
prints TSV of defi assets and exchange they're on
// prerequisites: node.js installed on your computer
// step 1: make a directory on your computer called "defi-assets-and-their-exchanges"
// step 2: copy and paste this file into that directory, call it "index.js"
// step 3: npm install --save got
// step 4: paste your messari API key into the empty string below
// step 5: node index.js
const YOUR_PERSONAL_MESSARI_API_KEY = "";
const got = require("got");
const uniqFilter = (value, index, self) => self.indexOf(value) === index;
const getMeridian = (path) =>
got
.get(`https://data.messari.io${path}`, {
headers: { "x-messari-api-key": YOUR_PERSONAL_MESSARI_API_KEY },
})
.json();
const getAllAssets = (page = 1, results = []) => {
console.error("getting /api/v2/assets page", page);
return getMeridian(
`/api/v2/assets?fields=id,name,symbol,slug,metrics/marketcap/current_marketcap_usd,metrics/market_data/price_usd,metrics/market_data/volume_last_24_hours,profile/general/overview/tags&limit=1500&page=${page}&sort=id`
)
.then((res) => {
if (res.data && res.data.length > 0) {
// return results.concat(res.data);
return getAllAssets(page + 1, results.concat(res.data));
} else {
return results.concat(res.data);
}
})
.catch((err) => {
// probably a 404, or the last page
return results;
});
};
const getAllAssetsWithTagsOrSlugs = (tags = [], slugs = []) =>
getAllAssets().then((assets) =>
assets.filter(
(a) =>
a.metrics &&
a.metrics.market_data &&
a.metrics.market_data.price_usd != null &&
((a.profile &&
a.profile.general &&
a.profile.general.overview &&
a.profile.general.overview.tags != null &&
tags.includes(a.profile.general.overview.tags)) ||
slugs.includes(a.slug))
)
);
const getExchangeSlugsForAssetId = (assetId) => {
console.log("getting exchanges for", assetId);
return getMeridian(
`/api/v1/assets/${assetId}/markets?fields=exchange_id,exchange_slug`
).then((res) => res.data.map((m) => m.exchange_slug));
};
const exchangeSlugColumns = [
"coinbase",
"kraken",
"gemini",
"binance",
"binanceus",
"huobi",
"okx",
"ftx",
"bitfinex",
"upbit",
];
const addExchanges = (assets) =>
Promise.all(
assets.map((asset) => {
return getExchangeSlugsForAssetId(asset.id).then((exchangeSlugs) => {
const exchangeCols = exchangeSlugColumns.map((slug) => {
return exchangeSlugs.indexOf(slug) !== -1 ? true : false;
});
return Object.assign({}, asset, { exchanges: exchangeCols });
});
})
);
getAllAssetsWithTagsOrSlugs(["DeFi"], ["band", "link"])
.then(addExchanges)
.then((res) => {
const headers = [
"id",
"name",
"symbol",
"price",
"reported marketcap",
].concat(exchangeSlugColumns);
const csv = [headers.join("\t")].concat(
res.map((a) =>
[
a.id,
a.name,
a.symbol,
a.metrics.market_data.price_usd,
a.metrics.marketcap.current_marketcap_usd,
]
.concat(a.exchanges)
.join("\t")
)
);
console.log(csv.join("\n"));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment