0

As in title, how do I set fee payer to different wallet than sender or receiver and sign the transaction with it's private key? I use solana/web3.js and phantom wallet API.

lets say A sends transaction with a destination to B wallet. How do I set fee payer to be C? A needs to sign the transacation in phantom wallet as the confirmation shows up, but i want fee payer to be different wallet and to sign transaction automatically with code.

1 Answer 1

2

If you are sending a standard transaction you would assign the feePayer while defining the transaction, and then you would also need to sign the transaction with the feePayer wallet before returning it to the user for their wallet approval:

const feeKey = process.env.PRIVATE_KEY!;
const feePayer = Keypair.fromSecretKey(b58.decode(feeKey)); //if privateKey is bs58 like how it is exported Phantom

const transaction = new Transaction({
   recentBlockhash: blockhash,
   feePayer: feePayer.publicKey,
});

transaction.add(ix);   //add your txn instructions
transaction.partialSign(feePayer); //partially sign the txn

const serializedTransaction = transaction.serialize({
  requireAllSignatures: false,
});
const base64 = serializedTransaction.toString("base64");

If you are sending a VersionedTransaction the concept is the same, but structure is a little different:

const messageV0 = new TransactionMessage({
   payerKey: feePayer.publicKey,
   recentBlockhash: blockhash,
   instructions: [
       ixOne,
       ixTwo
   ],
}).compileToV0Message();

const transaction = new VersionedTransaction(messageV0);
transaction.sign([feePayer.publicKey]);

const base64 = Buffer.from(transaction.serialize()).toString('base64'); 

One thing to note: if you are using a privateKey as the feePayer make sure it is in your .env and you are creating/signing the transaction from the server side of your app then sending it back to the front-end for user wallet approval, this way your privateKey is not exposed.

4
  • I will check it after i fix error i came across. You can check it out as well if you got time sir. Thank you solana.stackexchange.com/questions/12826/…
    – youwish
    Commented Apr 9 at 14:20
  • I’ve encountered decodeUnsafe when trying to sign with a privateKey on the Front End. Have you created an API route to build/partially sign/return your txn? You should be able to decode the private key safely on the server side.
    – matt_xyz
    Commented Apr 9 at 15:29
  • Can i somehow build transaction on front, then partial sign it on backend? If i create transaction on backend ig it won't show phantom popup to client.
    – youwish
    Commented Apr 9 at 15:31
  • The proper flow would be to build the txn on the backend (also helps prevent tampering), encode it into a base64 string (like in the snippets above), then return it to the front end and use the sendTransaction() function with useWallet() from @solana/wallet-adapter-react. Here is an example from the Solana Dapp Scaffold github.com/solana-labs/dapp-scaffold/blob/main/src/components/…
    – matt_xyz
    Commented Apr 9 at 18:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.