diff --git a/.changeset/young-jeans-sparkle.md b/.changeset/young-jeans-sparkle.md new file mode 100644 index 000000000000..03b95e2d7d8f --- /dev/null +++ b/.changeset/young-jeans-sparkle.md @@ -0,0 +1,5 @@ +--- +"@ledgerhq/live-common": minor +--- + +Update groupCurrenciesByProvider to get Coins instead of token when it's relevant diff --git a/libs/ledger-live-common/src/deposit/deposit.test.ts b/libs/ledger-live-common/src/deposit/deposit.test.ts index ede0915783e4..fbdd0fa6b00d 100644 --- a/libs/ledger-live-common/src/deposit/deposit.test.ts +++ b/libs/ledger-live-common/src/deposit/deposit.test.ts @@ -1,7 +1,7 @@ import { groupCurrenciesByProvider, searchByNameOrTicker, searchByProviderId } from "./helper"; -import { MOCK } from "./mock"; +import { MOCK, MOCK_POL } from "./mock"; import { MappedAsset } from "./type"; -import { getTokenById } from "../currencies/index"; +import { getCryptoCurrencyById, getTokenById } from "../currencies/index"; const MAPPED_ASSETS = MOCK as MappedAsset[]; @@ -25,5 +25,20 @@ describe("Deposit logic", () => { currenciesByNetwork: currencies, }, ]); + const currenciesPol = MOCK_POL.map(asset => + asset.$type === "Token" + ? getTokenById(asset.ledgerId) + : getCryptoCurrencyById(asset.ledgerId), + ); + const { currenciesByProvider: currenciesByProviderBis, sortedCryptoCurrencies } = + groupCurrenciesByProvider(MOCK_POL as MappedAsset[], currenciesPol); + expect(currenciesByProviderBis).toEqual([ + { + providerId: "matic-network", + currenciesByNetwork: currenciesPol, + }, + ]); + + expect(sortedCryptoCurrencies).toEqual([currenciesPol[1]]); }); }); diff --git a/libs/ledger-live-common/src/deposit/helper.ts b/libs/ledger-live-common/src/deposit/helper.ts index 784637205d34..dda904e22a97 100644 --- a/libs/ledger-live-common/src/deposit/helper.ts +++ b/libs/ledger-live-common/src/deposit/helper.ts @@ -36,13 +36,22 @@ export const groupCurrenciesByProvider = ( providerId: asset.providerId, currenciesByNetwork: [ledgerCurrency], }); - // in this case, the first currency of the provider is the one we want to display - sortedCryptoCurrencies.push(ledgerCurrency); } else { existingEntry.currenciesByNetwork.push(ledgerCurrency); } } } + + // in this case, the first currency of the provider is the one we want to display (Wasn't true) + // So we need to take the first crypto or token currency of each provider to fix that + for (const [, { currenciesByNetwork }] of assetsByProviderId.entries()) { + const firstCrypto = currenciesByNetwork.find(c => c.type === "CryptoCurrency"); + const elem = firstCrypto || currenciesByNetwork.find(c => c.type === "TokenCurrency"); + if (elem) { + sortedCryptoCurrencies.push(elem); + } + } + return { currenciesByProvider: Array.from(assetsByProviderId.values()), sortedCryptoCurrencies, diff --git a/libs/ledger-live-common/src/deposit/mock.ts b/libs/ledger-live-common/src/deposit/mock.ts index e7e5b311f249..770a768617d4 100644 --- a/libs/ledger-live-common/src/deposit/mock.ts +++ b/libs/ledger-live-common/src/deposit/mock.ts @@ -45,3 +45,49 @@ export const MOCK = [ ticker: "BSC-USD", }, ]; + +export const MOCK_POL = [ + { + $type: "Token", + ledgerId: "bsc/bep20/matic_token", + providerId: "matic-network", + name: "Matic Token", + ticker: "MATIC", + network: "bsc", + contract: "0xcc42724c6683b7e57334c4e856f4c9965ed682bd", + status: "Ok", + reason: null, + data: { + img: "https://proxycgassets.api.live.ledger.com/coins/images/4713/large/polygon.png", + marketCapRank: 121, + }, + }, + { + $type: "Coin", + ledgerId: "polygon", + providerId: "matic-network", + name: "Polygon", + ticker: "POL", + status: "Ok", + reason: "Overridden", + data: { + img: "https://proxycgassets.api.live.ledger.com/coins/images/4713/large/polygon.png", + marketCapRank: 121, + }, + }, + { + $type: "Token", + ledgerId: "ethereum/erc20/matic", + providerId: "matic-network", + name: "Matic", + ticker: "MATIC", + network: "ethereum", + contract: "0x7d1afa7b718fb893db30a3abc0cfc608aacfebb0", + status: "Ok", + reason: null, + data: { + img: "https://proxycgassets.api.live.ledger.com/coins/images/4713/large/polygon.png", + marketCapRank: 121, + }, + }, +];