I am finding it difficult to see the difference between these two, what are their respective use cases?
2 Answers
Hi Developer advocate from Chainstack here.
Basically provider.sendTransaction
takes in a hexed string of signed tx as its parameter. signer.sendTransaction
takes a transaction object as the parameter.
Unless you already have the decoded tx string, you need to use signer.signTransaction
before sending a transaction using provider.sendTransaction
.
In fact from ether.js's source code, this is exactly how it is done at the back.
async sendTransaction(transaction: Deferrable<TransactionRequest>): Promise<TransactionResponse>
{
this._checkProvider("sendTransaction");
const tx = await this.populateTransaction(transaction);
const signedTx = await this.signTransaction(tx);
return await this.provider.sendTransaction(signedTx);
}
Hope this helps, happy coding!
-
he needs to use signer.sendTransaction , because I don't think he signs it somewhere else– NulikJan 26 at 15:26
-
Not sure about that but yeah signer.sendTransaction is much more straightforward in most cases.🤔 Jan 30 at 2:59
A signer is a concept from ethersjs ( check their docs). Basically, it's a provider (another ethersjs concept, perhaps a little more transparent, that is just an abstraction of a connection to the ETH network (through an RPC)) + a private key (in practice, it's usually Metamask). Typically, if the signer is metamask, signer.sendTransaction
will prompt the user with a metamask pop-up asking them to sign and broadcast the transaction.
Provider.sendTransaction
expects an already signed transaction as input, and will simply broadcast it.