@ledgerhq/coin-aptos 2.5.1 → 2.5.2-nightly.1
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/.turbo/turbo-build.log +1 -1
- package/.unimportedrc.json +5 -8
- package/CHANGELOG.md +18 -0
- package/lib/__tests__/bridge/deviceTransactionConfig.test.js +115 -30
- package/lib/__tests__/bridge/deviceTransactionConfig.test.js.map +1 -1
- package/lib/__tests__/bridge/getFeesForTransaction.test.js +10 -10
- package/lib/__tests__/bridge/getFeesForTransaction.test.js.map +1 -1
- package/lib/__tests__/bridge/getTransactionStatus.test.js +193 -2
- package/lib/__tests__/bridge/getTransactionStatus.test.js.map +1 -1
- package/lib/__tests__/bridge/index.test.js +4 -1
- package/lib/__tests__/bridge/index.test.js.map +1 -1
- package/lib/__tests__/bridge/logic.test.js +9 -9
- package/lib/__tests__/bridge/logic.test.js.map +1 -1
- package/lib/__tests__/bridge/prepareTransaction.test.js +1 -0
- package/lib/__tests__/bridge/prepareTransaction.test.js.map +1 -1
- package/lib/__tests__/bridge/serialization.d.ts +2 -0
- package/lib/__tests__/bridge/serialization.d.ts.map +1 -0
- package/lib/__tests__/bridge/serialization.js +91 -0
- package/lib/__tests__/bridge/serialization.js.map +1 -0
- package/lib/__tests__/bridge/synchronisation.test.js +763 -23
- package/lib/__tests__/bridge/synchronisation.test.js.map +1 -1
- package/lib/__tests__/logic/staking.test.d.ts +2 -0
- package/lib/__tests__/logic/staking.test.d.ts.map +1 -0
- package/lib/__tests__/logic/staking.test.js +202 -0
- package/lib/__tests__/logic/staking.test.js.map +1 -0
- package/lib/__tests__/network/client.test.js +3 -3
- package/lib/__tests__/network/validators.test.d.ts +2 -0
- package/lib/__tests__/network/validators.test.d.ts.map +1 -0
- package/lib/__tests__/network/validators.test.js +98 -0
- package/lib/__tests__/network/validators.test.js.map +1 -0
- package/lib/__tests__/utils/IconGenerator.test.d.ts +2 -0
- package/lib/__tests__/utils/IconGenerator.test.d.ts.map +1 -0
- package/lib/__tests__/utils/IconGenerator.test.js +14 -0
- package/lib/__tests__/utils/IconGenerator.test.js.map +1 -0
- package/lib/__tests__/utils/index.test.d.ts +2 -0
- package/lib/__tests__/utils/index.test.d.ts.map +1 -0
- package/lib/__tests__/utils/index.test.js +11 -0
- package/lib/__tests__/utils/index.test.js.map +1 -0
- package/lib/bridge/bridge.fixture.d.ts.map +1 -1
- package/lib/bridge/bridge.fixture.js +12 -0
- package/lib/bridge/bridge.fixture.js.map +1 -1
- package/lib/bridge/deviceTransactionConfig.d.ts +7 -8
- package/lib/bridge/deviceTransactionConfig.d.ts.map +1 -1
- package/lib/bridge/deviceTransactionConfig.js +83 -16
- package/lib/bridge/deviceTransactionConfig.js.map +1 -1
- package/lib/bridge/estimateMaxSpendable.d.ts +4 -4
- package/lib/bridge/estimateMaxSpendable.d.ts.map +1 -1
- package/lib/bridge/estimateMaxSpendable.js +1 -1
- package/lib/bridge/estimateMaxSpendable.js.map +1 -1
- package/lib/bridge/getFeesForTransaction.d.ts.map +1 -1
- package/lib/bridge/getFeesForTransaction.js +8 -4
- package/lib/bridge/getFeesForTransaction.js.map +1 -1
- package/lib/bridge/getTransactionStatus.d.ts +2 -3
- package/lib/bridge/getTransactionStatus.d.ts.map +1 -1
- package/lib/bridge/getTransactionStatus.js +139 -25
- package/lib/bridge/getTransactionStatus.js.map +1 -1
- package/lib/bridge/index.d.ts +3 -3
- package/lib/bridge/index.d.ts.map +1 -1
- package/lib/bridge/index.js +9 -2
- package/lib/bridge/index.js.map +1 -1
- package/lib/bridge/logic.d.ts +3 -3
- package/lib/bridge/logic.d.ts.map +1 -1
- package/lib/bridge/logic.js +23 -9
- package/lib/bridge/logic.js.map +1 -1
- package/lib/bridge/prepareTransaction.d.ts +2 -3
- package/lib/bridge/prepareTransaction.d.ts.map +1 -1
- package/lib/bridge/prepareTransaction.js +46 -10
- package/lib/bridge/prepareTransaction.js.map +1 -1
- package/lib/bridge/serialization.d.ts +7 -0
- package/lib/bridge/serialization.d.ts.map +1 -0
- package/lib/bridge/serialization.js +51 -0
- package/lib/bridge/serialization.js.map +1 -0
- package/lib/bridge/signOperation.d.ts.map +1 -1
- package/lib/bridge/signOperation.js +17 -5
- package/lib/bridge/signOperation.js.map +1 -1
- package/lib/bridge/synchronisation.d.ts +6 -4
- package/lib/bridge/synchronisation.d.ts.map +1 -1
- package/lib/bridge/synchronisation.js +51 -4
- package/lib/bridge/synchronisation.js.map +1 -1
- package/lib/constants.d.ts +25 -1
- package/lib/constants.d.ts.map +1 -1
- package/lib/constants.js +54 -8
- package/lib/constants.js.map +1 -1
- package/lib/index.d.ts +0 -1
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +0 -15
- package/lib/index.js.map +1 -1
- package/lib/logic/buildTransaction.d.ts.map +1 -1
- package/lib/logic/buildTransaction.js +42 -19
- package/lib/logic/buildTransaction.js.map +1 -1
- package/lib/logic/getCoinAndAmounts.d.ts +2 -0
- package/lib/logic/getCoinAndAmounts.d.ts.map +1 -1
- package/lib/logic/getCoinAndAmounts.js +65 -38
- package/lib/logic/getCoinAndAmounts.js.map +1 -1
- package/lib/logic/staking.d.ts +12 -0
- package/lib/logic/staking.d.ts.map +1 -0
- package/lib/logic/staking.js +81 -0
- package/lib/logic/staking.js.map +1 -0
- package/lib/logic/transactionsToOperations.js +3 -3
- package/lib/logic/transactionsToOperations.js.map +1 -1
- package/lib/network/client.d.ts +4 -1
- package/lib/network/client.d.ts.map +1 -1
- package/lib/network/client.js +32 -1
- package/lib/network/client.js.map +1 -1
- package/lib/network/graphql/queries.d.ts +2 -0
- package/lib/network/graphql/queries.d.ts.map +1 -1
- package/lib/network/graphql/queries.js +42 -2
- package/lib/network/graphql/queries.js.map +1 -1
- package/lib/network/graphql/types.d.ts +57 -6
- package/lib/network/graphql/types.d.ts.map +1 -1
- package/lib/network/index.d.ts.map +1 -1
- package/lib/network/index.js.map +1 -1
- package/lib/network/validators.d.ts +3 -0
- package/lib/network/validators.d.ts.map +1 -0
- package/lib/network/validators.js +42 -0
- package/lib/network/validators.js.map +1 -0
- package/lib/preload-data.d.ts +7 -0
- package/lib/preload-data.d.ts.map +1 -0
- package/lib/preload-data.js +42 -0
- package/lib/preload-data.js.map +1 -0
- package/lib/preload.d.ts +6 -0
- package/lib/preload.d.ts.map +1 -0
- package/lib/preload.js +21 -0
- package/lib/preload.js.map +1 -0
- package/lib/test/bridgeDatasetTest.d.ts.map +1 -1
- package/lib/test/bridgeDatasetTest.js +1 -0
- package/lib/test/bridgeDatasetTest.js.map +1 -1
- package/lib/types/index.d.ts +92 -8
- package/lib/types/index.d.ts.map +1 -1
- package/lib/types/index.js.map +1 -1
- package/lib/utils/IconGenerator.d.ts +12 -0
- package/lib/utils/IconGenerator.d.ts.map +1 -0
- package/lib/utils/IconGenerator.js +138 -0
- package/lib/utils/IconGenerator.js.map +1 -0
- package/lib/utils/index.d.ts +3 -0
- package/lib/utils/index.d.ts.map +1 -0
- package/lib/utils/index.js +31 -0
- package/lib/utils/index.js.map +1 -0
- package/lib-es/__tests__/bridge/deviceTransactionConfig.test.js +113 -8
- package/lib-es/__tests__/bridge/deviceTransactionConfig.test.js.map +1 -1
- package/lib-es/__tests__/bridge/getFeesForTransaction.test.js +10 -10
- package/lib-es/__tests__/bridge/getFeesForTransaction.test.js.map +1 -1
- package/lib-es/__tests__/bridge/getTransactionStatus.test.js +194 -3
- package/lib-es/__tests__/bridge/getTransactionStatus.test.js.map +1 -1
- package/lib-es/__tests__/bridge/index.test.js +4 -1
- package/lib-es/__tests__/bridge/index.test.js.map +1 -1
- package/lib-es/__tests__/bridge/logic.test.js +10 -10
- package/lib-es/__tests__/bridge/logic.test.js.map +1 -1
- package/lib-es/__tests__/bridge/prepareTransaction.test.js +1 -0
- package/lib-es/__tests__/bridge/prepareTransaction.test.js.map +1 -1
- package/lib-es/__tests__/bridge/serialization.d.ts +2 -0
- package/lib-es/__tests__/bridge/serialization.d.ts.map +1 -0
- package/lib-es/__tests__/bridge/serialization.js +86 -0
- package/lib-es/__tests__/bridge/serialization.js.map +1 -0
- package/lib-es/__tests__/bridge/synchronisation.test.js +763 -23
- package/lib-es/__tests__/bridge/synchronisation.test.js.map +1 -1
- package/lib-es/__tests__/logic/staking.test.d.ts +2 -0
- package/lib-es/__tests__/logic/staking.test.d.ts.map +1 -0
- package/lib-es/__tests__/logic/staking.test.js +197 -0
- package/lib-es/__tests__/logic/staking.test.js.map +1 -0
- package/lib-es/__tests__/network/client.test.js +3 -3
- package/lib-es/__tests__/network/validators.test.d.ts +2 -0
- package/lib-es/__tests__/network/validators.test.d.ts.map +1 -0
- package/lib-es/__tests__/network/validators.test.js +93 -0
- package/lib-es/__tests__/network/validators.test.js.map +1 -0
- package/lib-es/__tests__/utils/IconGenerator.test.d.ts +2 -0
- package/lib-es/__tests__/utils/IconGenerator.test.d.ts.map +1 -0
- package/lib-es/__tests__/utils/IconGenerator.test.js +12 -0
- package/lib-es/__tests__/utils/IconGenerator.test.js.map +1 -0
- package/lib-es/__tests__/utils/index.test.d.ts +2 -0
- package/lib-es/__tests__/utils/index.test.d.ts.map +1 -0
- package/lib-es/__tests__/utils/index.test.js +9 -0
- package/lib-es/__tests__/utils/index.test.js.map +1 -0
- package/lib-es/bridge/bridge.fixture.d.ts.map +1 -1
- package/lib-es/bridge/bridge.fixture.js +12 -0
- package/lib-es/bridge/bridge.fixture.js.map +1 -1
- package/lib-es/bridge/deviceTransactionConfig.d.ts +7 -8
- package/lib-es/bridge/deviceTransactionConfig.d.ts.map +1 -1
- package/lib-es/bridge/deviceTransactionConfig.js +83 -14
- package/lib-es/bridge/deviceTransactionConfig.js.map +1 -1
- package/lib-es/bridge/estimateMaxSpendable.d.ts +4 -4
- package/lib-es/bridge/estimateMaxSpendable.d.ts.map +1 -1
- package/lib-es/bridge/estimateMaxSpendable.js +1 -1
- package/lib-es/bridge/estimateMaxSpendable.js.map +1 -1
- package/lib-es/bridge/getFeesForTransaction.d.ts.map +1 -1
- package/lib-es/bridge/getFeesForTransaction.js +9 -5
- package/lib-es/bridge/getFeesForTransaction.js.map +1 -1
- package/lib-es/bridge/getTransactionStatus.d.ts +2 -3
- package/lib-es/bridge/getTransactionStatus.d.ts.map +1 -1
- package/lib-es/bridge/getTransactionStatus.js +140 -26
- package/lib-es/bridge/getTransactionStatus.js.map +1 -1
- package/lib-es/bridge/index.d.ts +3 -3
- package/lib-es/bridge/index.d.ts.map +1 -1
- package/lib-es/bridge/index.js +9 -2
- package/lib-es/bridge/index.js.map +1 -1
- package/lib-es/bridge/logic.d.ts +3 -3
- package/lib-es/bridge/logic.d.ts.map +1 -1
- package/lib-es/bridge/logic.js +23 -9
- package/lib-es/bridge/logic.js.map +1 -1
- package/lib-es/bridge/prepareTransaction.d.ts +2 -3
- package/lib-es/bridge/prepareTransaction.d.ts.map +1 -1
- package/lib-es/bridge/prepareTransaction.js +46 -10
- package/lib-es/bridge/prepareTransaction.js.map +1 -1
- package/lib-es/bridge/serialization.d.ts +7 -0
- package/lib-es/bridge/serialization.d.ts.map +1 -0
- package/lib-es/bridge/serialization.js +41 -0
- package/lib-es/bridge/serialization.js.map +1 -0
- package/lib-es/bridge/signOperation.d.ts.map +1 -1
- package/lib-es/bridge/signOperation.js +17 -5
- package/lib-es/bridge/signOperation.js.map +1 -1
- package/lib-es/bridge/synchronisation.d.ts +6 -4
- package/lib-es/bridge/synchronisation.d.ts.map +1 -1
- package/lib-es/bridge/synchronisation.js +49 -3
- package/lib-es/bridge/synchronisation.js.map +1 -1
- package/lib-es/constants.d.ts +25 -1
- package/lib-es/constants.d.ts.map +1 -1
- package/lib-es/constants.js +53 -7
- package/lib-es/constants.js.map +1 -1
- package/lib-es/index.d.ts +0 -1
- package/lib-es/index.d.ts.map +1 -1
- package/lib-es/index.js +0 -1
- package/lib-es/index.js.map +1 -1
- package/lib-es/logic/buildTransaction.d.ts.map +1 -1
- package/lib-es/logic/buildTransaction.js +42 -19
- package/lib-es/logic/buildTransaction.js.map +1 -1
- package/lib-es/logic/getCoinAndAmounts.d.ts +2 -0
- package/lib-es/logic/getCoinAndAmounts.d.ts.map +1 -1
- package/lib-es/logic/getCoinAndAmounts.js +66 -39
- package/lib-es/logic/getCoinAndAmounts.js.map +1 -1
- package/lib-es/logic/staking.d.ts +12 -0
- package/lib-es/logic/staking.d.ts.map +1 -0
- package/lib-es/logic/staking.js +67 -0
- package/lib-es/logic/staking.js.map +1 -0
- package/lib-es/logic/transactionsToOperations.js +4 -4
- package/lib-es/logic/transactionsToOperations.js.map +1 -1
- package/lib-es/network/client.d.ts +4 -1
- package/lib-es/network/client.d.ts.map +1 -1
- package/lib-es/network/client.js +32 -1
- package/lib-es/network/client.js.map +1 -1
- package/lib-es/network/graphql/queries.d.ts +2 -0
- package/lib-es/network/graphql/queries.d.ts.map +1 -1
- package/lib-es/network/graphql/queries.js +41 -1
- package/lib-es/network/graphql/queries.js.map +1 -1
- package/lib-es/network/graphql/types.d.ts +57 -6
- package/lib-es/network/graphql/types.d.ts.map +1 -1
- package/lib-es/network/index.d.ts.map +1 -1
- package/lib-es/network/index.js.map +1 -1
- package/lib-es/network/validators.d.ts +3 -0
- package/lib-es/network/validators.d.ts.map +1 -0
- package/lib-es/network/validators.js +35 -0
- package/lib-es/network/validators.js.map +1 -0
- package/lib-es/preload-data.d.ts +7 -0
- package/lib-es/preload-data.d.ts.map +1 -0
- package/lib-es/preload-data.js +36 -0
- package/lib-es/preload-data.js.map +1 -0
- package/lib-es/preload.d.ts +6 -0
- package/lib-es/preload.d.ts.map +1 -0
- package/lib-es/preload.js +16 -0
- package/lib-es/preload.js.map +1 -0
- package/lib-es/test/bridgeDatasetTest.d.ts.map +1 -1
- package/lib-es/test/bridgeDatasetTest.js +1 -0
- package/lib-es/test/bridgeDatasetTest.js.map +1 -1
- package/lib-es/types/index.d.ts +92 -8
- package/lib-es/types/index.d.ts.map +1 -1
- package/lib-es/types/index.js.map +1 -1
- package/lib-es/utils/IconGenerator.d.ts +12 -0
- package/lib-es/utils/IconGenerator.d.ts.map +1 -0
- package/lib-es/utils/IconGenerator.js +134 -0
- package/lib-es/utils/IconGenerator.js.map +1 -0
- package/lib-es/utils/index.d.ts +3 -0
- package/lib-es/utils/index.d.ts.map +1 -0
- package/lib-es/utils/index.js +13 -0
- package/lib-es/utils/index.js.map +1 -0
- package/package.json +14 -9
- package/src/__tests__/bridge/deviceTransactionConfig.test.ts +120 -8
- package/src/__tests__/bridge/getFeesForTransaction.test.ts +10 -10
- package/src/__tests__/bridge/getTransactionStatus.test.ts +247 -3
- package/src/__tests__/bridge/index.test.ts +5 -1
- package/src/__tests__/bridge/logic.test.ts +10 -10
- package/src/__tests__/bridge/prepareTransaction.test.ts +4 -4
- package/src/__tests__/bridge/serialization.ts +103 -0
- package/src/__tests__/bridge/synchronisation.test.ts +798 -32
- package/src/__tests__/logic/staking.test.ts +221 -0
- package/src/__tests__/network/client.test.ts +3 -3
- package/src/__tests__/network/validators.test.ts +105 -0
- package/src/__tests__/utils/IconGenerator.test.ts +13 -0
- package/src/__tests__/utils/index.test.ts +11 -0
- package/src/bridge/bridge.fixture.ts +12 -0
- package/src/bridge/deviceTransactionConfig.ts +90 -21
- package/src/bridge/estimateMaxSpendable.ts +5 -5
- package/src/bridge/getFeesForTransaction.ts +17 -5
- package/src/bridge/getTransactionStatus.ts +208 -32
- package/src/bridge/index.ts +14 -5
- package/src/bridge/logic.ts +28 -12
- package/src/bridge/prepareTransaction.ts +72 -24
- package/src/bridge/serialization.ts +52 -0
- package/src/bridge/signOperation.ts +20 -7
- package/src/bridge/synchronisation.ts +69 -12
- package/src/constants.ts +64 -2
- package/src/index.ts +0 -2
- package/src/logic/buildTransaction.ts +49 -24
- package/src/logic/getCoinAndAmounts.ts +91 -41
- package/src/logic/staking.ts +105 -0
- package/src/logic/transactionsToOperations.ts +5 -5
- package/src/network/client.ts +44 -4
- package/src/network/graphql/queries.ts +46 -1
- package/src/network/graphql/types.ts +63 -6
- package/src/network/index.ts +1 -0
- package/src/network/validators.ts +48 -0
- package/src/preload-data.ts +43 -0
- package/src/preload.ts +23 -0
- package/src/test/bridgeDatasetTest.ts +1 -0
- package/src/types/index.ts +106 -8
- package/src/utils/IconGenerator.ts +166 -0
- package/src/utils/index.ts +16 -0
|
@@ -1,5 +1,10 @@
|
|
|
1
1
|
import {
|
|
2
2
|
NotEnoughBalance,
|
|
3
|
+
NotEnoughToStake,
|
|
4
|
+
UnstakeNotEnoughStakedBalanceLeft,
|
|
5
|
+
RestakeNotEnoughStakedBalanceLeft,
|
|
6
|
+
NotEnoughToRestake,
|
|
7
|
+
NotEnoughToUnstake,
|
|
3
8
|
RecipientRequired,
|
|
4
9
|
InvalidAddress,
|
|
5
10
|
FeeNotLoaded,
|
|
@@ -7,69 +12,240 @@ import {
|
|
|
7
12
|
AmountRequired,
|
|
8
13
|
NotEnoughBalanceFees,
|
|
9
14
|
} from "@ledgerhq/errors";
|
|
10
|
-
import type { Account } from "@ledgerhq/types-live";
|
|
11
|
-
import { BigNumber } from "bignumber.js";
|
|
12
|
-
import type { Transaction, TransactionStatus } from "../types";
|
|
13
|
-
|
|
14
15
|
import { AccountAddress } from "@aptos-labs/ts-sdk";
|
|
16
|
+
import { BigNumber } from "bignumber.js";
|
|
17
|
+
import type { AptosAccount, AptosStakingPosition, Transaction, TransactionStatus } from "../types";
|
|
15
18
|
import { getTokenAccount } from "./logic";
|
|
19
|
+
import {
|
|
20
|
+
APTOS_DELEGATION_RESERVE_IN_OCTAS,
|
|
21
|
+
APTOS_MINIMUM_RESTAKE,
|
|
22
|
+
APTOS_MINIMUM_RESTAKE_IN_OCTAS,
|
|
23
|
+
APTOS_PRECISION,
|
|
24
|
+
MIN_AMOUNT_TO_UNSTAKE,
|
|
25
|
+
MIN_AMOUNT_TO_UNSTAKE_IN_OCTAS,
|
|
26
|
+
MIN_COINS_ON_SHARES_POOL,
|
|
27
|
+
MIN_COINS_ON_SHARES_POOL_IN_OCTAS,
|
|
28
|
+
} from "../constants";
|
|
29
|
+
import { TokenAccount } from "@ledgerhq/types-live";
|
|
30
|
+
import { getStakingPosition } from "../logic/staking";
|
|
16
31
|
|
|
17
|
-
const
|
|
18
|
-
|
|
19
|
-
|
|
32
|
+
const checkSendTransaction = (
|
|
33
|
+
t: Transaction,
|
|
34
|
+
a: AptosAccount,
|
|
35
|
+
tokenAccount: TokenAccount | undefined,
|
|
36
|
+
estimatedFees: BigNumber,
|
|
37
|
+
errors: Record<string, Error>,
|
|
38
|
+
): Record<string, Error> => {
|
|
39
|
+
const newErrors = { ...errors };
|
|
20
40
|
|
|
21
41
|
if (!t.recipient) {
|
|
22
|
-
|
|
42
|
+
newErrors.recipient = new RecipientRequired();
|
|
23
43
|
}
|
|
24
44
|
|
|
25
|
-
if (!AccountAddress.isValid({ input: t.recipient }).valid && !
|
|
26
|
-
|
|
45
|
+
if (!AccountAddress.isValid({ input: t.recipient }).valid && !newErrors.recipient) {
|
|
46
|
+
newErrors.recipient = new InvalidAddress("", { currencyName: a.currency.name });
|
|
27
47
|
}
|
|
28
48
|
|
|
29
|
-
if (t.recipient === a.freshAddress && !
|
|
30
|
-
|
|
49
|
+
if (t.recipient === a.freshAddress && !newErrors.recipient) {
|
|
50
|
+
newErrors.recipient = new InvalidAddressBecauseDestinationIsAlsoSource();
|
|
31
51
|
}
|
|
32
52
|
|
|
33
|
-
if (
|
|
34
|
-
|
|
53
|
+
if (t.amount.gt(a.balance) && !newErrors.amount) {
|
|
54
|
+
newErrors.amount = new NotEnoughBalance();
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
if (tokenAccount && t.errors?.maxGasAmount === "GasInsufficientBalance" && !newErrors.amount) {
|
|
58
|
+
newErrors.amount = new NotEnoughBalanceFees();
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
if (
|
|
62
|
+
(tokenAccount
|
|
63
|
+
? tokenAccount.spendableBalance.isLessThan(t.amount) ||
|
|
64
|
+
a.spendableBalance.isLessThan(estimatedFees)
|
|
65
|
+
: a.spendableBalance.isLessThan(t.amount.plus(estimatedFees))) &&
|
|
66
|
+
!newErrors.amount
|
|
67
|
+
) {
|
|
68
|
+
newErrors.amount = new NotEnoughBalance();
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
return newErrors;
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
const checkStakeTransaction = (
|
|
75
|
+
t: Transaction,
|
|
76
|
+
stakingPosition: AptosStakingPosition | undefined,
|
|
77
|
+
a: AptosAccount,
|
|
78
|
+
errors: Record<string, Error>,
|
|
79
|
+
): Record<string, Error> => {
|
|
80
|
+
const newErrors = { ...errors };
|
|
81
|
+
|
|
82
|
+
if (t.amount.gt(a.spendableBalance)) {
|
|
83
|
+
newErrors.amount = new NotEnoughBalance();
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
if (
|
|
87
|
+
(((!stakingPosition || stakingPosition?.active.isZero()) &&
|
|
88
|
+
((!t.useAllAmount && t.amount.lt(MIN_COINS_ON_SHARES_POOL_IN_OCTAS)) ||
|
|
89
|
+
(t.useAllAmount &&
|
|
90
|
+
a.spendableBalance
|
|
91
|
+
.minus(APTOS_DELEGATION_RESERVE_IN_OCTAS)
|
|
92
|
+
.lt(MIN_COINS_ON_SHARES_POOL_IN_OCTAS)))) ||
|
|
93
|
+
(stakingPosition &&
|
|
94
|
+
!stakingPosition.active.isZero() &&
|
|
95
|
+
t.amount.lt(APTOS_MINIMUM_RESTAKE_IN_OCTAS))) &&
|
|
96
|
+
!newErrors.amount
|
|
97
|
+
) {
|
|
98
|
+
newErrors.amount = new NotEnoughToStake("", {
|
|
99
|
+
minStake:
|
|
100
|
+
stakingPosition && !stakingPosition.active.isZero()
|
|
101
|
+
? APTOS_MINIMUM_RESTAKE
|
|
102
|
+
: MIN_COINS_ON_SHARES_POOL,
|
|
103
|
+
currency: a.currency.ticker,
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
return newErrors;
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
const checkRestakeTransaction = (
|
|
111
|
+
t: Transaction,
|
|
112
|
+
stakingPosition: AptosStakingPosition | undefined,
|
|
113
|
+
errors: Record<string, Error>,
|
|
114
|
+
): Record<string, Error> => {
|
|
115
|
+
const newErrors = { ...errors };
|
|
116
|
+
|
|
117
|
+
if (!stakingPosition) {
|
|
118
|
+
newErrors.recipient = new RecipientRequired();
|
|
119
|
+
} else {
|
|
120
|
+
if ((t.amount.gt(stakingPosition.pendingInactive) || t.amount.isZero()) && !newErrors.amount) {
|
|
121
|
+
newErrors.amount = new NotEnoughBalance();
|
|
122
|
+
}
|
|
123
|
+
if (!t.useAllAmount && t.amount.lt(MIN_COINS_ON_SHARES_POOL_IN_OCTAS) && !newErrors.amount) {
|
|
124
|
+
newErrors.amount = new NotEnoughToRestake("", {
|
|
125
|
+
minAmount: `${MIN_COINS_ON_SHARES_POOL.toNumber().toString()} APT`,
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
if (
|
|
129
|
+
!t.useAllAmount &&
|
|
130
|
+
stakingPosition.pendingInactive.minus(t.amount).lt(MIN_COINS_ON_SHARES_POOL_IN_OCTAS) &&
|
|
131
|
+
!newErrors.amount
|
|
132
|
+
) {
|
|
133
|
+
newErrors.amount = new RestakeNotEnoughStakedBalanceLeft("", {
|
|
134
|
+
maxAmount: `${stakingPosition.pendingInactive.minus(MIN_COINS_ON_SHARES_POOL_IN_OCTAS).shiftedBy(-APTOS_PRECISION).toNumber().toString()} APT`,
|
|
135
|
+
totalAmount: `${stakingPosition.pendingInactive.shiftedBy(-APTOS_PRECISION).toNumber().toString()} APT`,
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
return newErrors;
|
|
141
|
+
};
|
|
142
|
+
|
|
143
|
+
const checkUnstakeTransaction = (
|
|
144
|
+
t: Transaction,
|
|
145
|
+
stakingPosition: AptosStakingPosition | undefined,
|
|
146
|
+
errors: Record<string, Error>,
|
|
147
|
+
): Record<string, Error> => {
|
|
148
|
+
const newErrors = { ...errors };
|
|
149
|
+
|
|
150
|
+
if (!stakingPosition) {
|
|
151
|
+
newErrors.recipient = new RecipientRequired();
|
|
152
|
+
} else {
|
|
153
|
+
if (t.amount.gt(stakingPosition.active) && !newErrors.amount) {
|
|
154
|
+
newErrors.amount = new NotEnoughBalance();
|
|
155
|
+
}
|
|
156
|
+
if (!t.useAllAmount && t.amount.lt(MIN_AMOUNT_TO_UNSTAKE_IN_OCTAS) && !newErrors.amount) {
|
|
157
|
+
newErrors.amount = new NotEnoughToUnstake("", {
|
|
158
|
+
minAmount: `${MIN_AMOUNT_TO_UNSTAKE.toNumber().toString()} APT`,
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
if (
|
|
162
|
+
!t.useAllAmount &&
|
|
163
|
+
stakingPosition.active.minus(t.amount).lt(MIN_AMOUNT_TO_UNSTAKE_IN_OCTAS) &&
|
|
164
|
+
!newErrors.amount
|
|
165
|
+
) {
|
|
166
|
+
newErrors.amount = new UnstakeNotEnoughStakedBalanceLeft("", {
|
|
167
|
+
maxAmount: `${stakingPosition.active.minus(MIN_AMOUNT_TO_UNSTAKE_IN_OCTAS).shiftedBy(-APTOS_PRECISION).toNumber().toString()} APT`,
|
|
168
|
+
totalAmount: `${stakingPosition.active.shiftedBy(-APTOS_PRECISION).toNumber().toString()} APT`,
|
|
169
|
+
});
|
|
170
|
+
}
|
|
35
171
|
}
|
|
36
172
|
|
|
173
|
+
return newErrors;
|
|
174
|
+
};
|
|
175
|
+
|
|
176
|
+
const checkWithdrawTransaction = (
|
|
177
|
+
t: Transaction,
|
|
178
|
+
stakingPosition: AptosStakingPosition | undefined,
|
|
179
|
+
errors: Record<string, Error>,
|
|
180
|
+
) => {
|
|
181
|
+
const newErrors = { ...errors };
|
|
182
|
+
|
|
183
|
+
if (!stakingPosition) {
|
|
184
|
+
newErrors.recipient = new RecipientRequired();
|
|
185
|
+
} else if (t.amount.gt(stakingPosition.inactive) && !newErrors.amount) {
|
|
186
|
+
newErrors.amount = new NotEnoughBalance();
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
return newErrors;
|
|
190
|
+
};
|
|
191
|
+
|
|
192
|
+
const getTransactionStatus = async (
|
|
193
|
+
a: AptosAccount,
|
|
194
|
+
t: Transaction,
|
|
195
|
+
): Promise<TransactionStatus> => {
|
|
196
|
+
let errors: Record<string, Error> = {};
|
|
197
|
+
const warnings = {};
|
|
198
|
+
|
|
37
199
|
const estimatedFees = t.fees || BigNumber(0);
|
|
200
|
+
const tokenAccount = getTokenAccount(a, t);
|
|
201
|
+
|
|
202
|
+
const stakingPosition = getStakingPosition(a, t.recipient);
|
|
38
203
|
|
|
39
|
-
if (t.amount.lte(0)) {
|
|
204
|
+
if (!t.useAllAmount && t.amount.lte(0)) {
|
|
40
205
|
errors.amount = new AmountRequired();
|
|
41
206
|
}
|
|
42
207
|
|
|
43
|
-
|
|
208
|
+
switch (t.mode) {
|
|
209
|
+
case "send":
|
|
210
|
+
errors = checkSendTransaction(t, a, tokenAccount, estimatedFees, errors);
|
|
211
|
+
break;
|
|
212
|
+
case "stake":
|
|
213
|
+
errors = checkStakeTransaction(t, stakingPosition, a, errors);
|
|
214
|
+
break;
|
|
215
|
+
case "restake":
|
|
216
|
+
errors = checkRestakeTransaction(t, stakingPosition, errors);
|
|
217
|
+
break;
|
|
218
|
+
case "unstake":
|
|
219
|
+
errors = checkUnstakeTransaction(t, stakingPosition, errors);
|
|
220
|
+
break;
|
|
221
|
+
case "withdraw":
|
|
222
|
+
errors = checkWithdrawTransaction(t, stakingPosition, errors);
|
|
223
|
+
break;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
if (!t.fees) {
|
|
227
|
+
errors.fees = new FeeNotLoaded();
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
const maxSpendable =
|
|
231
|
+
t.mode === "stake"
|
|
232
|
+
? a.spendableBalance.minus(estimatedFees).minus(APTOS_DELEGATION_RESERVE_IN_OCTAS)
|
|
233
|
+
: a.spendableBalance.minus(estimatedFees);
|
|
44
234
|
|
|
45
235
|
const amount = t.useAllAmount
|
|
46
236
|
? tokenAccount
|
|
47
237
|
? tokenAccount.spendableBalance
|
|
48
238
|
: a.spendableBalance.minus(estimatedFees).isLessThan(0)
|
|
49
239
|
? BigNumber(0)
|
|
50
|
-
:
|
|
240
|
+
: maxSpendable
|
|
51
241
|
: t.amount;
|
|
52
242
|
|
|
53
|
-
const totalSpent = tokenAccount ? amount : amount.plus(estimatedFees);
|
|
54
|
-
|
|
55
|
-
if (tokenAccount && t.errors?.maxGasAmount == "GasInsufficientBalance" && !errors.amount) {
|
|
56
|
-
errors.amount = new NotEnoughBalanceFees();
|
|
57
|
-
}
|
|
58
|
-
if (
|
|
59
|
-
tokenAccount
|
|
60
|
-
? tokenAccount.spendableBalance.isLessThan(totalSpent) ||
|
|
61
|
-
a.spendableBalance.isLessThan(estimatedFees)
|
|
62
|
-
: a.spendableBalance.isLessThan(totalSpent) && !errors.amount
|
|
63
|
-
) {
|
|
64
|
-
errors.amount = new NotEnoughBalance();
|
|
65
|
-
}
|
|
66
|
-
|
|
67
243
|
return Promise.resolve({
|
|
68
244
|
errors,
|
|
69
245
|
warnings,
|
|
70
246
|
estimatedFees,
|
|
71
247
|
amount,
|
|
72
|
-
totalSpent,
|
|
248
|
+
totalSpent: tokenAccount ? amount : amount.plus(estimatedFees),
|
|
73
249
|
});
|
|
74
250
|
};
|
|
75
251
|
|
package/src/bridge/index.ts
CHANGED
|
@@ -7,9 +7,10 @@ import {
|
|
|
7
7
|
} from "@ledgerhq/coin-framework/bridge/jsHelpers";
|
|
8
8
|
import getAddressWrapper from "@ledgerhq/coin-framework/bridge/getAddressWrapper";
|
|
9
9
|
import type { SignerContext } from "@ledgerhq/coin-framework/signer";
|
|
10
|
-
import type {
|
|
10
|
+
import type { AccountBridge, CurrencyBridge } from "@ledgerhq/types-live";
|
|
11
11
|
import resolver from "../signer";
|
|
12
|
-
import
|
|
12
|
+
import { assignToAccountRaw, assignFromAccountRaw } from "./serialization";
|
|
13
|
+
import type { Transaction, TransactionStatus, AptosSigner, AptosAccount } from "../types";
|
|
13
14
|
import getTransactionStatus from "./getTransactionStatus";
|
|
14
15
|
import estimateMaxSpendable from "./estimateMaxSpendable";
|
|
15
16
|
import prepareTransaction from "./prepareTransaction";
|
|
@@ -17,6 +18,12 @@ import { getAccountShape } from "./synchronisation";
|
|
|
17
18
|
import buildSignOperation from "./signOperation";
|
|
18
19
|
import broadcast from "./broadcast";
|
|
19
20
|
import createTransaction from "../logic/createTransaction";
|
|
21
|
+
import { hydrate, preloadWithValidators } from "../preload";
|
|
22
|
+
import { CryptoCurrency } from "@ledgerhq/types-cryptoassets";
|
|
23
|
+
|
|
24
|
+
function makePreload() {
|
|
25
|
+
return (currency: CryptoCurrency) => preloadWithValidators(currency);
|
|
26
|
+
}
|
|
20
27
|
|
|
21
28
|
function buildCurrencyBridge(signerContext: SignerContext<AptosSigner>): CurrencyBridge {
|
|
22
29
|
const getAddress = resolver(signerContext);
|
|
@@ -27,8 +34,8 @@ function buildCurrencyBridge(signerContext: SignerContext<AptosSigner>): Currenc
|
|
|
27
34
|
});
|
|
28
35
|
|
|
29
36
|
return {
|
|
30
|
-
preload: ()
|
|
31
|
-
hydrate
|
|
37
|
+
preload: makePreload(),
|
|
38
|
+
hydrate,
|
|
32
39
|
scanAccounts,
|
|
33
40
|
};
|
|
34
41
|
}
|
|
@@ -37,7 +44,7 @@ const sync = makeSync({ getAccountShape });
|
|
|
37
44
|
|
|
38
45
|
function buildAccountBridge(
|
|
39
46
|
signerContext: SignerContext<AptosSigner>,
|
|
40
|
-
): AccountBridge<Transaction,
|
|
47
|
+
): AccountBridge<Transaction, AptosAccount, TransactionStatus> {
|
|
41
48
|
const getAddress = resolver(signerContext);
|
|
42
49
|
|
|
43
50
|
const receive = makeAccountBridgeReceive(getAddressWrapper(getAddress));
|
|
@@ -54,6 +61,8 @@ function buildAccountBridge(
|
|
|
54
61
|
receive,
|
|
55
62
|
signOperation,
|
|
56
63
|
broadcast,
|
|
64
|
+
assignToAccountRaw,
|
|
65
|
+
assignFromAccountRaw,
|
|
57
66
|
};
|
|
58
67
|
}
|
|
59
68
|
|
package/src/bridge/logic.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import BigNumber from "bignumber.js";
|
|
1
2
|
import { EntryFunctionPayloadResponse } from "@aptos-labs/ts-sdk";
|
|
2
3
|
import type { Account, Operation, OperationType, TokenAccount } from "@ledgerhq/types-live";
|
|
3
4
|
import {
|
|
@@ -6,11 +7,10 @@ import {
|
|
|
6
7
|
findSubAccountById,
|
|
7
8
|
isTokenAccount,
|
|
8
9
|
} from "@ledgerhq/coin-framework/account/index";
|
|
9
|
-
import BigNumber from "bignumber.js";
|
|
10
10
|
import { encodeOperationId } from "@ledgerhq/coin-framework/operation";
|
|
11
|
-
import { APTOS_ASSET_ID, DIRECTION } from "../constants";
|
|
12
|
-
import type { AptosTransaction, Transaction } from "../types";
|
|
13
11
|
import { findTokenByAddressInCurrency } from "@ledgerhq/cryptoassets";
|
|
12
|
+
import { APTOS_ASSET_ID, OP_TYPE, DEFAULT_GAS, DEFAULT_GAS_PRICE } from "../constants";
|
|
13
|
+
import type { AptosTransaction, Transaction } from "../types";
|
|
14
14
|
import { convertFunctionPayloadResponseToInputEntryFunctionData } from "../logic/transactionsToOperations";
|
|
15
15
|
import { compareAddress, getCoinAndAmounts } from "../logic/getCoinAndAmounts";
|
|
16
16
|
import { calculateAmount } from "../logic/calculateAmount";
|
|
@@ -18,14 +18,17 @@ import { processRecipients } from "../logic/processRecipients";
|
|
|
18
18
|
import { getFunctionAddress } from "../logic/getFunctionAddress";
|
|
19
19
|
|
|
20
20
|
export const getMaxSendBalance = (
|
|
21
|
-
gas: BigNumber,
|
|
22
|
-
gasPrice: BigNumber,
|
|
23
21
|
account: Account,
|
|
24
22
|
transaction?: Transaction,
|
|
23
|
+
gas?: BigNumber,
|
|
24
|
+
gasPrice?: BigNumber,
|
|
25
25
|
): BigNumber => {
|
|
26
26
|
const tokenAccount = findSubAccountById(account, transaction?.subAccountId ?? "");
|
|
27
27
|
const fromTokenAccount = tokenAccount && isTokenAccount(tokenAccount);
|
|
28
28
|
|
|
29
|
+
gas = gas ?? BigNumber(DEFAULT_GAS);
|
|
30
|
+
gasPrice = gasPrice ?? BigNumber(DEFAULT_GAS_PRICE);
|
|
31
|
+
|
|
29
32
|
const totalGas = gas.multipliedBy(gasPrice);
|
|
30
33
|
|
|
31
34
|
return fromTokenAccount
|
|
@@ -59,10 +62,11 @@ export const txsToOps = (
|
|
|
59
62
|
info: { address: string },
|
|
60
63
|
id: string,
|
|
61
64
|
txs: (AptosTransaction | null)[],
|
|
62
|
-
): [Operation[], Operation[]] => {
|
|
65
|
+
): [Operation[], Operation[], Operation[]] => {
|
|
63
66
|
const { address } = info;
|
|
64
67
|
const ops: Operation[] = [];
|
|
65
68
|
const opsTokens: Operation[] = [];
|
|
69
|
+
const opsStaking: Operation[] = [];
|
|
66
70
|
|
|
67
71
|
txs.forEach(tx => {
|
|
68
72
|
if (tx !== null) {
|
|
@@ -79,9 +83,14 @@ export const txsToOps = (
|
|
|
79
83
|
return; // skip transaction without functions in payload
|
|
80
84
|
}
|
|
81
85
|
|
|
82
|
-
const { coin_id, amount_in, amount_out } = getCoinAndAmounts(tx, address);
|
|
86
|
+
const { coin_id, amount_in, amount_out, type } = getCoinAndAmounts(tx, address);
|
|
83
87
|
op.value = calculateAmount(tx.sender, address, amount_in, amount_out);
|
|
84
|
-
op.type =
|
|
88
|
+
op.type =
|
|
89
|
+
type !== OP_TYPE.UNKNOWN
|
|
90
|
+
? type
|
|
91
|
+
: compareAddress(tx.sender, address)
|
|
92
|
+
? OP_TYPE.OUT
|
|
93
|
+
: OP_TYPE.IN;
|
|
85
94
|
op.senders.push(tx.sender);
|
|
86
95
|
op.hasFailed = !tx.success;
|
|
87
96
|
op.id = encodeOperationId(op.accountId, tx.hash, op.type);
|
|
@@ -90,10 +99,17 @@ export const txsToOps = (
|
|
|
90
99
|
|
|
91
100
|
if (op.value.isZero()) {
|
|
92
101
|
// skip transaction that result no Aptos change
|
|
93
|
-
op.type =
|
|
102
|
+
op.type = OP_TYPE.UNKNOWN;
|
|
94
103
|
}
|
|
95
104
|
|
|
96
|
-
if (
|
|
105
|
+
if (
|
|
106
|
+
op.type === OP_TYPE.STAKE ||
|
|
107
|
+
op.type === OP_TYPE.UNSTAKE ||
|
|
108
|
+
op.type === OP_TYPE.WITHDRAW
|
|
109
|
+
) {
|
|
110
|
+
ops.push(op);
|
|
111
|
+
opsStaking.push(op);
|
|
112
|
+
} else if (op.type !== OP_TYPE.UNKNOWN && coin_id !== null) {
|
|
97
113
|
if (coin_id === APTOS_ASSET_ID) {
|
|
98
114
|
ops.push(op);
|
|
99
115
|
} else {
|
|
@@ -102,7 +118,7 @@ export const txsToOps = (
|
|
|
102
118
|
op.accountId = encodeTokenAccountId(id, token);
|
|
103
119
|
opsTokens.push(op);
|
|
104
120
|
|
|
105
|
-
if (op.type ===
|
|
121
|
+
if (op.type === OP_TYPE.OUT) {
|
|
106
122
|
ops.push({
|
|
107
123
|
...op,
|
|
108
124
|
accountId: decodeTokenAccountId(op.accountId).accountId,
|
|
@@ -116,7 +132,7 @@ export const txsToOps = (
|
|
|
116
132
|
}
|
|
117
133
|
});
|
|
118
134
|
|
|
119
|
-
return [ops, opsTokens];
|
|
135
|
+
return [ops, opsTokens, opsStaking];
|
|
120
136
|
};
|
|
121
137
|
|
|
122
138
|
export function getTokenAccount(
|
|
@@ -1,23 +1,69 @@
|
|
|
1
|
-
import type { Account } from "@ledgerhq/types-live";
|
|
2
1
|
import BigNumber from "bignumber.js";
|
|
3
2
|
|
|
4
3
|
import { AptosAPI } from "../network";
|
|
5
4
|
import { getEstimatedGas } from "./getFeesForTransaction";
|
|
6
|
-
import type { Transaction } from "../types";
|
|
5
|
+
import type { AptosAccount, Transaction } from "../types";
|
|
7
6
|
import { getMaxSendBalance } from "./logic";
|
|
8
|
-
import {
|
|
7
|
+
import {
|
|
8
|
+
APTOS_DELEGATION_RESERVE_IN_OCTAS,
|
|
9
|
+
APTOS_MINIMUM_RESTAKE_IN_OCTAS,
|
|
10
|
+
MIN_COINS_ON_SHARES_POOL_IN_OCTAS,
|
|
11
|
+
} from "./../constants";
|
|
12
|
+
import { getDelegationOpMaxAmount, getStakingPosition } from "../logic/staking";
|
|
13
|
+
|
|
14
|
+
const checkSendConditions = (transaction: Transaction, account: AptosAccount) =>
|
|
15
|
+
transaction.mode === "send" && transaction.amount.gt(account.spendableBalance);
|
|
16
|
+
|
|
17
|
+
const checkStakeConditions = (transaction: Transaction, account: AptosAccount) => {
|
|
18
|
+
const txAmount = transaction.useAllAmount ? account.spendableBalance : transaction.amount;
|
|
19
|
+
const stakingPosition = account.aptosResources?.stakingPositions
|
|
20
|
+
?.find(stakingPosition => stakingPosition.validatorId === transaction.recipient)
|
|
21
|
+
?.active.gt(MIN_COINS_ON_SHARES_POOL_IN_OCTAS);
|
|
22
|
+
const minimumToStake = stakingPosition
|
|
23
|
+
? APTOS_MINIMUM_RESTAKE_IN_OCTAS
|
|
24
|
+
: MIN_COINS_ON_SHARES_POOL_IN_OCTAS;
|
|
25
|
+
|
|
26
|
+
return (
|
|
27
|
+
transaction.mode === "stake" &&
|
|
28
|
+
(txAmount.gt(account.spendableBalance) || txAmount.lt(minimumToStake))
|
|
29
|
+
);
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
const checkRestakeConditions = (transaction: Transaction, account: AptosAccount) => {
|
|
33
|
+
const stakingPosition = getStakingPosition(account, transaction.recipient)?.pendingInactive || 0;
|
|
34
|
+
|
|
35
|
+
return transaction.mode === "restake" && transaction.amount.gt(stakingPosition);
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
const checkUnstakeConditions = (transaction: Transaction, account: AptosAccount) => {
|
|
39
|
+
const stakingPosition = getStakingPosition(account, transaction.recipient)?.active || 0;
|
|
40
|
+
|
|
41
|
+
return transaction.mode === "unstake" && transaction.amount.gt(stakingPosition);
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
const checkWithdrawConditions = (transaction: Transaction, account: AptosAccount) => {
|
|
45
|
+
const stakingPosition = getStakingPosition(account, transaction.recipient)?.inactive || 0;
|
|
46
|
+
|
|
47
|
+
return transaction.mode === "withdraw" && transaction.amount.gt(stakingPosition);
|
|
48
|
+
};
|
|
9
49
|
|
|
10
50
|
const prepareTransaction = async (
|
|
11
|
-
account:
|
|
51
|
+
account: AptosAccount,
|
|
12
52
|
transaction: Transaction,
|
|
13
53
|
): Promise<Transaction> => {
|
|
14
|
-
if (
|
|
54
|
+
if (
|
|
55
|
+
!transaction.recipient ||
|
|
56
|
+
checkSendConditions(transaction, account) ||
|
|
57
|
+
checkStakeConditions(transaction, account) ||
|
|
58
|
+
checkRestakeConditions(transaction, account) ||
|
|
59
|
+
checkUnstakeConditions(transaction, account) ||
|
|
60
|
+
checkWithdrawConditions(transaction, account)
|
|
61
|
+
)
|
|
15
62
|
return transaction;
|
|
16
|
-
}
|
|
17
63
|
|
|
18
64
|
// if transaction.useAllAmount is true, then we expect transaction.amount to be 0
|
|
19
65
|
// so to check that actual amount is zero or not, we also need to check if useAllAmount is false
|
|
20
|
-
if (transaction.
|
|
66
|
+
if (!transaction.useAllAmount && transaction.amount.isZero()) {
|
|
21
67
|
return {
|
|
22
68
|
...transaction,
|
|
23
69
|
fees: BigNumber(0),
|
|
@@ -27,27 +73,29 @@ const prepareTransaction = async (
|
|
|
27
73
|
const aptosClient = new AptosAPI(account.currency.id);
|
|
28
74
|
|
|
29
75
|
if (transaction.useAllAmount) {
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
transaction
|
|
36
|
-
)
|
|
76
|
+
if (transaction.mode === "send") {
|
|
77
|
+
transaction.amount = getMaxSendBalance(account, transaction);
|
|
78
|
+
} else if (
|
|
79
|
+
transaction.mode === "restake" ||
|
|
80
|
+
transaction.mode === "unstake" ||
|
|
81
|
+
transaction.mode === "withdraw"
|
|
82
|
+
) {
|
|
83
|
+
// Reserve a certain amount to cover future network fees to deactivate and withdraw
|
|
84
|
+
transaction.amount = getDelegationOpMaxAmount(
|
|
85
|
+
account,
|
|
86
|
+
transaction.recipient,
|
|
87
|
+
transaction.mode,
|
|
88
|
+
);
|
|
89
|
+
} else if (transaction.mode === "stake") {
|
|
90
|
+
// Reserve a certain amount to cover future network fees to deactivate and withdraw
|
|
91
|
+
transaction.amount = getMaxSendBalance(account, transaction).minus(
|
|
92
|
+
APTOS_DELEGATION_RESERVE_IN_OCTAS,
|
|
93
|
+
);
|
|
94
|
+
}
|
|
37
95
|
}
|
|
38
96
|
|
|
39
97
|
const { fees, estimate, errors } = await getEstimatedGas(account, transaction, aptosClient);
|
|
40
98
|
|
|
41
|
-
if (transaction.useAllAmount) {
|
|
42
|
-
// correct the transaction amount according to estimated fees
|
|
43
|
-
transaction.amount = getMaxSendBalance(
|
|
44
|
-
BigNumber(estimate.maxGasAmount),
|
|
45
|
-
BigNumber(estimate.gasUnitPrice),
|
|
46
|
-
account,
|
|
47
|
-
transaction,
|
|
48
|
-
);
|
|
49
|
-
}
|
|
50
|
-
|
|
51
99
|
transaction.fees = fees;
|
|
52
100
|
transaction.options = estimate;
|
|
53
101
|
transaction.errors = errors;
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import BigNumber from "bignumber.js";
|
|
2
|
+
import { AptosAccount, AptosAccountRaw, AptosResources, AptosResourcesRaw } from "../types";
|
|
3
|
+
import { Account, AccountRaw } from "@ledgerhq/types-live";
|
|
4
|
+
|
|
5
|
+
export function toAptosResourcesRaw(r: AptosResources): AptosResourcesRaw {
|
|
6
|
+
const { activeBalance, pendingInactiveBalance, inactiveBalance, stakingPositions } = r;
|
|
7
|
+
return {
|
|
8
|
+
activeBalance: activeBalance.toString(),
|
|
9
|
+
pendingInactiveBalance: pendingInactiveBalance.toString(),
|
|
10
|
+
inactiveBalance: inactiveBalance.toString(),
|
|
11
|
+
stakingPositions: stakingPositions.map(
|
|
12
|
+
({ active, validatorId, inactive, pendingInactive }) => ({
|
|
13
|
+
active: active.toString(),
|
|
14
|
+
pendingInactive: pendingInactive.toString(),
|
|
15
|
+
inactive: inactive.toString(),
|
|
16
|
+
validatorId,
|
|
17
|
+
}),
|
|
18
|
+
),
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function fromAptosResourcesRaw(r: AptosResourcesRaw): AptosResources {
|
|
23
|
+
const { activeBalance, pendingInactiveBalance, inactiveBalance, stakingPositions = [] } = r;
|
|
24
|
+
return {
|
|
25
|
+
activeBalance: new BigNumber(activeBalance),
|
|
26
|
+
pendingInactiveBalance: new BigNumber(pendingInactiveBalance),
|
|
27
|
+
inactiveBalance: new BigNumber(inactiveBalance),
|
|
28
|
+
stakingPositions: stakingPositions.map(
|
|
29
|
+
({ active, pendingInactive, inactive, validatorId }) => ({
|
|
30
|
+
active: new BigNumber(active),
|
|
31
|
+
pendingInactive: new BigNumber(pendingInactive),
|
|
32
|
+
inactive: new BigNumber(inactive),
|
|
33
|
+
validatorId,
|
|
34
|
+
}),
|
|
35
|
+
),
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export function assignToAccountRaw(account: Account, accountRaw: AccountRaw) {
|
|
40
|
+
const aptosAccount = account as AptosAccount;
|
|
41
|
+
if (aptosAccount.aptosResources) {
|
|
42
|
+
(accountRaw as AptosAccountRaw).aptosResources = toAptosResourcesRaw(
|
|
43
|
+
aptosAccount.aptosResources,
|
|
44
|
+
);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export function assignFromAccountRaw(accountRaw: AccountRaw, account: Account) {
|
|
49
|
+
const aptosResourcesRaw = (accountRaw as AptosAccountRaw).aptosResources;
|
|
50
|
+
if (aptosResourcesRaw)
|
|
51
|
+
(account as AptosAccount).aptosResources = fromAptosResourcesRaw(aptosResourcesRaw);
|
|
52
|
+
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { AptosAccount, Transaction } from "../types";
|
|
1
|
+
import type { AptosAccount, AptosOperation, Transaction } from "../types";
|
|
2
2
|
import { Observable } from "rxjs";
|
|
3
3
|
import { encodeOperationId } from "@ledgerhq/coin-framework/operation";
|
|
4
4
|
import BigNumber from "bignumber.js";
|
|
@@ -16,6 +16,21 @@ export const getAddress = (a: Account) => ({
|
|
|
16
16
|
derivationPath: a.freshAddressPath,
|
|
17
17
|
});
|
|
18
18
|
|
|
19
|
+
const getOperationType = (transaction: Transaction): OperationType => {
|
|
20
|
+
switch (transaction.mode) {
|
|
21
|
+
case "stake":
|
|
22
|
+
return "STAKE";
|
|
23
|
+
case "restake":
|
|
24
|
+
return "STAKE";
|
|
25
|
+
case "unstake":
|
|
26
|
+
return "UNSTAKE";
|
|
27
|
+
case "withdraw":
|
|
28
|
+
return "WITHDRAW";
|
|
29
|
+
default:
|
|
30
|
+
return "OUT";
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
|
|
19
34
|
const buildSignOperation =
|
|
20
35
|
(
|
|
21
36
|
signerContext: SignerContext<AptosSigner>,
|
|
@@ -35,22 +50,20 @@ const buildSignOperation =
|
|
|
35
50
|
|
|
36
51
|
const accountId = account.id;
|
|
37
52
|
const hash = "";
|
|
38
|
-
const type: OperationType =
|
|
53
|
+
const type: OperationType = getOperationType(transaction);
|
|
39
54
|
const fee = transaction.fees || new BigNumber(0);
|
|
40
55
|
const extra = {};
|
|
41
56
|
const senders: string[] = [];
|
|
42
57
|
const recipients: string[] = [];
|
|
43
58
|
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
recipients.push(transaction.recipient);
|
|
47
|
-
}
|
|
59
|
+
senders.push(account.freshAddress);
|
|
60
|
+
recipients.push(transaction.recipient);
|
|
48
61
|
|
|
49
62
|
const subAccount =
|
|
50
63
|
!!transaction.subAccountId && findSubAccountById(account, transaction.subAccountId);
|
|
51
64
|
|
|
52
65
|
// build optimistic operation
|
|
53
|
-
const operation:
|
|
66
|
+
const operation: AptosOperation = {
|
|
54
67
|
id: encodeOperationId(accountId, hash, type),
|
|
55
68
|
hash,
|
|
56
69
|
type,
|