web3-api-utils
Version:
utility funtions to get api names for different api providers
270 lines (239 loc) • 7.65 kB
JavaScript
import { raribleIdMap,
raribleTestnetIdMap,
alchemyIdMap,
websiteBaseUrls,
websiteTestnetBaseUrls,
openseaIdMap,
openseaTestnetIdMap,
scannerUrls
} from "./mappings.mjs";
import {
// fetchChain,
getChainIdsBySlug} from "@thirdweb-dev/chains"
import { getChainById } from 'eth-chainlist';
//https://www.npmjs.com/package/eth-chainlist
// rawChainData will return all the chain data in JSON format.
// getChainById takes in a chain id and will return you a single chain
// getChainByNetworkId takes in a network id and will return you a single chain
// getChainByName takes in a name and will return you a single chain
// getChainByShortName takes in a short name and will return you a single chain
export function getChainData(id, apiName) {
console.log('isValidChainId', isValidChainId(id))
if(isValidChainId(id)){
return getChainById(id)
}
if(isValidChainSlug(slug)){
return getChainBySlug(slug)
}
throw new Error(`unable to find chain using "${id}" not found`);
}
export function getApiSlug(id, apiName, isTestnet){
if(apiName === "alchemy"){
return alchemyIdMap[id];
}
if(apiName === "rarible"){
const mainNetsResult = raribleIdMap[id]
if(mainNetsResult){
return raribleIdMap[id];
}
// fallback to testnet
return raribleTestnetIdMap[id];
}
// if(apiName === "rarible" && !isTestnet){
// return raribleIdMap[id];
// }
// if(apiName === "rarible"){ // testnets
// return raribleTestnetIdMap[id];
// }
}
/**
* CHECK FOR TESTNETS
* @ddev this is quite basic for the moment, preparing for future
* @param {*} id
* @param {*} apiName
* @returns
*/
// fallback to be deprecated
export function isTestnet(id, apiName){
return chainIsTestnet(id, apiName);
}
export function chainIsTestnet(chainId, apiName){
// uses rarible testnet map as basic info
if(raribleTestnetIdMap[chainId]){
return true
} else {
return false;
}
}
export function getChainByChainId(chainId) {
// console.log('- chainId', chainId)
// console.log('- getChainById[chainId]', getChainById(chainId))
if (getChainById(chainId)) {
return getChainById(chainId)
}
if (isValidChainId(chainId)) {
// const chainsById = getChainsById();
return getChainById[chainId];
}
throw new Error(`Chain with chainId "${chainId}" not found`);
}
/**
* GET CHAIN ID
* arapper to fallback more detailed methods
*/
export function getChainId(input, apiName, isTestnet) {
if(!isNaN(Number(input))){
return Number(input)
}
let lowerCaseSlug = input?.toLowerCase()
if (isValidChainSlug(lowerCaseSlug)) {
const chainIdsBySlug = getChainIdsBySlug();
return chainIdsBySlug[lowerCaseSlug];
}
if(apiName){
return lookupIdBySlug(input, apiName)
}
//lastly
throw new Error(`Chain with vlaue "${value}" not found.`);
}
export function getChainBySlug(slug) {
if (isValidChainSlug(slug)) {
const chainIdsBySlug = getChainIdsBySlug();
return getChainById[chainIdsBySlug[slug]];
}
throw new Error(`Chain with slug "${slug}" not found`);
}
export function lookupIdBySlug(slug, apiName, isTestnet) {
console.log('::lookupIdBySlug::', slug)
// handle case where the slug is already a chain id
if(!isNaN(Number(slug))){
return Number(slug)
}
let bySlug = getChainBySlug(slug);
console.log('bySlug', bySlug)
if(apiName === 'rarible'){
const mainNetsResult = !isTestnet && getKeyByValue(raribleIdMap, slug)
if(mainNetsResult){
return mainNetsResult;
}
// fallback to testnet
const testNetsResult = getKeyByValue(raribleIdMap, slug)
if(testNetsResult){
return testNetsResult;
}
throw new Error(`Chain with slug "${slug}" not found`);
}
}
/**return all chains as object, slug, or id */
export function getChainsArray(mode='slug', apiName, isTestnet) {
let mappingObject;
if(apiName === 'rarible' && isTestnet){
mappingObject = raribleTestnetIdMap
}
if(apiName === 'rarible' && !isTestnet){
mappingObject = raribleIdMap
}
if(apiName === 'alchemy'){
mappingObject = alchemyIdMap
}
if(mode === 'object'){
return Object.entries(mappingObject).map(([key, value]) => {return {id: key, slug: value}})
}
return mode === 'slug' ?
Object.values(mappingObject).map((value, i) => value)
:
Object.keys(mappingObject).map((key, i) => {
return key && Number(key)
});
}
function getKeyByValue(object, value) {
return Object.keys(object).find(key => object[key] === value);
}
function isValidChainId(chainId){
// if(allChains)
// const chainsById = getChainsById(chainId);
if(getChainById(chainId)){
return true
} else {
return false
}
// return chainId in chainsById;
}
function isValidChainSlug(slug){
const chainIdsBySlug = getChainIdsBySlug();
return slug in chainIdsBySlug;
}
/**
* URL UTILS: item
* generate endpoint given chainid, tokenId, contractAddress, and apiname
*/
export function getWebsiteItemUrl({apiName, tokenId, contractAddress, chainId}){
let localChainId = getChainId(chainId)
let isTestnet = chainIsTestnet(localChainId, apiName);
if(apiName === 'rarible'){
const chainSlug = raribleIdMap[localChainId] || raribleTestnetIdMap[localChainId];
const baseUrl = isTestnet ? websiteTestnetBaseUrls[apiName] : websiteBaseUrls[apiName];
const url = `${baseUrl}/token/${chainSlug?.toLowerCase()}/${contractAddress}:${tokenId}`
return url;
}
if(apiName === 'opensea'){
const chainSlug = openseaIdMap[localChainId] || openseaTestnetIdMap[localChainId];
if(!chainSlug){
throw new Error(`Unable to find opensea chainSlug Chain with vlaue "${localChainId}".`);
}
const baseUrl = isTestnet ? websiteTestnetBaseUrls[apiName] : websiteBaseUrls[apiName];
const url = `${baseUrl}/assets/${chainSlug}/${contractAddress}/${tokenId}`
return url
}
return {
status: 'notfound',
message: "website Item Url not found"
}
}
/**
* URL UTILS: contract
* generate endpoint given chainid, contractAddress, and apiname
*/
export function getWebsiteContractUrl({apiName, contractAddress, chainId}){
let localChainId = getChainId(chainId)
let isTestnet = chainIsTestnet(localChainId, apiName);
if(apiName === 'rarible'){
const chainSlug = raribleIdMap[localChainId] || raribleTestnetIdMap[localChainId];
const baseUrl = isTestnet ? websiteTestnetBaseUrls[apiName] : websiteBaseUrls[apiName];
const url = `${baseUrl}/collection/${chainSlug?.toLowerCase()}/${contractAddress}`
return url;
}
if(apiName === 'opensea'){
return {
status: 'error',
message: "opensea collection not supported"
}
// const chainSlug = openseaIdMap[localChainId] || openseaTestnetIdMap[localChainId];
// if(!chainSlug){
// throw new Error(`Unable to find opensea chainSlug Chain with vlaue "${localChainId}".`);
// }
// const baseUrl = isTestnet ? websiteTestnetBaseUrls[apiName] : websiteBaseUrls[apiName];
// const url = `${baseUrl}/collections/${chainSlug}/${contractAddress}`
// return url
}
return {
status: 'notfound',
message: "website Collection Url not found"
}
}
/**
* GET SCANNER CONTRACT URL
* @param {*} contractAddress contract address
* @param {*} chainId chain id or slug
* @returns string
*/
export function getScannerContractUrl({contractAddress, chainId}){
let localChainId = getChainId(chainId)
if(scannerUrls[localChainId]){
return `${scannerUrls[localChainId]}address/${contractAddress}`
}
return {
status: 'notfound',
message: "Scanner Url not found"
}
}