Skip to content

Instantly share code, notes, and snippets.

@lukekim
Created March 23, 2022 09:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lukekim/d27829badb5ee2044b8228d6bfdc3edb to your computer and use it in GitHub Desktop.
Save lukekim/d27829badb5ee2044b8228d6bfdc3edb to your computer and use it in GitHub Desktop.
spice.xyz Ethers.js provider
"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