createTransaction static method
Future<String>
createTransaction(
- String from,
- String data,
- BigInt value,
- String to, {
- GasFeeLevel gasFeeLevel = GasFeeLevel.high,
Create transaction
from
is public address
data
is contract transaction parameter
value
is navtie amount
to
if it is a contract transaction, to is contract address, if it is a native transaciton, to is receiver address.
gasFeeLevel
is gas fee level, default is high.
Implementation
static Future<String> createTransaction(
String from, String data, BigInt value, String to,
{GasFeeLevel gasFeeLevel = GasFeeLevel.high}) async {
final valueHex = "0x${value.toRadixString(16)}";
final gasLimit = await EvmService.ethEstimateGas(from, to, valueHex, data);
final gasFees = await EvmService.suggestedGasFees();
String level;
switch (gasFeeLevel) {
case GasFeeLevel.high:
level = "high";
break;
case GasFeeLevel.medium:
level = "medium";
break;
case GasFeeLevel.low:
level = "low";
break;
}
final maxFeePerGas = double.parse(gasFees[level]["maxFeePerGas"]);
final maxFeePerGasHex =
"0x${BigInt.from(maxFeePerGas * pow(10, 9)).toRadixString(16)}";
final maxPriorityFeePerGas =
double.parse(gasFees[level]["maxPriorityFeePerGas"]);
final maxPriorityFeePerGasHex =
"0x${BigInt.from(maxPriorityFeePerGas * pow(10, 9)).toRadixString(16)}";
final chainId = await ParticleAuth.getChainId();
final chainInfo = ChainInfo.getEvmChain(chainId);
if (chainInfo == null) {
throw Exception("cant find chain info for chain id $chainId");
}
Map<String, dynamic> req;
final isSupportEIP1559 = chainInfo.isEIP1559Supported();
// evm transaction
if (isSupportEIP1559) {
req = {
"from": from,
"to": to,
"gasLimit": gasLimit,
"value": valueHex,
"maxFeePerGas": maxFeePerGasHex,
"maxPriorityFeePerGas": maxPriorityFeePerGasHex,
"data": data,
"type": "0x2",
"nonce": "0x0",
"chainId": "0x${chainId.toRadixString(16)}",
};
} else {
req = {
"from": from,
"to": to,
"gasLimit": gasLimit,
"value": valueHex,
"gasPrice": maxFeePerGasHex,
"data": data,
"type": "0x0",
"nonce": "0x0",
"chainId": "0x${chainId.toRadixString(16)}",
};
}
final reqStr = jsonEncode(req);
final reqHex = StringUtils.toHexString(reqStr);
return "0x$reqHex";
}