Skip to content

Commit

Permalink
Move transaction serialization in correct directory (#6663)
Browse files Browse the repository at this point in the history
* chore: move transaction serialization functions into serialization dir

Signed-off-by: Stéphane Prohaszka <stephane.prohaszka@ledger.fr>

* fix: update unimported file

Signed-off-by: Stéphane Prohaszka <stephane.prohaszka@ledger.fr>

* chore: feedback

Signed-off-by: Stéphane Prohaszka <stephane.prohaszka@ledger.fr>

* chore: changeset

Signed-off-by: Stéphane Prohaszka <stephane.prohaszka@ledger.fr>

---------

Signed-off-by: Stéphane Prohaszka <stephane.prohaszka@ledger.fr>
  • Loading branch information
sprohaszka-ledger committed Apr 29, 2024
1 parent 83e5690 commit de5de2d
Show file tree
Hide file tree
Showing 29 changed files with 204 additions and 188 deletions.
11 changes: 11 additions & 0 deletions .changeset/cyan-points-obey.md
@@ -0,0 +1,11 @@
---
"@ledgerhq/coin-algorand": patch
"@ledgerhq/coin-polkadot": patch
"@ledgerhq/coin-bitcoin": patch
"@ledgerhq/coin-near": patch
"@ledgerhq/coin-evm": patch
"@ledgerhq/live-common": patch
"@ledgerhq/coin-framework": patch
---

Reorganize coin serializaiton code
4 changes: 3 additions & 1 deletion libs/coin-framework/.unimportedrc.json
Expand Up @@ -5,14 +5,16 @@
"src/bot/specs.ts",
"src/bot/formatters.ts",
"src/bridge/jsHelpers.ts",
"src/cache.ts",
"src/currencies/index.ts",
"src/derivation.ts",
"src/mocks/helpers.ts",
"src/mocks/account.ts",
"src/network.ts",
"src/nft/helpers.ts",
"src/nft/support.ts",
"src/cache.ts",
"src/formatters.ts",
"src/index.ts",
"src/operation.ts",
"src/signer.ts",
"src/transaction/common.ts"
Expand Down
30 changes: 14 additions & 16 deletions libs/coin-framework/package.json
Expand Up @@ -20,33 +20,23 @@
},
"typesVersions": {
"*": {
"*.json": [
"*.json"
],
"*": [
"lib/*"
],
"lib/*": [
"lib/*"
],
"lib-es/*": [
"lib-es/*"
],
"serialization": [
"lib/serialization/index"
],
"*": [
"lib/*"
]
}
},
"exports": {
"./lib/*": "./lib/*.js",
"./lib/*.js": "./lib/*.js",
"./lib-es/*": "./lib-es/*.js",
"./lib-es/*.js": "./lib-es/*.js",
"./*": {
"require": "./lib/*.js",
"default": "./lib-es/*.js"
},
"./*.js": {
"require": "./lib/*.js",
"default": "./lib-es/*.js"
},
"./account/*": {
"require": "./lib/account/*.js",
"default": "./lib-es/account/*.js"
Expand All @@ -71,10 +61,18 @@
"require": "./lib/nft/*.js",
"default": "./lib-es/nft/*.js"
},
"./serialization": {
"require": "./lib/serialization/index.js",
"default": "./lib-es/serialization/index.js"
},
"./transaction/*": {
"require": "./lib/transaction/*.js",
"default": "./lib-es/transaction/*.js"
},
"./*": {
"require": "./lib/*.js",
"default": "./lib-es/*.js"
},
"./package.json": "./package.json"
},
"license": "Apache-2.0",
Expand Down
49 changes: 49 additions & 0 deletions libs/coin-framework/src/formatters.ts
@@ -0,0 +1,49 @@
import type { Account, TransactionCommon, TransactionStatusCommon } from "@ledgerhq/types-live";
import { getAccountUnit } from "./account";
import { formatCurrencyUnit } from "./currencies";

const formatErrorSmall = (e: Error): string => (e.name === "Error" ? e.message : e.name);

export const formatTransactionStatus = (
t: TransactionCommon,
{ errors, warnings, estimatedFees, amount, totalSpent }: TransactionStatusCommon,
mainAccount: Account,
): string => {
let str = "";
const account =
(t.subAccountId && (mainAccount.subAccounts || []).find(a => a.id === t.subAccountId)) ||
mainAccount;

str +=
"\n amount: " +
formatCurrencyUnit(getAccountUnit(account), amount, {
showCode: true,
disableRounding: true,
});
str +=
"\n estimated fees: " +
formatCurrencyUnit(getAccountUnit(mainAccount), estimatedFees, {
showCode: true,
disableRounding: true,
});
str +=
"\n total spent: " +
formatCurrencyUnit(getAccountUnit(account), totalSpent, {
showCode: true,
disableRounding: true,
});

str +=
"\n" +
`errors: ${Object.entries(errors)
.map(([key, error]) => `${key} ${formatErrorSmall(error)}`)
.join(", ")}`;

str +=
"\n" +
`warnings: ${Object.entries(warnings)
.map(([key, warning]) => `${key} ${formatErrorSmall(warning)}`)
.join(", ")}`;

return str;
};
5 changes: 5 additions & 0 deletions libs/coin-framework/src/index.ts
@@ -0,0 +1,5 @@
import * as serialization from "./serialization";

export default {
serialization,
};
1 change: 1 addition & 0 deletions libs/coin-framework/src/serialization/index.ts
@@ -1,3 +1,4 @@
export { fromAccountRaw, toAccountRaw } from "./account";
export * from "./nft";
export * from "./operation";
export * from "./transaction";
79 changes: 79 additions & 0 deletions libs/coin-framework/src/serialization/transaction.ts
@@ -0,0 +1,79 @@
import { deserializeError, serializeError } from "@ledgerhq/errors";
import type {
TransactionCommon,
TransactionCommonRaw,
TransactionStatusCommon,
TransactionStatusCommonRaw,
} from "@ledgerhq/types-live";
import { BigNumber } from "bignumber.js";
import mapValues from "lodash/mapValues";

export const fromTransactionCommonRaw = (raw: TransactionCommonRaw): TransactionCommon => {
const common: TransactionCommon = {
amount: new BigNumber(raw.amount),
recipient: raw.recipient,
};

if ("useAllAmount" in raw) {
common.useAllAmount = raw.useAllAmount;
}

if ("subAccountId" in raw) {
common.subAccountId = raw.subAccountId;
}

if ("recipientDomain" in raw) {
common.recipientDomain = raw.recipientDomain;
}

return common;
};

export const toTransactionCommonRaw = (raw: TransactionCommon): TransactionCommonRaw => {
const common: TransactionCommonRaw = {
amount: raw.amount.toString(),
recipient: raw.recipient,
};

if ("useAllAmount" in raw) {
common.useAllAmount = raw.useAllAmount;
}

if ("subAccountId" in raw) {
common.subAccountId = raw.subAccountId;
}

if ("recipientDomain" in raw) {
common.recipientDomain = raw.recipientDomain;
}

return common;
};

const fromErrorRaw = (raw: string): Error => {
return deserializeError(JSON.parse(raw)) || new Error("unknown reason");
};

const toErrorRaw = (raw: Error): string => JSON.stringify(serializeError(raw)) || "{}";

export const fromTransactionStatusRawCommon = (
ts: TransactionStatusCommonRaw,
): TransactionStatusCommon => ({
errors: mapValues(ts.errors, fromErrorRaw),
warnings: mapValues(ts.warnings, fromErrorRaw),
estimatedFees: new BigNumber(ts.estimatedFees),
amount: new BigNumber(ts.amount),
totalSpent: new BigNumber(ts.totalSpent),
recipientIsReadOnly: ts.recipientIsReadOnly,
});

export const toTransactionStatusRawCommon = (
ts: TransactionStatusCommon,
): TransactionStatusCommonRaw => ({
errors: mapValues<Record<string, Error>, string>(ts.errors, toErrorRaw),
warnings: mapValues<Record<string, Error>, string>(ts.warnings, toErrorRaw),
estimatedFees: ts.estimatedFees.toString(),
amount: ts.amount.toString(),
totalSpent: ts.totalSpent.toString(),
recipientIsReadOnly: ts.recipientIsReadOnly,
});
129 changes: 0 additions & 129 deletions libs/coin-framework/src/transaction/common.ts
@@ -1,16 +1,3 @@
import { deserializeError, serializeError } from "@ledgerhq/errors";
import type {
Account,
TransactionCommon,
TransactionCommonRaw,
TransactionStatusCommon,
TransactionStatusCommonRaw,
} from "@ledgerhq/types-live";
import { BigNumber } from "bignumber.js";
import mapValues from "lodash/mapValues";
import { getAccountUnit } from "../account";
import { formatCurrencyUnit } from "../currencies";

type tooltipArgs = Record<string, string>;
export type CommonDeviceTransactionField =
| {
Expand All @@ -33,119 +20,3 @@ export type CommonDeviceTransactionField =
tooltipI18nKey?: string;
tooltipI18nArgs?: tooltipArgs;
};

export const fromTransactionCommonRaw = (raw: TransactionCommonRaw): TransactionCommon => {
const common: TransactionCommon = {
amount: new BigNumber(raw.amount),
recipient: raw.recipient,
};

if ("useAllAmount" in raw) {
common.useAllAmount = raw.useAllAmount;
}

if ("subAccountId" in raw) {
common.subAccountId = raw.subAccountId;
}

if ("recipientDomain" in raw) {
common.recipientDomain = raw.recipientDomain;
}

return common;
};

export const toTransactionCommonRaw = (raw: TransactionCommon): TransactionCommonRaw => {
const common: TransactionCommonRaw = {
amount: raw.amount.toString(),
recipient: raw.recipient,
};

if ("useAllAmount" in raw) {
common.useAllAmount = raw.useAllAmount;
}

if ("subAccountId" in raw) {
common.subAccountId = raw.subAccountId;
}

if ("recipientDomain" in raw) {
common.recipientDomain = raw.recipientDomain;
}

return common;
};

const fromErrorRaw = (raw: string): Error => {
return deserializeError(JSON.parse(raw)) || new Error("unknown reason");
};

const toErrorRaw = (raw: Error): string => JSON.stringify(serializeError(raw)) || "{}";

export const fromTransactionStatusRawCommon = (
ts: TransactionStatusCommonRaw,
): TransactionStatusCommon => ({
errors: mapValues(ts.errors, fromErrorRaw),
warnings: mapValues(ts.warnings, fromErrorRaw),
estimatedFees: new BigNumber(ts.estimatedFees),
amount: new BigNumber(ts.amount),
totalSpent: new BigNumber(ts.totalSpent),
recipientIsReadOnly: ts.recipientIsReadOnly,
});

export const toTransactionStatusRawCommon = (
ts: TransactionStatusCommon,
): TransactionStatusCommonRaw => ({
errors: mapValues<Record<string, Error>, string>(ts.errors, toErrorRaw),
warnings: mapValues<Record<string, Error>, string>(ts.warnings, toErrorRaw),
estimatedFees: ts.estimatedFees.toString(),
amount: ts.amount.toString(),
totalSpent: ts.totalSpent.toString(),
recipientIsReadOnly: ts.recipientIsReadOnly,
});

const formatErrorSmall = (e: Error): string => (e.name === "Error" ? e.message : e.name);

export const formatTransactionStatusCommon = (
t: TransactionCommon,
{ errors, warnings, estimatedFees, amount, totalSpent }: TransactionStatusCommon,
mainAccount: Account,
): string => {
let str = "";
const account =
(t.subAccountId && (mainAccount.subAccounts || []).find(a => a.id === t.subAccountId)) ||
mainAccount;

str +=
"\n amount: " +
formatCurrencyUnit(getAccountUnit(account), amount, {
showCode: true,
disableRounding: true,
});
str +=
"\n estimated fees: " +
formatCurrencyUnit(getAccountUnit(mainAccount), estimatedFees, {
showCode: true,
disableRounding: true,
});
str +=
"\n total spent: " +
formatCurrencyUnit(getAccountUnit(account), totalSpent, {
showCode: true,
disableRounding: true,
});

str +=
"\n" +
`errors: ${Object.entries(errors)
.map(([key, error]) => `${key} ${formatErrorSmall(error)}`)
.join(", ")}`;

str +=
"\n" +
`errors: ${Object.entries(warnings)
.map(([key, warning]) => `${key} ${formatErrorSmall(warning)}`)
.join(", ")}`;

return str;
};
4 changes: 2 additions & 2 deletions libs/coin-modules/coin-algorand/src/transaction.ts
@@ -1,12 +1,12 @@
import { getAccountUnit } from "@ledgerhq/coin-framework/account/index";
import { formatCurrencyUnit } from "@ledgerhq/coin-framework/currencies/index";
import { formatTransactionStatus } from "@ledgerhq/coin-framework/formatters";
import {
formatTransactionStatusCommon as formatTransactionStatus,
fromTransactionCommonRaw,
fromTransactionStatusRawCommon as fromTransactionStatusRaw,
toTransactionCommonRaw,
toTransactionStatusRawCommon as toTransactionStatusRaw,
} from "@ledgerhq/coin-framework/transaction/common";
} from "@ledgerhq/coin-framework/serialization";
import type { Account } from "@ledgerhq/types-live";
import { BigNumber } from "bignumber.js";
import type { AlgorandTransaction, AlgorandTransactionRaw } from "./types";
Expand Down
4 changes: 2 additions & 2 deletions libs/coin-modules/coin-bitcoin/src/transaction.ts
Expand Up @@ -10,13 +10,13 @@ import type {
} from "./types";
import { bitcoinPickingStrategy } from "./types";
import { getEnv } from "@ledgerhq/live-env";
import { formatTransactionStatus as formatTransactionStatusCommon } from "@ledgerhq/coin-framework/formatters";
import {
formatTransactionStatusCommon,
fromTransactionCommonRaw,
fromTransactionStatusRawCommon,
toTransactionCommonRaw,
toTransactionStatusRawCommon,
} from "@ledgerhq/coin-framework/transaction/common";
} from "@ledgerhq/coin-framework/serialization";
import { getAccountUnit } from "@ledgerhq/coin-framework/account/index";
import { formatCurrencyUnit } from "@ledgerhq/coin-framework/currencies/index";
import type { Account } from "@ledgerhq/types-live";
Expand Down

0 comments on commit de5de2d

Please sign in to comment.