Skip to content

Commit

Permalink
fix front bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
karczuRF committed Nov 21, 2024
1 parent d4bfc21 commit 71f4cca
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 40 deletions.
5 changes: 3 additions & 2 deletions locales/en/components.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"open-logs-directory": "Logs",
"simulation-status": "Simulation status",
"simulation-error-msg": "Simulation error message",
"tx-simulation-status": "Simulated transaction error",
"tx-simulation-error-msg": "Simulated tx error message"
"tx-simulation-status": "Simulated transaction status",
"tx-simulation-error-msg": "Simulated tx error message",
"no-balance-update": "No balance updates available."
}
3 changes: 2 additions & 1 deletion locales/pl/components.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@
"simulation-status": "Status symulacji",
"simulation-error-msg": "Błąd symulacji",
"tx-simulation-status": "Status symulowanej transakcji",
"tx-simulation-error-msg": "Błąd symulowanej transakcji"
"tx-simulation-error-msg": "Błąd symulowanej transakcji",
"no-balance-update": "Aktualizacja balansów niedostępna"
}
46 changes: 13 additions & 33 deletions src/components/TransactionConfirmationModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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 (
<Dialog open={!!transaction} maxWidth="sm" fullWidth>
<DialogTitle textAlign="center">{t("transaction-confirmation", { ns: "components" })}:</DialogTitle>
Expand All @@ -88,7 +63,7 @@ export const TransactionConfirmationModal: React.FC = () => {
{getFunctionOrMethod(arg.instructions)
.flatMap((i) => i.instructionName + " with args: " + i.args)
.map((instruction, index) => (
<div key={index}>{instruction}</div> // Using <div> or <span> to wrap each instruction
<div key={index}>{instruction}</div>
))}
</DialogContentText>
))}
Expand All @@ -102,19 +77,24 @@ export const TransactionConfirmationModal: React.FC = () => {
</DialogContentText>
)}
<DialogContentText>
{t("balance-updates", { ns: "components" })}:
{t("balance-updates", { ns: "components" })}:{" "}
{Array.isArray(simulation?.balanceUpdates) && simulation.balanceUpdates.length > 0 ? (
simulation.balanceUpdates.map((update) => <BalanceUpdateView key={update.vaultAddress} {...update} />)
) : (
<span> No balance updates available.</span> // Optional: Provide feedback if there are no updates
<span>{t("no-balance-update", { ns: "components" })}</span>
)}
</DialogContentText>
<DialogContentText>
{t("tx-simulation-status", { ns: "components" })}: {simulation?.transaction?.status ?? "-"}
</DialogContentText>
<DialogContentText>
{t("tx-simulation-error-msg", { ns: "components" })}: {simulation?.transaction?.errorMsg ?? "-"}
{t("tx-simulation-status", { ns: "components" })}: {getTransactionStatusName(simulation?.transaction?.status)}
</DialogContentText>
{simulation?.transaction.errorMsg && (
<DialogContentText>
{t("tx-simulation-error-msg", { ns: "components" })}:{" "}
{typeof simulation?.transaction?.errorMsg === "string"
? simulation.transaction.errorMsg
: JSON.stringify(simulation?.transaction?.errorMsg)}
</DialogContentText>
)}
</DialogContent>
<DialogActions>
<Button onClick={handleClose} variant="contained">
Expand Down
31 changes: 31 additions & 0 deletions src/helpers/transaction.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { TransactionStatus } from "@tari-project/tarijs"
import { CallFunction, CallMethod } from "@tari-project/tarijs/dist/builders/types/Instruction"

interface InstructionWithArgs {
instructionName: string
args: any[]
}

export function getFunctionOrMethod(instructions: object[]): InstructionWithArgs[] {
let functionNames: InstructionWithArgs[] = []
instructions.forEach((instruction) => {
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
}

export function getTransactionStatusName(status?: TransactionStatus): string {
if (!status) return ""
return TransactionStatus[status]
}
27 changes: 23 additions & 4 deletions src/store/simulation/simulation.action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { ListenerEffectAPI, PayloadAction, ThunkDispatch, UnknownAction } from "
import { simulationActions } from "./simulation.slice"
import { SimulationRequestPayload } from "./simulation.types"
import { RootState } from "../store"
import { TransactionStatus } from "@tari-project/tarijs"

export const runTransactionSimulationAction = () => ({
actionCreator: simulationActions.runSimulationRequest,
Expand All @@ -16,16 +17,34 @@ export const runTransactionSimulationAction = () => ({
const { runSimulation } = state.transaction.entities[transactionId]

if (!provider) {
dispatch(simulationActions.runSimulationFailure({ transactionId, errorMsg: "Provider not found" }))
dispatch(
simulationActions.runSimulationFailure({
transactionId,
errorMsg: "Provider not found",
transaction: { status: TransactionStatus.InvalidTransaction, errorMsg: "Provider not found" },
})
)
return
}

try {
const balanceUpdates = await runSimulation()
dispatch(simulationActions.runSimulationSuccess({ transactionId, balanceUpdates }))
const simulationResult = await runSimulation()
dispatch(
simulationActions.runSimulationSuccess({
transactionId,
balanceUpdates: simulationResult.balanceUpdates,
transaction: simulationResult.txSimulation,
})
)
} catch (error) {
console.error(error)
dispatch(simulationActions.runSimulationFailure({ transactionId, errorMsg: String(error) }))
dispatch(
simulationActions.runSimulationFailure({
transactionId,
errorMsg: String(error),
transaction: { status: TransactionStatus.Rejected, errorMsg: String(error) },
})
)
}
},
})

0 comments on commit 71f4cca

Please sign in to comment.