-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add blockmeta block number provider #37
Changes from 2 commits
da24531
719df23
e2d952f
4819329
2b793cb
c56901f
80ee81f
80e05bf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export class UndefinedBlockNumber extends Error { | ||
constructor(isoTimestamp: string) { | ||
super(`Undefined block number at ${isoTimestamp}.`); | ||
|
||
this.name = "UndefinedBlockNumber"; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ import { FallbackTransport, HttpTransport, PublicClient } from "viem"; | |
import { UnsupportedChain } from "../exceptions/unsupportedChain.js"; | ||
import { Caip2ChainId } from "../types.js"; | ||
import { Caip2Utils } from "../utils/index.js"; | ||
import { BlockmetaJsonBlockNumberProvider } from "./blockmetaJsonBlockNumberProvider.js"; | ||
import { EvmBlockNumberProvider } from "./evmBlockNumberProvider.js"; | ||
|
||
const DEFAULT_PROVIDER_CONFIG = { | ||
|
@@ -16,20 +17,24 @@ export class BlockNumberProviderFactory { | |
* Build a `BlockNumberProvider` to handle communication with the specified chain. | ||
* | ||
* @param chainId CAIP-2 chain id | ||
* @param client a viem public client | ||
* @param evmClient a viem public client | ||
* @param logger a ILogger instance | ||
* @returns | ||
*/ | ||
public static buildProvider( | ||
chainId: Caip2ChainId, | ||
client: PublicClient<FallbackTransport<HttpTransport[]>>, | ||
evmClient: PublicClient<FallbackTransport<HttpTransport[]>>, | ||
blockmetaConfig: { baseUrl: URL; servicePath: string; bearerToken: string }, | ||
logger: ILogger, | ||
) { | ||
const chainNamespace = Caip2Utils.getNamespace(chainId); | ||
|
||
switch (chainNamespace) { | ||
case EBO_SUPPORTED_CHAINS_CONFIG.evm.namespace: | ||
return new EvmBlockNumberProvider(client, DEFAULT_PROVIDER_CONFIG, logger); | ||
return new EvmBlockNumberProvider(evmClient, DEFAULT_PROVIDER_CONFIG, logger); | ||
|
||
case EBO_SUPPORTED_CHAINS_CONFIG.solana.namespace: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Solana 👀 going shopping for those 200GB ram for running validators xd There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. xd |
||
return new BlockmetaJsonBlockNumberProvider(blockmetaConfig, logger); | ||
|
||
default: | ||
throw new UnsupportedChain(chainId); | ||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,126 @@ | ||||||
import { ILogger, Timestamp } from "@ebo-agent/shared"; | ||||||
import axios, { AxiosInstance, AxiosResponse, isAxiosError } from "axios"; | ||||||
|
||||||
import { UndefinedBlockNumber } from "../exceptions/undefinedBlockNumber.js"; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess we should import these from index.js but we can clean up later There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yep, got a task already for that! 👌 |
||||||
import { BlockNumberProvider } from "./blockNumberProvider.js"; | ||||||
|
||||||
type BlockByTimeResponse = { | ||||||
num: string; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe this should be blockNum so it's a bit more descriptive--I had to see where it's being used to understand the type There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is based on the blockmeta |
||||||
id: string; | ||||||
time: string; | ||||||
}; | ||||||
|
||||||
export type BlockmetaClientConfig = { | ||||||
baseUrl: URL; | ||||||
servicePath: string; | ||||||
bearerToken: string; | ||||||
}; | ||||||
|
||||||
/** | ||||||
* Consumes the blockmeta.BlockByTime substreams' service via HTTP POST JSON requests to provide | ||||||
* block numbers based on timestamps | ||||||
* | ||||||
* Refer to these web pages for more information: | ||||||
* * https://thegraph.market/ | ||||||
* * https://substreams.streamingfast.io/documentation/consume/authentication | ||||||
*/ | ||||||
export class BlockmetaJsonBlockNumberProvider implements BlockNumberProvider { | ||||||
private readonly axios: AxiosInstance; | ||||||
|
||||||
constructor( | ||||||
private readonly options: BlockmetaClientConfig, | ||||||
private readonly logger: ILogger, | ||||||
) { | ||||||
const { baseUrl, bearerToken } = options; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is this a long lived Bearer Token like an API Key? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, people have to get this token through the The Graph webapp which is kinda weird as they have no documented endpoint to automate it. But now that you mention, we should definitely notify when this token is going to expire, even if it's an extremely long-lived token (1 year). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Config or options ? 🤣 |
||||||
|
||||||
this.axios = axios.create({ | ||||||
baseURL: baseUrl.toString(), | ||||||
headers: { | ||||||
common: { | ||||||
"Content-Type": "application/json", | ||||||
Authorization: `Bearer ${bearerToken}`, | ||||||
}, | ||||||
}, | ||||||
}); | ||||||
} | ||||||
|
||||||
/** @inheritdoc */ | ||||||
async getEpochBlockNumber(timestamp: Timestamp): Promise<bigint> { | ||||||
if (timestamp > Number.MAX_SAFE_INTEGER || timestamp < Number.MIN_SAFE_INTEGER) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Number.MIN_SAFE_INTEGER = -9007199254740991 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just for fun I've requested the
There's probably a weird overflow bug in substreams lol should we report it? Doesn't seem too big of an issue as no sane person would try to fetch blocks before computers even existed There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmmm, maybe we should just tell the graph team on slack |
||||||
throw new RangeError(`Timestamp ${timestamp.toString()} cannot be casted to a Number.`); | ||||||
|
||||||
const timestampNumber = Number(timestamp); | ||||||
const isoTimestamp = new Date(timestampNumber).toISOString(); | ||||||
|
||||||
try { | ||||||
// Try to get the block number at a specific timestamp | ||||||
const blockNumberAt = await this.getBlockNumberAt(isoTimestamp); | ||||||
|
||||||
return blockNumberAt; | ||||||
} catch (err) { | ||||||
const isAxios404 = isAxiosError(err) && err.status === 404; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. considering this is an axios error are we sure it shouldn't be err.response.status? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's an interesting question. I cannot find any doc describing the error properties, although checking its source code it looks like the I'll change to the more explicit property access though, as you suggested 👌 Feels like it removes any ambiguous interpretation! |
||||||
const isUndefinedBlockNumber = err instanceof UndefinedBlockNumber; | ||||||
|
||||||
if (!isAxios404 && !isUndefinedBlockNumber) throw err; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. would we want to also handle Unauthorized errors? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. thinking on this, we could use some ep on constructor (like an echo get or smth simple) to check that credentials are working or else throw on constructor, wdyt? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's a nice idea. I'm generally reluctant about async-ing constructors but I think that by using a static method we could do something like: // BlockmetaJsonBlockNumberProvider.ts
async static initialize(config, ...) {
const provider = new BlockmetaJsonBlockNumberProvider();
const successfulConnection = await provider.testConnection();
if (successfulConnection) return provider;
else throw new Error("BANG");
} Wdyt? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is a pattern on our best practices :) https://dev.to/somedood/the-proper-way-to-write-async-constructors-in-javascript-1o8c There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lol we are aligned without even trying |
||||||
|
||||||
// If no block has its timestamp exactly equal to the specified timestamp, | ||||||
// try to get the most recent block before the specified timestamp. | ||||||
const blockNumberBefore = await this.getBlockNumberBefore(isoTimestamp); | ||||||
|
||||||
return blockNumberBefore; | ||||||
} | ||||||
} | ||||||
|
||||||
/** | ||||||
* Gets the block number at a specific timestamp. | ||||||
* | ||||||
* @param isoTimestamp ISO UTC timestamp | ||||||
* @throws { UndefinedBlockNumber } if request was successful but block number is invalid/not present | ||||||
* @throws { AxiosError } if request fails | ||||||
* @returns a promise with the block number at the timestamp | ||||||
*/ | ||||||
private async getBlockNumberAt(isoTimestamp: string): Promise<bigint> { | ||||||
const { servicePath } = this.options; | ||||||
|
||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should we validate isoTimestamp here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ended up passing around |
||||||
const response = await this.axios.post(`${servicePath}/At`, { time: isoTimestamp }); | ||||||
|
||||||
return this.parseBlockByTimeResponse(response, isoTimestamp); | ||||||
} | ||||||
|
||||||
/** | ||||||
* Gets the most recent block number before the specified timestamp. | ||||||
* | ||||||
* @param isoTimestamp ISO UTC timestamp | ||||||
* @throws { UndefinedBlockNumber } if request was successful but block number is invalid/not present | ||||||
* @throws { AxiosError } if request fails | ||||||
* @returns a promise with the most recent block number before the specified timestamp | ||||||
*/ | ||||||
private async getBlockNumberBefore(isoTimestamp: string): Promise<bigint> { | ||||||
const { servicePath } = this.options; | ||||||
|
||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ditto |
||||||
const response = await this.axios.post(`${servicePath}/Before`, { time: isoTimestamp }); | ||||||
|
||||||
return this.parseBlockByTimeResponse(response, isoTimestamp); | ||||||
} | ||||||
|
||||||
/** | ||||||
* Parse the BlockByTime response and extracts the block number. | ||||||
* | ||||||
* @param response an AxiosResponse of a request to BlockByTime endpoint | ||||||
* @param isoTimestamp the timestamp that was sent in the request | ||||||
* @returns | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What are we returning? |
||||||
*/ | ||||||
private parseBlockByTimeResponse(response: AxiosResponse, isoTimestamp: string) { | ||||||
const { data } = response; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we should specify what we're returning in the function def right? |
||||||
// TODO: validate with zod instead | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. hehehe |
||||||
const blockNumber = (data as BlockByTimeResponse)["num"]; | ||||||
|
||||||
if (blockNumber === undefined) { | ||||||
this.logger.error(`Couldn't find a block number for timestamp ${isoTimestamp}`); | ||||||
|
||||||
throw new UndefinedBlockNumber(isoTimestamp); | ||||||
} | ||||||
|
||||||
return BigInt(blockNumber); | ||||||
} | ||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export * from "./blockNumberProvider.js"; | ||
export * from "./blockNumberProviderFactory.js"; | ||
export * from "./blockmetaJsonBlockNumberProvider.js"; | ||
export * from "./evmBlockNumberProvider.js"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
im thinking if this should be a config object like:
and here validate that the proper client/config is defined depending on the switch case. passing an evmClient when instantiating a Solana's one is not necessary actually
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah it's kinda too "functional-y" as it is right now, this factory could probably be a instance initialized with the config you mention and then by defining the
new BlockNumberProviderFactory().build()
instance method, there's no need to pass the config as it's already inside the factory instance.