How to Sub-class Signer? #1295
-
I'm trying to build a SDK that wraps a Provider object eg. window.ethereum or ethers.providers.JsonRpcProvider I'm overriding the send, sendAsync and request method of the provider object and instead of letting the wallet handle the transaction signing request, i'm handling the request and redirecting them to off-chain relayers, which then broadcast transaction to blockchain and return transaction hash, then SDK return the response object from send, sendAsync or request method accordingly. I'm creating my SDK like this Then i create ethers instance, Create the contract instatnce, Now the problem is the Contact doesn't have any signer information when i do
I get this error Error: sending a transaction requires a signer (operation="sendTransaction", code=UNSUPPORTED_OPERATION, version=contracts/5.0.11) This is the workaround i found to let ethers know that there is a signer object associated, but its not actually required. I'm sure there is better way of doing this.
I'm not able to connect the signer object from window.ethereum to my JsonRpcProvider object also for obvious reasons. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
You probably want to sub-class Signer, not Provider. The ethers library handles Signers and Providers as completely separate things; it sounds like you are coming from Web3.js. If you sub-class Signer I think everything will just work for you. You only need to provide implementations for the 4 abstract methods and you should be good to go. You also probably want your Signer to take a provider in and expose it as the The VoidSigner is also a great starting point for a minimal Signer. Just drop in your implementation. :) |
Beta Was this translation helpful? Give feedback.
-
INFURA does not provide accounts. You will need to pass a signer in if that is how you wish to sub-class it. What are you trying to do with your sub-class that is different than the normal JsonRpcSigner? |
Beta Was this translation helpful? Give feedback.
You probably want to sub-class Signer, not Provider.
The ethers library handles Signers and Providers as completely separate things; it sounds like you are coming from Web3.js.
If you sub-class Signer I think everything will just work for you. You only need to provide implementations for the 4 abstract methods and you should be good to go. You also probably want your Signer to take a provider in and expose it as the
.provider
property.The VoidSigner is also a great starting point for a minimal Signer. Just drop in your implementation. :)