generated from web3/web3.js-plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement sample of chainstack traceblockbyhash
- Loading branch information
1 parent
cb63d5a
commit 742e751
Showing
5 changed files
with
276 additions
and
493 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = { | ||
debug_traceBlockByHash: (blockHash: string, tracer: { tracer: string }) => Promise<{result: { | ||
"result": Map<string, number> | ||
}[]}>; | ||
} | ||
|
||
export class ChainstackPlugin extends Web3PluginBase<ChainstackAPI> { | ||
public pluginNamespace = "chainstack"; | ||
|
||
public async traceBlockByHash(blockHash: string) { | ||
|
||
public test(param: string): void { | ||
console.log(param); | ||
// Specify the type of tracer: 4byteTracer, callTracer, or prestateTracer | ||
const tracer = { tracer: '4byteTracer' }; | ||
const res = await this.requestManager.send({ | ||
// plugin has access to web3.js internal features like request manager | ||
method: 'debug_traceBlockByHash', | ||
params: [blockHash, tracer], | ||
}); | ||
return res; | ||
} | ||
} | ||
|
||
// Module Augmentation | ||
declare module "web3" { | ||
interface Web3Context { | ||
template: TemplatePlugin; | ||
chainstack: ChainstackPlugin; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.