prepareTransaction method
Prepares a transaction by filling in missing fields.
Implementation
Future<TransactionRequest> prepareTransaction(
TransactionRequest request) async {
var tx = request;
// Set chain ID
if (tx.chainId == null) {
tx = tx.copyWith(chainId: chain.chainId);
}
// Set nonce
if (tx.nonce == null) {
final nonce = await getTransactionCount(address, 'pending');
tx = tx.copyWith(nonce: nonce);
}
// Set gas limit
if (tx.gasLimit == null) {
final gasLimit = await estimateGas(_toCallRequest(tx));
// Add 20% buffer
tx = tx.copyWith(
gasLimit: gasLimit * BigInt.from(120) ~/ BigInt.from(100),
);
}
// Set gas price / fee
if (tx.type == TransactionType.legacy) {
if (tx.gasPrice == null) {
tx = tx.copyWith(gasPrice: await getGasPrice());
}
} else {
if (tx.maxFeePerGas == null || tx.maxPriorityFeePerGas == null) {
final feeData = await getFeeData();
tx = tx.copyWith(
maxFeePerGas: tx.maxFeePerGas ?? feeData.maxFeePerGas,
maxPriorityFeePerGas:
tx.maxPriorityFeePerGas ?? feeData.maxPriorityFeePerGas,
);
}
}
return tx;
}