@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.
- package/.eslintrc.js +0 -1
- package/.turbo/turbo-build.log +1 -1
- package/CHANGELOG.md +20 -32
- package/{lib-es/__fixtures__/ethereum-erc20-weth.json → lib/__fixtures__/ethereum-erc20-usd_tether__erc20_.json} +8 -8
- package/lib/__fixtures__/{optimism-erc20-usd_coin.json → scroll_sepolia-erc20-new_token_mock.json} +20 -21
- package/lib/__tests__/fixtures/accounts.fixtures.d.ts +1 -1
- package/lib/__tests__/fixtures/accounts.fixtures.d.ts.map +1 -1
- package/lib/__tests__/fixtures/accounts.fixtures.js +19 -2
- package/lib/__tests__/fixtures/accounts.fixtures.js.map +1 -1
- package/lib/__tests__/fixtures/common.fixtures.d.ts +0 -1
- package/lib/__tests__/fixtures/common.fixtures.d.ts.map +1 -1
- package/lib/__tests__/fixtures/common.fixtures.js +0 -1
- package/lib/__tests__/fixtures/common.fixtures.js.map +1 -1
- package/lib/__tests__/fixtures/prepareTransaction.fixtures.d.ts.map +1 -1
- package/lib/__tests__/fixtures/prepareTransaction.fixtures.js +2 -3
- package/lib/__tests__/fixtures/prepareTransaction.fixtures.js.map +1 -1
- package/lib/__tests__/fixtures/synchronization.fixtures.d.ts +1 -1
- package/lib/__tests__/fixtures/synchronization.fixtures.d.ts.map +1 -1
- package/lib/__tests__/fixtures/synchronization.fixtures.js +18 -3
- package/lib/__tests__/fixtures/synchronization.fixtures.js.map +1 -1
- package/lib/__tests__/fixtures/transaction.fixtures.d.ts +2 -2
- package/lib/__tests__/fixtures/transaction.fixtures.d.ts.map +1 -1
- package/lib/__tests__/fixtures/transaction.fixtures.js +2 -3
- package/lib/__tests__/fixtures/transaction.fixtures.js.map +1 -1
- package/lib/__tests__/unit/accounts.unit.test.js +27 -0
- package/lib/__tests__/unit/accounts.unit.test.js.map +1 -1
- package/lib/__tests__/unit/adapters/etherscan.unit.test.js +7 -72
- package/lib/__tests__/unit/adapters/etherscan.unit.test.js.map +1 -1
- package/lib/__tests__/unit/adapters/ledger.unit.test.js +2 -15
- package/lib/__tests__/unit/adapters/ledger.unit.test.js.map +1 -1
- package/lib/__tests__/unit/api/explorer/etherscan.unit.test.js +0 -8
- package/lib/__tests__/unit/api/explorer/etherscan.unit.test.js.map +1 -1
- package/lib/__tests__/unit/api/explorer/ledger.unit.test.js +0 -12
- package/lib/__tests__/unit/api/explorer/ledger.unit.test.js.map +1 -1
- package/lib/__tests__/unit/broadcast.unit.test.js +1 -3
- package/lib/__tests__/unit/broadcast.unit.test.js.map +1 -1
- package/lib/__tests__/unit/buildOptimisticOperation.unit.test.js +1 -3
- package/lib/__tests__/unit/buildOptimisticOperation.unit.test.js.map +1 -1
- package/lib/__tests__/unit/deviceTransactionConfig.unit.test.d.ts +1 -1
- package/lib/__tests__/unit/deviceTransactionConfig.unit.test.d.ts.map +1 -1
- package/lib/__tests__/unit/deviceTransactionConfig.unit.test.js +0 -1
- package/lib/__tests__/unit/deviceTransactionConfig.unit.test.js.map +1 -1
- package/lib/__tests__/unit/estimateMaxSpendable.unit.test.js +1 -3
- package/lib/__tests__/unit/estimateMaxSpendable.unit.test.js.map +1 -1
- package/lib/__tests__/unit/getTransactionStatus.unit.test.js +1 -3
- package/lib/__tests__/unit/getTransactionStatus.unit.test.js.map +1 -1
- package/lib/__tests__/unit/logic.unit.test.js +18 -22
- package/lib/__tests__/unit/logic.unit.test.js.map +1 -1
- package/lib/__tests__/unit/operation.test.js +2 -6
- package/lib/__tests__/unit/operation.test.js.map +1 -1
- package/lib/__tests__/unit/prepareTransaction.unit.test.js +17 -4
- package/lib/__tests__/unit/prepareTransaction.unit.test.js.map +1 -1
- package/lib/adapters/etherscan.d.ts.map +1 -1
- package/lib/adapters/etherscan.js +2 -2
- package/lib/adapters/etherscan.js.map +1 -1
- package/lib/adapters/ledger.d.ts.map +1 -1
- package/lib/adapters/ledger.js +2 -2
- package/lib/adapters/ledger.js.map +1 -1
- package/lib/api/index.d.ts.map +1 -1
- package/lib/api/index.js +4 -2
- package/lib/api/index.js.map +1 -1
- package/lib/bridge/js.d.ts +1 -2
- package/lib/bridge/js.d.ts.map +1 -1
- package/lib/bridge/js.js +1 -3
- package/lib/bridge/js.js.map +1 -1
- package/lib/bridge/preload.d.ts +0 -1
- package/lib/bridge/preload.d.ts.map +1 -1
- package/lib/bridge/preload.js +1 -10
- package/lib/bridge/preload.js.map +1 -1
- package/lib/deviceTransactionConfig.d.ts.map +1 -1
- package/lib/deviceTransactionConfig.js +2 -2
- package/lib/deviceTransactionConfig.js.map +1 -1
- package/lib/logic/common.d.ts +0 -18
- package/lib/logic/common.d.ts.map +1 -1
- package/lib/logic/common.js +1 -35
- package/lib/logic/common.js.map +1 -1
- package/lib/logic/estimateFees.d.ts +1 -1
- package/lib/logic/estimateFees.d.ts.map +1 -1
- package/lib/logic/estimateFees.js +12 -25
- package/lib/logic/estimateFees.js.map +1 -1
- package/lib/logic/estimateFees.test.js +41 -93
- package/lib/logic/estimateFees.test.js.map +1 -1
- package/lib/logic/index.d.ts +0 -1
- package/lib/logic/index.d.ts.map +1 -1
- package/lib/logic/index.js +0 -1
- package/lib/logic/index.js.map +1 -1
- package/lib/types/transaction.d.ts +0 -9
- package/lib/types/transaction.d.ts.map +1 -1
- package/{lib/__fixtures__/ethereum-erc20-weth.json → lib-es/__fixtures__/ethereum-erc20-usd_tether__erc20_.json} +8 -8
- package/lib-es/__fixtures__/{optimism-erc20-usd_coin.json → scroll_sepolia-erc20-new_token_mock.json} +20 -21
- package/lib-es/__tests__/fixtures/accounts.fixtures.d.ts +1 -1
- package/lib-es/__tests__/fixtures/accounts.fixtures.d.ts.map +1 -1
- package/lib-es/__tests__/fixtures/accounts.fixtures.js +18 -1
- package/lib-es/__tests__/fixtures/accounts.fixtures.js.map +1 -1
- package/lib-es/__tests__/fixtures/common.fixtures.d.ts +0 -1
- package/lib-es/__tests__/fixtures/common.fixtures.d.ts.map +1 -1
- package/lib-es/__tests__/fixtures/common.fixtures.js +0 -1
- package/lib-es/__tests__/fixtures/common.fixtures.js.map +1 -1
- package/lib-es/__tests__/fixtures/prepareTransaction.fixtures.d.ts.map +1 -1
- package/lib-es/__tests__/fixtures/prepareTransaction.fixtures.js +2 -3
- package/lib-es/__tests__/fixtures/prepareTransaction.fixtures.js.map +1 -1
- package/lib-es/__tests__/fixtures/synchronization.fixtures.d.ts +1 -1
- package/lib-es/__tests__/fixtures/synchronization.fixtures.d.ts.map +1 -1
- package/lib-es/__tests__/fixtures/synchronization.fixtures.js +18 -3
- package/lib-es/__tests__/fixtures/synchronization.fixtures.js.map +1 -1
- package/lib-es/__tests__/fixtures/transaction.fixtures.d.ts +2 -2
- package/lib-es/__tests__/fixtures/transaction.fixtures.d.ts.map +1 -1
- package/lib-es/__tests__/fixtures/transaction.fixtures.js +2 -3
- package/lib-es/__tests__/fixtures/transaction.fixtures.js.map +1 -1
- package/lib-es/__tests__/unit/accounts.unit.test.js +27 -0
- package/lib-es/__tests__/unit/accounts.unit.test.js.map +1 -1
- package/lib-es/__tests__/unit/adapters/etherscan.unit.test.js +7 -72
- package/lib-es/__tests__/unit/adapters/etherscan.unit.test.js.map +1 -1
- package/lib-es/__tests__/unit/adapters/ledger.unit.test.js +2 -15
- package/lib-es/__tests__/unit/adapters/ledger.unit.test.js.map +1 -1
- package/lib-es/__tests__/unit/api/explorer/etherscan.unit.test.js +0 -8
- package/lib-es/__tests__/unit/api/explorer/etherscan.unit.test.js.map +1 -1
- package/lib-es/__tests__/unit/api/explorer/ledger.unit.test.js +0 -12
- package/lib-es/__tests__/unit/api/explorer/ledger.unit.test.js.map +1 -1
- package/lib-es/__tests__/unit/broadcast.unit.test.js +2 -4
- package/lib-es/__tests__/unit/broadcast.unit.test.js.map +1 -1
- package/lib-es/__tests__/unit/buildOptimisticOperation.unit.test.js +2 -4
- package/lib-es/__tests__/unit/buildOptimisticOperation.unit.test.js.map +1 -1
- package/lib-es/__tests__/unit/deviceTransactionConfig.unit.test.d.ts +1 -1
- package/lib-es/__tests__/unit/deviceTransactionConfig.unit.test.d.ts.map +1 -1
- package/lib-es/__tests__/unit/deviceTransactionConfig.unit.test.js +0 -1
- package/lib-es/__tests__/unit/deviceTransactionConfig.unit.test.js.map +1 -1
- package/lib-es/__tests__/unit/estimateMaxSpendable.unit.test.js +2 -4
- package/lib-es/__tests__/unit/estimateMaxSpendable.unit.test.js.map +1 -1
- package/lib-es/__tests__/unit/getTransactionStatus.unit.test.js +2 -4
- package/lib-es/__tests__/unit/getTransactionStatus.unit.test.js.map +1 -1
- package/lib-es/__tests__/unit/logic.unit.test.js +19 -23
- package/lib-es/__tests__/unit/logic.unit.test.js.map +1 -1
- package/lib-es/__tests__/unit/operation.test.js +3 -7
- package/lib-es/__tests__/unit/operation.test.js.map +1 -1
- package/lib-es/__tests__/unit/prepareTransaction.unit.test.js +17 -4
- package/lib-es/__tests__/unit/prepareTransaction.unit.test.js.map +1 -1
- package/lib-es/adapters/etherscan.d.ts.map +1 -1
- package/lib-es/adapters/etherscan.js +2 -2
- package/lib-es/adapters/etherscan.js.map +1 -1
- package/lib-es/adapters/ledger.d.ts.map +1 -1
- package/lib-es/adapters/ledger.js +2 -2
- package/lib-es/adapters/ledger.js.map +1 -1
- package/lib-es/api/index.d.ts.map +1 -1
- package/lib-es/api/index.js +5 -3
- package/lib-es/api/index.js.map +1 -1
- package/lib-es/bridge/js.d.ts +1 -2
- package/lib-es/bridge/js.d.ts.map +1 -1
- package/lib-es/bridge/js.js +1 -3
- package/lib-es/bridge/js.js.map +1 -1
- package/lib-es/bridge/preload.d.ts +0 -1
- package/lib-es/bridge/preload.d.ts.map +1 -1
- package/lib-es/bridge/preload.js +0 -8
- package/lib-es/bridge/preload.js.map +1 -1
- package/lib-es/deviceTransactionConfig.d.ts.map +1 -1
- package/lib-es/deviceTransactionConfig.js +2 -2
- package/lib-es/deviceTransactionConfig.js.map +1 -1
- package/lib-es/logic/common.d.ts +0 -18
- package/lib-es/logic/common.d.ts.map +1 -1
- package/lib-es/logic/common.js +0 -30
- package/lib-es/logic/common.js.map +1 -1
- package/lib-es/logic/estimateFees.d.ts +1 -1
- package/lib-es/logic/estimateFees.d.ts.map +1 -1
- package/lib-es/logic/estimateFees.js +12 -25
- package/lib-es/logic/estimateFees.js.map +1 -1
- package/lib-es/logic/estimateFees.test.js +41 -93
- package/lib-es/logic/estimateFees.test.js.map +1 -1
- package/lib-es/logic/index.d.ts +0 -1
- package/lib-es/logic/index.d.ts.map +1 -1
- package/lib-es/logic/index.js +0 -1
- package/lib-es/logic/index.js.map +1 -1
- package/lib-es/types/transaction.d.ts +0 -9
- package/lib-es/types/transaction.d.ts.map +1 -1
- package/package.json +13 -13
- package/src/__tests__/fixtures/accounts.fixtures.ts +23 -1
- package/src/__tests__/fixtures/common.fixtures.ts +0 -1
- package/src/__tests__/fixtures/prepareTransaction.fixtures.ts +4 -4
- package/src/__tests__/fixtures/synchronization.fixtures.ts +20 -3
- package/src/__tests__/fixtures/transaction.fixtures.ts +3 -4
- package/src/__tests__/unit/accounts.unit.test.ts +36 -0
- package/src/__tests__/unit/adapters/etherscan.unit.test.ts +7 -87
- package/src/__tests__/unit/adapters/ledger.unit.test.ts +2 -20
- package/src/__tests__/unit/api/explorer/etherscan.unit.test.ts +0 -12
- package/src/__tests__/unit/api/explorer/ledger.unit.test.ts +0 -16
- package/src/__tests__/unit/broadcast.unit.test.ts +3 -5
- package/src/__tests__/unit/buildOptimisticOperation.unit.test.ts +2 -5
- package/src/__tests__/unit/deviceTransactionConfig.unit.test.ts +0 -1
- package/src/__tests__/unit/estimateMaxSpendable.unit.test.ts +3 -5
- package/src/__tests__/unit/getTransactionStatus.unit.test.ts +2 -5
- package/src/__tests__/unit/logic.unit.test.ts +29 -41
- package/src/__tests__/unit/operation.test.ts +5 -9
- package/src/__tests__/unit/prepareTransaction.unit.test.ts +18 -4
- package/src/adapters/etherscan.ts +2 -5
- package/src/adapters/ledger.ts +2 -5
- package/src/api/index.ts +5 -4
- package/src/bridge/js.ts +0 -4
- package/src/bridge/preload.ts +0 -8
- package/src/deviceTransactionConfig.ts +2 -3
- package/src/logic/common.ts +0 -52
- package/src/logic/estimateFees.test.ts +57 -98
- package/src/logic/estimateFees.ts +18 -28
- package/src/logic/index.ts +0 -1
- package/src/types/transaction.ts +0 -12
- package/lib/__fixtures__/cardano-native-8654e8b350e298c80d2451beb5ed80fc9eee9f38ce6b039fb8706bc34c4f4253544552.json +0 -52
- package/lib/__fixtures__/ethereum-erc20-usd__coin-0x02b972f304dc24c9bc362e6435c4ad654241f9af916689a4790145c9bcbdf4cf-IN-i0.json +0 -20
- package/lib/__fixtures__/ethereum-erc20-usd__coin-0x02b972f304dc24c9bc362e6435c4ad654241f9af916689a4790145c9bcbdf4cf-OUT-i0.json +0 -19
- package/lib/__tests__/fixtures/cryptoAssetsStore.fixtures.d.ts +0 -2
- package/lib/__tests__/fixtures/cryptoAssetsStore.fixtures.d.ts.map +0 -1
- package/lib/__tests__/fixtures/cryptoAssetsStore.fixtures.js +0 -44
- package/lib/__tests__/fixtures/cryptoAssetsStore.fixtures.js.map +0 -1
- package/lib/cryptoAssetsStore.d.ts +0 -5
- package/lib/cryptoAssetsStore.d.ts.map +0 -1
- package/lib/cryptoAssetsStore.js +0 -17
- package/lib/cryptoAssetsStore.js.map +0 -1
- package/lib/cryptoAssetsStore.test.d.ts +0 -2
- package/lib/cryptoAssetsStore.test.d.ts.map +0 -1
- package/lib/cryptoAssetsStore.test.js +0 -15
- package/lib/cryptoAssetsStore.test.js.map +0 -1
- package/lib/logic/validateIntent.d.ts +0 -4
- package/lib/logic/validateIntent.d.ts.map +0 -1
- package/lib/logic/validateIntent.js +0 -192
- package/lib/logic/validateIntent.js.map +0 -1
- package/lib/logic/validateIntent.test.d.ts +0 -2
- package/lib/logic/validateIntent.test.d.ts.map +0 -1
- package/lib/logic/validateIntent.test.js +0 -485
- package/lib/logic/validateIntent.test.js.map +0 -1
- package/lib-es/__fixtures__/cardano-native-8654e8b350e298c80d2451beb5ed80fc9eee9f38ce6b039fb8706bc34c4f4253544552.json +0 -52
- package/lib-es/__fixtures__/ethereum-erc20-usd__coin-0x02b972f304dc24c9bc362e6435c4ad654241f9af916689a4790145c9bcbdf4cf-IN-i0.json +0 -20
- package/lib-es/__fixtures__/ethereum-erc20-usd__coin-0x02b972f304dc24c9bc362e6435c4ad654241f9af916689a4790145c9bcbdf4cf-OUT-i0.json +0 -19
- package/lib-es/__tests__/fixtures/cryptoAssetsStore.fixtures.d.ts +0 -2
- package/lib-es/__tests__/fixtures/cryptoAssetsStore.fixtures.d.ts.map +0 -1
- package/lib-es/__tests__/fixtures/cryptoAssetsStore.fixtures.js +0 -19
- package/lib-es/__tests__/fixtures/cryptoAssetsStore.fixtures.js.map +0 -1
- package/lib-es/cryptoAssetsStore.d.ts +0 -5
- package/lib-es/cryptoAssetsStore.d.ts.map +0 -1
- package/lib-es/cryptoAssetsStore.js +0 -12
- package/lib-es/cryptoAssetsStore.js.map +0 -1
- package/lib-es/cryptoAssetsStore.test.d.ts +0 -2
- package/lib-es/cryptoAssetsStore.test.d.ts.map +0 -1
- package/lib-es/cryptoAssetsStore.test.js +0 -13
- package/lib-es/cryptoAssetsStore.test.js.map +0 -1
- package/lib-es/logic/validateIntent.d.ts +0 -4
- package/lib-es/logic/validateIntent.d.ts.map +0 -1
- package/lib-es/logic/validateIntent.js +0 -185
- package/lib-es/logic/validateIntent.js.map +0 -1
- package/lib-es/logic/validateIntent.test.d.ts +0 -2
- package/lib-es/logic/validateIntent.test.d.ts.map +0 -1
- package/lib-es/logic/validateIntent.test.js +0 -480
- package/lib-es/logic/validateIntent.test.js.map +0 -1
- package/src/__fixtures__/cardano-native-8654e8b350e298c80d2451beb5ed80fc9eee9f38ce6b039fb8706bc34c4f4253544552.json +0 -52
- package/src/__fixtures__/ethereum-erc20-usd__coin-0x02b972f304dc24c9bc362e6435c4ad654241f9af916689a4790145c9bcbdf4cf-IN-i0.json +0 -20
- package/src/__fixtures__/ethereum-erc20-usd__coin-0x02b972f304dc24c9bc362e6435c4ad654241f9af916689a4790145c9bcbdf4cf-OUT-i0.json +0 -19
- package/src/__fixtures__/ethereum-erc20-usd__coin-0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48.json +0 -73
- package/src/__fixtures__/ethereum-erc20-weth.json +0 -73
- package/src/__fixtures__/optimism-erc20-usd_coin.json +0 -69
- package/src/__tests__/fixtures/cryptoAssetsStore.fixtures.ts +0 -24
- package/src/cryptoAssetsStore.test.ts +0 -19
- package/src/cryptoAssetsStore.ts +0 -16
- package/src/logic/validateIntent.test.ts +0 -659
- 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
|
-
}
|