-
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: calculate arbitrum block number for start of epoch #71
Changes from 4 commits
19747a4
218dab0
bf30fc6
40eac1a
d7da134
e94bfcc
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,6 @@ | ||
export class BlockNumberServiceRequiredError extends Error { | ||
constructor() { | ||
super("BlockNumberService is required to get the current epoch"); | ||
this.name = "BlockNumberServiceRequiredError"; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
import { UnsupportedChain } from "@ebo-agent/blocknumber"; | ||
import { BlockNumberService, UnsupportedChain } from "@ebo-agent/blocknumber"; | ||
import { Caip2ChainId, HexUtils, UnixTimestamp } from "@ebo-agent/shared"; | ||
import { | ||
Address, | ||
|
@@ -41,6 +41,7 @@ import { | |
oracleAbi, | ||
} from "../abis/index.js"; | ||
import { | ||
BlockNumberServiceRequiredError, | ||
ErrorFactory, | ||
InvalidAccountOnClient, | ||
InvalidBlockHashError, | ||
|
@@ -75,6 +76,7 @@ export class ProtocolProvider implements IProtocolProvider { | |
private l1ReadClient: PublicClient<FallbackTransport<HttpTransport[]>>; | ||
private l2ReadClient: PublicClient<FallbackTransport<HttpTransport[]>>; | ||
private l2WriteClient: WalletClient<FallbackTransport<HttpTransport[]>>; | ||
private readonly blockNumberService?: BlockNumberService; | ||
|
||
private oracleContract: GetContractReturnType< | ||
typeof oracleAbi, | ||
|
@@ -111,18 +113,21 @@ export class ProtocolProvider implements IProtocolProvider { | |
* @param rpcConfig The configuration for RPC connections including URLs, timeout, retry interval, and transaction receipt confirmations | ||
* @param contracts The addresses of the protocol contracts that will be instantiated | ||
* @param privateKey The private key of the account that will be used to interact with the contracts | ||
* @param blockNumberService The service that will be used to fetch block numbers | ||
*/ | ||
constructor( | ||
private readonly rpcConfig: ProtocolRpcConfig, | ||
contracts: ProtocolContractsAddresses, | ||
privateKey: Hex, | ||
blockNumberService: BlockNumberService, | ||
) { | ||
const l1Chain = this.getViemChain(rpcConfig.l1.chainId); | ||
const l2Chain = this.getViemChain(rpcConfig.l2.chainId); | ||
|
||
this.l1ReadClient = this.createReadClient(rpcConfig.l1, l1Chain); | ||
this.l2ReadClient = this.createReadClient(rpcConfig.l2, l2Chain); | ||
this.l2WriteClient = this.createWriteClient(rpcConfig.l2, l2Chain, privateKey); | ||
this.blockNumberService = blockNumberService; | ||
|
||
// Instantiate all the protocol contracts | ||
this.oracleContract = getContract({ | ||
|
@@ -269,6 +274,10 @@ export class ProtocolProvider implements IProtocolProvider { | |
* @returns {Promise<Epoch>} The current epoch, its block number, and its timestamp. | ||
*/ | ||
async getCurrentEpoch(): Promise<Epoch> { | ||
if (!this.blockNumberService) { | ||
throw new BlockNumberServiceRequiredError(); | ||
} | ||
|
||
const [epoch, epochFirstBlockNumber] = await Promise.all([ | ||
this.epochManagerContract.read.currentEpoch(), | ||
this.epochManagerContract.read.currentEpochBlock(), | ||
|
@@ -278,10 +287,18 @@ export class ProtocolProvider implements IProtocolProvider { | |
blockNumber: epochFirstBlockNumber, | ||
}); | ||
|
||
const startTimestamp = epochFirstBlock.timestamp as UnixTimestamp; | ||
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. Let's make the 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. ✅ |
||
|
||
const l2ChainId = this.rpcConfig.l2.chainId; | ||
const l2FirstBlockNumber = await this.blockNumberService.getEpochBlockNumber( | ||
startTimestamp, | ||
l2ChainId, | ||
); | ||
|
||
return { | ||
number: epoch, | ||
firstBlockNumber: epochFirstBlockNumber, | ||
startTimestamp: epochFirstBlock.timestamp as UnixTimestamp, | ||
firstBlockNumber: l2FirstBlockNumber, | ||
startTimestamp: startTimestamp, | ||
}; | ||
} | ||
|
||
|
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.
You should be able to drop this with the new changes! I think you'll need to add the optionality for
BlockNumberService
in theProtocolProvider
constructorThere 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.
oops this got filtered out in the merge