Skip to content
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: custom rpc #4

Merged
merged 3 commits into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions examples/zkevm/get_batch_proof.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const { getZkEvmClient, zkEvm, from } = require('../utils_zkevm');

const execute = async () => {
const client = await getZkEvmClient();
let blockNum = 1;
let isParaent = false
let batchProof = await client.getBatchProof(blockNum, isParaent)
/***
* batchProof:
*
* {
* "block_number": "",
* "proof": "{
* "pi_a": "",
* "pi_b": "",
* "pi_c": "",
* "protocol": "",
* "curve": ""
* }",
* "pre_state_root": "0x",
* "post_state_root": "0x"
* }
*/
console.log("batchProof", batchProof);
}
execute().then(() => {
}).catch(err => {
console.error("err", err);
}).finally(_ => {
process.exit(0);
})
2 changes: 1 addition & 1 deletion examples/zkevm/get_block.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const { getZkEvmClient, zkEvm, from } = require('../utils_zkevm');
const execute = async () => {
const client = await getZkEvmClient();
let blockNum = 1;
let isParaent = true
let isParaent = false
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we change it to false?

let block = await client.getBlock(blockNum, isParaent)
console.log("block", block);

Expand Down
15 changes: 15 additions & 0 deletions examples/zkevm/get_block_by_number.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const { getZkEvmClient, zkEvm, from } = require('../utils_zkevm');

const execute = async () => {
const client = await getZkEvmClient();
let blockNum = 1;
let isParaent = false
let block = await client.getBlockByNumber(blockNum, isParaent)
console.log("block", block);
}
execute().then(() => {
}).catch(err => {
console.error("err", err);
}).finally(_ => {
process.exit(0);
})
4 changes: 3 additions & 1 deletion src/abstracts/base_web3_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ export abstract class BaseWeb3Client {
abstract getAccounts(): string[];
abstract signTypedData(signer: string, typedData: object): string;

abstract getBlockByNumber(blockNumber): string;
abstract getBatchProof(blockNumber): string;

getRootHash?(startBlock: number, endBlock: number) {
return this.sendRPCRequest({
jsonrpc: '2.0',
Expand Down Expand Up @@ -59,5 +62,4 @@ export abstract class BaseWeb3Client {
abstract encodeParameters(params: any[], types: any[]): string;
abstract decodeParameters(hexString: string, types: any[]): any[];
abstract etheriumSha3(...value): string;

}
18 changes: 18 additions & 0 deletions src/utils/web3_side_chain_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,24 @@ export class Web3SideChainClient<T_CONFIG> {
}
}

getBlockByNumber(blockNum: any, isParent?: boolean){
if(isParent){
return this.parent.getBlockByNumber(blockNum);
}
else{
return this.child.getBlockByNumber(blockNum);
}
}

getBatchProof(blockNum: any, isParent?: boolean){
if(isParent){
return this.parent.getBatchProof(blockNum);
}
else{
return this.child.getBatchProof(blockNum);
}
}

isEIP1559Supported(chainId: number): boolean {
return this.getConfig(`${chainIdToConfigPath[chainId]}.SupportsEIP1559`);
}
Expand Down
8 changes: 8 additions & 0 deletions src/zkevm/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,14 @@ export class ZkEvmClient extends ZkEvmBridgeClient {
eigenGetBlock(blockNum: any, isParent?: boolean){
return this.client.eigenGetBlock(blockNum, isParent);
}

getBlockByNumber(blockNum: any, isParent?: boolean){
return this.client.getBlockByNumber(blockNum, isParent);
}

getBatchProof(blockNum: any, isParent?: boolean){
return this.client.getBatchProof(blockNum, isParent);
}

private getContracts_() {
return {
Expand Down
23 changes: 22 additions & 1 deletion zeth-web3/src/web3/web3_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ export class Web3Client extends BaseWeb3Client {
this.web3_ = new Web3(provider);
}


read(config: ITransactionRequestConfig) {
return this.web3_.eth.call(
zethTxRequestConfigToWeb3(config)
Expand Down Expand Up @@ -87,6 +86,28 @@ export class Web3Client extends BaseWeb3Client {
return (this.web3_.eth.getBlock(blockHashOrBlockNumber) as any);
}

getBlockByNumber(blockNumber){
return (this.sendRPCRequest({
jsonrpc: '2.0',
method: 'eigenrpc_getBlockByNumber',
params: [blockNumber],
id: new Date().getTime()
}).then(payload => {
return JSON.stringify(payload.result);
}) as any);
}

getBatchProof(blockNumber){
return (this.sendRPCRequest({
jsonrpc: '2.0',
method: 'eigenrpc_getBatchProof',
params: [blockNumber],
id: new Date().getTime()
}).then(payload => {
return JSON.stringify(payload.result);
}) as any);
}

getBalance(address) {
return (this.web3_.eth.getBalance(address) as any);
}
Expand Down
Loading