@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.
Files changed (78) hide show
  1. package/CHANGELOG.md +22 -0
  2. package/lib/api/index.d.ts +2 -1
  3. package/lib/api/index.d.ts.map +1 -1
  4. package/lib/api/index.integ.test.js +6 -1
  5. package/lib/api/index.integ.test.js.map +1 -1
  6. package/lib/api/index.js +3 -0
  7. package/lib/api/index.js.map +1 -1
  8. package/lib/api/index.test.js +19 -4
  9. package/lib/api/index.test.js.map +1 -1
  10. package/lib/logic/getBalance.d.ts +3 -1
  11. package/lib/logic/getBalance.d.ts.map +1 -1
  12. package/lib/logic/getBalance.js +6 -1
  13. package/lib/logic/getBalance.js.map +1 -1
  14. package/lib/logic/getBalance.test.d.ts +2 -0
  15. package/lib/logic/getBalance.test.d.ts.map +1 -0
  16. package/lib/logic/getBalance.test.js +27 -0
  17. package/lib/logic/getBalance.test.js.map +1 -0
  18. package/lib/logic/listOperations.d.ts +2 -1
  19. package/lib/logic/listOperations.d.ts.map +1 -1
  20. package/lib/logic/listOperations.js +6 -0
  21. package/lib/logic/listOperations.js.map +1 -1
  22. package/lib/logic/listOperations.test.js +24 -1
  23. package/lib/logic/listOperations.test.js.map +1 -1
  24. package/lib/logic/tezosToolkit.d.ts.map +1 -1
  25. package/lib/logic/tezosToolkit.js +9 -4
  26. package/lib/logic/tezosToolkit.js.map +1 -1
  27. package/lib/types/assets.d.ts +3 -0
  28. package/lib/types/assets.d.ts.map +1 -0
  29. package/lib/types/assets.js +3 -0
  30. package/lib/types/assets.js.map +1 -0
  31. package/lib/types/index.d.ts +1 -0
  32. package/lib/types/index.d.ts.map +1 -1
  33. package/lib/types/index.js +1 -0
  34. package/lib/types/index.js.map +1 -1
  35. package/lib-es/api/index.d.ts +2 -1
  36. package/lib-es/api/index.d.ts.map +1 -1
  37. package/lib-es/api/index.integ.test.js +6 -1
  38. package/lib-es/api/index.integ.test.js.map +1 -1
  39. package/lib-es/api/index.js +3 -0
  40. package/lib-es/api/index.js.map +1 -1
  41. package/lib-es/api/index.test.js +19 -4
  42. package/lib-es/api/index.test.js.map +1 -1
  43. package/lib-es/logic/getBalance.d.ts +3 -1
  44. package/lib-es/logic/getBalance.d.ts.map +1 -1
  45. package/lib-es/logic/getBalance.js +6 -1
  46. package/lib-es/logic/getBalance.js.map +1 -1
  47. package/lib-es/logic/getBalance.test.d.ts +2 -0
  48. package/lib-es/logic/getBalance.test.d.ts.map +1 -0
  49. package/lib-es/logic/getBalance.test.js +22 -0
  50. package/lib-es/logic/getBalance.test.js.map +1 -0
  51. package/lib-es/logic/listOperations.d.ts +2 -1
  52. package/lib-es/logic/listOperations.d.ts.map +1 -1
  53. package/lib-es/logic/listOperations.js +6 -0
  54. package/lib-es/logic/listOperations.js.map +1 -1
  55. package/lib-es/logic/listOperations.test.js +24 -1
  56. package/lib-es/logic/listOperations.test.js.map +1 -1
  57. package/lib-es/logic/tezosToolkit.d.ts.map +1 -1
  58. package/lib-es/logic/tezosToolkit.js +9 -4
  59. package/lib-es/logic/tezosToolkit.js.map +1 -1
  60. package/lib-es/types/assets.d.ts +3 -0
  61. package/lib-es/types/assets.d.ts.map +1 -0
  62. package/lib-es/types/assets.js +2 -0
  63. package/lib-es/types/assets.js.map +1 -0
  64. package/lib-es/types/index.d.ts +1 -0
  65. package/lib-es/types/index.d.ts.map +1 -1
  66. package/lib-es/types/index.js +1 -0
  67. package/lib-es/types/index.js.map +1 -1
  68. package/package.json +7 -6
  69. package/src/api/index.integ.test.ts +8 -2
  70. package/src/api/index.test.ts +29 -10
  71. package/src/api/index.ts +12 -7
  72. package/src/logic/getBalance.test.ts +33 -0
  73. package/src/logic/getBalance.ts +9 -2
  74. package/src/logic/listOperations.test.ts +34 -13
  75. package/src/logic/listOperations.ts +10 -3
  76. package/src/logic/tezosToolkit.ts +9 -5
  77. package/src/types/assets.ts +3 -0
  78. 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
+ });
@@ -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<bigint> {
9
+ export async function getBalance(address: string): Promise<Balance<TezosAsset>[]> {
8
10
  const apiAccount = await api.getAccountByAddress(address);
9
- return apiAccount.type === "user" ? BigInt(apiAccount.balance) : BigInt(-1);
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([undelegate, delegate, transfer])(
71
- "should return operation with proper recipient list",
72
- async operation => {
73
- // Given
74
- mockNetworkGetTransactions.mockResolvedValue([operation]);
75
- // When
76
- const [results, token] = await listOperations("any address", options);
77
- // Then
78
- expect(results.length).toEqual(1);
79
- expect(results[0].recipients).toEqual([someDestinationAddress]);
80
- expect(token).toEqual(JSON.stringify(operation.id));
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<void>[], string]> {
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<void>[]);
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<void> {
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
- let tezos: TezosToolkit | null = null;
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
- if (!tezos) {
8
- tezos = new TezosToolkit(coinConfig.getCoinConfig().node.url);
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 tezos;
14
+ return server;
11
15
  }
@@ -0,0 +1,3 @@
1
+ import { Asset } from "@ledgerhq/coin-framework/api/types";
2
+
3
+ export type TezosAsset = Asset;
@@ -1,3 +1,4 @@
1
+ export * from "./assets";
1
2
  export * from "./bridge";
2
3
  export * from "./errors";
3
4
  export * from "./model";