Skip to content

Commit

Permalink
implement sample of chainstack traceblockbyhash
Browse files Browse the repository at this point in the history
  • Loading branch information
Muhammad-Altabba committed Nov 4, 2023
1 parent cb63d5a commit 742e751
Show file tree
Hide file tree
Showing 5 changed files with 276 additions and 493 deletions.
34 changes: 33 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,37 @@
web3-plugin-template
web3-plugin-chainstack
===========
This is a sample repository for how to make a web3.js plugin for custom RPC providers like chainstack.

It currently implement https://docs.chainstack.com/reference/ethereum-traceblockbyhash for the `4byteTracer`. **Feel free to open PRs to add more functionality. Or sponsor this work**


Plugin usage by the users
------------
At your typescript project first run:
`yarn add web3 @conx3/web3-plugin-chainstack`

And here is how to use the plugin:
```ts
import { Web3 } from 'web3';
import { ChainstackPlugin } from '@conx3/web3-plugin-chainstack';

async function main() {
const web3 = new Web3("https://nd-422-757-666.p2pify.com/0a9d79d93fb2f4a4b1e04695da2b77a7/");
web3.registerPlugin(new ChainstackPlugin());

const hash = "0x66103840578be3bc9c865e0961c4a4de31b5df7a45dcd13ffe2679ff9c7315d8";
const response = await web3.chainstack.traceBlockByHash(hash);

console.log(response);
}
main();

```


Publish a new version to the npm registry
------------
Run: `yarn build && npm publish --access public`

Contributing
------------
Expand Down
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "web3-plugin-template",
"version": "1.0.0",
"description": "Template plugin to extend web3.js with additional methods",
"name": "@conx3/web3-plugin-chainstack",
"version": "1.0.0-alpha.2",
"description": "web3.js plugin for chainstack custom RPC methods",
"main": "lib/index.js",
"types": "lib/index.d.ts",
"homepage": "https://github.com/web3/web3.js-plugin-template#readme",
Expand Down Expand Up @@ -33,7 +33,7 @@
"ts-jest": "^29.1.0",
"ts-node": "^10.9.1",
"typescript": "^5.1.3",
"web3": "^4.0.3"
"web3": "^4.2.2"
},
"peerDependencies": {
"web3": ">= 4.0.3"
Expand Down
24 changes: 19 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,30 @@
import { Web3PluginBase } from "web3";

export class TemplatePlugin extends Web3PluginBase {
public pluginNamespace = "template";
declare type ChainstackAPI = {

Check failure on line 3 in src/index.ts

View workflow job for this annotation

GitHub Actions / Build

Delete `·`
debug_traceBlockByHash: (blockHash: string, tracer: { tracer: string }) => Promise<{result: {

Check failure on line 4 in src/index.ts

View workflow job for this annotation

GitHub Actions / Build

Replace `blockHash:·string,·tracer:·{·tracer:·string·})·=>·Promise<{` with `⏎····blockHash:·string,⏎····tracer:·{·tracer:·string·}⏎··)·=>·Promise<{⏎····`
"result": Map<string, number>

Check failure on line 5 in src/index.ts

View workflow job for this annotation

GitHub Actions / Build

Replace `"result":·Map<string,·number>` with `··result:·Map<string,·number>;`
}[]}>;

Check failure on line 6 in src/index.ts

View workflow job for this annotation

GitHub Actions / Build

Replace `}[]` with `··}[];⏎··`
}

Check failure on line 7 in src/index.ts

View workflow job for this annotation

GitHub Actions / Build

Insert `;`

export class ChainstackPlugin extends Web3PluginBase<ChainstackAPI> {
public pluginNamespace = "chainstack";

public async traceBlockByHash(blockHash: string) {

Check failure on line 12 in src/index.ts

View workflow job for this annotation

GitHub Actions / Build

Missing return type on function

Check failure on line 13 in src/index.ts

View workflow job for this annotation

GitHub Actions / Build

Delete `⏎`
public test(param: string): void {
console.log(param);
// Specify the type of tracer: 4byteTracer, callTracer, or prestateTracer
const tracer = { tracer: '4byteTracer' };

Check failure on line 15 in src/index.ts

View workflow job for this annotation

GitHub Actions / Build

Replace `'4byteTracer'` with `"4byteTracer"`
const res = await this.requestManager.send({
// plugin has access to web3.js internal features like request manager

Check failure on line 17 in src/index.ts

View workflow job for this annotation

GitHub Actions / Build

Delete `··`
method: 'debug_traceBlockByHash',

Check failure on line 18 in src/index.ts

View workflow job for this annotation

GitHub Actions / Build

Replace `··method:·'debug_traceBlockByHash'` with `method:·"debug_traceBlockByHash"`
params: [blockHash, tracer],
});
return res;
}
}

// Module Augmentation
declare module "web3" {
interface Web3Context {
template: TemplatePlugin;
chainstack: ChainstackPlugin;
}
}
41 changes: 25 additions & 16 deletions test/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,40 @@
import Web3, { core } from "web3";
import { TemplatePlugin } from "../src";
import { ChainstackPlugin } from "../src";

describe("TemplatePlugin Tests", () => {
it("should register TokensPlugin plugin on Web3Context instance", () => {
const web3Context = new core.Web3Context("http://127.0.0.1:8545");
web3Context.registerPlugin(new TemplatePlugin());
expect(web3Context.template).toBeDefined();
describe("ChainstackPlugin Tests", () => {
it("should register ChainstackPlugin plugin on Web3Context instance", () => {
const web3Context = new core.Web3Context("https://nd-422-757-666.p2pify.com/0a9d79d93fb2f4a4b1e04695da2b77a7/");
web3Context.registerPlugin(new ChainstackPlugin());
expect(web3Context.chainstack).toBeDefined();
});

describe("TemplatePlugin method tests", () => {
let consoleSpy: jest.SpiedFunction<typeof global.console.log>;
describe("ChainstackPlugin method tests", () => {

let web3Context: Web3;
let web3: Web3;

beforeAll(() => {
web3Context = new Web3("http://127.0.0.1:8545");
web3Context.registerPlugin(new TemplatePlugin());
consoleSpy = jest.spyOn(global.console, "log").mockImplementation();
web3 = new Web3("https://nd-422-757-666.p2pify.com/0a9d79d93fb2f4a4b1e04695da2b77a7/");
web3.registerPlugin(new ChainstackPlugin());
});

afterAll(() => {
consoleSpy.mockRestore();
});

it("should call TempltyPlugin test method with expected param", () => {
web3Context.template.test("test-param");
expect(consoleSpy).toHaveBeenCalledWith("test-param");
it("should call traceBlockByHash method with expected param", async () => {
const hash = "0x66103840578be3bc9c865e0961c4a4de31b5df7a45dcd13ffe2679ff9c7315d8";
const response = await web3.chainstack.traceBlockByHash(hash);
// expect response.result to be an array of the following shape:
// [
// {
// result: {
// "result": Map<string, number>
// }
// }
// ]
expect(response).toBeDefined();


console.log(response);
});
});
});
Loading

0 comments on commit 742e751

Please sign in to comment.