-
Notifications
You must be signed in to change notification settings - Fork 315
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move transaction serialization in correct directory (#6663)
* chore: move transaction serialization functions into serialization dir Signed-off-by: Stéphane Prohaszka <[email protected]> * fix: update unimported file Signed-off-by: Stéphane Prohaszka <[email protected]> * chore: feedback Signed-off-by: Stéphane Prohaszka <[email protected]> * chore: changeset Signed-off-by: Stéphane Prohaszka <[email protected]> --------- Signed-off-by: Stéphane Prohaszka <[email protected]>
- Loading branch information
1 parent
83e5690
commit de5de2d
Showing
29 changed files
with
204 additions
and
188 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import * as serialization from "./serialization"; | ||
|
||
export default { | ||
serialization, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export { fromAccountRaw, toAccountRaw } from "./account"; | ||
export * from "./nft"; | ||
export * from "./operation"; | ||
export * from "./transaction"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.