@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,231 @@
|
|
|
1
|
+
import { encodeOperationId } from "@ledgerhq/coin-framework/operation";
|
|
2
|
+
import type { Account, OperationType } from "@ledgerhq/types-live";
|
|
3
|
+
import { BigNumber } from "bignumber.js";
|
|
4
|
+
import type {
|
|
5
|
+
BandwidthInfo,
|
|
6
|
+
NetworkInfo,
|
|
7
|
+
Transaction,
|
|
8
|
+
TrongridTxInfo,
|
|
9
|
+
TronOperation,
|
|
10
|
+
TronOperationMode,
|
|
11
|
+
TronResources,
|
|
12
|
+
} from "../types";
|
|
13
|
+
|
|
14
|
+
// see: https://solidity.readthedocs.io/en/v0.6.1/abi-spec.html#function-selector-and-argument-encoding
|
|
15
|
+
export const abiEncodeTrc20Transfer = (address: string, amount: BigNumber): string => {
|
|
16
|
+
const encodedAddress = address.padStart(64, "0");
|
|
17
|
+
const hexAmount = amount.toNumber().toString(16); // convert to hexadecimal
|
|
18
|
+
|
|
19
|
+
const encodedAmount = hexAmount.padStart(64, "0");
|
|
20
|
+
return encodedAddress.concat(encodedAmount);
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
export const hexToAscii = (hex: string): string => Buffer.from(hex, "hex").toString("ascii");
|
|
24
|
+
|
|
25
|
+
const parentTx = [
|
|
26
|
+
"TransferContract",
|
|
27
|
+
"VoteWitnessContract",
|
|
28
|
+
"WithdrawBalanceContract",
|
|
29
|
+
"ExchangeTransactionContract",
|
|
30
|
+
"FreezeBalanceV2Contract",
|
|
31
|
+
"UnfreezeBalanceV2Contract",
|
|
32
|
+
"WithdrawExpireUnfreezeContract",
|
|
33
|
+
"UnDelegateResourceContract",
|
|
34
|
+
"FreezeBalanceContract",
|
|
35
|
+
"UnfreezeBalanceContract",
|
|
36
|
+
];
|
|
37
|
+
|
|
38
|
+
export const isParentTx = (tx: TrongridTxInfo): boolean => parentTx.includes(tx.type);
|
|
39
|
+
|
|
40
|
+
// This is an estimation, there is no endpoint to calculate the real size of a block before broadcasting it.
|
|
41
|
+
export const getEstimatedBlockSize = (a: Account, t: Transaction): BigNumber => {
|
|
42
|
+
switch (t.mode) {
|
|
43
|
+
case "send": {
|
|
44
|
+
const subAccount =
|
|
45
|
+
t.subAccountId && a.subAccounts ? a.subAccounts.find(sa => sa.id === t.subAccountId) : null;
|
|
46
|
+
|
|
47
|
+
if (subAccount && subAccount.type === "TokenAccount") {
|
|
48
|
+
if (subAccount.token.tokenType === "trc10") return new BigNumber(285);
|
|
49
|
+
if (subAccount.token.tokenType === "trc20") return new BigNumber(350);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
return new BigNumber(270);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
case "freeze":
|
|
56
|
+
case "unfreeze":
|
|
57
|
+
case "claimReward":
|
|
58
|
+
case "withdrawExpireUnfreeze":
|
|
59
|
+
case "unDelegateResource":
|
|
60
|
+
case "legacyUnfreeze":
|
|
61
|
+
return new BigNumber(260);
|
|
62
|
+
|
|
63
|
+
case "vote":
|
|
64
|
+
return new BigNumber(290 + t.votes.length * 19);
|
|
65
|
+
|
|
66
|
+
default:
|
|
67
|
+
return new BigNumber(0);
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
export const getOperationTypefromMode = (mode: TronOperationMode): OperationType => {
|
|
72
|
+
switch (mode) {
|
|
73
|
+
case "send":
|
|
74
|
+
return "OUT";
|
|
75
|
+
|
|
76
|
+
case "freeze":
|
|
77
|
+
return "FREEZE";
|
|
78
|
+
|
|
79
|
+
case "unfreeze":
|
|
80
|
+
return "UNFREEZE";
|
|
81
|
+
|
|
82
|
+
case "vote":
|
|
83
|
+
return "VOTE";
|
|
84
|
+
|
|
85
|
+
case "claimReward":
|
|
86
|
+
return "REWARD";
|
|
87
|
+
|
|
88
|
+
case "withdrawExpireUnfreeze":
|
|
89
|
+
return "WITHDRAW_EXPIRE_UNFREEZE";
|
|
90
|
+
|
|
91
|
+
case "unDelegateResource":
|
|
92
|
+
return "UNDELEGATE_RESOURCE";
|
|
93
|
+
|
|
94
|
+
case "legacyUnfreeze":
|
|
95
|
+
return "LEGACY_UNFREEZE";
|
|
96
|
+
|
|
97
|
+
default:
|
|
98
|
+
return "OUT";
|
|
99
|
+
}
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
const getOperationType = (
|
|
103
|
+
tx: TrongridTxInfo,
|
|
104
|
+
accountAddr: string,
|
|
105
|
+
): OperationType | null | undefined => {
|
|
106
|
+
switch (tx.type) {
|
|
107
|
+
case "TransferContract":
|
|
108
|
+
case "TransferAssetContract":
|
|
109
|
+
case "TriggerSmartContract":
|
|
110
|
+
return tx.from === accountAddr ? "OUT" : "IN";
|
|
111
|
+
|
|
112
|
+
case "ExchangeTransactionContract":
|
|
113
|
+
return "OUT";
|
|
114
|
+
|
|
115
|
+
case "VoteWitnessContract":
|
|
116
|
+
return "VOTE";
|
|
117
|
+
|
|
118
|
+
case "WithdrawBalanceContract":
|
|
119
|
+
return "REWARD";
|
|
120
|
+
|
|
121
|
+
case "FreezeBalanceContract":
|
|
122
|
+
case "FreezeBalanceV2Contract":
|
|
123
|
+
return "FREEZE";
|
|
124
|
+
|
|
125
|
+
case "UnfreezeBalanceV2Contract":
|
|
126
|
+
return "UNFREEZE";
|
|
127
|
+
|
|
128
|
+
case "WithdrawExpireUnfreezeContract":
|
|
129
|
+
return "WITHDRAW_EXPIRE_UNFREEZE";
|
|
130
|
+
|
|
131
|
+
case "UnDelegateResourceContract":
|
|
132
|
+
return "UNDELEGATE_RESOURCE";
|
|
133
|
+
|
|
134
|
+
case "UnfreezeBalanceContract":
|
|
135
|
+
return "LEGACY_UNFREEZE";
|
|
136
|
+
|
|
137
|
+
default:
|
|
138
|
+
return undefined;
|
|
139
|
+
}
|
|
140
|
+
};
|
|
141
|
+
|
|
142
|
+
export const txInfoToOperation = (
|
|
143
|
+
id: string,
|
|
144
|
+
address: string,
|
|
145
|
+
tx: TrongridTxInfo,
|
|
146
|
+
): TronOperation | null | undefined => {
|
|
147
|
+
const {
|
|
148
|
+
txID,
|
|
149
|
+
date,
|
|
150
|
+
from,
|
|
151
|
+
to,
|
|
152
|
+
type,
|
|
153
|
+
value = new BigNumber(0),
|
|
154
|
+
fee = new BigNumber(0),
|
|
155
|
+
blockHeight,
|
|
156
|
+
extra = {},
|
|
157
|
+
hasFailed,
|
|
158
|
+
} = tx;
|
|
159
|
+
const hash = txID;
|
|
160
|
+
const operationType = getOperationType(tx, address);
|
|
161
|
+
|
|
162
|
+
if (operationType) {
|
|
163
|
+
return {
|
|
164
|
+
id: encodeOperationId(id, hash, operationType),
|
|
165
|
+
hash,
|
|
166
|
+
type: operationType,
|
|
167
|
+
value: operationType === "OUT" && type === "TransferContract" ? value.plus(fee) : value,
|
|
168
|
+
// fee is not charged in TRC tokens
|
|
169
|
+
fee: fee,
|
|
170
|
+
blockHeight,
|
|
171
|
+
blockHash: null,
|
|
172
|
+
accountId: id,
|
|
173
|
+
senders: [from],
|
|
174
|
+
recipients: to ? [to] : [],
|
|
175
|
+
date,
|
|
176
|
+
extra,
|
|
177
|
+
hasFailed,
|
|
178
|
+
};
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
return undefined;
|
|
182
|
+
};
|
|
183
|
+
|
|
184
|
+
export const extractBandwidthInfo = (
|
|
185
|
+
networkInfo: NetworkInfo | null | undefined,
|
|
186
|
+
): BandwidthInfo => {
|
|
187
|
+
// Calculate bandwidth info :
|
|
188
|
+
if (networkInfo) {
|
|
189
|
+
const { freeNetUsed, freeNetLimit, netUsed, netLimit } = networkInfo;
|
|
190
|
+
return {
|
|
191
|
+
freeUsed: freeNetUsed,
|
|
192
|
+
freeLimit: freeNetLimit,
|
|
193
|
+
gainedUsed: netUsed,
|
|
194
|
+
gainedLimit: netLimit,
|
|
195
|
+
};
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
return {
|
|
199
|
+
freeUsed: new BigNumber(0),
|
|
200
|
+
freeLimit: new BigNumber(0),
|
|
201
|
+
gainedUsed: new BigNumber(0),
|
|
202
|
+
gainedLimit: new BigNumber(0),
|
|
203
|
+
};
|
|
204
|
+
};
|
|
205
|
+
|
|
206
|
+
export const defaultTronResources: TronResources = {
|
|
207
|
+
frozen: {
|
|
208
|
+
bandwidth: undefined,
|
|
209
|
+
energy: undefined,
|
|
210
|
+
},
|
|
211
|
+
unFrozen: {
|
|
212
|
+
bandwidth: undefined,
|
|
213
|
+
energy: undefined,
|
|
214
|
+
},
|
|
215
|
+
delegatedFrozen: {
|
|
216
|
+
bandwidth: undefined,
|
|
217
|
+
energy: undefined,
|
|
218
|
+
},
|
|
219
|
+
legacyFrozen: {
|
|
220
|
+
bandwidth: undefined,
|
|
221
|
+
energy: undefined,
|
|
222
|
+
},
|
|
223
|
+
votes: [],
|
|
224
|
+
tronPower: 0,
|
|
225
|
+
energy: new BigNumber(0),
|
|
226
|
+
bandwidth: extractBandwidthInfo(null),
|
|
227
|
+
unwithdrawnReward: new BigNumber(0),
|
|
228
|
+
lastWithdrawnRewardDate: undefined,
|
|
229
|
+
lastVotedDate: undefined,
|
|
230
|
+
cacheTransactionInfoById: {},
|
|
231
|
+
};
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
import get from "lodash/get";
|
|
2
|
+
import { TrongridExtraTxInfo, TrongridTxInfo, TronTransactionInfo } from "../types";
|
|
3
|
+
import { TransactionTronAPI, Trc20API } from "./types";
|
|
4
|
+
import BigNumber from "bignumber.js";
|
|
5
|
+
import { log } from "@ledgerhq/logs";
|
|
6
|
+
import bs58check from "bs58check";
|
|
7
|
+
|
|
8
|
+
export const decode58Check = (base58: string): string =>
|
|
9
|
+
Buffer.from(bs58check.decode(base58)).toString("hex");
|
|
10
|
+
|
|
11
|
+
export const encode58Check = (hex: string): string => bs58check.encode(Buffer.from(hex, "hex"));
|
|
12
|
+
|
|
13
|
+
export const formatTrongridTrc20TxResponse = (tx: Trc20API): TrongridTxInfo | null | undefined => {
|
|
14
|
+
try {
|
|
15
|
+
const { from, to, block_timestamp, detail, value, transaction_id, token_info } = tx;
|
|
16
|
+
const type = "TriggerSmartContract";
|
|
17
|
+
const txID = transaction_id;
|
|
18
|
+
const date = new Date(block_timestamp);
|
|
19
|
+
const tokenId = token_info.address ?? undefined;
|
|
20
|
+
const formattedValue = value ? new BigNumber(value) : new BigNumber(0);
|
|
21
|
+
const fee = tx.detail.ret[0].fee || undefined;
|
|
22
|
+
const blockHeight = detail ? detail.blockNumber : undefined;
|
|
23
|
+
return {
|
|
24
|
+
txID,
|
|
25
|
+
date,
|
|
26
|
+
type,
|
|
27
|
+
tokenId,
|
|
28
|
+
from,
|
|
29
|
+
to,
|
|
30
|
+
blockHeight,
|
|
31
|
+
value: formattedValue,
|
|
32
|
+
fee: new BigNumber(fee || 0),
|
|
33
|
+
hasFailed: false, // trc20 txs are succeeded if returned by trongrid,
|
|
34
|
+
};
|
|
35
|
+
} catch (e) {
|
|
36
|
+
log("tron-error", "could not parse transaction", tx);
|
|
37
|
+
return undefined;
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
export const formatTrongridTxResponse = (
|
|
42
|
+
tx: TransactionTronAPI & { detail?: TronTransactionInfo },
|
|
43
|
+
): TrongridTxInfo | null | undefined => {
|
|
44
|
+
try {
|
|
45
|
+
const { txID, block_timestamp, detail, blockNumber, unfreeze_amount, withdraw_amount } = tx;
|
|
46
|
+
const date = new Date(block_timestamp);
|
|
47
|
+
const type = tx.raw_data.contract[0].type;
|
|
48
|
+
const {
|
|
49
|
+
amount,
|
|
50
|
+
asset_name,
|
|
51
|
+
owner_address,
|
|
52
|
+
to_address,
|
|
53
|
+
contract_address,
|
|
54
|
+
quant,
|
|
55
|
+
frozen_balance,
|
|
56
|
+
votes,
|
|
57
|
+
unfreeze_balance,
|
|
58
|
+
balance,
|
|
59
|
+
receiver_address,
|
|
60
|
+
} = tx.raw_data.contract[0].parameter.value;
|
|
61
|
+
const hasFailed = get(tx, "ret[0].contractRet", "SUCCESS") !== "SUCCESS";
|
|
62
|
+
const tokenId =
|
|
63
|
+
type === "TransferAssetContract"
|
|
64
|
+
? asset_name
|
|
65
|
+
: type === "TriggerSmartContract" && contract_address
|
|
66
|
+
? encode58Check(contract_address)
|
|
67
|
+
: undefined;
|
|
68
|
+
const from = encode58Check(owner_address);
|
|
69
|
+
const to = to_address ? encode58Check(to_address) : undefined;
|
|
70
|
+
|
|
71
|
+
const getValue = (): BigNumber => {
|
|
72
|
+
switch (type) {
|
|
73
|
+
case "WithdrawBalanceContract":
|
|
74
|
+
return new BigNumber(withdraw_amount || detail?.withdraw_amount || 0);
|
|
75
|
+
|
|
76
|
+
case "ExchangeTransactionContract":
|
|
77
|
+
return new BigNumber(quant || 0);
|
|
78
|
+
|
|
79
|
+
default:
|
|
80
|
+
return amount ? new BigNumber(amount) : new BigNumber(0);
|
|
81
|
+
}
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
const value = getValue();
|
|
85
|
+
const fee = get(tx, "ret[0].fee", detail && detail.fee ? detail.fee : undefined);
|
|
86
|
+
const blockHeight = blockNumber || detail?.blockNumber;
|
|
87
|
+
const txInfo: TrongridTxInfo = {
|
|
88
|
+
txID,
|
|
89
|
+
date,
|
|
90
|
+
type,
|
|
91
|
+
tokenId,
|
|
92
|
+
from,
|
|
93
|
+
to,
|
|
94
|
+
value: !value.isNaN() ? value : new BigNumber(0),
|
|
95
|
+
fee: new BigNumber(fee || 0),
|
|
96
|
+
blockHeight,
|
|
97
|
+
hasFailed,
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
const getExtra = (): TrongridExtraTxInfo | null | undefined => {
|
|
101
|
+
switch (type) {
|
|
102
|
+
case "VoteWitnessContract":
|
|
103
|
+
return {
|
|
104
|
+
votes: votes?.map(v => ({
|
|
105
|
+
address: encode58Check(v.vote_address as string),
|
|
106
|
+
voteCount: v.vote_count,
|
|
107
|
+
})),
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
case "FreezeBalanceContract":
|
|
111
|
+
case "FreezeBalanceV2Contract":
|
|
112
|
+
return {
|
|
113
|
+
frozenAmount: new BigNumber(frozen_balance!),
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
case "UnfreezeBalanceV2Contract":
|
|
117
|
+
return {
|
|
118
|
+
unfreezeAmount: new BigNumber(unfreeze_balance!),
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
case "UnDelegateResourceContract":
|
|
122
|
+
return {
|
|
123
|
+
unDelegatedAmount: new BigNumber(balance!),
|
|
124
|
+
receiverAddress: encode58Check(receiver_address!),
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
case "UnfreezeBalanceContract":
|
|
128
|
+
return {
|
|
129
|
+
unfreezeAmount: new BigNumber(unfreeze_amount || detail!.unfreeze_amount!),
|
|
130
|
+
};
|
|
131
|
+
|
|
132
|
+
default:
|
|
133
|
+
return undefined;
|
|
134
|
+
}
|
|
135
|
+
};
|
|
136
|
+
|
|
137
|
+
const extra = getExtra();
|
|
138
|
+
|
|
139
|
+
if (extra) {
|
|
140
|
+
txInfo.extra = extra;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
return txInfo;
|
|
144
|
+
} catch (e) {
|
|
145
|
+
log("tron-error", "could not parse transaction", tx);
|
|
146
|
+
return undefined;
|
|
147
|
+
}
|
|
148
|
+
};
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { fetchTronAccountTxs } from ".";
|
|
2
|
+
import { setCoinConfig } from "../config";
|
|
3
|
+
|
|
4
|
+
describe("fetchTronAccountTxs", () => {
|
|
5
|
+
beforeAll(() => {
|
|
6
|
+
setCoinConfig(() => ({
|
|
7
|
+
status: {
|
|
8
|
+
type: "active",
|
|
9
|
+
},
|
|
10
|
+
explorer: {
|
|
11
|
+
url: "https://tron.coin.ledger.com",
|
|
12
|
+
},
|
|
13
|
+
}));
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
it(
|
|
17
|
+
"maps all fields correctly",
|
|
18
|
+
async () => {
|
|
19
|
+
// WHEN
|
|
20
|
+
// const addr = "TL24LCps5FKwp3PoU1MvrYrwhi5LU1tHre";
|
|
21
|
+
// const addr = "TAVrrARNdnjHgCGMQYeQV7hv4PSu7mVsMj";
|
|
22
|
+
// const addr = "THAe4BNVxp293qgyQEqXEkHMpPcqtG73bi";
|
|
23
|
+
// const addr = "TRqkRnAj6ceJFYAn2p1eE7aWrgBBwtdhS9";
|
|
24
|
+
// const addr = "TUxd6v64YTWkfpFpNDdtgc5Ps4SfGxwizT";
|
|
25
|
+
const addr = "TY2ksFgpvb82TgGPwUSa7iseqPW5weYQyh";
|
|
26
|
+
const results = await fetchTronAccountTxs(addr, txs => txs.length < 100, {});
|
|
27
|
+
|
|
28
|
+
// THEN
|
|
29
|
+
expect(results).not.toHaveLength(0);
|
|
30
|
+
},
|
|
31
|
+
10 * 1000,
|
|
32
|
+
);
|
|
33
|
+
});
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { HttpResponse, http } from "msw";
|
|
2
|
+
import { setupServer } from "msw/node";
|
|
3
|
+
import { TRANSACTION_DETAIL_FIXTURE, TRANSACTION_FIXTURE, TRC20_FIXTURE } from "./types.fixture";
|
|
4
|
+
import { setCoinConfig } from "../config";
|
|
5
|
+
import { fetchTronAccountTxs } from ".";
|
|
6
|
+
|
|
7
|
+
export const TRON_BASE_URL_TEST = "https://httpbin.org";
|
|
8
|
+
|
|
9
|
+
const handlers = [
|
|
10
|
+
http.get(`${TRON_BASE_URL_TEST}/v1/accounts/:addr/transactions`, () => {
|
|
11
|
+
// const url = new URL(request.url);
|
|
12
|
+
// const _ = url.searchParams.get("get_detail");
|
|
13
|
+
return HttpResponse.json(TRANSACTION_FIXTURE);
|
|
14
|
+
}),
|
|
15
|
+
http.get(`${TRON_BASE_URL_TEST}/v1/accounts/:addr/transactions/trc20`, () => {
|
|
16
|
+
// const url = new URL(request.url);
|
|
17
|
+
// const _ = url.searchParams.get("get_detail");
|
|
18
|
+
return HttpResponse.json(TRC20_FIXTURE);
|
|
19
|
+
}),
|
|
20
|
+
http.get(`${TRON_BASE_URL_TEST}/wallet/gettransactioninfobyid`, ({ request }) => {
|
|
21
|
+
const url = new URL(request.url);
|
|
22
|
+
const value = url.searchParams.get("value") ?? "UNKNOWN";
|
|
23
|
+
return HttpResponse.json(TRANSACTION_DETAIL_FIXTURE(value));
|
|
24
|
+
}),
|
|
25
|
+
];
|
|
26
|
+
|
|
27
|
+
const mockServer = setupServer(...handlers);
|
|
28
|
+
|
|
29
|
+
describe("fetchTronAccountTxs", () => {
|
|
30
|
+
beforeAll(() => {
|
|
31
|
+
setCoinConfig(() => ({
|
|
32
|
+
status: {
|
|
33
|
+
type: "active",
|
|
34
|
+
},
|
|
35
|
+
explorer: {
|
|
36
|
+
url: TRON_BASE_URL_TEST,
|
|
37
|
+
},
|
|
38
|
+
}));
|
|
39
|
+
|
|
40
|
+
mockServer.listen();
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
beforeEach(() => {
|
|
44
|
+
mockServer.resetHandlers();
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
afterAll(() => {
|
|
48
|
+
mockServer.close();
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
it("convert correctly operations from the blockchain", async () => {
|
|
52
|
+
// WHEN
|
|
53
|
+
const results = await fetchTronAccountTxs("ADDRESS", txs => txs.length < 100, {});
|
|
54
|
+
|
|
55
|
+
// THEN
|
|
56
|
+
const tx = results.find(tx => tx.blockHeight === 62258698);
|
|
57
|
+
expect(tx).toBeDefined();
|
|
58
|
+
expect(tx!.from).toEqual("TQ7pF3NTDL2Tjz5rdJ6ECjQWjaWHpLZJMH");
|
|
59
|
+
expect(tx!.to).toEqual("TAVrrARNdnjHgCGMQYeQV7hv4PSu7mVsMj");
|
|
60
|
+
});
|
|
61
|
+
});
|