@ledgerhq/coin-tezos 2.2.0 → 3.0.0-next.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 +22 -0
- package/lib/api/index.d.ts +2 -1
- package/lib/api/index.d.ts.map +1 -1
- package/lib/api/index.integ.test.js +6 -1
- package/lib/api/index.integ.test.js.map +1 -1
- package/lib/api/index.js +3 -0
- package/lib/api/index.js.map +1 -1
- package/lib/api/index.test.js +19 -4
- package/lib/api/index.test.js.map +1 -1
- package/lib/logic/getBalance.d.ts +3 -1
- package/lib/logic/getBalance.d.ts.map +1 -1
- package/lib/logic/getBalance.js +6 -1
- package/lib/logic/getBalance.js.map +1 -1
- package/lib/logic/getBalance.test.d.ts +2 -0
- package/lib/logic/getBalance.test.d.ts.map +1 -0
- package/lib/logic/getBalance.test.js +27 -0
- package/lib/logic/getBalance.test.js.map +1 -0
- package/lib/logic/listOperations.d.ts +2 -1
- package/lib/logic/listOperations.d.ts.map +1 -1
- package/lib/logic/listOperations.js +6 -0
- package/lib/logic/listOperations.js.map +1 -1
- package/lib/logic/listOperations.test.js +24 -1
- package/lib/logic/listOperations.test.js.map +1 -1
- package/lib/logic/tezosToolkit.d.ts.map +1 -1
- package/lib/logic/tezosToolkit.js +9 -4
- package/lib/logic/tezosToolkit.js.map +1 -1
- package/lib/types/assets.d.ts +3 -0
- package/lib/types/assets.d.ts.map +1 -0
- package/lib/types/assets.js +3 -0
- package/lib/types/assets.js.map +1 -0
- package/lib/types/index.d.ts +1 -0
- package/lib/types/index.d.ts.map +1 -1
- package/lib/types/index.js +1 -0
- package/lib/types/index.js.map +1 -1
- package/lib-es/api/index.d.ts +2 -1
- package/lib-es/api/index.d.ts.map +1 -1
- package/lib-es/api/index.integ.test.js +6 -1
- package/lib-es/api/index.integ.test.js.map +1 -1
- package/lib-es/api/index.js +3 -0
- package/lib-es/api/index.js.map +1 -1
- package/lib-es/api/index.test.js +19 -4
- package/lib-es/api/index.test.js.map +1 -1
- package/lib-es/logic/getBalance.d.ts +3 -1
- package/lib-es/logic/getBalance.d.ts.map +1 -1
- package/lib-es/logic/getBalance.js +6 -1
- package/lib-es/logic/getBalance.js.map +1 -1
- package/lib-es/logic/getBalance.test.d.ts +2 -0
- package/lib-es/logic/getBalance.test.d.ts.map +1 -0
- package/lib-es/logic/getBalance.test.js +22 -0
- package/lib-es/logic/getBalance.test.js.map +1 -0
- package/lib-es/logic/listOperations.d.ts +2 -1
- package/lib-es/logic/listOperations.d.ts.map +1 -1
- package/lib-es/logic/listOperations.js +6 -0
- package/lib-es/logic/listOperations.js.map +1 -1
- package/lib-es/logic/listOperations.test.js +24 -1
- package/lib-es/logic/listOperations.test.js.map +1 -1
- package/lib-es/logic/tezosToolkit.d.ts.map +1 -1
- package/lib-es/logic/tezosToolkit.js +9 -4
- package/lib-es/logic/tezosToolkit.js.map +1 -1
- package/lib-es/types/assets.d.ts +3 -0
- package/lib-es/types/assets.d.ts.map +1 -0
- package/lib-es/types/assets.js +2 -0
- package/lib-es/types/assets.js.map +1 -0
- package/lib-es/types/index.d.ts +1 -0
- package/lib-es/types/index.d.ts.map +1 -1
- package/lib-es/types/index.js +1 -0
- package/lib-es/types/index.js.map +1 -1
- package/package.json +7 -6
- package/src/api/index.integ.test.ts +8 -2
- package/src/api/index.test.ts +29 -10
- package/src/api/index.ts +12 -7
- package/src/logic/getBalance.test.ts +33 -0
- package/src/logic/getBalance.ts +9 -2
- package/src/logic/listOperations.test.ts +34 -13
- package/src/logic/listOperations.ts +10 -3
- package/src/logic/tezosToolkit.ts +9 -5
- package/src/types/assets.ts +3 -0
- package/src/types/index.ts +1 -0
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { getBalance } from "./getBalance";
|
|
2
|
+
import { setupServer } from "msw/node";
|
|
3
|
+
import coinConfig, { TezosCoinConfig } from "../config";
|
|
4
|
+
import { HttpResponse, http } from "msw";
|
|
5
|
+
|
|
6
|
+
describe("getBalance", () => {
|
|
7
|
+
const mockServer = setupServer();
|
|
8
|
+
coinConfig.setCoinConfig(
|
|
9
|
+
() =>
|
|
10
|
+
({
|
|
11
|
+
status: { type: "active" },
|
|
12
|
+
explorer: { url: "http://tezos.explorer.com" },
|
|
13
|
+
}) as unknown as TezosCoinConfig,
|
|
14
|
+
);
|
|
15
|
+
it("gets the balance of a Tezos account", async () => {
|
|
16
|
+
mockServer.listen({ onUnhandledRequest: "error" });
|
|
17
|
+
mockServer.use(
|
|
18
|
+
http.get("http://tezos.explorer.com/v1/accounts/tz1WvvbEGpBXGeTVbLiR6DYBe1izmgiYuZbq", () =>
|
|
19
|
+
HttpResponse.json({ type: "empty" }),
|
|
20
|
+
),
|
|
21
|
+
http.get("http://tezos.explorer.com/v1/accounts/tz1TzrmTBSuiVHV2VfMnGRMYvTEPCP42oSM8", () =>
|
|
22
|
+
HttpResponse.json({ type: "user", balance: 25 }),
|
|
23
|
+
),
|
|
24
|
+
);
|
|
25
|
+
|
|
26
|
+
expect(await getBalance("tz1WvvbEGpBXGeTVbLiR6DYBe1izmgiYuZbq")).toEqual([
|
|
27
|
+
{ value: BigInt(-1), asset: { type: "native" } },
|
|
28
|
+
]);
|
|
29
|
+
expect(await getBalance("tz1TzrmTBSuiVHV2VfMnGRMYvTEPCP42oSM8")).toEqual([
|
|
30
|
+
{ value: BigInt(25), asset: { type: "native" } },
|
|
31
|
+
]);
|
|
32
|
+
});
|
|
33
|
+
});
|
package/src/logic/getBalance.ts
CHANGED
|
@@ -1,10 +1,17 @@
|
|
|
1
|
+
import { Balance } from "@ledgerhq/coin-framework/api/types";
|
|
1
2
|
import api from "../network/tzkt";
|
|
3
|
+
import { TezosAsset } from "../types";
|
|
2
4
|
|
|
3
5
|
/**
|
|
4
6
|
* Retunrs the balance of the given address.
|
|
5
7
|
* If the address is an empty account, returns -1.
|
|
6
8
|
*/
|
|
7
|
-
export async function getBalance(address: string): Promise<
|
|
9
|
+
export async function getBalance(address: string): Promise<Balance<TezosAsset>[]> {
|
|
8
10
|
const apiAccount = await api.getAccountByAddress(address);
|
|
9
|
-
return
|
|
11
|
+
return [
|
|
12
|
+
{
|
|
13
|
+
value: apiAccount.type === "user" ? BigInt(apiAccount.balance) : BigInt(-1),
|
|
14
|
+
asset: { type: "native" },
|
|
15
|
+
},
|
|
16
|
+
];
|
|
10
17
|
}
|
|
@@ -50,6 +50,8 @@ describe("listOperations", () => {
|
|
|
50
50
|
storageFee: 1,
|
|
51
51
|
bakerFee: 2,
|
|
52
52
|
allocationFee: 3,
|
|
53
|
+
gasLimit: 4,
|
|
54
|
+
storageLimit: 5,
|
|
53
55
|
};
|
|
54
56
|
|
|
55
57
|
const undelegate: APIDelegationType = {
|
|
@@ -67,19 +69,38 @@ describe("listOperations", () => {
|
|
|
67
69
|
target: { address: someDestinationAddress },
|
|
68
70
|
};
|
|
69
71
|
|
|
70
|
-
it.each([
|
|
71
|
-
"
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
72
|
+
it.each([
|
|
73
|
+
["undelegate", undelegate],
|
|
74
|
+
["delegate", delegate],
|
|
75
|
+
["transfer", transfer],
|
|
76
|
+
])("should return %s operation with proper recipient list", async (_label, operation) => {
|
|
77
|
+
// Given
|
|
78
|
+
mockNetworkGetTransactions.mockResolvedValue([operation]);
|
|
79
|
+
// When
|
|
80
|
+
const [results, token] = await listOperations("any address", options);
|
|
81
|
+
// Then
|
|
82
|
+
expect(results.length).toEqual(1);
|
|
83
|
+
expect(results[0].recipients).toEqual([someDestinationAddress]);
|
|
84
|
+
expect(token).toEqual(JSON.stringify(operation.id));
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
it.each([
|
|
88
|
+
["undelegate", undelegate],
|
|
89
|
+
["delegate", delegate],
|
|
90
|
+
["transfer", transfer],
|
|
91
|
+
])("should return %s operation with expected details", async (_label, operation) => {
|
|
92
|
+
// Given
|
|
93
|
+
mockNetworkGetTransactions.mockResolvedValue([operation]);
|
|
94
|
+
// When
|
|
95
|
+
const [results, _] = await listOperations("any address", options);
|
|
96
|
+
// Then
|
|
97
|
+
expect(results.length).toEqual(1);
|
|
98
|
+
expect(results[0].details).toEqual({
|
|
99
|
+
counter: 65214462,
|
|
100
|
+
gasLimit: 4,
|
|
101
|
+
storageLimit: 5,
|
|
102
|
+
});
|
|
103
|
+
});
|
|
83
104
|
|
|
84
105
|
it.each([
|
|
85
106
|
{ ...undelegate, newDelegate: null, prevDelegate: null },
|
|
@@ -8,6 +8,7 @@ import {
|
|
|
8
8
|
isAPITransactionType,
|
|
9
9
|
} from "../network/types";
|
|
10
10
|
import { Operation } from "@ledgerhq/coin-framework/api/types";
|
|
11
|
+
import { TezosAsset } from "../types";
|
|
11
12
|
|
|
12
13
|
/**
|
|
13
14
|
* Returns list of "Transfer", "Delegate" and "Undelegate" Operations associated to an account.
|
|
@@ -29,7 +30,7 @@ export async function listOperations(
|
|
|
29
30
|
sort,
|
|
30
31
|
minHeight,
|
|
31
32
|
}: { limit?: number; token?: string; sort: "Ascending" | "Descending"; minHeight: number },
|
|
32
|
-
): Promise<[Operation<
|
|
33
|
+
): Promise<[Operation<TezosAsset>[], string]> {
|
|
33
34
|
let options: AccountsGetOperationsOptions = { limit, sort, "level.ge": minHeight };
|
|
34
35
|
if (token) {
|
|
35
36
|
options = { ...options, lastId: JSON.parse(token) };
|
|
@@ -41,7 +42,7 @@ export async function listOperations(
|
|
|
41
42
|
const nextToken = lastOperation ? JSON.stringify(lastOperation?.id) : "";
|
|
42
43
|
const filteredOperations = operations
|
|
43
44
|
.filter(op => isAPITransactionType(op) || isAPIDelegationType(op))
|
|
44
|
-
.reduce((acc, op) => acc.concat(convertOperation(address, op)), [] as Operation<
|
|
45
|
+
.reduce((acc, op) => acc.concat(convertOperation(address, op)), [] as Operation<TezosAsset>[]);
|
|
45
46
|
if (sort === "Ascending") {
|
|
46
47
|
//results are always sorted in descending order
|
|
47
48
|
filteredOperations.reverse();
|
|
@@ -53,7 +54,7 @@ export async function listOperations(
|
|
|
53
54
|
function convertOperation(
|
|
54
55
|
address: string,
|
|
55
56
|
operation: APITransactionType | APIDelegationType,
|
|
56
|
-
): Operation<
|
|
57
|
+
): Operation<TezosAsset> {
|
|
57
58
|
const { amount, hash, sender, type } = operation;
|
|
58
59
|
|
|
59
60
|
let targetAddress = undefined;
|
|
@@ -79,6 +80,7 @@ function convertOperation(
|
|
|
79
80
|
BigInt(operation.allocationFee ?? 0);
|
|
80
81
|
|
|
81
82
|
return {
|
|
83
|
+
asset: { type: "native" },
|
|
82
84
|
operationIndex: 0,
|
|
83
85
|
tx: {
|
|
84
86
|
// hash id defined nullable in the tzkt API, but I wonder when it would be null ?
|
|
@@ -96,5 +98,10 @@ function convertOperation(
|
|
|
96
98
|
value: BigInt(amount),
|
|
97
99
|
senders: senders,
|
|
98
100
|
recipients: recipients,
|
|
101
|
+
details: {
|
|
102
|
+
counter: operation.counter,
|
|
103
|
+
gasLimit: operation.gasLimit,
|
|
104
|
+
storageLimit: operation.storageLimit,
|
|
105
|
+
},
|
|
99
106
|
};
|
|
100
107
|
}
|
|
@@ -1,11 +1,15 @@
|
|
|
1
1
|
import { TezosToolkit } from "@taquito/taquito";
|
|
2
2
|
import coinConfig from "../config";
|
|
3
3
|
|
|
4
|
-
|
|
5
|
-
|
|
4
|
+
// Tezos client instance is cached to avoid costly rebuild at every request
|
|
5
|
+
// Watch out: cache key is the URL, coin module can be instantiated several times with different URLs
|
|
6
|
+
const servers = new Map<string, TezosToolkit>();
|
|
6
7
|
export function getTezosToolkit(): TezosToolkit {
|
|
7
|
-
|
|
8
|
-
|
|
8
|
+
const url = coinConfig.getCoinConfig().node.url;
|
|
9
|
+
let server = servers.get(url);
|
|
10
|
+
if (server === undefined) {
|
|
11
|
+
server = new TezosToolkit(url);
|
|
12
|
+
servers.set(url, server);
|
|
9
13
|
}
|
|
10
|
-
return
|
|
14
|
+
return server;
|
|
11
15
|
}
|
package/src/types/index.ts
CHANGED