# Calculate max sendable with eip1559 fees

Fetching fees using provider `rpc.getFeeData()` which returns `FeeData`

FeeData consist of 3 props:

`gasPrice`, `maxFeePerGas`, `maxPriorityFeePerGas`

`gasPrice` - represents legacy transaction fee

`maxPriorityFeePerGas` - minior tip

`maxFeePerGas` - baseFee * 2 + maxPriorityFeePerGas

When sending transaction i'm executing following formulas:

`estimatedFeeInWei` = `maxFeePerGas * 21_000` - That give us the maximum wei that will be used by

then we subtract `estimatedFeeInWei` from balance:

`BigNumber.from(balanceInWei).sub(estimatedGasInWei)` - This give us maximum spendable amount by the user.

Issue is there that big chunk of the fee is refunded back to the sender so is not even dust because change is:

Tried another approach as well:

``````const gasPrice = BigNumber.from(feeData.gasPrice).add(feeData.maxPriorityFeePerGas as bigint)

const estimatedGasInWei = gasPrice.mul(gasLimit)

and again:

BigNumber.from(balanceInWei).sub(estimatedGasInWei)
``````

for some reason with this approach i do get `Insufficient fee` ( will debug this one )

just wondering what is the correct approach for really minimizing returns and possible if user select to send max amount really to be max without any change ( little bit of dust is ok but if can be covered even better )

About transaction body, there is nothing special just `to` and `value`, is it possible that my mistake is there and i need to set any of the fee props?

This is how looks transaction object and populated one before sending transaction, basically populated result it passed to `sendTransaction`

Edit:

I have updated formula to:

``````wallet.estimateGas * feeData.maxFeePerGas
``````

and seems quite fine just it overpays to much and then change from transaction is being returned `0,0005336731415377411` which is way to much for `dust` IMO and some of the amount can go into `value` for max transaction any ideas how to and what else can be improved?