@ledgerhq/coin-tron 4.5.0 → 4.5.1-nightly.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +7 -0
- package/lib/api/index.d.ts +2 -2
- package/lib/api/index.d.ts.map +1 -1
- package/lib/api/index.integ.test.js.map +1 -1
- package/lib/api/index.js.map +1 -1
- package/lib/api/index.test.js +2 -3
- package/lib/api/index.test.js.map +1 -1
- package/lib/bridge/getEstimateFees.d.ts.map +1 -1
- package/lib/bridge/getEstimateFees.js +3 -4
- package/lib/bridge/getEstimateFees.js.map +1 -1
- package/lib/logic/craftTransaction.d.ts +3 -3
- package/lib/logic/craftTransaction.d.ts.map +1 -1
- package/lib/logic/craftTransaction.integ.test.js +11 -16
- package/lib/logic/craftTransaction.integ.test.js.map +1 -1
- package/lib/logic/craftTransaction.js +6 -5
- package/lib/logic/craftTransaction.js.map +1 -1
- package/lib/logic/craftTransaction.test.js +10 -14
- package/lib/logic/craftTransaction.test.js.map +1 -1
- package/lib/logic/estimateFees.d.ts +2 -2
- package/lib/logic/estimateFees.d.ts.map +1 -1
- package/lib/logic/estimateFees.js +1 -1
- package/lib/logic/estimateFees.js.map +1 -1
- package/lib/logic/estimateFees.test.js +4 -6
- package/lib/logic/estimateFees.test.js.map +1 -1
- package/lib/logic/getBalance.d.ts +3 -4
- package/lib/logic/getBalance.d.ts.map +1 -1
- package/lib/logic/getBalance.integ.test.js +4 -1
- package/lib/logic/getBalance.integ.test.js.map +1 -1
- package/lib/logic/getBalance.js +4 -6
- package/lib/logic/getBalance.js.map +1 -1
- package/lib/logic/getBalance.test.js +60 -20
- package/lib/logic/getBalance.test.js.map +1 -1
- package/lib/logic/listOperations.d.ts +1 -2
- package/lib/logic/listOperations.d.ts.map +1 -1
- package/lib/logic/listOperations.integ.test.js +8 -12
- package/lib/logic/listOperations.integ.test.js.map +1 -1
- package/lib/logic/listOperations.js.map +1 -1
- package/lib/logic/listOperations.unit.test.js.map +1 -1
- package/lib/network/trongrid/trongrid-adapters.d.ts +2 -2
- package/lib/network/trongrid/trongrid-adapters.d.ts.map +1 -1
- package/lib/network/trongrid/trongrid-adapters.js +4 -6
- package/lib/network/trongrid/trongrid-adapters.js.map +1 -1
- package/lib/network/trongrid/trongrid-adapters.test.js +1 -1
- package/lib/network/trongrid/trongrid-adapters.test.js.map +1 -1
- package/lib/types/assets.d.ts +1 -11
- package/lib/types/assets.d.ts.map +1 -1
- package/lib-es/api/index.d.ts +2 -2
- package/lib-es/api/index.d.ts.map +1 -1
- package/lib-es/api/index.integ.test.js.map +1 -1
- package/lib-es/api/index.js.map +1 -1
- package/lib-es/api/index.test.js +2 -3
- package/lib-es/api/index.test.js.map +1 -1
- package/lib-es/bridge/getEstimateFees.d.ts.map +1 -1
- package/lib-es/bridge/getEstimateFees.js +3 -4
- package/lib-es/bridge/getEstimateFees.js.map +1 -1
- package/lib-es/logic/craftTransaction.d.ts +3 -3
- package/lib-es/logic/craftTransaction.d.ts.map +1 -1
- package/lib-es/logic/craftTransaction.integ.test.js +11 -16
- package/lib-es/logic/craftTransaction.integ.test.js.map +1 -1
- package/lib-es/logic/craftTransaction.js +6 -5
- package/lib-es/logic/craftTransaction.js.map +1 -1
- package/lib-es/logic/craftTransaction.test.js +10 -14
- package/lib-es/logic/craftTransaction.test.js.map +1 -1
- package/lib-es/logic/estimateFees.d.ts +2 -2
- package/lib-es/logic/estimateFees.d.ts.map +1 -1
- package/lib-es/logic/estimateFees.js +1 -1
- package/lib-es/logic/estimateFees.js.map +1 -1
- package/lib-es/logic/estimateFees.test.js +4 -6
- package/lib-es/logic/estimateFees.test.js.map +1 -1
- package/lib-es/logic/getBalance.d.ts +3 -4
- package/lib-es/logic/getBalance.d.ts.map +1 -1
- package/lib-es/logic/getBalance.integ.test.js +4 -1
- package/lib-es/logic/getBalance.integ.test.js.map +1 -1
- package/lib-es/logic/getBalance.js +4 -6
- package/lib-es/logic/getBalance.js.map +1 -1
- package/lib-es/logic/getBalance.test.js +60 -20
- package/lib-es/logic/getBalance.test.js.map +1 -1
- package/lib-es/logic/listOperations.d.ts +1 -2
- package/lib-es/logic/listOperations.d.ts.map +1 -1
- package/lib-es/logic/listOperations.integ.test.js +8 -12
- package/lib-es/logic/listOperations.integ.test.js.map +1 -1
- package/lib-es/logic/listOperations.js.map +1 -1
- package/lib-es/logic/listOperations.unit.test.js.map +1 -1
- package/lib-es/network/trongrid/trongrid-adapters.d.ts +2 -2
- package/lib-es/network/trongrid/trongrid-adapters.d.ts.map +1 -1
- package/lib-es/network/trongrid/trongrid-adapters.js +4 -6
- package/lib-es/network/trongrid/trongrid-adapters.js.map +1 -1
- package/lib-es/network/trongrid/trongrid-adapters.test.js +1 -1
- package/lib-es/network/trongrid/trongrid-adapters.test.js.map +1 -1
- package/lib-es/types/assets.d.ts +1 -11
- package/lib-es/types/assets.d.ts.map +1 -1
- package/package.json +7 -7
- package/src/api/index.integ.test.ts +1 -2
- package/src/api/index.test.ts +4 -6
- package/src/api/index.ts +5 -7
- package/src/bridge/getEstimateFees.ts +5 -6
- package/src/logic/craftTransaction.integ.test.ts +11 -16
- package/src/logic/craftTransaction.test.ts +15 -20
- package/src/logic/craftTransaction.ts +11 -11
- package/src/logic/estimateFees.test.ts +7 -10
- package/src/logic/estimateFees.ts +3 -3
- package/src/logic/getBalance.integ.test.ts +4 -1
- package/src/logic/getBalance.test.ts +60 -20
- package/src/logic/getBalance.ts +12 -15
- package/src/logic/listOperations.integ.test.ts +12 -17
- package/src/logic/listOperations.ts +1 -2
- package/src/logic/listOperations.unit.test.ts +5 -5
- package/src/network/trongrid/trongrid-adapters.test.ts +1 -1
- package/src/network/trongrid/trongrid-adapters.ts +8 -10
- package/src/types/assets.ts +1 -13
package/src/api/index.test.ts
CHANGED
|
@@ -11,7 +11,6 @@ import coinConfig from "../config";
|
|
|
11
11
|
import { TronConfig } from "../config";
|
|
12
12
|
import { AlpacaApi, Pagination, TransactionIntent } from "@ledgerhq/coin-framework/api/types";
|
|
13
13
|
import { createApi } from ".";
|
|
14
|
-
import { TronAsset } from "../types";
|
|
15
14
|
|
|
16
15
|
jest.mock("../config", () => ({
|
|
17
16
|
setCoinConfig: jest.fn(),
|
|
@@ -49,16 +48,15 @@ describe("createApi", () => {
|
|
|
49
48
|
});
|
|
50
49
|
|
|
51
50
|
it("should pass parameters correctly", async () => {
|
|
52
|
-
const api: AlpacaApi
|
|
53
|
-
const intent: TransactionIntent
|
|
51
|
+
const api: AlpacaApi = createApi(mockTronConfig);
|
|
52
|
+
const intent: TransactionIntent = {
|
|
54
53
|
type: "send",
|
|
55
54
|
sender: "sender",
|
|
56
55
|
recipient: "recipient",
|
|
57
56
|
amount: BigInt(10),
|
|
58
57
|
asset: {
|
|
59
|
-
type: "
|
|
60
|
-
|
|
61
|
-
tokenId: "1002000",
|
|
58
|
+
type: "trc10",
|
|
59
|
+
assetReference: "1002000",
|
|
62
60
|
},
|
|
63
61
|
};
|
|
64
62
|
// Simulate calling all methods
|
package/src/api/index.ts
CHANGED
|
@@ -18,9 +18,9 @@ import {
|
|
|
18
18
|
lastBlock,
|
|
19
19
|
Options,
|
|
20
20
|
} from "../logic";
|
|
21
|
-
import type {
|
|
21
|
+
import type { TronMemo } from "../types";
|
|
22
22
|
|
|
23
|
-
export function createApi(config: TronConfig): AlpacaApi<
|
|
23
|
+
export function createApi(config: TronConfig): AlpacaApi<TronMemo> {
|
|
24
24
|
coinConfig.setCoinConfig(() => ({ ...config, status: { type: "active" } }));
|
|
25
25
|
|
|
26
26
|
return {
|
|
@@ -31,7 +31,7 @@ export function createApi(config: TronConfig): AlpacaApi<TronAsset, TronMemo> {
|
|
|
31
31
|
getBalance,
|
|
32
32
|
lastBlock,
|
|
33
33
|
listOperations,
|
|
34
|
-
getBlock(_height): Promise<Block
|
|
34
|
+
getBlock(_height): Promise<Block> {
|
|
35
35
|
throw new Error("getBlock is not supported");
|
|
36
36
|
},
|
|
37
37
|
getBlockInfo(_height: number): Promise<BlockInfo> {
|
|
@@ -40,9 +40,7 @@ export function createApi(config: TronConfig): AlpacaApi<TronAsset, TronMemo> {
|
|
|
40
40
|
};
|
|
41
41
|
}
|
|
42
42
|
|
|
43
|
-
async function estimate(
|
|
44
|
-
transactionIntent: TransactionIntent<TronAsset, TronMemo>,
|
|
45
|
-
): Promise<FeeEstimation> {
|
|
43
|
+
async function estimate(transactionIntent: TransactionIntent<TronMemo>): Promise<FeeEstimation> {
|
|
46
44
|
const fees = await estimateFees(transactionIntent);
|
|
47
45
|
return { value: fees };
|
|
48
46
|
}
|
|
@@ -50,7 +48,7 @@ async function estimate(
|
|
|
50
48
|
async function listOperations(
|
|
51
49
|
address: string,
|
|
52
50
|
pagination: Pagination,
|
|
53
|
-
): Promise<[Operation
|
|
51
|
+
): Promise<[Operation[], string]> {
|
|
54
52
|
const { minHeight } = pagination;
|
|
55
53
|
const options: Options = {
|
|
56
54
|
softLimit: 200,
|
|
@@ -2,7 +2,7 @@ import { Account, TokenAccount } from "@ledgerhq/types-live";
|
|
|
2
2
|
import BigNumber from "bignumber.js";
|
|
3
3
|
import { ACTIVATION_FEES, STANDARD_FEES_NATIVE, STANDARD_FEES_TRC_20 } from "../logic/constants";
|
|
4
4
|
import type { AccountTronAPI } from "../network/types";
|
|
5
|
-
import type { Transaction
|
|
5
|
+
import type { Transaction } from "../types";
|
|
6
6
|
import { extractBandwidthInfo, getEstimatedBlockSize } from "./utils";
|
|
7
7
|
import { estimateFees, getAccount } from "../logic";
|
|
8
8
|
import type { TransactionIntent } from "@ledgerhq/coin-framework/api/index";
|
|
@@ -48,7 +48,7 @@ const getFeesFromAccountActivation = async (
|
|
|
48
48
|
return ACTIVATION_FEES; // cost is around 1 TRX
|
|
49
49
|
}
|
|
50
50
|
|
|
51
|
-
const transactionIntent: TransactionIntent
|
|
51
|
+
const transactionIntent: TransactionIntent = {
|
|
52
52
|
type: transaction.mode,
|
|
53
53
|
sender: account.freshAddress,
|
|
54
54
|
recipient: transaction.recipient,
|
|
@@ -56,11 +56,10 @@ const getFeesFromAccountActivation = async (
|
|
|
56
56
|
asset:
|
|
57
57
|
tokenAccount?.token.tokenType === "trc20"
|
|
58
58
|
? {
|
|
59
|
-
type: "
|
|
60
|
-
|
|
61
|
-
contractAddress: tokenAccount.token.contractAddress,
|
|
59
|
+
type: "trc20",
|
|
60
|
+
assetReference: tokenAccount.token.contractAddress,
|
|
62
61
|
}
|
|
63
|
-
: { type: "
|
|
62
|
+
: { type: "trc10", assetReference: tokenAccount?.token.id },
|
|
64
63
|
};
|
|
65
64
|
|
|
66
65
|
// if we have a token account but the recipient is either not active or the account does not have a trc20 balance for the given token.
|
|
@@ -25,9 +25,8 @@ describe("Testing craftTransaction function", () => {
|
|
|
25
25
|
const result = await craftTransaction({
|
|
26
26
|
type: "send",
|
|
27
27
|
asset: {
|
|
28
|
-
type: "
|
|
29
|
-
|
|
30
|
-
tokenId: "1002000",
|
|
28
|
+
type: "trc10",
|
|
29
|
+
assetReference: "1002000",
|
|
31
30
|
},
|
|
32
31
|
sender,
|
|
33
32
|
recipient,
|
|
@@ -67,9 +66,8 @@ describe("Testing craftTransaction function", () => {
|
|
|
67
66
|
const result = await craftTransaction({
|
|
68
67
|
type: "send",
|
|
69
68
|
asset: {
|
|
70
|
-
type: "
|
|
71
|
-
|
|
72
|
-
contractAddress: "TLa2f6VPqDgRE67v1736s7bJ8Ray5wYjU7",
|
|
69
|
+
type: "trc20",
|
|
70
|
+
assetReference: "TLa2f6VPqDgRE67v1736s7bJ8Ray5wYjU7",
|
|
73
71
|
},
|
|
74
72
|
sender,
|
|
75
73
|
recipient,
|
|
@@ -107,9 +105,8 @@ describe("Testing craftTransaction function", () => {
|
|
|
107
105
|
const result = await craftTransaction({
|
|
108
106
|
type: "send",
|
|
109
107
|
asset: {
|
|
110
|
-
type: "
|
|
111
|
-
|
|
112
|
-
contractAddress: "TLa2f6VPqDgRE67v1736s7bJ8Ray5wYjU7",
|
|
108
|
+
type: "trc20",
|
|
109
|
+
assetReference: "TLa2f6VPqDgRE67v1736s7bJ8Ray5wYjU7",
|
|
113
110
|
},
|
|
114
111
|
sender,
|
|
115
112
|
recipient,
|
|
@@ -136,15 +133,14 @@ describe("Testing craftTransaction function", () => {
|
|
|
136
133
|
{
|
|
137
134
|
type: "send",
|
|
138
135
|
asset: {
|
|
139
|
-
type: "
|
|
140
|
-
|
|
141
|
-
contractAddress: "TLa2f6VPqDgRE67v1736s7bJ8Ray5wYjU7",
|
|
136
|
+
type: "trc20",
|
|
137
|
+
assetReference: "TLa2f6VPqDgRE67v1736s7bJ8Ray5wYjU7",
|
|
142
138
|
},
|
|
143
139
|
sender,
|
|
144
140
|
recipient,
|
|
145
141
|
amount,
|
|
146
142
|
},
|
|
147
|
-
customFees,
|
|
143
|
+
{ value: customFees },
|
|
148
144
|
);
|
|
149
145
|
|
|
150
146
|
const decodeResult = await decodeTransaction(result);
|
|
@@ -233,9 +229,8 @@ describe("Testing craftTransaction function", () => {
|
|
|
233
229
|
craftTransaction({
|
|
234
230
|
type: "send",
|
|
235
231
|
asset: {
|
|
236
|
-
type: "
|
|
237
|
-
|
|
238
|
-
contractAddress: "TLa2f6VPqDgRE67v1736s7bJ8Ray5wYjU7",
|
|
232
|
+
type: "trc20",
|
|
233
|
+
assetReference: "TLa2f6VPqDgRE67v1736s7bJ8Ray5wYjU7",
|
|
239
234
|
},
|
|
240
235
|
sender,
|
|
241
236
|
recipient,
|
|
@@ -2,7 +2,6 @@ import { TransactionIntent } from "@ledgerhq/coin-framework/api/index";
|
|
|
2
2
|
import BigNumber from "bignumber.js";
|
|
3
3
|
import { craftStandardTransaction, craftTrc20Transaction } from "../network";
|
|
4
4
|
import { decode58Check } from "../network/format";
|
|
5
|
-
import { TronAsset } from "../types";
|
|
6
5
|
import { craftTransaction } from "./craftTransaction";
|
|
7
6
|
|
|
8
7
|
jest.mock("../network/format", () => ({
|
|
@@ -22,7 +21,7 @@ describe("craftTransaction", () => {
|
|
|
22
21
|
});
|
|
23
22
|
|
|
24
23
|
it("should craft a standard transaction", async () => {
|
|
25
|
-
const transactionIntent: TransactionIntent
|
|
24
|
+
const transactionIntent: TransactionIntent = {
|
|
26
25
|
asset: { type: "native" },
|
|
27
26
|
type: "send",
|
|
28
27
|
recipient: "recipient",
|
|
@@ -52,12 +51,11 @@ describe("craftTransaction", () => {
|
|
|
52
51
|
});
|
|
53
52
|
|
|
54
53
|
it("should craft a TRC20 transaction", async () => {
|
|
55
|
-
const transactionIntent: TransactionIntent
|
|
54
|
+
const transactionIntent: TransactionIntent = {
|
|
56
55
|
type: "send",
|
|
57
56
|
asset: {
|
|
58
|
-
type: "
|
|
59
|
-
|
|
60
|
-
contractAddress: "contractAddress",
|
|
57
|
+
type: "trc20",
|
|
58
|
+
assetReference: "contractAddress",
|
|
61
59
|
},
|
|
62
60
|
recipient: "recipient",
|
|
63
61
|
sender: "sender",
|
|
@@ -89,19 +87,18 @@ describe("craftTransaction", () => {
|
|
|
89
87
|
const amount: number = 1000;
|
|
90
88
|
const transactionIntent = {
|
|
91
89
|
asset: {
|
|
92
|
-
type: "
|
|
93
|
-
|
|
94
|
-
contractAddress: "contractAddress",
|
|
90
|
+
type: "trc20",
|
|
91
|
+
assetReference: "contractAddress",
|
|
95
92
|
},
|
|
96
93
|
amount: BigInt(amount),
|
|
97
|
-
} as TransactionIntent
|
|
94
|
+
} as TransactionIntent;
|
|
98
95
|
|
|
99
96
|
(decode58Check as jest.Mock).mockImplementation(_address => undefined);
|
|
100
97
|
(craftTrc20Transaction as jest.Mock).mockResolvedValue({
|
|
101
98
|
raw_data_hex: "extendedRawDataHex",
|
|
102
99
|
});
|
|
103
100
|
|
|
104
|
-
await craftTransaction(transactionIntent, customFees);
|
|
101
|
+
await craftTransaction(transactionIntent, { value: customFees });
|
|
105
102
|
expect(craftTrc20Transaction).toHaveBeenCalledWith(
|
|
106
103
|
"contractAddress",
|
|
107
104
|
undefined,
|
|
@@ -116,12 +113,11 @@ describe("craftTransaction", () => {
|
|
|
116
113
|
const amount = 1000;
|
|
117
114
|
const transactionIntent = {
|
|
118
115
|
asset: {
|
|
119
|
-
type: "
|
|
120
|
-
|
|
121
|
-
contractAddress: "contractAddress",
|
|
116
|
+
type: "trc20",
|
|
117
|
+
assetReference: "contractAddress",
|
|
122
118
|
},
|
|
123
119
|
amount: BigInt(amount),
|
|
124
|
-
} as TransactionIntent
|
|
120
|
+
} as TransactionIntent;
|
|
125
121
|
|
|
126
122
|
(decode58Check as jest.Mock).mockImplementation(_address => undefined);
|
|
127
123
|
(craftTrc20Transaction as jest.Mock).mockResolvedValue({
|
|
@@ -146,12 +142,11 @@ describe("craftTransaction", () => {
|
|
|
146
142
|
await craftTransaction(
|
|
147
143
|
{
|
|
148
144
|
asset: {
|
|
149
|
-
type: "
|
|
150
|
-
|
|
151
|
-
contractAddress: "contractAddress",
|
|
145
|
+
type: "trc20",
|
|
146
|
+
assetReference: "contractAddress",
|
|
152
147
|
},
|
|
153
|
-
} as TransactionIntent
|
|
154
|
-
customFees,
|
|
148
|
+
} as TransactionIntent,
|
|
149
|
+
{ value: customFees },
|
|
155
150
|
);
|
|
156
151
|
} catch (error) {
|
|
157
152
|
expect((error as Error).message).toEqual(
|
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
import { TransactionIntent } from "@ledgerhq/coin-framework/api/index";
|
|
1
|
+
import { FeeEstimation, TransactionIntent } from "@ledgerhq/coin-framework/api/index";
|
|
2
2
|
import BigNumber from "bignumber.js";
|
|
3
3
|
import { craftStandardTransaction, craftTrc20Transaction } from "../network";
|
|
4
4
|
import { decode58Check } from "../network/format";
|
|
5
|
-
import {
|
|
5
|
+
import { TronMemo } from "../types";
|
|
6
6
|
import { feesToNumber } from "./utils";
|
|
7
7
|
|
|
8
8
|
export async function craftTransaction(
|
|
9
|
-
transactionIntent: TransactionIntent<
|
|
10
|
-
customFees?:
|
|
9
|
+
transactionIntent: TransactionIntent<TronMemo>,
|
|
10
|
+
customFees?: FeeEstimation,
|
|
11
11
|
): Promise<string> {
|
|
12
12
|
const { asset, recipient, sender, amount, expiration } = transactionIntent;
|
|
13
13
|
const rawMemo = "memo" in transactionIntent ? transactionIntent.memo : undefined;
|
|
@@ -16,8 +16,9 @@ export async function craftTransaction(
|
|
|
16
16
|
const recipientAddress = decode58Check(recipient);
|
|
17
17
|
const senderAddress = decode58Check(sender);
|
|
18
18
|
|
|
19
|
-
if (asset.type === "
|
|
20
|
-
|
|
19
|
+
if (asset.type === "trc20" && asset.assetReference) {
|
|
20
|
+
const fees = customFees?.value;
|
|
21
|
+
if (fees !== undefined && (fees <= 0 || fees > Number.MAX_SAFE_INTEGER)) {
|
|
21
22
|
throw new Error(
|
|
22
23
|
`fees must be between 0 and ${Number.MAX_SAFE_INTEGER} (Typescript Number type value limit)`,
|
|
23
24
|
);
|
|
@@ -28,18 +29,17 @@ export async function craftTransaction(
|
|
|
28
29
|
}
|
|
29
30
|
|
|
30
31
|
const { raw_data_hex: rawDataHex } = await craftTrc20Transaction(
|
|
31
|
-
asset.
|
|
32
|
+
asset.assetReference,
|
|
32
33
|
recipientAddress,
|
|
33
34
|
senderAddress,
|
|
34
35
|
new BigNumber(amount.toString()),
|
|
35
|
-
feesToNumber(
|
|
36
|
+
feesToNumber(fees),
|
|
36
37
|
expiration,
|
|
37
38
|
);
|
|
38
39
|
return rawDataHex as string;
|
|
39
40
|
} else {
|
|
40
|
-
const isTransferAsset = asset.type === "
|
|
41
|
-
const tokenId =
|
|
42
|
-
asset.type === "token" && asset.standard === "trc10" ? asset.tokenId : undefined;
|
|
41
|
+
const isTransferAsset = asset.type === "trc10";
|
|
42
|
+
const tokenId = asset.type === "trc10" ? asset.assetReference : undefined;
|
|
43
43
|
const { raw_data_hex: rawDataHex } = await craftStandardTransaction(
|
|
44
44
|
tokenId,
|
|
45
45
|
recipientAddress,
|
|
@@ -1,11 +1,10 @@
|
|
|
1
1
|
import { estimateFees } from "./estimateFees";
|
|
2
2
|
import { ACTIVATION_FEES_TRC_20, STANDARD_FEES_NATIVE } from "./constants";
|
|
3
3
|
import { TransactionIntent } from "@ledgerhq/coin-framework/api/index";
|
|
4
|
-
import { TronAsset } from "../types";
|
|
5
4
|
|
|
6
5
|
describe("estimateFees", () => {
|
|
7
6
|
it("should calculate fees for native trx transactionIntent", async () => {
|
|
8
|
-
const transactionIntent: TransactionIntent
|
|
7
|
+
const transactionIntent: TransactionIntent = {
|
|
9
8
|
type: "send",
|
|
10
9
|
sender: "sender1",
|
|
11
10
|
recipient: "recipient1",
|
|
@@ -18,15 +17,14 @@ describe("estimateFees", () => {
|
|
|
18
17
|
expect(result).toEqual(BigInt(STANDARD_FEES_NATIVE.toString()));
|
|
19
18
|
});
|
|
20
19
|
it("should calculate fees for trc10 transactionIntent", async () => {
|
|
21
|
-
const transactionIntent: TransactionIntent
|
|
20
|
+
const transactionIntent: TransactionIntent = {
|
|
22
21
|
type: "send",
|
|
23
22
|
sender: "sender1",
|
|
24
23
|
recipient: "recipient1",
|
|
25
24
|
amount: BigInt(1000),
|
|
26
25
|
asset: {
|
|
27
|
-
type: "
|
|
28
|
-
|
|
29
|
-
tokenId: "1002000",
|
|
26
|
+
type: "trc10",
|
|
27
|
+
assetReference: "1002000",
|
|
30
28
|
},
|
|
31
29
|
};
|
|
32
30
|
|
|
@@ -36,15 +34,14 @@ describe("estimateFees", () => {
|
|
|
36
34
|
});
|
|
37
35
|
|
|
38
36
|
it("should calculate fees for trc20 transactionIntent", async () => {
|
|
39
|
-
const transactionIntent: TransactionIntent
|
|
37
|
+
const transactionIntent: TransactionIntent = {
|
|
40
38
|
type: "send",
|
|
41
39
|
sender: "sender1",
|
|
42
40
|
recipient: "recipient1",
|
|
43
41
|
amount: BigInt(1000),
|
|
44
42
|
asset: {
|
|
45
|
-
type: "
|
|
46
|
-
|
|
47
|
-
contractAddress: "T9yD14Nj9j7xAB4dbGeiX9h8unkKHxuWwb",
|
|
43
|
+
type: "trc20",
|
|
44
|
+
assetReference: "T9yD14Nj9j7xAB4dbGeiX9h8unkKHxuWwb",
|
|
48
45
|
},
|
|
49
46
|
};
|
|
50
47
|
const result = await estimateFees(transactionIntent);
|
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { TronMemo } from "../types";
|
|
2
2
|
import { ACTIVATION_FEES_TRC_20, STANDARD_FEES_NATIVE } from "./constants";
|
|
3
3
|
import type { TransactionIntent } from "@ledgerhq/coin-framework/api/index";
|
|
4
4
|
|
|
5
5
|
export async function estimateFees(
|
|
6
|
-
transactionIntent: TransactionIntent<
|
|
6
|
+
transactionIntent: TransactionIntent<TronMemo>,
|
|
7
7
|
): Promise<bigint> {
|
|
8
|
-
if (transactionIntent.asset.type === "
|
|
8
|
+
if (transactionIntent.asset.type === "trc20") {
|
|
9
9
|
return BigInt(ACTIVATION_FEES_TRC_20.toString());
|
|
10
10
|
} else {
|
|
11
11
|
return BigInt(STANDARD_FEES_NATIVE.toString());
|
|
@@ -17,7 +17,10 @@ describe("getBalance", () => {
|
|
|
17
17
|
const balances = await getBalance("TRqkRnAj6ceJFYAn2p1eE7aWrgBBwtdhS9");
|
|
18
18
|
|
|
19
19
|
expect(balances[0].asset).toEqual({ type: "native" });
|
|
20
|
-
|
|
20
|
+
// Backend either returns trc10 or trc20 first (randomly)
|
|
21
|
+
balances
|
|
22
|
+
.slice(1)
|
|
23
|
+
.forEach(balance => expect(["trc20", "trc10"].includes(balance.asset.type)).toBe(true));
|
|
21
24
|
balances.forEach(balance => expect(balance.value).toBeGreaterThanOrEqual(0));
|
|
22
25
|
});
|
|
23
26
|
});
|
|
@@ -224,33 +224,73 @@ describe("getBalance", () => {
|
|
|
224
224
|
const balance = await getBalance("41ae18eb0a9e067f8884058470ed187f44135d816d");
|
|
225
225
|
expect(balance).toEqual([
|
|
226
226
|
{ asset: { type: "native" }, value: 27781772n },
|
|
227
|
-
{
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
{
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
{
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
{
|
|
240
|
-
|
|
227
|
+
{
|
|
228
|
+
asset: { type: "trc10", assetReference: "1002897" },
|
|
229
|
+
value: 10000000n,
|
|
230
|
+
},
|
|
231
|
+
{
|
|
232
|
+
asset: { type: "trc10", assetReference: "1002775" },
|
|
233
|
+
value: 5000000n,
|
|
234
|
+
},
|
|
235
|
+
{
|
|
236
|
+
asset: { type: "trc10", assetReference: "1002830" },
|
|
237
|
+
value: 10000000n,
|
|
238
|
+
},
|
|
239
|
+
{
|
|
240
|
+
asset: { type: "trc10", assetReference: "1002962" },
|
|
241
|
+
value: 10000000n,
|
|
242
|
+
},
|
|
243
|
+
{
|
|
244
|
+
asset: { type: "trc10", assetReference: "1002876" },
|
|
245
|
+
value: 10000000n,
|
|
246
|
+
},
|
|
247
|
+
{
|
|
248
|
+
asset: { type: "trc10", assetReference: "1002398" },
|
|
249
|
+
value: 5000000n,
|
|
250
|
+
},
|
|
251
|
+
{
|
|
252
|
+
asset: { type: "trc10", assetReference: "1002573" },
|
|
253
|
+
value: 5000000n,
|
|
254
|
+
},
|
|
255
|
+
{
|
|
256
|
+
asset: { type: "trc10", assetReference: "1002881" },
|
|
257
|
+
value: 10000000n,
|
|
258
|
+
},
|
|
259
|
+
{
|
|
260
|
+
asset: { type: "trc10", assetReference: "1002927" },
|
|
261
|
+
value: 10000000n,
|
|
262
|
+
},
|
|
263
|
+
{
|
|
264
|
+
asset: { type: "trc10", assetReference: "1002736" },
|
|
265
|
+
value: 5000000n,
|
|
266
|
+
},
|
|
267
|
+
{
|
|
268
|
+
asset: { type: "trc10", assetReference: "1002814" },
|
|
269
|
+
value: 10000000n,
|
|
270
|
+
},
|
|
271
|
+
{
|
|
272
|
+
asset: { type: "trc10", assetReference: "1002858" },
|
|
273
|
+
value: 10000000n,
|
|
274
|
+
},
|
|
275
|
+
{
|
|
276
|
+
asset: { type: "trc10", assetReference: "1002000" },
|
|
277
|
+
value: 26888000n,
|
|
278
|
+
},
|
|
279
|
+
{
|
|
280
|
+
asset: { type: "trc10", assetReference: "1004031" },
|
|
281
|
+
value: 9856699n,
|
|
282
|
+
},
|
|
241
283
|
{
|
|
242
284
|
asset: {
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
type: "token",
|
|
285
|
+
type: "trc20",
|
|
286
|
+
assetReference: "TLa2f6VPqDgRE67v1736s7bJ8Ray5wYjU7",
|
|
246
287
|
},
|
|
247
288
|
value: 46825830n,
|
|
248
289
|
},
|
|
249
290
|
{
|
|
250
291
|
asset: {
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
type: "token",
|
|
292
|
+
type: "trc20",
|
|
293
|
+
assetReference: "TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t",
|
|
254
294
|
},
|
|
255
295
|
value: 376n,
|
|
256
296
|
},
|
package/src/logic/getBalance.ts
CHANGED
|
@@ -1,14 +1,13 @@
|
|
|
1
1
|
import BigNumber from "bignumber.js";
|
|
2
2
|
import type { AccountTronAPI } from "../network/types";
|
|
3
3
|
import { getTronResources } from "./utils";
|
|
4
|
-
import {
|
|
4
|
+
import { Balance } from "@ledgerhq/coin-framework/api/index";
|
|
5
5
|
import { fetchTronAccount } from "../network";
|
|
6
|
-
import { Trc10Token, Trc20Token, TronAsset } from "../types";
|
|
7
6
|
|
|
8
7
|
const bigIntOrZero = (val: number | BigNumber | undefined | null): bigint =>
|
|
9
8
|
BigInt(val?.toString() ?? 0);
|
|
10
9
|
|
|
11
|
-
export async function getBalance(address: string): Promise<Balance
|
|
10
|
+
export async function getBalance(address: string): Promise<Balance[]> {
|
|
12
11
|
const accounts = await fetchTronAccount(address);
|
|
13
12
|
|
|
14
13
|
// if account is not activated, an empty array is returned
|
|
@@ -16,43 +15,41 @@ export async function getBalance(address: string): Promise<Balance<TronAsset>[]>
|
|
|
16
15
|
|
|
17
16
|
const account = accounts[0];
|
|
18
17
|
|
|
19
|
-
const nativeBalance: Balance
|
|
20
|
-
const trc10Balance: Balance
|
|
21
|
-
const trc20Balance: Balance
|
|
18
|
+
const nativeBalance: Balance = computeBalance(account);
|
|
19
|
+
const trc10Balance: Balance[] = extractTrc10Balance(account);
|
|
20
|
+
const trc20Balance: Balance[] = extractTrc20Balance(account);
|
|
22
21
|
|
|
23
22
|
return [nativeBalance].concat(trc10Balance).concat(trc20Balance);
|
|
24
23
|
}
|
|
25
24
|
|
|
26
|
-
function extractTrc10Balance(account: AccountTronAPI): Balance
|
|
25
|
+
function extractTrc10Balance(account: AccountTronAPI): Balance[] {
|
|
27
26
|
return (
|
|
28
27
|
account.assetV2?.map(trc => {
|
|
29
28
|
return {
|
|
30
29
|
value: BigInt(trc.value),
|
|
31
30
|
asset: {
|
|
32
|
-
type: "
|
|
33
|
-
|
|
34
|
-
tokenId: trc.key,
|
|
31
|
+
type: "trc10",
|
|
32
|
+
assetReference: trc.key,
|
|
35
33
|
},
|
|
36
34
|
};
|
|
37
35
|
}) ?? []
|
|
38
36
|
);
|
|
39
37
|
}
|
|
40
38
|
|
|
41
|
-
function extractTrc20Balance(account: AccountTronAPI): Balance
|
|
39
|
+
function extractTrc20Balance(account: AccountTronAPI): Balance[] {
|
|
42
40
|
return account.trc20.map(trc => {
|
|
43
41
|
const [[contractAddress, balance]] = Object.entries(trc);
|
|
44
42
|
return {
|
|
45
43
|
value: BigInt(balance),
|
|
46
44
|
asset: {
|
|
47
|
-
type: "
|
|
48
|
-
|
|
49
|
-
contractAddress,
|
|
45
|
+
type: "trc20",
|
|
46
|
+
assetReference: contractAddress,
|
|
50
47
|
},
|
|
51
48
|
};
|
|
52
49
|
});
|
|
53
50
|
}
|
|
54
51
|
|
|
55
|
-
export function computeBalance(account: AccountTronAPI): Balance
|
|
52
|
+
export function computeBalance(account: AccountTronAPI): Balance {
|
|
56
53
|
const tronResources = getTronResources(account);
|
|
57
54
|
|
|
58
55
|
let balance = bigIntOrZero(account.balance ?? 0);
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { defaultOptions, listOperations, Options } from "./listOperations";
|
|
2
2
|
import coinConfig from "../config";
|
|
3
3
|
import { Operation } from "@ledgerhq/coin-framework/api/types";
|
|
4
|
-
import { TronAsset } from "../types";
|
|
5
4
|
|
|
6
5
|
describe("listOperations", () => {
|
|
7
6
|
beforeAll(() => {
|
|
@@ -18,7 +17,7 @@ describe("listOperations", () => {
|
|
|
18
17
|
describe("Account TRqkRnAj6ceJFYAn2p1eE7aWrgBBwtdhS9 with minHeight", () => {
|
|
19
18
|
// https://tronscan.org/#/address/TRqkRnAj6ceJFYAn2p1eE7aWrgBBwtdhS9
|
|
20
19
|
|
|
21
|
-
let operations: Operation
|
|
20
|
+
let operations: Operation[];
|
|
22
21
|
|
|
23
22
|
const testingAccount = "TRqkRnAj6ceJFYAn2p1eE7aWrgBBwtdhS9";
|
|
24
23
|
|
|
@@ -44,7 +43,7 @@ describe("listOperations", () => {
|
|
|
44
43
|
describe("Account TRRYfGVrzuUvJYRe9UaA8KqxjgVSwU9m6L withe more than 15k+ txs, with minHeight 0 / order asc / softLimit 2", () => {
|
|
45
44
|
// https://tronscan.org/#/address/TRRYfGVrzuUvJYRe9UaA8KqxjgVSwU9m6L
|
|
46
45
|
|
|
47
|
-
let operations: Operation
|
|
46
|
+
let operations: Operation[];
|
|
48
47
|
|
|
49
48
|
const testingAccount = "TRRYfGVrzuUvJYRe9UaA8KqxjgVSwU9m6L";
|
|
50
49
|
|
|
@@ -77,7 +76,7 @@ describe("listOperations", () => {
|
|
|
77
76
|
describe("Account TRRYfGVrzuUvJYRe9UaA8KqxjgVSwU9m6L withe more than 15k+ txs, with minHeight / order asc / softLimit 2", () => {
|
|
78
77
|
// https://tronscan.org/#/address/TRRYfGVrzuUvJYRe9UaA8KqxjgVSwU9m6L
|
|
79
78
|
|
|
80
|
-
let operations: Operation
|
|
79
|
+
let operations: Operation[];
|
|
81
80
|
|
|
82
81
|
const testingAccount = "TRRYfGVrzuUvJYRe9UaA8KqxjgVSwU9m6L";
|
|
83
82
|
|
|
@@ -117,7 +116,7 @@ describe("listOperations", () => {
|
|
|
117
116
|
// 255 as of 17/02/2025
|
|
118
117
|
const historySize = 255;
|
|
119
118
|
|
|
120
|
-
let operations: Operation
|
|
119
|
+
let operations: Operation[];
|
|
121
120
|
|
|
122
121
|
const testingAccount = "TRqkRnAj6ceJFYAn2p1eE7aWrgBBwtdhS9";
|
|
123
122
|
|
|
@@ -223,9 +222,8 @@ describe("listOperations", () => {
|
|
|
223
222
|
recipients: [testingAccount],
|
|
224
223
|
senders: ["TWBEcQ57vbFSEhrQCvsHLDuSb39wprpsEX"],
|
|
225
224
|
asset: {
|
|
226
|
-
type: "
|
|
227
|
-
|
|
228
|
-
tokenId: "1004031",
|
|
225
|
+
type: "trc10",
|
|
226
|
+
assetReference: "1004031",
|
|
229
227
|
},
|
|
230
228
|
});
|
|
231
229
|
});
|
|
@@ -240,9 +238,8 @@ describe("listOperations", () => {
|
|
|
240
238
|
senders: [testingAccount],
|
|
241
239
|
recipients: ["TVKG4gUar24bpAVrDv4GSzyDRtPkjPkogL"],
|
|
242
240
|
asset: {
|
|
243
|
-
type: "
|
|
244
|
-
|
|
245
|
-
tokenId: "1002000",
|
|
241
|
+
type: "trc10",
|
|
242
|
+
assetReference: "1002000",
|
|
246
243
|
},
|
|
247
244
|
});
|
|
248
245
|
});
|
|
@@ -260,9 +257,8 @@ describe("listOperations", () => {
|
|
|
260
257
|
senders: ["TUgU8FRUFSUfxTAoSPsaUBzJgSwpUuJs9N"],
|
|
261
258
|
recipients: [testingAccount],
|
|
262
259
|
asset: {
|
|
263
|
-
type: "
|
|
264
|
-
|
|
265
|
-
contractAddress: "TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t",
|
|
260
|
+
type: "trc20",
|
|
261
|
+
assetReference: "TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t",
|
|
266
262
|
},
|
|
267
263
|
});
|
|
268
264
|
});
|
|
@@ -278,9 +274,8 @@ describe("listOperations", () => {
|
|
|
278
274
|
senders: [testingAccount],
|
|
279
275
|
recipients: ["TLAhq1ds7UR339t48TpzYcJWtfGnXk1KzX"],
|
|
280
276
|
asset: {
|
|
281
|
-
type: "
|
|
282
|
-
|
|
283
|
-
contractAddress: "TLa2f6VPqDgRE67v1736s7bJ8Ray5wYjU7",
|
|
277
|
+
type: "trc20",
|
|
278
|
+
assetReference: "TLa2f6VPqDgRE67v1736s7bJ8Ray5wYjU7",
|
|
284
279
|
},
|
|
285
280
|
});
|
|
286
281
|
});
|
|
@@ -6,7 +6,6 @@ import {
|
|
|
6
6
|
getBlock,
|
|
7
7
|
} from "../network";
|
|
8
8
|
import { fromTrongridTxInfoToOperation } from "../network/trongrid/trongrid-adapters";
|
|
9
|
-
import { TronAsset } from "../types";
|
|
10
9
|
|
|
11
10
|
export type Options = {
|
|
12
11
|
// the soft limit is an indicative number of transactions to fetch
|
|
@@ -27,7 +26,7 @@ export const defaultOptions: Options = {
|
|
|
27
26
|
export async function listOperations(
|
|
28
27
|
address: string,
|
|
29
28
|
options: Options,
|
|
30
|
-
): Promise<[Operation
|
|
29
|
+
): Promise<[Operation[], string]> {
|
|
31
30
|
// there is a possible optimisation here: when height is 0, set the minTimestamp to 0
|
|
32
31
|
const block = await getBlock(options.minHeight);
|
|
33
32
|
const minTimestamp = block.time?.getTime() ?? defaultFetchParams.minTimestamp;
|