-
Notifications
You must be signed in to change notification settings - Fork 5.1k
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
Suggestion to simplify Chainlink plugin usage by importing and reexporting modules #5596
Comments
And here is the implementation of this suggestion along with a section added to README.md file on how the user can use it: ChainSafe/web3.js-plugin-chainlink#4 |
The issue with this is that the plugin author still doesn't know what module the plugin user is going to be augmenting by registering the plugin. In your above example, const web3Eth = new Web3Eth(someProvider);
web3Eth.registerPlugin(new ChainlinkPlugin()); Then the only module that will actually have a |
Exactly, the plugin writer (the plugin author) will enable the typescript intellisense for his plugin for all And it is the responsibility of the plugin user to call |
This code: // Module Augmentation
declare module './reexported_web3_objects' {
interface Web3Context {
chainlink: ChainlinkPlugin;
}
interface Web3Eth {
chainlink: ChainlinkPlugin;
}
interface Web {
chainlink: ChainlinkPlugin;
}
} can be refactored to be: // Module Augmentation
declare module './reexported_web3_objects' {
interface Web3Context {
chainlink: ChainlinkPlugin;
}
} And this export: export { Web3, Web3Context, Web3Eth }; can be removed as it's not needed. The above refactoring of the module augmentation resolves my concern that the plugin author is unaware of what module/class the plugin user would be registering the plugin onto, and would therefore need to augment all Web3 modules that extend After implementing the above module augmentation refactor, the following should show the type error via intellisense or TSC, but instead the error only appears when attempting to run the code: const web3EthContract = new Contract(AggregatorV3InterfaceABI, { provider: 'http://127.0.0.1:8545' });
await web3EthContract.chainlink.getPrice(MainnetPriceFeeds.LinkEth); TypeError: Cannot read properties of undefined (reading 'getPrice')
42 | it('Should call ChainlinkPlugin.getPrice', async () => {
43 | const web3EthContract = new Contract(AggregatorV3InterfaceABI, { provider: 'http://127.0.0.1:8545' });
> 44 | await web3EthContract.chainlink.getPrice(MainnetPriceFeeds.LinkEth); this is because we've augmented Even with this limitation, this solution is still probably better than passing the burden of module augmentation onto the user. Instead, the user will just have to be made aware that they must call Here is a PR for the above changes |
Great to see that we are aligned on this now 😄 However, the plugin writer cannot skip reexporting the augmented object. As I described here: ChainSafe/web3.js-plugin-chainlink#5 (review) |
* WIP WIP plugin doc refactors * Add module augmentation to CustomRpcMethodsPlugin * Remove module augmentation from CustomRpcMethodsPlugin tests * Init reexport_web3_context.ts * Refactor module augmentation for CustomRpcMethodsPlugin * Remove no longer needed assets * Finish plugin doc refactors * Update ContractMethodWrappersPlugin and tests * Apply suggestions from code review Co-authored-by: Muhammad Altabba <[email protected]> * Update example plugin version from 0.0.1 to 0.1.0. Reword part of Using the Inherited Web3Context section * Grammer changes in A Quick Disclaimer section * Reword Re-declaring the Module section * Replace usage of we and our * Update use of "side effect" for Web3Context augmentation * Replace usage of SimplePlugin with CustomRpcMethods * Update docs/docs/guides/web3_plugin_guide/plugin_authors.md Co-authored-by: Muhammad Altabba <[email protected]> * Add Extending Web3EthPluginBase section * Grammer fix Co-authored-by: Muhammad Altabba <[email protected]>
This is not a Bug Report, Feature Request, or related to Documentation
Is there an existing issue for this?
What's up?
The current implementation of Chainlink plugin implies that the plugin user will need to do the module augmentation.
However, I suggest importing and then reexporting web3.js objects by the plugin writer (plugin author).
This would be basically by doing this:
By the plugin writer
At file named for example
reexported_web3_objects
:At another file,
chainlink_plugin.ts
for example:By the plugin user
And then the plugin user (plugin consumer) needs to do the import of Web3 objects from the plugin package. And so, the plugin user would import Web3 like this:
Background note: This was all needed to keep backward compatibility for how web3.js users can import Web3 object(s). But that required a trick for module augmentation. And here is more on this point: #5393 (comment)
Note: this implementation was inspired by #5393 (comment) and ChainSafe/web3.js-plugin-chainlink#1 (comment)
The text was updated successfully, but these errors were encountered: