@ledgerhq/coin-tron 0.0.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/.eslintrc.js +20 -0
- package/.turbo/turbo-build.log +4 -0
- package/.unimportedrc.json +25 -0
- package/LICENSE.txt +21 -0
- package/jest.config.js +8 -0
- package/jest.integ.config.js +7 -0
- package/lib/bridge/broadcast.d.ts +7 -0
- package/lib/bridge/broadcast.d.ts.map +1 -0
- package/lib/bridge/broadcast.js +26 -0
- package/lib/bridge/broadcast.js.map +1 -0
- package/lib/bridge/buildOptimisticOperation.d.ts +5 -0
- package/lib/bridge/buildOptimisticOperation.d.ts.map +1 -0
- package/lib/bridge/buildOptimisticOperation.js +101 -0
- package/lib/bridge/buildOptimisticOperation.js.map +1 -0
- package/lib/bridge/createTransaction.d.ts +4 -0
- package/lib/bridge/createTransaction.d.ts.map +1 -0
- package/lib/bridge/createTransaction.js +19 -0
- package/lib/bridge/createTransaction.js.map +1 -0
- package/lib/bridge/deviceTransactionConfig.d.ts +20 -0
- package/lib/bridge/deviceTransactionConfig.d.ts.map +1 -0
- package/lib/bridge/deviceTransactionConfig.js +65 -0
- package/lib/bridge/deviceTransactionConfig.js.map +1 -0
- package/lib/bridge/estimateMaxSpendable.d.ts +5 -0
- package/lib/bridge/estimateMaxSpendable.d.ts.map +1 -0
- package/lib/bridge/estimateMaxSpendable.js +30 -0
- package/lib/bridge/estimateMaxSpendable.js.map +1 -0
- package/lib/bridge/getEstimateFees.d.ts +6 -0
- package/lib/bridge/getEstimateFees.d.ts.map +1 -0
- package/lib/bridge/getEstimateFees.js +52 -0
- package/lib/bridge/getEstimateFees.js.map +1 -0
- package/lib/bridge/getTransactionStatus.d.ts +4 -0
- package/lib/bridge/getTransactionStatus.d.ts.map +1 -0
- package/lib/bridge/getTransactionStatus.js +218 -0
- package/lib/bridge/getTransactionStatus.js.map +1 -0
- package/lib/bridge/index.d.ts +10 -0
- package/lib/bridge/index.d.ts.map +1 -0
- package/lib/bridge/index.js +60 -0
- package/lib/bridge/index.js.map +1 -0
- package/lib/bridge/preload.d.ts +8 -0
- package/lib/bridge/preload.d.ts.map +1 -0
- package/lib/bridge/preload.js +32 -0
- package/lib/bridge/preload.js.map +1 -0
- package/lib/bridge/prepareTransaction.d.ts +4 -0
- package/lib/bridge/prepareTransaction.d.ts.map +1 -0
- package/lib/bridge/prepareTransaction.js +19 -0
- package/lib/bridge/prepareTransaction.js.map +1 -0
- package/lib/bridge/serialization.d.ts +9 -0
- package/lib/bridge/serialization.d.ts.map +1 -0
- package/lib/bridge/serialization.js +238 -0
- package/lib/bridge/serialization.js.map +1 -0
- package/lib/bridge/signOperation.d.ts +5 -0
- package/lib/bridge/signOperation.d.ts.map +1 -0
- package/lib/bridge/signOperation.js +92 -0
- package/lib/bridge/signOperation.js.map +1 -0
- package/lib/bridge/synchronization.d.ts +5 -0
- package/lib/bridge/synchronization.d.ts.map +1 -0
- package/lib/bridge/synchronization.integ.test.d.ts +3 -0
- package/lib/bridge/synchronization.integ.test.d.ts.map +1 -0
- package/lib/bridge/synchronization.integ.test.js +96 -0
- package/lib/bridge/synchronization.integ.test.js.map +1 -0
- package/lib/bridge/synchronization.js +216 -0
- package/lib/bridge/synchronization.js.map +1 -0
- package/lib/bridge/transaction.d.ts +15 -0
- package/lib/bridge/transaction.d.ts.map +1 -0
- package/lib/bridge/transaction.js +61 -0
- package/lib/bridge/transaction.js.map +1 -0
- package/lib/config.d.ts +10 -0
- package/lib/config.d.ts.map +1 -0
- package/lib/config.js +17 -0
- package/lib/config.js.map +1 -0
- package/lib/index.d.ts +3 -0
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +21 -0
- package/lib/index.js.map +1 -0
- package/lib/logic/constants.d.ts +4 -0
- package/lib/logic/constants.d.ts.map +1 -0
- package/lib/logic/constants.js +7 -0
- package/lib/logic/constants.js.map +1 -0
- package/lib/logic/pagination.d.ts +3 -0
- package/lib/logic/pagination.d.ts.map +1 -0
- package/lib/logic/pagination.js +19 -0
- package/lib/logic/pagination.js.map +1 -0
- package/lib/logic/utils.d.ts +12 -0
- package/lib/logic/utils.d.ts.map +1 -0
- package/lib/logic/utils.js +177 -0
- package/lib/logic/utils.js.map +1 -0
- package/lib/network/format.d.ts +9 -0
- package/lib/network/format.d.ts.map +1 -0
- package/lib/network/format.js +127 -0
- package/lib/network/format.js.map +1 -0
- package/lib/network/index.d.ts +30 -0
- package/lib/network/index.d.ts.map +1 -0
- package/lib/network/index.integ.test.d.ts +2 -0
- package/lib/network/index.integ.test.d.ts.map +1 -0
- package/lib/network/index.integ.test.js +38 -0
- package/lib/network/index.integ.test.js.map +1 -0
- package/lib/network/index.js +555 -0
- package/lib/network/index.js.map +1 -0
- package/lib/network/index.test.d.ts +2 -0
- package/lib/network/index.test.d.ts.map +1 -0
- package/lib/network/index.test.js +66 -0
- package/lib/network/index.test.js.map +1 -0
- package/lib/network/superRepresentativesData.mock.d.ts +69 -0
- package/lib/network/superRepresentativesData.mock.d.ts.map +1 -0
- package/lib/network/superRepresentativesData.mock.js +2208 -0
- package/lib/network/superRepresentativesData.mock.js.map +1 -0
- package/lib/network/types.d.ts +122 -0
- package/lib/network/types.d.ts.map +1 -0
- package/lib/network/types.fixture.d.ts +498 -0
- package/lib/network/types.fixture.d.ts.map +1 -0
- package/lib/network/types.fixture.js +4375 -0
- package/lib/network/types.fixture.js.map +1 -0
- package/lib/network/types.js +13 -0
- package/lib/network/types.js.map +1 -0
- package/lib/signer/getAddress.d.ts +6 -0
- package/lib/signer/getAddress.d.ts.map +1 -0
- package/lib/signer/getAddress.js +19 -0
- package/lib/signer/getAddress.js.map +1 -0
- package/lib/signer/index.d.ts +6 -0
- package/lib/signer/index.d.ts.map +1 -0
- package/lib/signer/index.js +11 -0
- package/lib/signer/index.js.map +1 -0
- package/lib/test/bot-deviceActions.d.ts +4 -0
- package/lib/test/bot-deviceActions.d.ts.map +1 -0
- package/lib/test/bot-deviceActions.js +103 -0
- package/lib/test/bot-deviceActions.js.map +1 -0
- package/lib/test/bot-specs.d.ts +7 -0
- package/lib/test/bot-specs.d.ts.map +1 -0
- package/lib/test/bot-specs.js +433 -0
- package/lib/test/bot-specs.js.map +1 -0
- package/lib/test/bridgeDatasetTest.d.ts +4 -0
- package/lib/test/bridgeDatasetTest.d.ts.map +1 -0
- package/lib/test/bridgeDatasetTest.js +1284 -0
- package/lib/test/bridgeDatasetTest.js.map +1 -0
- package/lib/test/cli.d.ts +52 -0
- package/lib/test/cli.d.ts.map +1 -0
- package/lib/test/cli.js +155 -0
- package/lib/test/cli.js.map +1 -0
- package/lib/test/index.d.ts +6 -0
- package/lib/test/index.d.ts.map +1 -0
- package/lib/test/index.js +26 -0
- package/lib/test/index.js.map +1 -0
- package/lib/types/bridge.d.ts +245 -0
- package/lib/types/bridge.d.ts.map +1 -0
- package/lib/types/bridge.js +16 -0
- package/lib/types/bridge.js.map +1 -0
- package/lib/types/errors.d.ts +49 -0
- package/lib/types/errors.d.ts.map +1 -0
- package/lib/types/errors.js +21 -0
- package/lib/types/errors.js.map +1 -0
- package/lib/types/index.d.ts +4 -0
- package/lib/types/index.d.ts.map +1 -0
- package/lib/types/index.js +21 -0
- package/lib/types/index.js.map +1 -0
- package/lib/types/signer.d.ts +10 -0
- package/lib/types/signer.d.ts.map +1 -0
- package/lib/types/signer.js +3 -0
- package/lib/types/signer.js.map +1 -0
- package/lib-es/bridge/broadcast.d.ts +7 -0
- package/lib-es/bridge/broadcast.d.ts.map +1 -0
- package/lib-es/bridge/broadcast.js +24 -0
- package/lib-es/bridge/broadcast.js.map +1 -0
- package/lib-es/bridge/buildOptimisticOperation.d.ts +5 -0
- package/lib-es/bridge/buildOptimisticOperation.d.ts.map +1 -0
- package/lib-es/bridge/buildOptimisticOperation.js +94 -0
- package/lib-es/bridge/buildOptimisticOperation.js.map +1 -0
- package/lib-es/bridge/createTransaction.d.ts +4 -0
- package/lib-es/bridge/createTransaction.d.ts.map +1 -0
- package/lib-es/bridge/createTransaction.js +14 -0
- package/lib-es/bridge/createTransaction.js.map +1 -0
- package/lib-es/bridge/deviceTransactionConfig.d.ts +20 -0
- package/lib-es/bridge/deviceTransactionConfig.d.ts.map +1 -0
- package/lib-es/bridge/deviceTransactionConfig.js +63 -0
- package/lib-es/bridge/deviceTransactionConfig.js.map +1 -0
- package/lib-es/bridge/estimateMaxSpendable.d.ts +5 -0
- package/lib-es/bridge/estimateMaxSpendable.d.ts.map +1 -0
- package/lib-es/bridge/estimateMaxSpendable.js +25 -0
- package/lib-es/bridge/estimateMaxSpendable.js.map +1 -0
- package/lib-es/bridge/getEstimateFees.d.ts +6 -0
- package/lib-es/bridge/getEstimateFees.d.ts.map +1 -0
- package/lib-es/bridge/getEstimateFees.js +47 -0
- package/lib-es/bridge/getEstimateFees.js.map +1 -0
- package/lib-es/bridge/getTransactionStatus.d.ts +4 -0
- package/lib-es/bridge/getTransactionStatus.d.ts.map +1 -0
- package/lib-es/bridge/getTransactionStatus.js +213 -0
- package/lib-es/bridge/getTransactionStatus.js.map +1 -0
- package/lib-es/bridge/index.d.ts +10 -0
- package/lib-es/bridge/index.d.ts.map +1 -0
- package/lib-es/bridge/index.js +53 -0
- package/lib-es/bridge/index.js.map +1 -0
- package/lib-es/bridge/preload.d.ts +8 -0
- package/lib-es/bridge/preload.d.ts.map +1 -0
- package/lib-es/bridge/preload.js +27 -0
- package/lib-es/bridge/preload.js.map +1 -0
- package/lib-es/bridge/prepareTransaction.d.ts +4 -0
- package/lib-es/bridge/prepareTransaction.d.ts.map +1 -0
- package/lib-es/bridge/prepareTransaction.js +15 -0
- package/lib-es/bridge/prepareTransaction.js.map +1 -0
- package/lib-es/bridge/serialization.d.ts +9 -0
- package/lib-es/bridge/serialization.d.ts.map +1 -0
- package/lib-es/bridge/serialization.js +229 -0
- package/lib-es/bridge/serialization.js.map +1 -0
- package/lib-es/bridge/signOperation.d.ts +5 -0
- package/lib-es/bridge/signOperation.d.ts.map +1 -0
- package/lib-es/bridge/signOperation.js +85 -0
- package/lib-es/bridge/signOperation.js.map +1 -0
- package/lib-es/bridge/synchronization.d.ts +5 -0
- package/lib-es/bridge/synchronization.d.ts.map +1 -0
- package/lib-es/bridge/synchronization.integ.test.d.ts +3 -0
- package/lib-es/bridge/synchronization.integ.test.d.ts.map +1 -0
- package/lib-es/bridge/synchronization.integ.test.js +89 -0
- package/lib-es/bridge/synchronization.integ.test.js.map +1 -0
- package/lib-es/bridge/synchronization.js +209 -0
- package/lib-es/bridge/synchronization.js.map +1 -0
- package/lib-es/bridge/transaction.d.ts +15 -0
- package/lib-es/bridge/transaction.d.ts.map +1 -0
- package/lib-es/bridge/transaction.js +55 -0
- package/lib-es/bridge/transaction.js.map +1 -0
- package/lib-es/config.d.ts +10 -0
- package/lib-es/config.d.ts.map +1 -0
- package/lib-es/config.js +12 -0
- package/lib-es/config.js.map +1 -0
- package/lib-es/index.d.ts +3 -0
- package/lib-es/index.d.ts.map +1 -0
- package/lib-es/index.js +3 -0
- package/lib-es/index.js.map +1 -0
- package/lib-es/logic/constants.d.ts +4 -0
- package/lib-es/logic/constants.d.ts.map +1 -0
- package/lib-es/logic/constants.js +4 -0
- package/lib-es/logic/constants.js.map +1 -0
- package/lib-es/logic/pagination.d.ts +3 -0
- package/lib-es/logic/pagination.d.ts.map +1 -0
- package/lib-es/logic/pagination.js +15 -0
- package/lib-es/logic/pagination.js.map +1 -0
- package/lib-es/logic/utils.d.ts +12 -0
- package/lib-es/logic/utils.d.ts.map +1 -0
- package/lib-es/logic/utils.js +167 -0
- package/lib-es/logic/utils.js.map +1 -0
- package/lib-es/network/format.d.ts +9 -0
- package/lib-es/network/format.d.ts.map +1 -0
- package/lib-es/network/format.js +117 -0
- package/lib-es/network/format.js.map +1 -0
- package/lib-es/network/index.d.ts +30 -0
- package/lib-es/network/index.d.ts.map +1 -0
- package/lib-es/network/index.integ.test.d.ts +2 -0
- package/lib-es/network/index.integ.test.d.ts.map +1 -0
- package/lib-es/network/index.integ.test.js +36 -0
- package/lib-es/network/index.integ.test.js.map +1 -0
- package/lib-es/network/index.js +524 -0
- package/lib-es/network/index.js.map +1 -0
- package/lib-es/network/index.test.d.ts +2 -0
- package/lib-es/network/index.test.d.ts.map +1 -0
- package/lib-es/network/index.test.js +63 -0
- package/lib-es/network/index.test.js.map +1 -0
- package/lib-es/network/superRepresentativesData.mock.d.ts +69 -0
- package/lib-es/network/superRepresentativesData.mock.d.ts.map +1 -0
- package/lib-es/network/superRepresentativesData.mock.js +2206 -0
- package/lib-es/network/superRepresentativesData.mock.js.map +1 -0
- package/lib-es/network/types.d.ts +122 -0
- package/lib-es/network/types.d.ts.map +1 -0
- package/lib-es/network/types.fixture.d.ts +498 -0
- package/lib-es/network/types.fixture.d.ts.map +1 -0
- package/lib-es/network/types.fixture.js +4371 -0
- package/lib-es/network/types.fixture.js.map +1 -0
- package/lib-es/network/types.js +8 -0
- package/lib-es/network/types.js.map +1 -0
- package/lib-es/signer/getAddress.d.ts +6 -0
- package/lib-es/signer/getAddress.d.ts.map +1 -0
- package/lib-es/signer/getAddress.js +17 -0
- package/lib-es/signer/getAddress.js.map +1 -0
- package/lib-es/signer/index.d.ts +6 -0
- package/lib-es/signer/index.d.ts.map +1 -0
- package/lib-es/signer/index.js +6 -0
- package/lib-es/signer/index.js.map +1 -0
- package/lib-es/test/bot-deviceActions.d.ts +4 -0
- package/lib-es/test/bot-deviceActions.d.ts.map +1 -0
- package/lib-es/test/bot-deviceActions.js +100 -0
- package/lib-es/test/bot-deviceActions.js.map +1 -0
- package/lib-es/test/bot-specs.d.ts +7 -0
- package/lib-es/test/bot-specs.d.ts.map +1 -0
- package/lib-es/test/bot-specs.js +428 -0
- package/lib-es/test/bot-specs.js.map +1 -0
- package/lib-es/test/bridgeDatasetTest.d.ts +4 -0
- package/lib-es/test/bridgeDatasetTest.d.ts.map +1 -0
- package/lib-es/test/bridgeDatasetTest.js +1278 -0
- package/lib-es/test/bridgeDatasetTest.js.map +1 -0
- package/lib-es/test/cli.d.ts +52 -0
- package/lib-es/test/cli.d.ts.map +1 -0
- package/lib-es/test/cli.js +149 -0
- package/lib-es/test/cli.js.map +1 -0
- package/lib-es/test/index.d.ts +6 -0
- package/lib-es/test/index.d.ts.map +1 -0
- package/lib-es/test/index.js +6 -0
- package/lib-es/test/index.js.map +1 -0
- package/lib-es/types/bridge.d.ts +245 -0
- package/lib-es/types/bridge.d.ts.map +1 -0
- package/lib-es/types/bridge.js +11 -0
- package/lib-es/types/bridge.js.map +1 -0
- package/lib-es/types/errors.d.ts +49 -0
- package/lib-es/types/errors.d.ts.map +1 -0
- package/lib-es/types/errors.js +18 -0
- package/lib-es/types/errors.js.map +1 -0
- package/lib-es/types/index.d.ts +4 -0
- package/lib-es/types/index.d.ts.map +1 -0
- package/lib-es/types/index.js +5 -0
- package/lib-es/types/index.js.map +1 -0
- package/lib-es/types/signer.d.ts +10 -0
- package/lib-es/types/signer.d.ts.map +1 -0
- package/lib-es/types/signer.js +2 -0
- package/lib-es/types/signer.js.map +1 -0
- package/package.json +133 -0
- package/src/bridge/broadcast.ts +24 -0
- package/src/bridge/buildOptimisticOperation.ts +133 -0
- package/src/bridge/createTransaction.ts +16 -0
- package/src/bridge/deviceTransactionConfig.ts +99 -0
- package/src/bridge/estimateMaxSpendable.ts +32 -0
- package/src/bridge/getEstimateFees.ts +52 -0
- package/src/bridge/getTransactionStatus.ts +263 -0
- package/src/bridge/index.ts +78 -0
- package/src/bridge/preload.ts +24 -0
- package/src/bridge/prepareTransaction.ts +12 -0
- package/src/bridge/serialization.ts +288 -0
- package/src/bridge/signOperation.ts +123 -0
- package/src/bridge/synchronization.integ.test.ts +105 -0
- package/src/bridge/synchronization.ts +284 -0
- package/src/bridge/transaction.ts +84 -0
- package/src/config.ts +24 -0
- package/src/index.ts +3 -0
- package/src/logic/constants.ts +4 -0
- package/src/logic/pagination.ts +21 -0
- package/src/logic/utils.ts +231 -0
- package/src/network/format.ts +148 -0
- package/src/network/index.integ.test.ts +33 -0
- package/src/network/index.test.ts +61 -0
- package/src/network/index.ts +747 -0
- package/src/network/superRepresentativesData.mock.ts +2205 -0
- package/src/network/types.fixture.ts +4476 -0
- package/src/network/types.ts +147 -0
- package/src/signer/getAddress.ts +13 -0
- package/src/signer/index.ts +7 -0
- package/src/test/bot-deviceActions.ts +121 -0
- package/src/test/bot-specs.ts +457 -0
- package/src/test/bridgeDatasetTest.ts +1343 -0
- package/src/test/cli.ts +219 -0
- package/src/test/index.ts +6 -0
- package/src/types/bridge.ts +304 -0
- package/src/types/errors.ts +22 -0
- package/src/types/index.ts +4 -0
- package/src/types/signer.ts +9 -0
- package/tsconfig.json +14 -0
- package/types/tronweb/index.d.ts +5 -0
|
@@ -0,0 +1,263 @@
|
|
|
1
|
+
import { getAccountCurrency } from "@ledgerhq/coin-framework/account";
|
|
2
|
+
import {
|
|
3
|
+
AmountRequired,
|
|
4
|
+
InvalidAddress,
|
|
5
|
+
InvalidAddressBecauseDestinationIsAlsoSource,
|
|
6
|
+
NotEnoughBalance,
|
|
7
|
+
RecipientRequired,
|
|
8
|
+
} from "@ledgerhq/errors";
|
|
9
|
+
import { formatCurrencyUnit } from "@ledgerhq/coin-framework/currencies";
|
|
10
|
+
import BigNumber from "bignumber.js";
|
|
11
|
+
import sumBy from "lodash/sumBy";
|
|
12
|
+
import { ONE_TRX } from "../logic/constants";
|
|
13
|
+
import {
|
|
14
|
+
fetchTronAccount,
|
|
15
|
+
fetchTronContract,
|
|
16
|
+
getContractUserEnergyRatioConsumption,
|
|
17
|
+
getDelegatedResource,
|
|
18
|
+
getTronSuperRepresentatives,
|
|
19
|
+
validateAddress,
|
|
20
|
+
} from "../network";
|
|
21
|
+
import { Transaction, TransactionStatus, TronAccount } from "../types";
|
|
22
|
+
import {
|
|
23
|
+
TronInvalidFreezeAmount,
|
|
24
|
+
TronInvalidUnDelegateResourceAmount,
|
|
25
|
+
TronInvalidVoteCount,
|
|
26
|
+
TronLegacyUnfreezeNotExpired,
|
|
27
|
+
TronNoFrozenForBandwidth,
|
|
28
|
+
TronNoFrozenForEnergy,
|
|
29
|
+
TronNoReward,
|
|
30
|
+
TronNotEnoughEnergy,
|
|
31
|
+
TronNotEnoughTronPower,
|
|
32
|
+
TronNoUnfrozenResource,
|
|
33
|
+
TronRewardNotAvailable,
|
|
34
|
+
TronSendTrc20ToNewAccountForbidden,
|
|
35
|
+
TronUnexpectedFees,
|
|
36
|
+
TronUnfreezeNotExpired,
|
|
37
|
+
TronVoteRequired,
|
|
38
|
+
} from "../types/errors";
|
|
39
|
+
import getEstimatedFees from "./getEstimateFees";
|
|
40
|
+
|
|
41
|
+
const getTransactionStatus = async (
|
|
42
|
+
acc: TronAccount,
|
|
43
|
+
transaction: Transaction,
|
|
44
|
+
): Promise<TransactionStatus> => {
|
|
45
|
+
const errors: Record<string, Error> = {};
|
|
46
|
+
const warnings: Record<string, Error> = {};
|
|
47
|
+
const { family, mode, recipient, resource, votes, useAllAmount = false } = transaction;
|
|
48
|
+
const tokenAccount = !transaction.subAccountId
|
|
49
|
+
? null
|
|
50
|
+
: acc.subAccounts && acc.subAccounts.find(ta => ta.id === transaction.subAccountId);
|
|
51
|
+
const account = tokenAccount || acc;
|
|
52
|
+
const isContractAddressRecipient = (await fetchTronContract(recipient)) !== undefined;
|
|
53
|
+
|
|
54
|
+
if (mode === "send" && !recipient) {
|
|
55
|
+
errors.recipient = new RecipientRequired();
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
if (["send", "unDelegateResource", "legacyUnfreeze"].includes(mode)) {
|
|
59
|
+
if (recipient === acc.freshAddress) {
|
|
60
|
+
errors.recipient = new InvalidAddressBecauseDestinationIsAlsoSource();
|
|
61
|
+
} else if (recipient && !(await validateAddress(recipient))) {
|
|
62
|
+
errors.recipient = new InvalidAddress(undefined, {
|
|
63
|
+
currencyName: acc.currency.name,
|
|
64
|
+
});
|
|
65
|
+
} else if (
|
|
66
|
+
recipient &&
|
|
67
|
+
mode === "send" &&
|
|
68
|
+
account.type === "TokenAccount" &&
|
|
69
|
+
account.token.tokenType === "trc20" &&
|
|
70
|
+
!isContractAddressRecipient && // send trc20 to a smart contract is allowed
|
|
71
|
+
(await fetchTronAccount(recipient)).length === 0
|
|
72
|
+
) {
|
|
73
|
+
// send trc20 to a new account is forbidden by us (because it will not activate the account)
|
|
74
|
+
errors.recipient = new TronSendTrc20ToNewAccountForbidden();
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
if (mode === "unfreeze") {
|
|
79
|
+
const { bandwidth, energy } = acc.tronResources.frozen;
|
|
80
|
+
if (resource === "BANDWIDTH" && transaction.amount.gt(bandwidth?.amount || new BigNumber(0))) {
|
|
81
|
+
errors.resource = new TronNoFrozenForBandwidth();
|
|
82
|
+
} else if (resource === "ENERGY" && transaction.amount.gt(energy?.amount || new BigNumber(0))) {
|
|
83
|
+
errors.resource = new TronNoFrozenForEnergy();
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
if (mode === "legacyUnfreeze") {
|
|
88
|
+
const now = new Date();
|
|
89
|
+
const expirationDate =
|
|
90
|
+
resource === "ENERGY"
|
|
91
|
+
? acc.tronResources.legacyFrozen.energy?.expiredAt
|
|
92
|
+
: acc.tronResources.legacyFrozen.bandwidth?.expiredAt;
|
|
93
|
+
|
|
94
|
+
if (!expirationDate) {
|
|
95
|
+
if (resource === "BANDWIDTH") {
|
|
96
|
+
errors.resource = new TronNoFrozenForBandwidth();
|
|
97
|
+
} else {
|
|
98
|
+
errors.resource = new TronNoFrozenForEnergy();
|
|
99
|
+
}
|
|
100
|
+
} else if (now.getTime() < expirationDate.getTime()) {
|
|
101
|
+
errors.resource = new TronLegacyUnfreezeNotExpired();
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
if (mode === "withdrawExpireUnfreeze") {
|
|
106
|
+
const now = new Date();
|
|
107
|
+
if (
|
|
108
|
+
(!acc.tronResources.unFrozen.bandwidth ||
|
|
109
|
+
acc.tronResources.unFrozen.bandwidth.length === 0) &&
|
|
110
|
+
(!acc.tronResources.unFrozen.energy || acc.tronResources.unFrozen.energy.length === 0)
|
|
111
|
+
) {
|
|
112
|
+
errors.resource = new TronNoUnfrozenResource();
|
|
113
|
+
} else {
|
|
114
|
+
const unfreezingResources = [
|
|
115
|
+
...(acc.tronResources.unFrozen.bandwidth ?? []),
|
|
116
|
+
...(acc.tronResources.unFrozen.energy ?? []),
|
|
117
|
+
];
|
|
118
|
+
|
|
119
|
+
const hasNoExpiredResource = !unfreezingResources.some(
|
|
120
|
+
unfrozen => unfrozen.expireTime.getTime() <= now.getTime(),
|
|
121
|
+
);
|
|
122
|
+
|
|
123
|
+
if (hasNoExpiredResource) {
|
|
124
|
+
const closestExpireTime = unfreezingResources.reduce((closest, current) => {
|
|
125
|
+
if (!closest) {
|
|
126
|
+
return current;
|
|
127
|
+
}
|
|
128
|
+
const closestTimeDifference = Math.abs(closest.expireTime.getTime() - now.getTime());
|
|
129
|
+
const currentTimeDifference = Math.abs(current.expireTime.getTime() - now.getTime());
|
|
130
|
+
|
|
131
|
+
return currentTimeDifference < closestTimeDifference ? current : closest;
|
|
132
|
+
});
|
|
133
|
+
errors.resource = new TronUnfreezeNotExpired(undefined, {
|
|
134
|
+
time: closestExpireTime.expireTime.toISOString(),
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
if (mode === "unDelegateResource" && resource && acc.tronResources) {
|
|
141
|
+
const delegatedResourceAmount = await getDelegatedResource(acc, transaction, resource);
|
|
142
|
+
if (delegatedResourceAmount.lt(transaction.amount)) {
|
|
143
|
+
errors.resource = new TronInvalidUnDelegateResourceAmount();
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
if (mode === "vote") {
|
|
148
|
+
if (votes.length === 0) {
|
|
149
|
+
errors.vote = new TronVoteRequired();
|
|
150
|
+
} else {
|
|
151
|
+
const superRepresentatives = await getTronSuperRepresentatives();
|
|
152
|
+
const isValidVoteCounts = votes.every(v => v.voteCount > 0);
|
|
153
|
+
const isValidAddresses = votes.every(v =>
|
|
154
|
+
superRepresentatives.some(s => s.address === v.address),
|
|
155
|
+
);
|
|
156
|
+
|
|
157
|
+
if (!isValidAddresses) {
|
|
158
|
+
errors.vote = new InvalidAddress("", {
|
|
159
|
+
currencyName: acc.currency.name,
|
|
160
|
+
});
|
|
161
|
+
} else if (!isValidVoteCounts) {
|
|
162
|
+
errors.vote = new TronInvalidVoteCount();
|
|
163
|
+
} else {
|
|
164
|
+
const totalVoteCount = sumBy(votes, "voteCount");
|
|
165
|
+
const tronPower = (acc.tronResources && acc.tronResources.tronPower) || 0;
|
|
166
|
+
|
|
167
|
+
if (totalVoteCount > tronPower) {
|
|
168
|
+
errors.vote = new TronNotEnoughTronPower();
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
if (mode === "claimReward") {
|
|
175
|
+
const lastRewardOp = account.operations.find(o => o.type === "REWARD");
|
|
176
|
+
const claimableRewardDate = lastRewardOp
|
|
177
|
+
? new Date(lastRewardOp.date.getTime() + 24 * 60 * 60 * 1000) // date + 24 hours
|
|
178
|
+
: new Date();
|
|
179
|
+
|
|
180
|
+
if (acc.tronResources && acc.tronResources.unwithdrawnReward.eq(0)) {
|
|
181
|
+
errors.reward = new TronNoReward();
|
|
182
|
+
} else if (lastRewardOp && claimableRewardDate.valueOf() > new Date().valueOf()) {
|
|
183
|
+
errors.reward = new TronRewardNotAvailable("Reward is not claimable", {
|
|
184
|
+
until: claimableRewardDate.toISOString(),
|
|
185
|
+
});
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
const estimatedFees =
|
|
190
|
+
Object.entries(errors).length > 0
|
|
191
|
+
? new BigNumber(0)
|
|
192
|
+
: await getEstimatedFees(acc, transaction, isContractAddressRecipient);
|
|
193
|
+
const balance =
|
|
194
|
+
account.type === "Account"
|
|
195
|
+
? BigNumber.max(0, account.spendableBalance.minus(estimatedFees))
|
|
196
|
+
: account.balance;
|
|
197
|
+
const amount = useAllAmount ? balance : transaction.amount;
|
|
198
|
+
const amountSpent = ["send", "freeze", "undelegateResource"].includes(mode)
|
|
199
|
+
? amount
|
|
200
|
+
: new BigNumber(0);
|
|
201
|
+
|
|
202
|
+
if (mode === "freeze" && amount.lt(ONE_TRX)) {
|
|
203
|
+
errors.amount = new TronInvalidFreezeAmount();
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
// fees are applied in the parent only (TRX)
|
|
207
|
+
const totalSpent = account.type === "Account" ? amountSpent.plus(estimatedFees) : amountSpent;
|
|
208
|
+
|
|
209
|
+
if (["send", "freeze"].includes(mode)) {
|
|
210
|
+
if (amount.eq(0)) {
|
|
211
|
+
errors.amount = new AmountRequired();
|
|
212
|
+
}
|
|
213
|
+
if (amountSpent.eq(0)) {
|
|
214
|
+
errors.amount = useAllAmount ? new NotEnoughBalance() : new AmountRequired();
|
|
215
|
+
} else if (amount.gt(balance)) {
|
|
216
|
+
errors.amount = new NotEnoughBalance();
|
|
217
|
+
} else if (account.type === "TokenAccount" && estimatedFees.gt(acc.balance)) {
|
|
218
|
+
errors.amount = new NotEnoughBalance();
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
const energy = (acc.tronResources && acc.tronResources.energy) || new BigNumber(0);
|
|
222
|
+
|
|
223
|
+
// For the moment, we rely on this rule:
|
|
224
|
+
// Add a 'TronNotEnoughEnergy' warning only if the account sastifies theses 3 conditions:
|
|
225
|
+
// - no energy
|
|
226
|
+
// - balance is lower than 1 TRX
|
|
227
|
+
// - contract consumes user energy (ie: user's ratio > 0%)
|
|
228
|
+
if (
|
|
229
|
+
account.type === "TokenAccount" &&
|
|
230
|
+
account.token.tokenType === "trc20" &&
|
|
231
|
+
energy.lt(47619) // temporary value corresponding to usdt trc20 energy
|
|
232
|
+
) {
|
|
233
|
+
const contractUserEnergyConsumption = await getContractUserEnergyRatioConsumption(
|
|
234
|
+
account.token.contractAddress,
|
|
235
|
+
);
|
|
236
|
+
|
|
237
|
+
if (contractUserEnergyConsumption > 0) {
|
|
238
|
+
warnings.amount = new TronNotEnoughEnergy();
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
if (!errors.recipient && estimatedFees.gt(0)) {
|
|
244
|
+
const fees = formatCurrencyUnit(getAccountCurrency(acc).units[0], estimatedFees, {
|
|
245
|
+
showCode: true,
|
|
246
|
+
disableRounding: true,
|
|
247
|
+
});
|
|
248
|
+
warnings.fee = new TronUnexpectedFees("Estimated fees", {
|
|
249
|
+
fees,
|
|
250
|
+
});
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
return Promise.resolve({
|
|
254
|
+
errors,
|
|
255
|
+
warnings,
|
|
256
|
+
amount: amountSpent,
|
|
257
|
+
estimatedFees,
|
|
258
|
+
totalSpent,
|
|
259
|
+
family,
|
|
260
|
+
});
|
|
261
|
+
};
|
|
262
|
+
|
|
263
|
+
export default getTransactionStatus;
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import getAddressWrapper from "@ledgerhq/coin-framework/bridge/getAddressWrapper";
|
|
2
|
+
import {
|
|
3
|
+
defaultUpdateTransaction,
|
|
4
|
+
makeAccountBridgeReceive,
|
|
5
|
+
makeScanAccounts,
|
|
6
|
+
} from "@ledgerhq/coin-framework/bridge/jsHelpers";
|
|
7
|
+
import { CoinConfig } from "@ledgerhq/coin-framework/config";
|
|
8
|
+
import { SignerContext } from "@ledgerhq/coin-framework/signer";
|
|
9
|
+
import type { AccountBridge, CurrencyBridge } from "@ledgerhq/types-live";
|
|
10
|
+
import { type Transaction, TronAccount, TronSigner } from "../types";
|
|
11
|
+
import signerGetAddress from "../signer";
|
|
12
|
+
import broadcast from "./broadcast";
|
|
13
|
+
import createTransaction from "./createTransaction";
|
|
14
|
+
import estimateMaxSpendable from "./estimateMaxSpendable";
|
|
15
|
+
import getTransactionStatus from "./getTransactionStatus";
|
|
16
|
+
import { hydrate, preload } from "./preload";
|
|
17
|
+
import { prepareTransaction } from "./prepareTransaction";
|
|
18
|
+
import {
|
|
19
|
+
assignFromAccountRaw,
|
|
20
|
+
assignToAccountRaw,
|
|
21
|
+
fromOperationExtraRaw,
|
|
22
|
+
toOperationExtraRaw,
|
|
23
|
+
} from "./serialization";
|
|
24
|
+
import { buildSignOperation } from "./signOperation";
|
|
25
|
+
import { getAccountShape, sync } from "./synchronization";
|
|
26
|
+
import { setCoinConfig, TronCoinConfig } from "../config";
|
|
27
|
+
|
|
28
|
+
function buildCurrencyBridge(signerContext: SignerContext<TronSigner>): CurrencyBridge {
|
|
29
|
+
const getAddress = signerGetAddress(signerContext);
|
|
30
|
+
|
|
31
|
+
const scanAccounts = makeScanAccounts({
|
|
32
|
+
getAccountShape,
|
|
33
|
+
getAddressFn: getAddressWrapper(getAddress),
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
return {
|
|
37
|
+
preload,
|
|
38
|
+
hydrate,
|
|
39
|
+
scanAccounts,
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function buildAccountBridge(
|
|
44
|
+
signerContext: SignerContext<TronSigner>,
|
|
45
|
+
): AccountBridge<Transaction, TronAccount> {
|
|
46
|
+
const getAddress = signerGetAddress(signerContext);
|
|
47
|
+
|
|
48
|
+
const receive = makeAccountBridgeReceive(getAddressWrapper(getAddress));
|
|
49
|
+
const signOperation = buildSignOperation(signerContext);
|
|
50
|
+
|
|
51
|
+
return {
|
|
52
|
+
estimateMaxSpendable,
|
|
53
|
+
createTransaction,
|
|
54
|
+
updateTransaction: defaultUpdateTransaction,
|
|
55
|
+
getTransactionStatus,
|
|
56
|
+
prepareTransaction,
|
|
57
|
+
sync,
|
|
58
|
+
receive,
|
|
59
|
+
signOperation,
|
|
60
|
+
broadcast,
|
|
61
|
+
assignFromAccountRaw,
|
|
62
|
+
assignToAccountRaw,
|
|
63
|
+
fromOperationExtraRaw,
|
|
64
|
+
toOperationExtraRaw,
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export function createBridges(
|
|
69
|
+
signerContext: SignerContext<TronSigner>,
|
|
70
|
+
coinConfig: CoinConfig<TronCoinConfig>,
|
|
71
|
+
) {
|
|
72
|
+
setCoinConfig(coinConfig);
|
|
73
|
+
|
|
74
|
+
return {
|
|
75
|
+
currencyBridge: buildCurrencyBridge(signerContext),
|
|
76
|
+
accountBridge: buildAccountBridge(signerContext),
|
|
77
|
+
};
|
|
78
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { getTronSuperRepresentatives, hydrateSuperRepresentatives } from "../network";
|
|
2
|
+
import { SuperRepresentative } from "../types";
|
|
3
|
+
|
|
4
|
+
export const preload = async () => {
|
|
5
|
+
const superRepresentatives = await getTronSuperRepresentatives();
|
|
6
|
+
return {
|
|
7
|
+
superRepresentatives,
|
|
8
|
+
};
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
export const hydrate = (data?: { superRepresentatives?: SuperRepresentative[] }) => {
|
|
12
|
+
if (!data || !data.superRepresentatives) return;
|
|
13
|
+
|
|
14
|
+
const { superRepresentatives } = data;
|
|
15
|
+
|
|
16
|
+
if (
|
|
17
|
+
!superRepresentatives ||
|
|
18
|
+
typeof superRepresentatives !== "object" ||
|
|
19
|
+
!Array.isArray(superRepresentatives)
|
|
20
|
+
)
|
|
21
|
+
return;
|
|
22
|
+
|
|
23
|
+
hydrateSuperRepresentatives(superRepresentatives);
|
|
24
|
+
};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { AccountBridge } from "@ledgerhq/types-live";
|
|
2
|
+
import { getTronAccountNetwork } from "../network";
|
|
3
|
+
import { Transaction, TronAccount } from "../types";
|
|
4
|
+
|
|
5
|
+
export const prepareTransaction: AccountBridge<
|
|
6
|
+
Transaction,
|
|
7
|
+
TronAccount
|
|
8
|
+
>["prepareTransaction"] = async (account, transaction) => {
|
|
9
|
+
const networkInfo =
|
|
10
|
+
transaction.networkInfo || (await getTronAccountNetwork(account.freshAddress));
|
|
11
|
+
return transaction.networkInfo === networkInfo ? transaction : { ...transaction, networkInfo };
|
|
12
|
+
};
|
|
@@ -0,0 +1,288 @@
|
|
|
1
|
+
import { BigNumber } from "bignumber.js";
|
|
2
|
+
import {
|
|
3
|
+
TronTransactionInfo,
|
|
4
|
+
TronTransactionInfoRaw,
|
|
5
|
+
isTrongridExtraTxInfo,
|
|
6
|
+
isTrongridExtraTxInfoRaw,
|
|
7
|
+
type TronAccount,
|
|
8
|
+
type TronAccountRaw,
|
|
9
|
+
type TronResources,
|
|
10
|
+
type TronResourcesRaw,
|
|
11
|
+
type TrongridExtraTxInfo,
|
|
12
|
+
type TrongridExtraTxInfoRaw,
|
|
13
|
+
} from "../types";
|
|
14
|
+
import { Account, AccountRaw, OperationExtra, OperationExtraRaw } from "@ledgerhq/types-live";
|
|
15
|
+
|
|
16
|
+
export const toTronResourcesRaw = ({
|
|
17
|
+
frozen,
|
|
18
|
+
unFrozen,
|
|
19
|
+
delegatedFrozen,
|
|
20
|
+
legacyFrozen,
|
|
21
|
+
votes,
|
|
22
|
+
tronPower,
|
|
23
|
+
energy,
|
|
24
|
+
bandwidth,
|
|
25
|
+
unwithdrawnReward,
|
|
26
|
+
lastWithdrawnRewardDate,
|
|
27
|
+
lastVotedDate,
|
|
28
|
+
cacheTransactionInfoById: cacheTx,
|
|
29
|
+
}: TronResources): TronResourcesRaw => {
|
|
30
|
+
const frozenBandwidth = frozen.bandwidth;
|
|
31
|
+
const frozenEnergy = frozen.energy;
|
|
32
|
+
const delegatedFrozenBandwidth = delegatedFrozen.bandwidth;
|
|
33
|
+
const delegatedFrozenEnergy = delegatedFrozen.energy;
|
|
34
|
+
const cacheTransactionInfoById: Record<string, TronTransactionInfoRaw> = {};
|
|
35
|
+
const unFrozenBandwidth = unFrozen?.bandwidth;
|
|
36
|
+
const unFrozenEnergy = unFrozen?.energy;
|
|
37
|
+
const legacyFrozenBandwidth = legacyFrozen?.bandwidth;
|
|
38
|
+
const legacyFrozenEnergy = legacyFrozen?.energy;
|
|
39
|
+
|
|
40
|
+
for (const k in cacheTx) {
|
|
41
|
+
const { fee, blockNumber, withdraw_amount, unfreeze_amount } = cacheTx[k];
|
|
42
|
+
cacheTransactionInfoById[k] = [fee, blockNumber, withdraw_amount, unfreeze_amount];
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
return {
|
|
46
|
+
frozen: {
|
|
47
|
+
bandwidth: frozenBandwidth
|
|
48
|
+
? {
|
|
49
|
+
amount: frozenBandwidth.amount.toString(),
|
|
50
|
+
}
|
|
51
|
+
: undefined,
|
|
52
|
+
energy: frozenEnergy
|
|
53
|
+
? {
|
|
54
|
+
amount: frozenEnergy.amount.toString(),
|
|
55
|
+
}
|
|
56
|
+
: undefined,
|
|
57
|
+
},
|
|
58
|
+
delegatedFrozen: {
|
|
59
|
+
bandwidth: delegatedFrozenBandwidth
|
|
60
|
+
? {
|
|
61
|
+
amount: delegatedFrozenBandwidth.amount.toString(),
|
|
62
|
+
}
|
|
63
|
+
: undefined,
|
|
64
|
+
energy: delegatedFrozenEnergy
|
|
65
|
+
? {
|
|
66
|
+
amount: delegatedFrozenEnergy.amount.toString(),
|
|
67
|
+
}
|
|
68
|
+
: undefined,
|
|
69
|
+
},
|
|
70
|
+
unFrozen: {
|
|
71
|
+
bandwidth: unFrozenBandwidth
|
|
72
|
+
? unFrozenBandwidth.map(entry => {
|
|
73
|
+
return { amount: entry.amount.toString(), expireTime: entry.expireTime.toISOString() };
|
|
74
|
+
})
|
|
75
|
+
: undefined,
|
|
76
|
+
energy: unFrozenEnergy
|
|
77
|
+
? unFrozenEnergy.map(entry => {
|
|
78
|
+
return { amount: entry.amount.toString(), expireTime: entry.expireTime.toISOString() };
|
|
79
|
+
})
|
|
80
|
+
: undefined,
|
|
81
|
+
},
|
|
82
|
+
legacyFrozen: {
|
|
83
|
+
bandwidth: legacyFrozenBandwidth
|
|
84
|
+
? {
|
|
85
|
+
amount: legacyFrozenBandwidth.amount.toString(),
|
|
86
|
+
expiredAt: legacyFrozenBandwidth.expiredAt.toISOString(),
|
|
87
|
+
}
|
|
88
|
+
: undefined,
|
|
89
|
+
energy: legacyFrozenEnergy
|
|
90
|
+
? {
|
|
91
|
+
amount: legacyFrozenEnergy.amount.toString(),
|
|
92
|
+
expiredAt: legacyFrozenEnergy.expiredAt.toISOString(),
|
|
93
|
+
}
|
|
94
|
+
: undefined,
|
|
95
|
+
},
|
|
96
|
+
votes,
|
|
97
|
+
tronPower,
|
|
98
|
+
energy: energy.toString(),
|
|
99
|
+
bandwidth: {
|
|
100
|
+
freeUsed: bandwidth.freeUsed.toString(),
|
|
101
|
+
freeLimit: bandwidth.freeLimit.toString(),
|
|
102
|
+
gainedUsed: bandwidth.gainedUsed.toString(),
|
|
103
|
+
gainedLimit: bandwidth.gainedLimit.toString(),
|
|
104
|
+
},
|
|
105
|
+
unwithdrawnReward: unwithdrawnReward.toString(),
|
|
106
|
+
lastWithdrawnRewardDate: lastWithdrawnRewardDate
|
|
107
|
+
? lastWithdrawnRewardDate.toISOString()
|
|
108
|
+
: undefined,
|
|
109
|
+
lastVotedDate: lastVotedDate ? lastVotedDate.toISOString() : undefined,
|
|
110
|
+
cacheTransactionInfoById,
|
|
111
|
+
};
|
|
112
|
+
};
|
|
113
|
+
export const fromTronResourcesRaw = ({
|
|
114
|
+
frozen,
|
|
115
|
+
unFrozen,
|
|
116
|
+
delegatedFrozen,
|
|
117
|
+
legacyFrozen,
|
|
118
|
+
votes,
|
|
119
|
+
tronPower,
|
|
120
|
+
energy,
|
|
121
|
+
bandwidth,
|
|
122
|
+
unwithdrawnReward,
|
|
123
|
+
lastWithdrawnRewardDate,
|
|
124
|
+
lastVotedDate,
|
|
125
|
+
cacheTransactionInfoById: cacheTransactionInfoByIdRaw,
|
|
126
|
+
}: TronResourcesRaw): TronResources => {
|
|
127
|
+
const frozenBandwidth = frozen.bandwidth;
|
|
128
|
+
const frozenEnergy = frozen.energy;
|
|
129
|
+
const delegatedFrozenBandwidth = delegatedFrozen.bandwidth;
|
|
130
|
+
const delegatedFrozenEnergy = delegatedFrozen.energy;
|
|
131
|
+
const unFrozenBandwidth = unFrozen?.bandwidth;
|
|
132
|
+
const unFrozenEnergy = unFrozen?.energy;
|
|
133
|
+
const legacyFrozenBandwidth = legacyFrozen?.bandwidth;
|
|
134
|
+
const legacyFrozenEnergy = legacyFrozen?.energy;
|
|
135
|
+
|
|
136
|
+
const cacheTransactionInfoById: Record<string, TronTransactionInfo> = {};
|
|
137
|
+
|
|
138
|
+
if (cacheTransactionInfoByIdRaw) {
|
|
139
|
+
for (const k in cacheTransactionInfoByIdRaw) {
|
|
140
|
+
const [fee, blockNumber, withdraw_amount, unfreeze_amount] = cacheTransactionInfoByIdRaw[k];
|
|
141
|
+
cacheTransactionInfoById[k] = {
|
|
142
|
+
fee,
|
|
143
|
+
blockNumber,
|
|
144
|
+
withdraw_amount,
|
|
145
|
+
unfreeze_amount,
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
return {
|
|
151
|
+
frozen: {
|
|
152
|
+
bandwidth: frozenBandwidth
|
|
153
|
+
? {
|
|
154
|
+
amount: new BigNumber(frozenBandwidth.amount),
|
|
155
|
+
}
|
|
156
|
+
: undefined,
|
|
157
|
+
energy: frozenEnergy
|
|
158
|
+
? {
|
|
159
|
+
amount: new BigNumber(frozenEnergy.amount),
|
|
160
|
+
}
|
|
161
|
+
: undefined,
|
|
162
|
+
},
|
|
163
|
+
delegatedFrozen: {
|
|
164
|
+
bandwidth: delegatedFrozenBandwidth
|
|
165
|
+
? {
|
|
166
|
+
amount: new BigNumber(delegatedFrozenBandwidth.amount),
|
|
167
|
+
}
|
|
168
|
+
: undefined,
|
|
169
|
+
energy: delegatedFrozenEnergy
|
|
170
|
+
? {
|
|
171
|
+
amount: new BigNumber(delegatedFrozenEnergy.amount),
|
|
172
|
+
}
|
|
173
|
+
: undefined,
|
|
174
|
+
},
|
|
175
|
+
unFrozen: {
|
|
176
|
+
bandwidth: unFrozenBandwidth
|
|
177
|
+
? unFrozenBandwidth.map(entry => {
|
|
178
|
+
return { amount: new BigNumber(entry.amount), expireTime: new Date(entry.expireTime) };
|
|
179
|
+
})
|
|
180
|
+
: undefined,
|
|
181
|
+
energy: unFrozenEnergy
|
|
182
|
+
? unFrozenEnergy.map(entry => {
|
|
183
|
+
return { amount: new BigNumber(entry.amount), expireTime: new Date(entry.expireTime) };
|
|
184
|
+
})
|
|
185
|
+
: undefined,
|
|
186
|
+
},
|
|
187
|
+
legacyFrozen: {
|
|
188
|
+
bandwidth: legacyFrozenBandwidth
|
|
189
|
+
? {
|
|
190
|
+
amount: new BigNumber(legacyFrozenBandwidth.amount),
|
|
191
|
+
expiredAt: new Date(legacyFrozenBandwidth.expiredAt),
|
|
192
|
+
}
|
|
193
|
+
: undefined,
|
|
194
|
+
energy: legacyFrozenEnergy
|
|
195
|
+
? {
|
|
196
|
+
amount: new BigNumber(legacyFrozenEnergy.amount),
|
|
197
|
+
expiredAt: new Date(legacyFrozenEnergy.expiredAt),
|
|
198
|
+
}
|
|
199
|
+
: undefined,
|
|
200
|
+
},
|
|
201
|
+
votes,
|
|
202
|
+
tronPower,
|
|
203
|
+
energy: new BigNumber(energy),
|
|
204
|
+
bandwidth: {
|
|
205
|
+
freeUsed: new BigNumber(bandwidth.freeUsed),
|
|
206
|
+
freeLimit: new BigNumber(bandwidth.freeLimit),
|
|
207
|
+
gainedUsed: new BigNumber(bandwidth.gainedUsed),
|
|
208
|
+
gainedLimit: new BigNumber(bandwidth.gainedLimit),
|
|
209
|
+
},
|
|
210
|
+
unwithdrawnReward: new BigNumber(unwithdrawnReward),
|
|
211
|
+
lastWithdrawnRewardDate: lastWithdrawnRewardDate
|
|
212
|
+
? new Date(lastWithdrawnRewardDate)
|
|
213
|
+
: undefined,
|
|
214
|
+
lastVotedDate: lastVotedDate ? new Date(lastVotedDate) : undefined,
|
|
215
|
+
cacheTransactionInfoById,
|
|
216
|
+
};
|
|
217
|
+
};
|
|
218
|
+
|
|
219
|
+
export function assignToAccountRaw(account: Account, accountRaw: AccountRaw) {
|
|
220
|
+
const tronAccount = account as TronAccount;
|
|
221
|
+
if (tronAccount.tronResources) {
|
|
222
|
+
(accountRaw as TronAccountRaw).tronResources = toTronResourcesRaw(tronAccount.tronResources);
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
export function assignFromAccountRaw(accountRaw: AccountRaw, account: Account) {
|
|
227
|
+
const tronResourcesRaw = (accountRaw as TronAccountRaw).tronResources;
|
|
228
|
+
if (tronResourcesRaw)
|
|
229
|
+
(account as TronAccount).tronResources = fromTronResourcesRaw(tronResourcesRaw);
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
export function fromOperationExtraRaw(extraRaw: OperationExtraRaw): TrongridExtraTxInfo {
|
|
233
|
+
const extra: TrongridExtraTxInfo = {};
|
|
234
|
+
if (!isTrongridExtraTxInfoRaw(extraRaw)) {
|
|
235
|
+
return extra;
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
if (extraRaw.frozenAmount) {
|
|
239
|
+
extra.frozenAmount = new BigNumber(extraRaw.frozenAmount);
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
if (extraRaw.unfreezeAmount) {
|
|
243
|
+
extra.unfreezeAmount = new BigNumber(extraRaw.unfreezeAmount);
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
if (extraRaw.votes) {
|
|
247
|
+
extra.votes = extraRaw.votes;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
if (extraRaw.unDelegatedAmount) {
|
|
251
|
+
extra.unDelegatedAmount = new BigNumber(extraRaw.unDelegatedAmount);
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
if (extraRaw.receiverAddress) {
|
|
255
|
+
extra.receiverAddress = extraRaw.receiverAddress;
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
return extra;
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
export function toOperationExtraRaw(extra: OperationExtra): TrongridExtraTxInfoRaw {
|
|
262
|
+
const extraRaw: TrongridExtraTxInfoRaw = {};
|
|
263
|
+
if (!isTrongridExtraTxInfo(extra)) {
|
|
264
|
+
return extraRaw;
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
if (extra.frozenAmount) {
|
|
268
|
+
extraRaw.frozenAmount = extra.frozenAmount.toString();
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
if (extra.unfreezeAmount) {
|
|
272
|
+
extraRaw.unfreezeAmount = extra.unfreezeAmount.toString();
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
if (extra.votes) {
|
|
276
|
+
extraRaw.votes = extra.votes;
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
if (extra.unDelegatedAmount) {
|
|
280
|
+
extraRaw.unDelegatedAmount = extra.unDelegatedAmount.toString();
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
if (extra.receiverAddress) {
|
|
284
|
+
extraRaw.receiverAddress = extra.receiverAddress;
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
return extraRaw;
|
|
288
|
+
}
|