I use @solana/web3.js and would like to set a feepayer for a normal sol transaction. When I use the new Transaction constructor it says it's deprecated but I can't find another way to do it. can anyone send me some up-to-date methods because I can't find any?
1 Answer
You can set it using the non-deprecated constructor, which takes as input TransactionBlockhashCtor with shape:
{
blockhash: Blockhash,
feePayer?: PublicKey | null,
lastValidBlockHeight: number,
signatures?: SignaturePubkeyPair[]
}
Note that connection.getLatestBlockhash returns both blockhash
and lastValidBlockHeight
.
If you have a Transaction
object (created by that constructor, or the deprecated one, or whatever else), you can also just set transaction.feePayer = somePublicKey
to set that field at any point.
-
Thank you! I have one more question. Where do I verify that I am able to pay the transaction fee? Because with only the public key you can't do anything. Do I set it when I sendAndConfirmTransaction at the Signer array?– NoahCommented Sep 8, 2022 at 9:00
-
1Yep, it'll need to be a signer of the transaction to be the fee payer, at the point that the transaction is broadcast. There's different ways to do that depending how you want to sign it. You might want to use
sendTransaction
from wallet-adapter to use the browser wallet, or you might want to add it as a signer insendAndConfirmTransaction
as you said if you already have the private key.– Callum MCommented Sep 8, 2022 at 9:12 -
If I set a different feepayer than the sender, the fee is double the price. Why is that?– NoahCommented Sep 8, 2022 at 9:52
-
IIRC the fee is per signer, so if you have 2 signers you'll pay twice the fee– Callum MCommented Sep 8, 2022 at 9:57