Created
March 23, 2022 09:01
-
-
Save lukekim/d27829badb5ee2044b8228d6bfdc3edb to your computer and use it in GitHub Desktop.
spice.xyz Ethers.js provider
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict" | |
import { | |
UrlJsonRpcProvider, | |
showThrottleMessage, | |
} from "@ethersproject/providers" | |
import { Logger } from "@ethersproject/logger" | |
const logger = new Logger("providers/5.5.1") | |
const defaultApiKey = "3439|6806e8faf10643e2bde903920bbcb1ff" | |
export class SpiceProvider extends UrlJsonRpcProvider { | |
static getApiKey(apiKey) { | |
if (apiKey == null) { | |
return defaultApiKey | |
} | |
if (apiKey && typeof apiKey !== "string") { | |
logger.throwArgumentError("invalid apiKey", "apiKey", apiKey) | |
} | |
return apiKey | |
} | |
static getUrl(network, apiKey) { | |
const host = "https://data.spiceai.io" | |
let path = null | |
switch (network.name) { | |
case "homestead": | |
path = "/eth" | |
break | |
default: | |
logger.throwArgumentError( | |
"unsupported network", | |
"network", | |
arguments[0] | |
) | |
} | |
return { | |
allowGzip: true, | |
url: host + path, | |
throttleCallback: (attempt, url) => { | |
if (apiKey === defaultApiKey) { | |
showThrottleMessage() | |
} | |
return Promise.resolve(true) | |
}, | |
} | |
} | |
isCommunityResource() { | |
return this.apiKey === defaultApiKey | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment