Skip to content

Commit

Permalink
fix(wallet): fix ui tx submission
Browse files Browse the repository at this point in the history
  • Loading branch information
mrcnk committed Oct 30, 2024
1 parent 3730856 commit 4116180
Showing 1 changed file with 9 additions and 21 deletions.
30 changes: 9 additions & 21 deletions packages/features/src/send/hooks/use-transaction-confirmation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@ import type { ChainOperationArgs } from "@palladxyz/key-management"
import { Mina } from "@palladxyz/mina-core"
import { useVault } from "@palladxyz/vault"
import dayjs from "dayjs"
import type {
Payment,
SignedLegacy,
} from "mina-signer/dist/node/mina-signer/src/TSTypes"
import type { SubmitHandler, UseFormReturn } from "react-hook-form"
import { useMixpanel } from "react-mixpanel-browser"
import { useNavigate } from "react-router-dom"
Expand All @@ -29,14 +25,11 @@ export const useTransactionConfirmation = ({
}: UseTransactionConfirmationProps) => {
const mixpanel = useMixpanel()
const navigate = useNavigate()
// can use
// const request = useVault((state) => state.request) for signing
const sign = useVault((state) => state.sign)
const submitTx = useVault((state) => state.submitTx)
const constructTx = useVault((state) => state.constructTx)
const syncWallet = useVault((state) => state._syncWallet)
const currentNetworkName = useVault((state) => state.currentNetworkName)
//const currentWallet = useVault((state) => state.getCurrentWallet())
const { publicKey, data: accountProperties } = useAccount()
const outgoingTransaction = useTransactionStore(
(state) => state.outgoingTransaction,
Expand All @@ -48,27 +41,19 @@ export const useTransactionConfirmation = ({
const rawFee = outgoingTransaction.fee || 0.001
const amount = BigInt(rawAmount * 1_000_000_000).toString()
const fee = BigInt(rawFee * 1_000_000_000).toString()
const rawTransaction: Mina.TransactionBody = {
const rawTransaction = {
to: outgoingTransaction.to,
from: publicKey,
memo: outgoingTransaction.memo,
validUntil: "4294967295",
fee,
amount,
nonce: accountProperties?.inferredNonce ?? 0, // need a util for this whole `onSubmit` to remove Mina dependency
type,
}
const constructTransaction = () => {
const constructTxArgs = {
transaction: rawTransaction,
transactionType: type,
}
return constructTx(constructTxArgs)
}
const submitTransaction: SubmitHandler<ConfirmTransactionData> = async (
data,
) => {
const constructedTx = await constructTransaction()
const constructedTx = constructTx({ transaction: rawTransaction })
const getPassphrase = () => utf8ToBytes(data.spendingPassword)
let signedTx
// TODO: make chain agnostic depending on the currentWallet chain and it's corresponding operation e.g. 'eth_signTransaction' vs 'mina_signTransaction'
Expand All @@ -78,7 +63,11 @@ export const useTransactionConfirmation = ({
networkType: currentNetworkName === "Mainnet" ? "mainnet" : "testnet",
}
try {
signedTx = await sign(constructedTx as any, operationArgs, getPassphrase)
signedTx = await sign(
{ transaction: constructedTx },
operationArgs,
getPassphrase,
)
} catch (error: unknown) {
if (error instanceof Error) {
if (error.name === "AuthenticationError")
Expand All @@ -88,9 +77,8 @@ export const useTransactionConfirmation = ({
})
}
}
// TODO: Make a util for this
const submitTxArgs = {
signedTransaction: signedTx as unknown as SignedLegacy<Payment>,
signedTransaction: signedTx as never,
type,
transactionDetails: {
fee: rawTransaction.fee,
Expand All @@ -102,7 +90,7 @@ export const useTransactionConfirmation = ({
validUntil: rawTransaction.validUntil,
},
}
const submittedTx = (await submitTx(submitTxArgs as any)) as any
const submittedTx = (await submitTx(submitTxArgs)) as any
const hash =
submittedTx?.sendPayment?.payment?.hash ??
submittedTx?.sendDelegation?.delegation?.hash
Expand Down

0 comments on commit 4116180

Please sign in to comment.