Replay Attack Protection
ChainID
Background:
ETH token-nomic (Inflation, Deflation)
Miner income
Block Reward (also, Uncle Block Reward)
Transaction Fee (Gas) -- reached nearly half of Block Reward - Security Issue
MEV
Issues
Gas War
Congestion
Security threat by Miners
Base Fee -- Burn
Priority Fee -- Miner
Max Fee -- Remaining Part will be refund to sender
Legacy TX:
EIP-1559 TX:
Gas War
NO Gas CELING
Sample Code with Web3j:
// Get baseFee from last block
BigInteger baseFeePerGas = web3j.ethGetBlockByNumber(DefaultBlockParameterName.LATEST, false).send().getBlock().getBaseFeePerGas();
BigInteger maxPriorityFeePerGas = web3j.ethMaxPriorityFeePerGas().send().getMaxPriorityFeePerGas();
BigInteger maxFeePerGas = baseFeePerGas.add(maxPriorityFeePerGas);
Before the execution, would like to how much gas the transaction need to get successfully executed.
For better gas preparation;
For more reasonable fee structure;
For avoid most TX execution failures;
etc.
Sample Code with Web3j:
private static ContractEIP1559GasProvider buildGasProviderForApprove(Web3j web3j, String fromAddress,
String scAddress, String spender, BigInteger amountInWei) throws IOException {
long chainId = web3j.ethChainId().send().getChainId().longValue();
BigInteger baseFeePerGas = web3j.ethGetBlockByNumber(DefaultBlockParameterName.LATEST, false).send().getBlock().getBaseFeePerGas();
BigInteger maxPriorityFeePerGas = web3j.ethMaxPriorityFeePerGas().send().getMaxPriorityFeePerGas();
BigInteger maxFeePerGas = baseFeePerGas.add(maxPriorityFeePerGas);
BigInteger gasLimit = estimateApproveGasLimit(web3j, fromAddress, scAddress, spender, amountInWei, maxFeePerGas, maxPriorityFeePerGas);
return new StaticEIP1559GasProvider(chainId, maxFeePerGas, maxPriorityFeePerGas, gasLimit);
}
private static BigInteger estimateApproveGasLimit(Web3j web3j, String fromAddress, String scAddress, String spender, BigInteger amountInWei,
BigInteger maxFeePerGas, BigInteger maxPriorityFeePerGas) throws IOException {
Function function = new org.web3j.abi.datatypes.Function(
"approve",
Arrays.<Type>asList(new org.web3j.abi.datatypes.Address(160, spender),
new org.web3j.abi.datatypes.generated.Uint256(amountInWei)),
Collections.<TypeReference<?>>emptyList());
EthGetTransactionCount ethGetTransactionCount = web3j.ethGetTransactionCount(fromAddress, DefaultBlockParameterName.PENDING).send();
BigInteger nonce = ethGetTransactionCount.getTransactionCount();
long chainId = web3j.ethChainId().send().getChainId().longValue();
Transaction transaction = new Transaction(fromAddress, nonce, null, BigInteger.valueOf(4300000),
scAddress, BigInteger.ZERO, FunctionEncoder.encode(function), chainId, maxPriorityFeePerGas, maxFeePerGas);
BigInteger gasLimit = web3j.ethEstimateGas(transaction).send().getAmountUsed();
return gasLimit;
}