From ba02bd012ff1c2706f96d61d23d9542271ccd573 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Prohaszka?= Date: Mon, 13 May 2024 17:18:02 +0200 Subject: [PATCH] fix: revert coin-fmk remove of areAllOperationsLoaded MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Stéphane Prohaszka --- .../src/account/helpers.test.ts | 38 ++++++++++++++++++- libs/coin-framework/src/account/helpers.ts | 12 ++++++ libs/coin-framework/src/account/index.ts | 1 + .../src/bridge/jsHelpers.test.ts | 18 ++++++--- .../src/account/helpers.test.ts | 38 ------------------- .../ledger-live-common/src/account/helpers.ts | 12 ------ 6 files changed, 62 insertions(+), 57 deletions(-) delete mode 100644 libs/ledger-live-common/src/account/helpers.test.ts diff --git a/libs/coin-framework/src/account/helpers.test.ts b/libs/coin-framework/src/account/helpers.test.ts index df270490299f..6bdc3c0319a0 100644 --- a/libs/coin-framework/src/account/helpers.test.ts +++ b/libs/coin-framework/src/account/helpers.test.ts @@ -1,6 +1,6 @@ import BigNumber from "bignumber.js"; import { CryptoCurrency, TokenCurrency } from "@ledgerhq/types-cryptoassets"; -import { Account, SubAccount, TokenAccount } from "@ledgerhq/types-live"; +import type { Account, Operation, SubAccount, TokenAccount } from "@ledgerhq/types-live"; import { getTokenById } from "@ledgerhq/cryptoassets/tokens"; import { emptyHistoryCache, @@ -8,7 +8,7 @@ import { getAccountSpendableBalance, getFeesCurrency, } from "."; -import { isAccountEmpty, clearAccount } from "./helpers"; +import { isAccountEmpty, clearAccount, areAllOperationsLoaded } from "./helpers"; const mockAccount = {} as Account; const tokenAccount = { @@ -219,3 +219,37 @@ describe(clearAccount.name, () => { }); }); }); + +describe(areAllOperationsLoaded.name, () => { + describe("given an account with subAccounts", () => { + beforeEach(() => { + mockAccount.type = "Account"; + mockAccount.operations = []; + mockAccount.operationsCount = 0; + mockAccount.subAccounts = [ + { + operations: [], + operationsCount: 0, + }, + { + operations: [{} as Operation], + operationsCount: 1, + }, + ] as SubAccount[]; + }); + describe("when sub account operation aren't loaded", () => { + beforeEach(() => { + (mockAccount.subAccounts as SubAccount[])[1].operations = []; + }); + it("should return false", () => { + expect(areAllOperationsLoaded(mockAccount)).toEqual(false); + }); + }); + + describe("when sub account operation are loaded", () => { + it("should return true", () => { + expect(areAllOperationsLoaded(mockAccount)).toEqual(true); + }); + }); + }); +}); diff --git a/libs/coin-framework/src/account/helpers.ts b/libs/coin-framework/src/account/helpers.ts index e096373548de..048e752b40f0 100644 --- a/libs/coin-framework/src/account/helpers.ts +++ b/libs/coin-framework/src/account/helpers.ts @@ -293,3 +293,15 @@ export function getParentAccount(account: AccountLike, accounts: AccountLike[]): } } } + +export function areAllOperationsLoaded(account: AccountLike): boolean { + if (account.operationsCount !== account.operations.length) { + return false; + } + + if (account.type === "Account" && account.subAccounts) { + return account.subAccounts.every(areAllOperationsLoaded); + } + + return true; +} diff --git a/libs/coin-framework/src/account/index.ts b/libs/coin-framework/src/account/index.ts index 489216df39ec..153a316ab44d 100644 --- a/libs/coin-framework/src/account/index.ts +++ b/libs/coin-framework/src/account/index.ts @@ -27,6 +27,7 @@ export { isAccount, isTokenAccount, getParentAccount, + areAllOperationsLoaded, } from "./helpers"; export { addPendingOperation } from "./pending"; export { getReceiveFlowError, checkAccountSupported } from "./support"; diff --git a/libs/coin-framework/src/bridge/jsHelpers.test.ts b/libs/coin-framework/src/bridge/jsHelpers.test.ts index 531987fb7592..6631d1550112 100644 --- a/libs/coin-framework/src/bridge/jsHelpers.test.ts +++ b/libs/coin-framework/src/bridge/jsHelpers.test.ts @@ -43,7 +43,11 @@ describe("jsHelpers", () => { describe("makeSync", () => { it("returns a function to update account that give a new instance of account", async () => { // Given - const account = createAccount("12"); + const account = createAccount({ + id: "12", + creationDate: new Date("2024-05-12T17:04:12"), + lastSyncDate: new Date("2024-05-12T17:04:12"), + }); // When const accountUpdater = makeSync({ @@ -68,7 +72,11 @@ describe("jsHelpers", () => { it("returns a account with a corrected formatted id", async () => { // Given - const account = createAccount("12"); + const account = createAccount({ + id: "12", + creationDate: new Date("2024-05-12T17:04:12"), + lastSyncDate: new Date("2024-05-12T17:04:12"), + }); // When const accountUpdater = makeSync({ @@ -105,12 +113,12 @@ const emptyHistoryCache = { // Call once for all tests the currencies. Relies on real implementation to check also consistency. const bitcoinCurrency = listCryptoCurrencies(true).find(c => c.id === "bitcoin")!; -function createAccount(id: string): Account { +function createAccount(init: Partial): Account { const currency = bitcoinCurrency; return { type: "Account", - id, + id: init.id ?? "12", seedIdentifier: "", derivationMode: "", index: 0, @@ -119,7 +127,7 @@ function createAccount(id: string): Account { used: true, balance: new BigNumber(0), spendableBalance: new BigNumber(0), - creationDate: new Date(), + creationDate: init.creationDate ?? new Date(), blockHeight: 0, currency, operationsCount: 0, diff --git a/libs/ledger-live-common/src/account/helpers.test.ts b/libs/ledger-live-common/src/account/helpers.test.ts deleted file mode 100644 index 4dd6c67c54a7..000000000000 --- a/libs/ledger-live-common/src/account/helpers.test.ts +++ /dev/null @@ -1,38 +0,0 @@ -import { Account, Operation, SubAccount } from "@ledgerhq/types-live"; -import { areAllOperationsLoaded } from "./helpers"; - -const mockAccount = {} as Account; - -describe(areAllOperationsLoaded.name, () => { - describe("given an account with subAccounts", () => { - beforeEach(() => { - mockAccount.type = "Account"; - mockAccount.operations = []; - mockAccount.operationsCount = 0; - mockAccount.subAccounts = [ - { - operations: [], - operationsCount: 0, - }, - { - operations: [{} as Operation], - operationsCount: 1, - }, - ] as SubAccount[]; - }); - describe("when sub account operation aren't loaded", () => { - beforeEach(() => { - (mockAccount.subAccounts as SubAccount[])[1].operations = []; - }); - it("should return false", () => { - expect(areAllOperationsLoaded(mockAccount)).toEqual(false); - }); - }); - - describe("when sub account operation are loaded", () => { - it("should return true", () => { - expect(areAllOperationsLoaded(mockAccount)).toEqual(true); - }); - }); - }); -}); diff --git a/libs/ledger-live-common/src/account/helpers.ts b/libs/ledger-live-common/src/account/helpers.ts index b7e35fd5f605..af1ef27af069 100644 --- a/libs/ledger-live-common/src/account/helpers.ts +++ b/libs/ledger-live-common/src/account/helpers.ts @@ -83,15 +83,3 @@ export const getVotesCount = ( return 0; } }; - -export function areAllOperationsLoaded(account: AccountLike): boolean { - if (account.operationsCount !== account.operations.length) { - return false; - } - - if (account.type === "Account" && account.subAccounts) { - return account.subAccounts.every(areAllOperationsLoaded); - } - - return true; -}