libethsync/src/client/client.ts

47 lines
1.2 KiB
TypeScript
Raw Normal View History

2023-07-10 20:44:58 +00:00
import BaseClient, { BaseClientOptions } from "#baseClient.js";
import { IProver, IVerifyingProviderConstructor } from "#interfaces.js";
import { IClientVerifyingProvider } from "#client/verifyingProvider.js";
2023-07-10 20:44:58 +00:00
interface Config extends BaseClientOptions {
prover: IProver;
provider: IVerifyingProviderConstructor<IClientVerifyingProvider>;
2023-07-10 20:44:58 +00:00
rpcHandler: Function;
}
export default class Client extends BaseClient {
protected declare options: Config;
constructor(options: Config) {
super(options);
}
private provider?: IClientVerifyingProvider;
2023-07-10 20:44:58 +00:00
async sync(): Promise<void> {
await super.sync();
if (!this.provider) {
2023-07-10 20:44:58 +00:00
const { blockHash, blockNumber } = await this.getNextValidExecutionInfo();
const factory = this.options.provider;
const provider = new factory(
this.options.rpcHandler,
blockNumber,
blockHash,
);
this.subscribe((ei) => {
console.log(
`Received a new blockheader: ${ei.blockNumber} ${ei.blockHash}`,
);
provider.update(ei.blockNumber, ei.blockHash);
});
this.provider = provider;
2023-07-10 20:44:58 +00:00
this.booted = true;
}
}
public async rpcCall(method: string, params: any) {
return this.provider?.rpcMethod(method, params);
}
2023-07-10 20:44:58 +00:00
}