@ledgerhq/coin-evm 0.5.0 → 0.5.1-next.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 (57) hide show
  1. package/.eslintrc.js +20 -0
  2. package/CHANGELOG.md +68 -20
  3. package/lib/__tests__/unit/api/explorer/types.unit.test.js +2 -0
  4. package/lib/__tests__/unit/api/explorer/types.unit.test.js.map +1 -1
  5. package/lib/__tests__/unit/logic.unit.test.js +32 -4
  6. package/lib/__tests__/unit/logic.unit.test.js.map +1 -1
  7. package/lib/api/explorer/index.d.ts.map +1 -1
  8. package/lib/api/explorer/index.js +1 -0
  9. package/lib/api/explorer/index.js.map +1 -1
  10. package/lib/api/explorer/types.d.ts +2 -2
  11. package/lib/api/explorer/types.d.ts.map +1 -1
  12. package/lib/api/explorer/types.js +1 -1
  13. package/lib/api/explorer/types.js.map +1 -1
  14. package/lib/api/node/rpc.common.js +1 -1
  15. package/lib/api/node/rpc.common.js.map +1 -1
  16. package/lib/logic.d.ts +5 -1
  17. package/lib/logic.d.ts.map +1 -1
  18. package/lib/logic.js +9 -2
  19. package/lib/logic.js.map +1 -1
  20. package/lib/specs.d.ts +1 -1
  21. package/lib/specs.d.ts.map +1 -1
  22. package/lib/specs.js +11 -3
  23. package/lib/specs.js.map +1 -1
  24. package/lib/types/transaction.d.ts +1 -0
  25. package/lib/types/transaction.d.ts.map +1 -1
  26. package/lib-es/__tests__/unit/api/explorer/types.unit.test.js +2 -0
  27. package/lib-es/__tests__/unit/api/explorer/types.unit.test.js.map +1 -1
  28. package/lib-es/__tests__/unit/logic.unit.test.js +32 -4
  29. package/lib-es/__tests__/unit/logic.unit.test.js.map +1 -1
  30. package/lib-es/api/explorer/index.d.ts.map +1 -1
  31. package/lib-es/api/explorer/index.js +1 -0
  32. package/lib-es/api/explorer/index.js.map +1 -1
  33. package/lib-es/api/explorer/types.d.ts +2 -2
  34. package/lib-es/api/explorer/types.d.ts.map +1 -1
  35. package/lib-es/api/explorer/types.js +1 -1
  36. package/lib-es/api/explorer/types.js.map +1 -1
  37. package/lib-es/api/node/rpc.common.js +1 -1
  38. package/lib-es/api/node/rpc.common.js.map +1 -1
  39. package/lib-es/logic.d.ts +5 -1
  40. package/lib-es/logic.d.ts.map +1 -1
  41. package/lib-es/logic.js +7 -1
  42. package/lib-es/logic.js.map +1 -1
  43. package/lib-es/specs.d.ts +1 -1
  44. package/lib-es/specs.d.ts.map +1 -1
  45. package/lib-es/specs.js +11 -3
  46. package/lib-es/specs.js.map +1 -1
  47. package/lib-es/types/transaction.d.ts +1 -0
  48. package/lib-es/types/transaction.d.ts.map +1 -1
  49. package/package.json +12 -12
  50. package/src/__tests__/unit/api/explorer/types.unit.test.ts +2 -0
  51. package/src/__tests__/unit/logic.unit.test.ts +48 -11
  52. package/src/api/explorer/index.ts +1 -0
  53. package/src/api/explorer/types.ts +6 -2
  54. package/src/api/node/rpc.common.ts +1 -1
  55. package/src/logic.ts +10 -2
  56. package/src/specs.ts +14 -6
  57. package/src/types/transaction.ts +2 -0
@@ -1,20 +1,14 @@
1
- import BigNumber from "bignumber.js";
2
- import { getEnv, setEnv } from "@ledgerhq/live-env";
3
- import { TokenCurrency } from "@ledgerhq/types-cryptoassets";
4
- import * as cryptoAssetsTokens from "@ledgerhq/cryptoassets/tokens";
5
1
  import { getCryptoCurrencyById, getTokenById } from "@ledgerhq/cryptoassets";
2
+ import * as cryptoAssetsTokens from "@ledgerhq/cryptoassets/tokens";
3
+ import { getEnv, setEnv } from "@ledgerhq/live-env";
4
+ import { CryptoCurrency, TokenCurrency, Unit } from "@ledgerhq/types-cryptoassets";
5
+ import BigNumber from "bignumber.js";
6
6
  import * as RPC_API from "../../api/node/rpc.common";
7
- import {
8
- deepFreeze,
9
- makeAccount,
10
- makeNftOperation,
11
- makeOperation,
12
- makeTokenAccount,
13
- } from "../fixtures/common.fixtures";
14
7
  import {
15
8
  attachOperations,
16
9
  eip1559TransactionHasFees,
17
10
  getAdditionalLayer2Fees,
11
+ getDefaultFeeUnit,
18
12
  getEstimatedFees,
19
13
  getGasLimit,
20
14
  getSyncHash,
@@ -22,6 +16,15 @@ import {
22
16
  mergeSubAccounts,
23
17
  padHexString,
24
18
  } from "../../logic";
19
+
20
+ import {
21
+ deepFreeze,
22
+ makeAccount,
23
+ makeNftOperation,
24
+ makeOperation,
25
+ makeTokenAccount,
26
+ } from "../fixtures/common.fixtures";
27
+
25
28
  import {
26
29
  EvmTransactionEIP1559,
27
30
  EvmTransactionLegacy,
@@ -202,6 +205,40 @@ describe("EVM Family", () => {
202
205
  });
203
206
  });
204
207
 
208
+ describe("getDefaultFeeUnit", () => {
209
+ it("should return the unit when currency has only one", () => {
210
+ const expectedUnit: Unit = {
211
+ name: "name",
212
+ code: "code",
213
+ magnitude: 18,
214
+ };
215
+
216
+ const currency: Partial<CryptoCurrency> = {
217
+ units: [expectedUnit],
218
+ };
219
+
220
+ expect(getDefaultFeeUnit(currency as CryptoCurrency)).toEqual(expectedUnit);
221
+ });
222
+
223
+ it("should return the second unit when currency has multiple", () => {
224
+ const expectedUnit: Unit = {
225
+ name: "name",
226
+ code: "code",
227
+ magnitude: 18,
228
+ };
229
+
230
+ const currency: Partial<CryptoCurrency> = {
231
+ units: [
232
+ { ...expectedUnit, name: "unit0" },
233
+ expectedUnit,
234
+ { ...expectedUnit, name: "unit2" },
235
+ ],
236
+ };
237
+
238
+ expect(getDefaultFeeUnit(currency as CryptoCurrency)).toEqual(expectedUnit);
239
+ });
240
+ });
241
+
205
242
  describe("getAdditionalLayer2Fees", () => {
206
243
  const optimism = getCryptoCurrencyById("optimism");
207
244
  const ethereum = getCryptoCurrencyById("ethereum");
@@ -14,6 +14,7 @@ export const getExplorerApi = (currency: CryptoCurrency): ExplorerApi => {
14
14
  case "etherscan":
15
15
  case "blockscout":
16
16
  case "teloscan":
17
+ case "klaytnfinder":
17
18
  return etherscanLikeApi;
18
19
  case "ledger":
19
20
  return ledgerExplorerApi;
@@ -43,6 +43,10 @@ export const isLedgerExplorerConfig = (
43
43
  */
44
44
  export const isEtherscanLikeExplorerConfig = (
45
45
  explorerConfig: ExplorerConfig,
46
- ): explorerConfig is ExplorerConfig & { type: "etherscan" | "blockscout" } => {
47
- return ["etherscan", "blockscout"].includes(explorerConfig?.type as string);
46
+ ): explorerConfig is ExplorerConfig & {
47
+ type: "etherscan" | "blockscout" | "teloscan" | "klaytnfinder";
48
+ } => {
49
+ return ["etherscan", "blockscout", "teloscan", "klaytnfinder"].includes(
50
+ explorerConfig?.type as string,
51
+ );
48
52
  };
@@ -226,7 +226,7 @@ export const getBlockByHeight: NodeApi["getBlockByHeight"] = (currency, blockHei
226
226
  export const getOptimismAdditionalFees: NodeApi["getOptimismAdditionalFees"] = makeLRUCache(
227
227
  async (currency, transaction) =>
228
228
  withApi(currency, async api => {
229
- if (!["optimism", "optimism_goerli"].includes(currency.id)) {
229
+ if (!["optimism", "optimism_goerli", "base", "base_goerli"].includes(currency.id)) {
230
230
  return new BigNumber(0);
231
231
  }
232
232
 
package/src/logic.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import { ethers } from "ethers";
2
2
  import BigNumber from "bignumber.js";
3
- import { CryptoCurrency } from "@ledgerhq/types-cryptoassets";
4
3
  import { isNFTActive } from "@ledgerhq/coin-framework/nft/support";
4
+ import { CryptoCurrency, Unit } from "@ledgerhq/types-cryptoassets";
5
5
  import { mergeOps } from "@ledgerhq/coin-framework/bridge/jsHelpers";
6
6
  import { Account, SubAccount, Operation } from "@ledgerhq/types-live";
7
7
  import { encodeOperationId } from "@ledgerhq/coin-framework/operation";
@@ -44,6 +44,12 @@ export const getEstimatedFees = (tx: EvmTransaction): BigNumber => {
44
44
  return tx.maxFeePerGas?.multipliedBy(gasLimit) || new BigNumber(0);
45
45
  };
46
46
 
47
+ /**
48
+ * Helper to get the currency unit to be used for the fee field
49
+ */
50
+ export const getDefaultFeeUnit = (currency: CryptoCurrency): Unit =>
51
+ currency.units.length > 1 ? currency.units[1] : currency.units[0];
52
+
47
53
  /**
48
54
  * Helper returning the potential additional fees necessary for layer twos
49
55
  * to settle the transaction on layer 1.
@@ -54,7 +60,9 @@ export const getAdditionalLayer2Fees = async (
54
60
  ): Promise<BigNumber | undefined> => {
55
61
  switch (currency.id) {
56
62
  case "optimism":
57
- case "optimism_goerli": {
63
+ case "optimism_goerli":
64
+ case "base":
65
+ case "base_goerli": {
58
66
  const nodeApi = getNodeApi(currency);
59
67
  const additionalFees = await nodeApi.getOptimismAdditionalFees(currency, transaction);
60
68
  return additionalFees;
package/src/specs.ts CHANGED
@@ -14,7 +14,7 @@ import {
14
14
  getCryptoCurrencyById,
15
15
  parseCurrencyUnit,
16
16
  } from "@ledgerhq/coin-framework/currencies/index";
17
- import { CryptoCurrencyIds } from "@ledgerhq/types-live";
17
+ import { CryptoCurrency } from "@ledgerhq/types-cryptoassets";
18
18
  import { cryptocurrenciesById } from "@ledgerhq/cryptoassets/currencies";
19
19
  import { botTest, genericTestDestination, pickSiblings } from "@ledgerhq/coin-framework/bot/specs";
20
20
  import { acceptTransaction } from "./speculos-deviceActions";
@@ -26,7 +26,7 @@ const testTimeout = 10 * 60 * 1000;
26
26
  const ETH_UNIT = { code: "ETH", name: "ETH", magnitude: 18 };
27
27
  const MBTC_UNIT = { name: "mBTC", code: "mBTC", magnitude: 5 };
28
28
 
29
- const minBalancePerCurrencyId: Record<CryptoCurrencyIds, BigNumber> = {
29
+ const minBalancePerCurrencyId: Partial<Record<CryptoCurrency["id"], BigNumber>> = {
30
30
  arbitrum: parseCurrencyUnit(ETH_UNIT, "0.001"),
31
31
  arbitrum_goerli: parseCurrencyUnit(ETH_UNIT, "0.001"),
32
32
  optimism: parseCurrencyUnit(ETH_UNIT, "0.001"),
@@ -35,6 +35,10 @@ const minBalancePerCurrencyId: Record<CryptoCurrencyIds, BigNumber> = {
35
35
  metis: parseCurrencyUnit(ETH_UNIT, "0.01"),
36
36
  moonriver: parseCurrencyUnit(ETH_UNIT, "0.1"),
37
37
  rsk: parseCurrencyUnit(MBTC_UNIT, "0.05"),
38
+ polygon_zk_evm: parseCurrencyUnit(ETH_UNIT, "0.001"),
39
+ polygon_zk_evm_testnet: parseCurrencyUnit(ETH_UNIT, "0.001"),
40
+ base: parseCurrencyUnit(ETH_UNIT, "0.001"),
41
+ base_goerli: parseCurrencyUnit(ETH_UNIT, "0.001"),
38
42
  };
39
43
 
40
44
  /**
@@ -79,8 +83,12 @@ const testCoinBalance: MutationSpec<EvmTransaction>["test"] = ({
79
83
  // effectively used gas but the "bid"/proposition of gas of the transaction
80
84
  // resulting in inconsistencies regarding the cumulated value of a tx.
81
85
  // value + gasLimit * gasPrice <-- gasPrice can be wrong here.
82
- const underValuedFeesCurrencies = ["optimism", "optimism_goerli"];
83
- const overValuedFeesCurrencies = ["arbitrum", "arbitrum_goerli"];
86
+ //
87
+ // Klaytn is not providing the right gasPrice either at the moment
88
+ // and their explorers are using the transaction gasPrice
89
+ // instead of the effectiveGasPrice from the receipt
90
+ const underValuedFeesCurrencies = ["optimism", "optimism_goerli", "base", "base_goerli"];
91
+ const overValuedFeesCurrencies = ["arbitrum", "arbitrum_goerli", "klaytn"];
84
92
  const currenciesWithFlakyBehaviour = [...underValuedFeesCurrencies, ...overValuedFeesCurrencies];
85
93
 
86
94
  // Classic test verifying exactly the balance
@@ -286,14 +294,14 @@ const evmBasicMutations: ({
286
294
 
287
295
  export default Object.values(cryptocurrenciesById)
288
296
  .filter(currency => currency.family === "evm")
289
- .reduce<Record<CryptoCurrencyIds, AppSpec<EvmTransaction>>>((acc, currency) => {
297
+ .reduce<Partial<Record<CryptoCurrency["id"], AppSpec<EvmTransaction>>>>((acc, currency) => {
290
298
  acc[currency.id] = {
291
299
  name: currency.name,
292
300
  currency,
293
301
  appQuery: {
294
302
  model: DeviceModelId.nanoS,
295
303
  appName: "Ethereum",
296
- appVersion: "1.10.2",
304
+ appVersion: "1.10.3",
297
305
  },
298
306
  testTimeout,
299
307
  transactionCheck: transactionCheck(currency.id),
@@ -137,6 +137,8 @@ export type GasOptions = {
137
137
  [key in "slow" | "medium" | "fast"]: FeeData;
138
138
  };
139
139
 
140
+ export type Strategy = keyof GasOptions;
141
+
140
142
  export type EvmTransactionNftParam = {
141
143
  tokenId: ProtoNFT["tokenId"];
142
144
  contract: ProtoNFT["contract"];