2

I'm designing a solana contract where an admin performs an approval after a user makes a request.

The approval instruction closes a PDA corresponding to the request and returns its SOLs to the user who created the request.

I want to calcuate the cost of the approval txn and deduct it from the SOLs of the request PDA before returning the rest to the user.

Is it possible to calculate this from inside the contract?

2 Answers 2

1

It isn't possible to get the fees for the current transaction on-chain, since a transaction can include additional instructions, signatures, priority fees, etc.

You could potentially figure it out using the instructions sysvar and reading through the number of signatures and priority fees paid, but this is likely a brittle solution: https://docs.solana.com/developing/runtime-facilities/sysvars#instructions

You can access the fees sysvar, but it's deprecated https://docs.solana.com/developing/runtime-facilities/sysvars#fees

Unfortunately, you're best off choosing a fixed amount to refund to the user, something like 5000 lamports.

-1

yes, it's possible. you can just do this.

const transaction = new Transaction(/Your transaction here/);

const response = await connection.getFeeForMessage( transaction.compileMessage(), 'confirmed', ); const feeInLamports = response.value;

1
  • While that definitely works off-chain, the question was about determining fees from inside the contract, ie on-chain
    – Jon C
    Commented May 18, 2023 at 12:34

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.