@ledgerhq/coin-evm 2.28.0-nightly.3 → 2.28.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (260) hide show
  1. package/.eslintrc.js +0 -1
  2. package/.turbo/turbo-build.log +1 -1
  3. package/CHANGELOG.md +20 -32
  4. package/{lib-es/__fixtures__/ethereum-erc20-weth.json → lib/__fixtures__/ethereum-erc20-usd_tether__erc20_.json} +8 -8
  5. package/lib/__fixtures__/{optimism-erc20-usd_coin.json → scroll_sepolia-erc20-new_token_mock.json} +20 -21
  6. package/lib/__tests__/fixtures/accounts.fixtures.d.ts +1 -1
  7. package/lib/__tests__/fixtures/accounts.fixtures.d.ts.map +1 -1
  8. package/lib/__tests__/fixtures/accounts.fixtures.js +19 -2
  9. package/lib/__tests__/fixtures/accounts.fixtures.js.map +1 -1
  10. package/lib/__tests__/fixtures/common.fixtures.d.ts +0 -1
  11. package/lib/__tests__/fixtures/common.fixtures.d.ts.map +1 -1
  12. package/lib/__tests__/fixtures/common.fixtures.js +0 -1
  13. package/lib/__tests__/fixtures/common.fixtures.js.map +1 -1
  14. package/lib/__tests__/fixtures/prepareTransaction.fixtures.d.ts.map +1 -1
  15. package/lib/__tests__/fixtures/prepareTransaction.fixtures.js +2 -3
  16. package/lib/__tests__/fixtures/prepareTransaction.fixtures.js.map +1 -1
  17. package/lib/__tests__/fixtures/synchronization.fixtures.d.ts +1 -1
  18. package/lib/__tests__/fixtures/synchronization.fixtures.d.ts.map +1 -1
  19. package/lib/__tests__/fixtures/synchronization.fixtures.js +18 -3
  20. package/lib/__tests__/fixtures/synchronization.fixtures.js.map +1 -1
  21. package/lib/__tests__/fixtures/transaction.fixtures.d.ts +2 -2
  22. package/lib/__tests__/fixtures/transaction.fixtures.d.ts.map +1 -1
  23. package/lib/__tests__/fixtures/transaction.fixtures.js +2 -3
  24. package/lib/__tests__/fixtures/transaction.fixtures.js.map +1 -1
  25. package/lib/__tests__/unit/accounts.unit.test.js +27 -0
  26. package/lib/__tests__/unit/accounts.unit.test.js.map +1 -1
  27. package/lib/__tests__/unit/adapters/etherscan.unit.test.js +7 -72
  28. package/lib/__tests__/unit/adapters/etherscan.unit.test.js.map +1 -1
  29. package/lib/__tests__/unit/adapters/ledger.unit.test.js +2 -15
  30. package/lib/__tests__/unit/adapters/ledger.unit.test.js.map +1 -1
  31. package/lib/__tests__/unit/api/explorer/etherscan.unit.test.js +0 -8
  32. package/lib/__tests__/unit/api/explorer/etherscan.unit.test.js.map +1 -1
  33. package/lib/__tests__/unit/api/explorer/ledger.unit.test.js +0 -12
  34. package/lib/__tests__/unit/api/explorer/ledger.unit.test.js.map +1 -1
  35. package/lib/__tests__/unit/broadcast.unit.test.js +1 -3
  36. package/lib/__tests__/unit/broadcast.unit.test.js.map +1 -1
  37. package/lib/__tests__/unit/buildOptimisticOperation.unit.test.js +1 -3
  38. package/lib/__tests__/unit/buildOptimisticOperation.unit.test.js.map +1 -1
  39. package/lib/__tests__/unit/deviceTransactionConfig.unit.test.d.ts +1 -1
  40. package/lib/__tests__/unit/deviceTransactionConfig.unit.test.d.ts.map +1 -1
  41. package/lib/__tests__/unit/deviceTransactionConfig.unit.test.js +0 -1
  42. package/lib/__tests__/unit/deviceTransactionConfig.unit.test.js.map +1 -1
  43. package/lib/__tests__/unit/estimateMaxSpendable.unit.test.js +1 -3
  44. package/lib/__tests__/unit/estimateMaxSpendable.unit.test.js.map +1 -1
  45. package/lib/__tests__/unit/getTransactionStatus.unit.test.js +1 -3
  46. package/lib/__tests__/unit/getTransactionStatus.unit.test.js.map +1 -1
  47. package/lib/__tests__/unit/logic.unit.test.js +18 -22
  48. package/lib/__tests__/unit/logic.unit.test.js.map +1 -1
  49. package/lib/__tests__/unit/operation.test.js +2 -6
  50. package/lib/__tests__/unit/operation.test.js.map +1 -1
  51. package/lib/__tests__/unit/prepareTransaction.unit.test.js +17 -4
  52. package/lib/__tests__/unit/prepareTransaction.unit.test.js.map +1 -1
  53. package/lib/adapters/etherscan.d.ts.map +1 -1
  54. package/lib/adapters/etherscan.js +2 -2
  55. package/lib/adapters/etherscan.js.map +1 -1
  56. package/lib/adapters/ledger.d.ts.map +1 -1
  57. package/lib/adapters/ledger.js +2 -2
  58. package/lib/adapters/ledger.js.map +1 -1
  59. package/lib/api/index.d.ts.map +1 -1
  60. package/lib/api/index.js +4 -2
  61. package/lib/api/index.js.map +1 -1
  62. package/lib/bridge/js.d.ts +1 -2
  63. package/lib/bridge/js.d.ts.map +1 -1
  64. package/lib/bridge/js.js +1 -3
  65. package/lib/bridge/js.js.map +1 -1
  66. package/lib/bridge/preload.d.ts +0 -1
  67. package/lib/bridge/preload.d.ts.map +1 -1
  68. package/lib/bridge/preload.js +1 -10
  69. package/lib/bridge/preload.js.map +1 -1
  70. package/lib/deviceTransactionConfig.d.ts.map +1 -1
  71. package/lib/deviceTransactionConfig.js +2 -2
  72. package/lib/deviceTransactionConfig.js.map +1 -1
  73. package/lib/logic/common.d.ts +0 -18
  74. package/lib/logic/common.d.ts.map +1 -1
  75. package/lib/logic/common.js +1 -35
  76. package/lib/logic/common.js.map +1 -1
  77. package/lib/logic/estimateFees.d.ts +1 -1
  78. package/lib/logic/estimateFees.d.ts.map +1 -1
  79. package/lib/logic/estimateFees.js +12 -25
  80. package/lib/logic/estimateFees.js.map +1 -1
  81. package/lib/logic/estimateFees.test.js +41 -93
  82. package/lib/logic/estimateFees.test.js.map +1 -1
  83. package/lib/logic/index.d.ts +0 -1
  84. package/lib/logic/index.d.ts.map +1 -1
  85. package/lib/logic/index.js +0 -1
  86. package/lib/logic/index.js.map +1 -1
  87. package/lib/types/transaction.d.ts +0 -9
  88. package/lib/types/transaction.d.ts.map +1 -1
  89. package/{lib/__fixtures__/ethereum-erc20-weth.json → lib-es/__fixtures__/ethereum-erc20-usd_tether__erc20_.json} +8 -8
  90. package/lib-es/__fixtures__/{optimism-erc20-usd_coin.json → scroll_sepolia-erc20-new_token_mock.json} +20 -21
  91. package/lib-es/__tests__/fixtures/accounts.fixtures.d.ts +1 -1
  92. package/lib-es/__tests__/fixtures/accounts.fixtures.d.ts.map +1 -1
  93. package/lib-es/__tests__/fixtures/accounts.fixtures.js +18 -1
  94. package/lib-es/__tests__/fixtures/accounts.fixtures.js.map +1 -1
  95. package/lib-es/__tests__/fixtures/common.fixtures.d.ts +0 -1
  96. package/lib-es/__tests__/fixtures/common.fixtures.d.ts.map +1 -1
  97. package/lib-es/__tests__/fixtures/common.fixtures.js +0 -1
  98. package/lib-es/__tests__/fixtures/common.fixtures.js.map +1 -1
  99. package/lib-es/__tests__/fixtures/prepareTransaction.fixtures.d.ts.map +1 -1
  100. package/lib-es/__tests__/fixtures/prepareTransaction.fixtures.js +2 -3
  101. package/lib-es/__tests__/fixtures/prepareTransaction.fixtures.js.map +1 -1
  102. package/lib-es/__tests__/fixtures/synchronization.fixtures.d.ts +1 -1
  103. package/lib-es/__tests__/fixtures/synchronization.fixtures.d.ts.map +1 -1
  104. package/lib-es/__tests__/fixtures/synchronization.fixtures.js +18 -3
  105. package/lib-es/__tests__/fixtures/synchronization.fixtures.js.map +1 -1
  106. package/lib-es/__tests__/fixtures/transaction.fixtures.d.ts +2 -2
  107. package/lib-es/__tests__/fixtures/transaction.fixtures.d.ts.map +1 -1
  108. package/lib-es/__tests__/fixtures/transaction.fixtures.js +2 -3
  109. package/lib-es/__tests__/fixtures/transaction.fixtures.js.map +1 -1
  110. package/lib-es/__tests__/unit/accounts.unit.test.js +27 -0
  111. package/lib-es/__tests__/unit/accounts.unit.test.js.map +1 -1
  112. package/lib-es/__tests__/unit/adapters/etherscan.unit.test.js +7 -72
  113. package/lib-es/__tests__/unit/adapters/etherscan.unit.test.js.map +1 -1
  114. package/lib-es/__tests__/unit/adapters/ledger.unit.test.js +2 -15
  115. package/lib-es/__tests__/unit/adapters/ledger.unit.test.js.map +1 -1
  116. package/lib-es/__tests__/unit/api/explorer/etherscan.unit.test.js +0 -8
  117. package/lib-es/__tests__/unit/api/explorer/etherscan.unit.test.js.map +1 -1
  118. package/lib-es/__tests__/unit/api/explorer/ledger.unit.test.js +0 -12
  119. package/lib-es/__tests__/unit/api/explorer/ledger.unit.test.js.map +1 -1
  120. package/lib-es/__tests__/unit/broadcast.unit.test.js +2 -4
  121. package/lib-es/__tests__/unit/broadcast.unit.test.js.map +1 -1
  122. package/lib-es/__tests__/unit/buildOptimisticOperation.unit.test.js +2 -4
  123. package/lib-es/__tests__/unit/buildOptimisticOperation.unit.test.js.map +1 -1
  124. package/lib-es/__tests__/unit/deviceTransactionConfig.unit.test.d.ts +1 -1
  125. package/lib-es/__tests__/unit/deviceTransactionConfig.unit.test.d.ts.map +1 -1
  126. package/lib-es/__tests__/unit/deviceTransactionConfig.unit.test.js +0 -1
  127. package/lib-es/__tests__/unit/deviceTransactionConfig.unit.test.js.map +1 -1
  128. package/lib-es/__tests__/unit/estimateMaxSpendable.unit.test.js +2 -4
  129. package/lib-es/__tests__/unit/estimateMaxSpendable.unit.test.js.map +1 -1
  130. package/lib-es/__tests__/unit/getTransactionStatus.unit.test.js +2 -4
  131. package/lib-es/__tests__/unit/getTransactionStatus.unit.test.js.map +1 -1
  132. package/lib-es/__tests__/unit/logic.unit.test.js +19 -23
  133. package/lib-es/__tests__/unit/logic.unit.test.js.map +1 -1
  134. package/lib-es/__tests__/unit/operation.test.js +3 -7
  135. package/lib-es/__tests__/unit/operation.test.js.map +1 -1
  136. package/lib-es/__tests__/unit/prepareTransaction.unit.test.js +17 -4
  137. package/lib-es/__tests__/unit/prepareTransaction.unit.test.js.map +1 -1
  138. package/lib-es/adapters/etherscan.d.ts.map +1 -1
  139. package/lib-es/adapters/etherscan.js +2 -2
  140. package/lib-es/adapters/etherscan.js.map +1 -1
  141. package/lib-es/adapters/ledger.d.ts.map +1 -1
  142. package/lib-es/adapters/ledger.js +2 -2
  143. package/lib-es/adapters/ledger.js.map +1 -1
  144. package/lib-es/api/index.d.ts.map +1 -1
  145. package/lib-es/api/index.js +5 -3
  146. package/lib-es/api/index.js.map +1 -1
  147. package/lib-es/bridge/js.d.ts +1 -2
  148. package/lib-es/bridge/js.d.ts.map +1 -1
  149. package/lib-es/bridge/js.js +1 -3
  150. package/lib-es/bridge/js.js.map +1 -1
  151. package/lib-es/bridge/preload.d.ts +0 -1
  152. package/lib-es/bridge/preload.d.ts.map +1 -1
  153. package/lib-es/bridge/preload.js +0 -8
  154. package/lib-es/bridge/preload.js.map +1 -1
  155. package/lib-es/deviceTransactionConfig.d.ts.map +1 -1
  156. package/lib-es/deviceTransactionConfig.js +2 -2
  157. package/lib-es/deviceTransactionConfig.js.map +1 -1
  158. package/lib-es/logic/common.d.ts +0 -18
  159. package/lib-es/logic/common.d.ts.map +1 -1
  160. package/lib-es/logic/common.js +0 -30
  161. package/lib-es/logic/common.js.map +1 -1
  162. package/lib-es/logic/estimateFees.d.ts +1 -1
  163. package/lib-es/logic/estimateFees.d.ts.map +1 -1
  164. package/lib-es/logic/estimateFees.js +12 -25
  165. package/lib-es/logic/estimateFees.js.map +1 -1
  166. package/lib-es/logic/estimateFees.test.js +41 -93
  167. package/lib-es/logic/estimateFees.test.js.map +1 -1
  168. package/lib-es/logic/index.d.ts +0 -1
  169. package/lib-es/logic/index.d.ts.map +1 -1
  170. package/lib-es/logic/index.js +0 -1
  171. package/lib-es/logic/index.js.map +1 -1
  172. package/lib-es/types/transaction.d.ts +0 -9
  173. package/lib-es/types/transaction.d.ts.map +1 -1
  174. package/package.json +13 -13
  175. package/src/__tests__/fixtures/accounts.fixtures.ts +23 -1
  176. package/src/__tests__/fixtures/common.fixtures.ts +0 -1
  177. package/src/__tests__/fixtures/prepareTransaction.fixtures.ts +4 -4
  178. package/src/__tests__/fixtures/synchronization.fixtures.ts +20 -3
  179. package/src/__tests__/fixtures/transaction.fixtures.ts +3 -4
  180. package/src/__tests__/unit/accounts.unit.test.ts +36 -0
  181. package/src/__tests__/unit/adapters/etherscan.unit.test.ts +7 -87
  182. package/src/__tests__/unit/adapters/ledger.unit.test.ts +2 -20
  183. package/src/__tests__/unit/api/explorer/etherscan.unit.test.ts +0 -12
  184. package/src/__tests__/unit/api/explorer/ledger.unit.test.ts +0 -16
  185. package/src/__tests__/unit/broadcast.unit.test.ts +3 -5
  186. package/src/__tests__/unit/buildOptimisticOperation.unit.test.ts +2 -5
  187. package/src/__tests__/unit/deviceTransactionConfig.unit.test.ts +0 -1
  188. package/src/__tests__/unit/estimateMaxSpendable.unit.test.ts +3 -5
  189. package/src/__tests__/unit/getTransactionStatus.unit.test.ts +2 -5
  190. package/src/__tests__/unit/logic.unit.test.ts +29 -41
  191. package/src/__tests__/unit/operation.test.ts +5 -9
  192. package/src/__tests__/unit/prepareTransaction.unit.test.ts +18 -4
  193. package/src/adapters/etherscan.ts +2 -5
  194. package/src/adapters/ledger.ts +2 -5
  195. package/src/api/index.ts +5 -4
  196. package/src/bridge/js.ts +0 -4
  197. package/src/bridge/preload.ts +0 -8
  198. package/src/deviceTransactionConfig.ts +2 -3
  199. package/src/logic/common.ts +0 -52
  200. package/src/logic/estimateFees.test.ts +57 -98
  201. package/src/logic/estimateFees.ts +18 -28
  202. package/src/logic/index.ts +0 -1
  203. package/src/types/transaction.ts +0 -12
  204. package/lib/__fixtures__/cardano-native-8654e8b350e298c80d2451beb5ed80fc9eee9f38ce6b039fb8706bc34c4f4253544552.json +0 -52
  205. package/lib/__fixtures__/ethereum-erc20-usd__coin-0x02b972f304dc24c9bc362e6435c4ad654241f9af916689a4790145c9bcbdf4cf-IN-i0.json +0 -20
  206. package/lib/__fixtures__/ethereum-erc20-usd__coin-0x02b972f304dc24c9bc362e6435c4ad654241f9af916689a4790145c9bcbdf4cf-OUT-i0.json +0 -19
  207. package/lib/__tests__/fixtures/cryptoAssetsStore.fixtures.d.ts +0 -2
  208. package/lib/__tests__/fixtures/cryptoAssetsStore.fixtures.d.ts.map +0 -1
  209. package/lib/__tests__/fixtures/cryptoAssetsStore.fixtures.js +0 -44
  210. package/lib/__tests__/fixtures/cryptoAssetsStore.fixtures.js.map +0 -1
  211. package/lib/cryptoAssetsStore.d.ts +0 -5
  212. package/lib/cryptoAssetsStore.d.ts.map +0 -1
  213. package/lib/cryptoAssetsStore.js +0 -17
  214. package/lib/cryptoAssetsStore.js.map +0 -1
  215. package/lib/cryptoAssetsStore.test.d.ts +0 -2
  216. package/lib/cryptoAssetsStore.test.d.ts.map +0 -1
  217. package/lib/cryptoAssetsStore.test.js +0 -15
  218. package/lib/cryptoAssetsStore.test.js.map +0 -1
  219. package/lib/logic/validateIntent.d.ts +0 -4
  220. package/lib/logic/validateIntent.d.ts.map +0 -1
  221. package/lib/logic/validateIntent.js +0 -192
  222. package/lib/logic/validateIntent.js.map +0 -1
  223. package/lib/logic/validateIntent.test.d.ts +0 -2
  224. package/lib/logic/validateIntent.test.d.ts.map +0 -1
  225. package/lib/logic/validateIntent.test.js +0 -485
  226. package/lib/logic/validateIntent.test.js.map +0 -1
  227. package/lib-es/__fixtures__/cardano-native-8654e8b350e298c80d2451beb5ed80fc9eee9f38ce6b039fb8706bc34c4f4253544552.json +0 -52
  228. package/lib-es/__fixtures__/ethereum-erc20-usd__coin-0x02b972f304dc24c9bc362e6435c4ad654241f9af916689a4790145c9bcbdf4cf-IN-i0.json +0 -20
  229. package/lib-es/__fixtures__/ethereum-erc20-usd__coin-0x02b972f304dc24c9bc362e6435c4ad654241f9af916689a4790145c9bcbdf4cf-OUT-i0.json +0 -19
  230. package/lib-es/__tests__/fixtures/cryptoAssetsStore.fixtures.d.ts +0 -2
  231. package/lib-es/__tests__/fixtures/cryptoAssetsStore.fixtures.d.ts.map +0 -1
  232. package/lib-es/__tests__/fixtures/cryptoAssetsStore.fixtures.js +0 -19
  233. package/lib-es/__tests__/fixtures/cryptoAssetsStore.fixtures.js.map +0 -1
  234. package/lib-es/cryptoAssetsStore.d.ts +0 -5
  235. package/lib-es/cryptoAssetsStore.d.ts.map +0 -1
  236. package/lib-es/cryptoAssetsStore.js +0 -12
  237. package/lib-es/cryptoAssetsStore.js.map +0 -1
  238. package/lib-es/cryptoAssetsStore.test.d.ts +0 -2
  239. package/lib-es/cryptoAssetsStore.test.d.ts.map +0 -1
  240. package/lib-es/cryptoAssetsStore.test.js +0 -13
  241. package/lib-es/cryptoAssetsStore.test.js.map +0 -1
  242. package/lib-es/logic/validateIntent.d.ts +0 -4
  243. package/lib-es/logic/validateIntent.d.ts.map +0 -1
  244. package/lib-es/logic/validateIntent.js +0 -185
  245. package/lib-es/logic/validateIntent.js.map +0 -1
  246. package/lib-es/logic/validateIntent.test.d.ts +0 -2
  247. package/lib-es/logic/validateIntent.test.d.ts.map +0 -1
  248. package/lib-es/logic/validateIntent.test.js +0 -480
  249. package/lib-es/logic/validateIntent.test.js.map +0 -1
  250. package/src/__fixtures__/cardano-native-8654e8b350e298c80d2451beb5ed80fc9eee9f38ce6b039fb8706bc34c4f4253544552.json +0 -52
  251. package/src/__fixtures__/ethereum-erc20-usd__coin-0x02b972f304dc24c9bc362e6435c4ad654241f9af916689a4790145c9bcbdf4cf-IN-i0.json +0 -20
  252. package/src/__fixtures__/ethereum-erc20-usd__coin-0x02b972f304dc24c9bc362e6435c4ad654241f9af916689a4790145c9bcbdf4cf-OUT-i0.json +0 -19
  253. package/src/__fixtures__/ethereum-erc20-usd__coin-0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48.json +0 -73
  254. package/src/__fixtures__/ethereum-erc20-weth.json +0 -73
  255. package/src/__fixtures__/optimism-erc20-usd_coin.json +0 -69
  256. package/src/__tests__/fixtures/cryptoAssetsStore.fixtures.ts +0 -24
  257. package/src/cryptoAssetsStore.test.ts +0 -19
  258. package/src/cryptoAssetsStore.ts +0 -16
  259. package/src/logic/validateIntent.test.ts +0 -659
  260. package/src/logic/validateIntent.ts +0 -274
@@ -1,274 +0,0 @@
1
- import { ethers } from "ethers";
2
- import BigNumber from "bignumber.js";
3
- import type {
4
- AssetInfo,
5
- Balance,
6
- FeeEstimation,
7
- TransactionIntent,
8
- TransactionValidation,
9
- } from "@ledgerhq/coin-framework/api/types";
10
- import {
11
- AmountRequired,
12
- ETHAddressNonEIP,
13
- FeeNotLoaded,
14
- GasLessThanEstimate,
15
- InvalidAddress,
16
- MaxFeeTooLow,
17
- NotEnoughBalance,
18
- NotEnoughGas,
19
- PriorityFeeHigherThanMaxFee,
20
- PriorityFeeTooHigh,
21
- PriorityFeeTooLow,
22
- RecipientRequired,
23
- } from "@ledgerhq/errors";
24
- import { CryptoCurrency } from "@ledgerhq/types-cryptoassets";
25
- import { TransactionTypes } from "ethers/lib/utils";
26
- import { formatCurrencyUnit } from "@ledgerhq/coin-framework/currencies/formatCurrencyUnit";
27
- import { getFeesUnit } from "@ledgerhq/coin-framework/account/helpers";
28
- import { isNative } from "../types";
29
- import { DEFAULT_GAS_LIMIT } from "../utils";
30
- import { getGasTracker } from "../network/gasTracker";
31
- import estimateFees from "./estimateFees";
32
- import getBalance from "./getBalance";
33
- import {
34
- getTransactionType,
35
- isApiGasOptions,
36
- isEip1559FeeEstimation,
37
- isLegacyFeeEstimation,
38
- } from "./common";
39
-
40
- function assetsAreEqual(asset1: AssetInfo, asset2: AssetInfo): boolean {
41
- if (asset1.type !== asset2.type) return false;
42
-
43
- if ("assetReference" in asset1 && "assetReference" in asset2) {
44
- return asset1.assetReference === asset2.assetReference;
45
- }
46
-
47
- return asset1.type === "native";
48
- }
49
-
50
- function findBalance(asset: AssetInfo, balances: Balance[]): Balance {
51
- return balances.find(b => assetsAreEqual(b.asset, asset)) ?? { asset, value: 0n };
52
- }
53
-
54
- // This regex will not work with Starknet since addresses are 65 caracters long after the 0x
55
- const ethAddressRegEx = /^(0x)?[0-9a-fA-F]{40}$/;
56
-
57
- /**
58
- * Validate the amount of a transaction for an account
59
- */
60
- async function validateAmount(
61
- balances: Balance[],
62
- intent: TransactionIntent,
63
- totalSpent: bigint,
64
- ): Promise<Pick<TransactionValidation, "errors" | "warnings">> {
65
- const balance = findBalance(intent.asset, balances);
66
-
67
- if (!intent.amount) {
68
- return { errors: { amount: new AmountRequired() }, warnings: {} };
69
- }
70
-
71
- if (totalSpent > balance.value) {
72
- return { errors: { amount: new NotEnoughBalance() }, warnings: {} };
73
- }
74
-
75
- return { errors: {}, warnings: {} };
76
- }
77
-
78
- /**
79
- * Validate an address for a transaction
80
- */
81
- function validateRecipient(
82
- currency: CryptoCurrency,
83
- intent: TransactionIntent,
84
- ): Pick<TransactionValidation, "errors" | "warnings"> {
85
- if (!intent.recipient) {
86
- return { errors: { recipient: new RecipientRequired() }, warnings: {} };
87
- }
88
-
89
- if (!intent.recipient.match(ethAddressRegEx)) {
90
- return {
91
- errors: {
92
- recipient: new InvalidAddress("", {
93
- currencyName: currency.name,
94
- }),
95
- },
96
- warnings: {},
97
- };
98
- }
99
-
100
- // Check if address is respecting EIP-55
101
- try {
102
- const recipientChecksumed = ethers.utils.getAddress(intent.recipient);
103
- if (intent.recipient !== recipientChecksumed) {
104
- // this case can happen if the user is entering an ICAP address.
105
- throw new Error();
106
- }
107
- } catch (e) {
108
- // either getAddress throws for a bad checksum or we throw manually if the recipient isn't the same.
109
- return { errors: {}, warnings: { recipient: new ETHAddressNonEIP() } }; // "Auto-verification not available: carefully verify the address"
110
- }
111
-
112
- return { errors: {}, warnings: {} };
113
- }
114
-
115
- /**
116
- * Validate gas properties of a transaction, depending on its type and the account emitter
117
- */
118
- async function validateGas(
119
- currency: CryptoCurrency,
120
- intent: TransactionIntent,
121
- balances: Balance[],
122
- estimatedFees: FeeEstimation,
123
- ): Promise<Pick<TransactionValidation, "errors" | "warnings">> {
124
- const errors: Record<string, Error> = {};
125
- const warnings: Record<string, Error> = {};
126
-
127
- const nativeBalance = findBalance({ type: "native" }, balances);
128
-
129
- const gasLimit =
130
- typeof estimatedFees.parameters?.gasLimit === "bigint" ? estimatedFees.parameters.gasLimit : 0n;
131
- const customGasLimit =
132
- typeof estimatedFees.parameters?.customGasLimit === "bigint" &&
133
- estimatedFees.parameters.customGasLimit;
134
-
135
- // Gas Limit
136
- if (typeof customGasLimit === "bigint") {
137
- if (customGasLimit === 0n) {
138
- errors.gasLimit = new FeeNotLoaded();
139
- } else if (customGasLimit < BigInt(DEFAULT_GAS_LIMIT.toFixed(0))) {
140
- errors.gasLimit = new GasLessThanEstimate();
141
- }
142
- } else {
143
- if (gasLimit === 0n) {
144
- errors.gasLimit = new FeeNotLoaded();
145
- } else if (gasLimit < BigInt(DEFAULT_GAS_LIMIT.toFixed(0))) {
146
- errors.gasLimit = new GasLessThanEstimate();
147
- }
148
- }
149
-
150
- if (typeof customGasLimit === "bigint" && customGasLimit < gasLimit) {
151
- warnings.gasLimit = new GasLessThanEstimate();
152
- }
153
-
154
- const transactionType = getTransactionType(intent.type);
155
- const hasLegacyGasPrice =
156
- transactionType === TransactionTypes.legacy && isLegacyFeeEstimation(estimatedFees);
157
- const hasEip1559GasPrice =
158
- transactionType === TransactionTypes.eip1559 && isEip1559FeeEstimation(estimatedFees);
159
-
160
- // Gas Price
161
- if (!(hasLegacyGasPrice || hasEip1559GasPrice)) {
162
- errors.gasPrice = new FeeNotLoaded();
163
- } else if (intent.recipient && estimatedFees.value > nativeBalance.value) {
164
- errors.gasPrice = new NotEnoughGas(undefined, {
165
- // "You need {{fees}} {{ticker}} for network fees to swap as you are on {{cryptoName}} network. <link0>Buy {{ticker}}</link0>"
166
- fees: formatCurrencyUnit(
167
- getFeesUnit(currency),
168
- new BigNumber(estimatedFees.value.toString()),
169
- ),
170
- ticker: currency.ticker,
171
- cryptoName: currency.name,
172
- links: ["ledgerlive://buy"],
173
- });
174
- }
175
-
176
- // eip1559 specific
177
- if (transactionType === TransactionTypes.eip1559 && isEip1559FeeEstimation(estimatedFees)) {
178
- const { maxFeePerGas, maxPriorityFeePerGas, gasOptions } = estimatedFees.parameters;
179
- const { maximalPriorityFee, minimalPriorityFee, recommendedNextBaseFee } = await (async (
180
- options: unknown,
181
- ): Promise<{
182
- maximalPriorityFee: bigint;
183
- minimalPriorityFee: bigint;
184
- recommendedNextBaseFee: bigint;
185
- }> => {
186
- if (isApiGasOptions(options)) {
187
- const maximalPriorityFee = options.fast.maxPriorityFeePerGas;
188
- const minimalPriorityFee = options.slow.maxPriorityFeePerGas;
189
- const recommendedNextBaseFee = options.medium.nextBaseFee;
190
-
191
- if (
192
- typeof maximalPriorityFee === "bigint" &&
193
- typeof minimalPriorityFee === "bigint" &&
194
- typeof recommendedNextBaseFee === "bigint"
195
- ) {
196
- return { maximalPriorityFee, minimalPriorityFee, recommendedNextBaseFee };
197
- }
198
- }
199
-
200
- const gasTracker = getGasTracker(currency);
201
- const gasOptions = await gasTracker?.getGasOptions({
202
- currency,
203
- options: { useEIP1559: true },
204
- });
205
-
206
- return {
207
- maximalPriorityFee: BigInt(gasOptions?.fast?.maxPriorityFeePerGas?.toFixed(0) ?? "0"),
208
- minimalPriorityFee: BigInt(gasOptions?.slow?.maxPriorityFeePerGas?.toFixed(0) ?? "0"),
209
- recommendedNextBaseFee: BigInt(gasOptions?.medium?.nextBaseFee?.toFixed(0) ?? "0"),
210
- };
211
- })(gasOptions);
212
-
213
- if (maxPriorityFeePerGas === 0n) {
214
- errors.maxPriorityFee = new PriorityFeeTooLow();
215
- } else if (maxPriorityFeePerGas > maxFeePerGas) {
216
- // priority fee is more than max fee (total fee for the transaction) which doesn't make sense
217
- errors.maxPriorityFee = new PriorityFeeHigherThanMaxFee();
218
- }
219
-
220
- if (maximalPriorityFee && maxPriorityFeePerGas > maximalPriorityFee) {
221
- warnings.maxPriorityFee = new PriorityFeeTooHigh();
222
- } else if (minimalPriorityFee && maxPriorityFeePerGas < minimalPriorityFee) {
223
- warnings.maxPriorityFee = new PriorityFeeTooLow();
224
- }
225
-
226
- if (recommendedNextBaseFee && maxFeePerGas < recommendedNextBaseFee) {
227
- warnings.maxFee = new MaxFeeTooLow();
228
- }
229
- }
230
-
231
- return { errors, warnings };
232
- }
233
-
234
- export async function validateIntent(
235
- currency: CryptoCurrency,
236
- intent: TransactionIntent,
237
- customFees?: FeeEstimation,
238
- ): Promise<TransactionValidation> {
239
- const estimatedFees = customFees ?? (await estimateFees(currency, intent));
240
- const balances = await getBalance(currency, intent.sender);
241
- const totalSpent = isNative(intent.asset) ? intent.amount + estimatedFees.value : intent.amount;
242
-
243
- const { errors: recipientErr, warnings: recipientWarn } = validateRecipient(currency, intent);
244
- const { errors: amountErr, warnings: amountWarn } = await validateAmount(
245
- balances,
246
- intent,
247
- totalSpent,
248
- );
249
- const { errors: gasErr, warnings: gasWarn } = await validateGas(
250
- currency,
251
- intent,
252
- balances,
253
- estimatedFees,
254
- );
255
-
256
- const errors = {
257
- ...recipientErr,
258
- ...amountErr,
259
- ...gasErr,
260
- };
261
- const warnings = {
262
- ...recipientWarn,
263
- ...amountWarn,
264
- ...gasWarn,
265
- };
266
-
267
- return {
268
- errors,
269
- warnings,
270
- estimatedFees: estimatedFees.value,
271
- totalSpent,
272
- amount: intent.amount,
273
- };
274
- }