@ledgerhq/coin-framework 0.3.0 → 0.3.1-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 CHANGED
@@ -1,5 +1,18 @@
1
1
  # @ledgerhq/coin-framework
2
2
 
3
+ ## 0.3.1-next.0
4
+
5
+ ### Patch Changes
6
+
7
+ - [#2817](https://github.com/LedgerHQ/ledger-live/pull/2817) [`d5cf1abc6e`](https://github.com/LedgerHQ/ledger-live/commit/d5cf1abc6eb30d399c83b827452dc4bc61fd2253) Thanks [@thomasrogerlux](https://github.com/thomasrogerlux)! - Add Stax-enabled NFTs in the mock fixtures
8
+
9
+ - [#3164](https://github.com/LedgerHQ/ledger-live/pull/3164) [`be5589dac6`](https://github.com/LedgerHQ/ledger-live/commit/be5589dac675e2c8c1771b135bd0f330a868ed2d) Thanks [@haammar-ledger](https://github.com/haammar-ledger)! - Add a 'feesCurrency' field to 'Account' type, and use it
10
+
11
+ - Updated dependencies [[`3242a0a794`](https://github.com/LedgerHQ/ledger-live/commit/3242a0a7948c20fb0100ce3cc73e55e338534d32), [`be5589dac6`](https://github.com/LedgerHQ/ledger-live/commit/be5589dac675e2c8c1771b135bd0f330a868ed2d), [`d5cf1abc6e`](https://github.com/LedgerHQ/ledger-live/commit/d5cf1abc6eb30d399c83b827452dc4bc61fd2253)]:
12
+ - @ledgerhq/cryptoassets@9.4.0-next.0
13
+ - @ledgerhq/types-live@6.32.1-next.0
14
+ - @ledgerhq/live-portfolio@0.0.3-next.0
15
+
3
16
  ## 0.3.0
4
17
 
5
18
  ### Minor Changes
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ledgerhq/coin-framework",
3
- "version": "0.3.0",
3
+ "version": "0.3.1-next.0",
4
4
  "description": "Ledger framework for Coin integration",
5
5
  "keywords": [
6
6
  "Ledger",
@@ -61,16 +61,16 @@
61
61
  "prando": "^6.0.1",
62
62
  "rxjs": "^6.6.7",
63
63
  "rxjs-compat": "^6.6.7",
64
- "@ledgerhq/cryptoassets": "^9.3.0",
64
+ "@ledgerhq/cryptoassets": "^9.4.0-next.0",
65
65
  "@ledgerhq/devices": "^8.0.1",
66
66
  "@ledgerhq/errors": "^6.12.4",
67
67
  "@ledgerhq/hw-transport": "^6.28.2",
68
68
  "@ledgerhq/live-env": "^0.1.0",
69
- "@ledgerhq/live-portfolio": "^0.0.2",
69
+ "@ledgerhq/live-portfolio": "^0.0.3-next.0",
70
70
  "@ledgerhq/live-promise": "^0.0.1",
71
71
  "@ledgerhq/logs": "^6.10.1",
72
72
  "@ledgerhq/types-cryptoassets": "^7.1.0",
73
- "@ledgerhq/types-live": "^6.32.0"
73
+ "@ledgerhq/types-live": "^6.32.1-next.0"
74
74
  },
75
75
  "devDependencies": {
76
76
  "@types/invariant": "^2.2.2",
@@ -90,7 +90,7 @@
90
90
  "prettier": "^2.8.1",
91
91
  "timemachine": "^0.3.2",
92
92
  "ts-jest": "^28.0.5",
93
- "typescript": "^4.9.4"
93
+ "typescript": "^4.9.5"
94
94
  },
95
95
  "scripts": {
96
96
  "clean": "rm -rf lib lib-es",
@@ -18,6 +18,7 @@ import {
18
18
  getAccountName,
19
19
  getAccountSpendableBalance,
20
20
  getAccountUnit,
21
+ getFeesCurrency,
21
22
  } from ".";
22
23
  import {
23
24
  isAccountEmpty,
@@ -135,6 +136,65 @@ describe(getAccountUnit.name, () => {
135
136
  });
136
137
  });
137
138
 
139
+ describe(getFeesCurrency.name, () => {
140
+ describe("given an Account", () => {
141
+ beforeEach(() => {
142
+ mockAccount.type = "Account";
143
+ });
144
+ describe("without feesCurrency", () => {
145
+ it("should return the account's currency", () => {
146
+ const sampleCurrency = { family: "bitcoin" } as CryptoCurrency;
147
+ mockAccount.currency = sampleCurrency;
148
+ expect(getFeesCurrency(mockAccount)).toEqual(sampleCurrency);
149
+ });
150
+ });
151
+ describe("with feesCurrency", () => {
152
+ it("should return the fees currency", () => {
153
+ const sampleCurrency = { family: "vechain" } as CryptoCurrency;
154
+ const sampleFeesCurrency = { id: "VTHO" } as TokenCurrency;
155
+ mockAccount.currency = sampleCurrency;
156
+ mockAccount.feesCurrency = sampleFeesCurrency;
157
+ expect(getFeesCurrency(mockAccount)).toEqual(sampleFeesCurrency);
158
+ });
159
+ });
160
+ });
161
+
162
+ describe("given a ChildAccount", () => {
163
+ it("should return the currency", () => {
164
+ const sampleCurrency = { family: "bitcoin" } as CryptoCurrency;
165
+ childAccount.currency = sampleCurrency;
166
+ expect(getFeesCurrency(childAccount)).toEqual(sampleCurrency);
167
+ });
168
+ });
169
+
170
+ describe("given a TokenAccount", () => {
171
+ it("should return the token currency", () => {
172
+ const sampleToken = { id: "tokenId" } as TokenCurrency;
173
+ tokenAccount.token = sampleToken;
174
+ expect(getFeesCurrency(tokenAccount)).toEqual(sampleToken);
175
+ });
176
+ });
177
+
178
+ describe("given an unknown type Account", () => {
179
+ beforeEach(() => {
180
+ (mockAccount as any).type = "DefinitelyNotAStandardAccount";
181
+ });
182
+
183
+ it("should throw an error", () => {
184
+ expect(() => getFeesCurrency(mockAccount)).toThrow(Error);
185
+ });
186
+
187
+ it("should display the account type in the error message", () => {
188
+ expect.assertions(1);
189
+ try {
190
+ getFeesCurrency(mockAccount);
191
+ } catch (e: unknown) {
192
+ expect((e as Error).message.includes(mockAccount.type)).toEqual(true);
193
+ }
194
+ });
195
+ });
196
+ });
197
+
138
198
  describe(getAccountName.name, () => {
139
199
  describe("given an Account", () => {
140
200
  beforeEach(() => {
@@ -17,9 +17,9 @@ import {
17
17
  Unit,
18
18
  } from "@ledgerhq/types-cryptoassets";
19
19
 
20
- // by convention, a main account is the top level account
21
- // in case of an Account is the account itself
22
- // in case of a TokenAccount it's the parentAccount
20
+ // By convention, a main account is the top level account
21
+ // - in case of an Account is the account itself
22
+ // - in case of a SubAccount it's the parentAccount
23
23
  export const getMainAccount = (
24
24
  account: AccountLike,
25
25
  parentAccount?: Account | null | undefined
@@ -29,6 +29,32 @@ export const getMainAccount = (
29
29
  return mainAccount as Account;
30
30
  };
31
31
 
32
+ // Return the currency in which fees are paid for this account
33
+ export const getFeesCurrency = (
34
+ account?: AccountLike
35
+ ): TokenCurrency | CryptoCurrency => {
36
+ switch (account?.type) {
37
+ case "Account":
38
+ return account.feesCurrency || account.currency;
39
+
40
+ case "ChildAccount":
41
+ return account.currency;
42
+
43
+ case "TokenAccount":
44
+ return account.token;
45
+
46
+ default:
47
+ throw new Error(
48
+ "invalid account.type=" + (account as unknown as { type: string })?.type
49
+ );
50
+ }
51
+ };
52
+
53
+ // Return the unit to use with the fees currency
54
+ export const getFeesUnit = (currency: TokenCurrency | CryptoCurrency): Unit => {
55
+ return currency.units[0];
56
+ };
57
+
32
58
  export const getAccountCurrency = (
33
59
  account?: AccountLike
34
60
  ): TokenCurrency | CryptoCurrency => {
@@ -65,7 +65,6 @@ const hardcodedMarketcap = [
65
65
  "tezos",
66
66
  "iota",
67
67
  "ethereum/erc20/link_chainlink",
68
- "neo",
69
68
  "ethereum/erc20/makerdao",
70
69
  "ethereum/erc20/usd__coin",
71
70
  "ontology",
@@ -391,7 +390,8 @@ export function genAccount(
391
390
  ...(withNft && {
392
391
  nfts: Array(10)
393
392
  .fill(null)
394
- .map(() => createFixtureNFT(accountId, currency)),
393
+ // The index === 0 ensure at least one NFT is a Stax NFT if the currency is Ethereum
394
+ .map((_, index) => createFixtureNFT(accountId, currency, index === 0)),
395
395
  }),
396
396
  };
397
397
 
@@ -307,6 +307,19 @@ export const NFTs = [
307
307
  },
308
308
  ];
309
309
 
310
+ // Ethereum NFTs with the special "staxImage" metadata designed to fit the Ledger Stax screen
311
+ export const NFTs_ETHEREUM_STAX_METADATA = [
312
+ {
313
+ id: "js:2:ethereum:0xB98d10d9f6d07bA283bFD21B2dFEc050f9Ae282A:+0xf4ac11a8967bc88c9ce5acf886bce605c9db9d6e+8482",
314
+ tokenId: "8482",
315
+ amount: "1",
316
+ collection: {
317
+ contract: "0xf4ac11a8967bc88c9ce5acf886bce605c9db9d6e",
318
+ standard: "ERC721",
319
+ },
320
+ },
321
+ ];
322
+
310
323
  export const NFTs_POLYGON = [
311
324
  {
312
325
  id: "js:2:ethereum:0xB98d10d9f6d07bA283bFD21B2dFEc050f9Ae282A:+0x68a0B29526f342de944BBd6bF61D9c644B96b771+7",
@@ -340,9 +353,15 @@ export const NFTs_POLYGON = [
340
353
  ];
341
354
  export function createFixtureNFT(
342
355
  accountId: string,
343
- currency: CryptoCurrency = defaultEthCryptoFamily
356
+ currency: CryptoCurrency = defaultEthCryptoFamily,
357
+ useStaxNFTs?: boolean
344
358
  ): ProtoNFT {
345
- const nfts = currency.id === "ethereum" ? NFTs : NFTs_POLYGON;
359
+ const nfts =
360
+ currency.id === "ethereum"
361
+ ? useStaxNFTs
362
+ ? NFTs_ETHEREUM_STAX_METADATA
363
+ : NFTs
364
+ : NFTs_POLYGON;
346
365
  const index = Math.floor(Math.random() * nfts.length);
347
366
 
348
367
  const nft = nfts[index];