apiAccountEth.dart 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import 'package:polkawallet_sdk/service/eth/accountEth.dart';
  2. import 'package:polkawallet_sdk/storage/keyringEVM.dart';
  3. import '../api.dart';
  4. class ApiAccountEth {
  5. ApiAccountEth(this.apiRoot, this.service);
  6. final PolkawalletApi apiRoot;
  7. final ServiceAccountEth service;
  8. /// This method query account icons and set icons to [Keyring.store]
  9. /// so we can get icon of an account from [Keyring] instance.
  10. Future<void> updateAddressIconsMap(KeyringEVM keyring,
  11. [List? address]) async {
  12. final List<String?> ls = [];
  13. if (address != null) {
  14. ls.addAll(List<String>.from(address));
  15. } else {
  16. ls.addAll(keyring.keyPairs.map((e) => e.address).toList());
  17. ls.addAll(keyring.contacts.map((e) => e.address).toList());
  18. }
  19. if (ls.length == 0) return;
  20. // get icons from webView.
  21. final res = await service.getAddressIcons(ls);
  22. // set new icons to Keyring instance.
  23. if (res != null) {
  24. final data = {};
  25. res.forEach((e) {
  26. data[e[0]] = e[1];
  27. });
  28. keyring.store.updateIconsMap(Map<String, String>.from(data));
  29. }
  30. }
  31. Future<String> getNativeTokenBalance(String address) async {
  32. return service.getNativeTokenBalance(address);
  33. }
  34. Future<List?> getTokenBalance(
  35. String address, List<String> contractAddresses) async {
  36. final List? res = await service.getTokenBalance(address, contractAddresses);
  37. return res;
  38. }
  39. Future<String?> getAddress(String address) async {
  40. final String? res = await service.getAddress(address);
  41. return res;
  42. }
  43. Map<String, String> getAcalaGasParams() {
  44. return {"gasPrice": "0x33a70303ea", "gasLimit": "0x329b140"};
  45. }
  46. Future<EvmGasParams> queryEthGasParams({int gasLimit = 200000}) async {
  47. final data = await service.queryEthGasParams();
  48. return EvmGasParams(
  49. gasLimit: gasLimit,
  50. gasPrice: double.parse(data['estimatedBaseFee']),
  51. estimatedBaseFee: double.parse(data['estimatedBaseFee']),
  52. estimatedFee: {
  53. EstimatedFeeLevel.low: EvmGasParamsEIP1559(
  54. maxFeePerGas: double.parse(data['low']['suggestedMaxFeePerGas']),
  55. maxPriorityFeePerGas:
  56. double.parse(data['low']['suggestedMaxPriorityFeePerGas'])),
  57. EstimatedFeeLevel.medium: EvmGasParamsEIP1559(
  58. maxFeePerGas: double.parse(data['medium']['suggestedMaxFeePerGas']),
  59. maxPriorityFeePerGas:
  60. double.parse(data['medium']['suggestedMaxPriorityFeePerGas'])),
  61. EstimatedFeeLevel.high: EvmGasParamsEIP1559(
  62. maxFeePerGas: double.parse(data['high']['suggestedMaxFeePerGas']),
  63. maxPriorityFeePerGas:
  64. double.parse(data['high']['suggestedMaxPriorityFeePerGas']))
  65. },
  66. );
  67. }
  68. }
  69. class EvmGasParams {
  70. EvmGasParams(
  71. {required this.gasLimit,
  72. required this.gasPrice,
  73. this.estimatedBaseFee,
  74. this.estimatedFee});
  75. final int gasLimit;
  76. final double gasPrice;
  77. final double? estimatedBaseFee;
  78. final Map<EstimatedFeeLevel, EvmGasParamsEIP1559>? estimatedFee;
  79. }
  80. class EvmGasParamsEIP1559 {
  81. EvmGasParamsEIP1559(
  82. {required this.maxFeePerGas, required this.maxPriorityFeePerGas});
  83. final double maxFeePerGas;
  84. final double maxPriorityFeePerGas;
  85. }
  86. enum EstimatedFeeLevel { low, medium, high }