From e225e0d6a062f1e4a98a804e052434bad973fc38 Mon Sep 17 00:00:00 2001 From: karczuRF <88723742+karczuRF@users.noreply.github.com> Date: Fri, 29 Nov 2024 12:01:28 +0100 Subject: [PATCH] fix(ui): show tx simulation (#147) Description --- Simulation dialog didn't show the updated balances and shows 'failure' status every time. Error wasn't informative. Before: [Screencast from 21.11.2024 10:55:22.webm](https://github.com/user-attachments/assets/c4005d53-77c1-43ed-9a06-cad1b0f27de7) After: [Screencast from 21.11.2024 10:49:15.webm](https://github.com/user-attachments/assets/ed212139-8c64-472b-bd86-6dc6fe47066e) Motivation and Context --- Solves #101 How Has This Been Tested? --- What process can a PR reviewer use to test or verify this change? --- Breaking Changes --- - [x] None - [ ] Requires data directory on base node to be deleted - [ ] Requires hard fork - [ ] Other - Please specify --- locales/en/components.json | 5 +- locales/pl/components.json | 5 +- .../TransactionConfirmationModal.tsx | 50 +++++------- src/helpers/transaction.ts | 31 ++++++++ src/store/provider/provider.action.ts | 76 ++++++++++++++----- src/store/simulation/simulation.action.ts | 27 ++++++- src/store/simulation/simulation.slice.ts | 23 +++++- src/store/simulation/simulation.types.ts | 10 +++ src/store/transaction/transaction.types.ts | 4 +- 9 files changed, 172 insertions(+), 59 deletions(-) create mode 100644 src/helpers/transaction.ts diff --git a/locales/en/components.json b/locales/en/components.json index cb5fd8c..607901e 100644 --- a/locales/en/components.json +++ b/locales/en/components.json @@ -18,5 +18,8 @@ "create-account": "New account", "open-logs-directory": "Logs", "simulation-status": "Simulation status", - "simulation-error-msg": "Simulation error" + "simulation-error-msg": "Simulation error message", + "tx-simulation-status": "Simulated transaction status", + "tx-simulation-error-msg": "Simulated tx error message", + "no-balance-update": "No balance updates available." } diff --git a/locales/pl/components.json b/locales/pl/components.json index e9c7946..0b80289 100644 --- a/locales/pl/components.json +++ b/locales/pl/components.json @@ -18,5 +18,8 @@ "create-account": "Dodaj nowe konto", "open-logs-directory": "Logi", "simulation-status": "Status symulacji", - "simulation-error-msg": "Błąd symulacji" + "simulation-error-msg": "Błąd symulacji", + "tx-simulation-status": "Status symulowanej transakcji", + "tx-simulation-error-msg": "Błąd symulowanej transakcji", + "no-balance-update": "Aktualizacja balansów niedostępna" } diff --git a/src/components/TransactionConfirmationModal.tsx b/src/components/TransactionConfirmationModal.tsx index 145b6ca..9ae8a9a 100644 --- a/src/components/TransactionConfirmationModal.tsx +++ b/src/components/TransactionConfirmationModal.tsx @@ -10,9 +10,9 @@ import { simulationActions } from "../store/simulation/simulation.slice" import { BalanceUpdateView } from "./BalanceUpdate" import { useTranslation } from "react-i18next" import { ErrorSource } from "../store/error/error.types" -import { CallFunction, CallMethod } from "@tari-project/tarijs/dist/builders/types/Instruction" import { resolveBackendErrorMessage } from "./ErrorSnackBar" import { metadataSelector } from "../store/metadata/metadata.selector" +import { getFunctionOrMethod, getTransactionStatusName } from "../helpers/transaction" const selectSimulationById = (state: RootState, id?: number) => (id ? simulationsSelectors.selectById(state, id) : null) @@ -50,31 +50,6 @@ export const TransactionConfirmationModal: React.FC = () => { ) } - interface InstructionWithArgs { - instructionName: string - args: any[] - } - // Function to get function or method fields - function getFunctionOrMethod(instructions: object[]): InstructionWithArgs[] { - let functionNames: InstructionWithArgs[] = [] - instructions.forEach((instruction) => { - // Check if the instruction is an object and not a string - if (typeof instruction === "object" && instruction !== null) { - if ("CallFunction" in instruction) { - const callFunction = instruction as CallFunction - functionNames.push({ - instructionName: callFunction.CallFunction.function, - args: callFunction.CallFunction.args, - }) - } else if ("CallMethod" in instruction) { - const callMethod = instruction as CallMethod - functionNames.push({ instructionName: callMethod.CallMethod.method, args: callMethod.CallMethod.args }) - } - } - }) - return functionNames - } - return ( {t("transaction-confirmation", { ns: "components" })}: @@ -88,7 +63,7 @@ export const TransactionConfirmationModal: React.FC = () => { {getFunctionOrMethod(arg.instructions) .flatMap((i) => i.instructionName + " with args: " + i.args) .map((instruction, index) => ( -
{instruction}
// Using
or to wrap each instruction +
{instruction}
))} ))} @@ -102,11 +77,24 @@ export const TransactionConfirmationModal: React.FC = () => { )} - {t("balance-updates", { ns: "components" })}: - {simulation?.balanceUpdates?.map((update) => ( - - ))} + {t("balance-updates", { ns: "components" })}:{" "} + {Array.isArray(simulation?.balanceUpdates) && simulation.balanceUpdates.length > 0 ? ( + simulation.balanceUpdates.map((update) => ) + ) : ( + {t("no-balance-update", { ns: "components" })} + )} + + + {t("tx-simulation-status", { ns: "components" })}: {getTransactionStatusName(simulation?.transaction?.status)} + {simulation?.transaction.errorMsg && ( + + {t("tx-simulation-error-msg", { ns: "components" })}:{" "} + {typeof simulation?.transaction?.errorMsg === "string" + ? simulation.transaction.errorMsg + : JSON.stringify(simulation?.transaction?.errorMsg)} + + )}