Skip to content

Commit

Permalink
CR: updaye
Browse files Browse the repository at this point in the history
  • Loading branch information
banklesss committed Jan 27, 2025
1 parent 54aef3f commit 8a77090
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export const useDappConnectorManager = () => {
onSuccess: (args) => {
shouldResolve = false
if (isEmptyString(args?.rootKey) || args?.rootKey == null) {
reject('useDappConnectorManager::handleSignTx: invalid state')
reject(new Error('useDappConnectorManager::handleSignTx: invalid state'))
return
}

Expand Down Expand Up @@ -87,7 +87,7 @@ export const useDappConnectorManager = () => {
onSuccess: (args) => {
shouldResolve = false
if (!args?.tx) {
reject('useDappConnectorManager::handleSignTxWithHW: invalid state')
reject(new Error('useDappConnectorManager::handleSignTxWithHW: invalid state'))
return
}
resolve(args?.tx)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@ export const useLegacyOnConfirm = ({
}

const legacyOnConfirm = () => {
if (meta.isHW && unsignedTx) {
if (!unsignedTx) {
throw new Error('useLegacyOnConfirm:: unsignedTx is required')
}
const {isHW, isEasyConfirmationEnabled} = meta
if (isHW) {
openModal(
strings.signTransaction,
<ConfirmTxWithHwModal
Expand All @@ -63,7 +67,7 @@ export const useLegacyOnConfirm = ({
return
}

if (!meta.isHW && !meta.isEasyConfirmationEnabled && unsignedTx) {
if (!isHW && !isEasyConfirmationEnabled) {
openModal(
strings.signTransaction,
<ConfirmTxWithSpendingPasswordModal
Expand All @@ -75,15 +79,10 @@ export const useLegacyOnConfirm = ({
return
}

if (!meta.isHW && meta.isEasyConfirmationEnabled && unsignedTx) {
openModal(
strings.signTransaction,
<ConfirmTxWithOsModal unsignedTx={unsignedTx} onSuccess={handleOnSuccess} onError={handleOnError} />,
)
return
}

throw new Error('useLegacyOnConfirm:: invalid state')
openModal(
strings.signTransaction,
<ConfirmTxWithOsModal unsignedTx={unsignedTx} onSuccess={handleOnSuccess} onError={handleOnError} />,
)
}

return {legacyOnConfirm} as const
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,9 @@ export const OpenOrders = () => {
const intl = useIntl()
const {wallet, meta} = useSelectedWallet()
const {order: swapApiOrder} = useSwap()
const {navigateToTxHistory} = useWalletNavigation()
const {navigateToTxReview} = useWalletNavigation()
const [isLoading, setIsLoading] = React.useState(false)
const navigateTo = useNavigateTo()

const orders = useSwapOrdersByStatusOpen()
const {numberLocale} = useLanguage()
Expand Down Expand Up @@ -125,34 +126,34 @@ export const OpenOrders = () => {
})
}

const onRawTxConfirm = async (rootKey: string, order: MappedOpenOrder) => {
const onRawTxConfirm = async (rootKey: string, order: MappedOpenOrder, cbor: string) => {
try {
const tx = await createCancellationTxAndSign(order.id, rootKey)
const tx = await createCancellationTxAndSign(order.id, rootKey, cbor)
if (!tx) return
await wallet.submitTransaction(tx.txBase64)
trackCancellationSubmitted(order)
navigateTo.submittedTx()
closeModal()
navigateToTxHistory()
} catch (error) {
if (error instanceof SubmitTxInsufficientCollateralError) {
handleCollateralError()
return
}
throw error
navigateTo.failedTx()
}
}

const onRawTxHwConfirm = (order: MappedOpenOrder) => {
try {
trackCancellationSubmitted(order)
navigateTo.submittedTx()
closeModal()
navigateToTxHistory()
} catch (error) {
if (error instanceof SubmitTxInsufficientCollateralError) {
handleCollateralError()
return
}
throw error
navigateTo.failedTx()
}
}

Expand All @@ -163,26 +164,46 @@ export const OpenOrders = () => {
return !!collateral.utxo && collateral.amount.quantity >= BigInt(getCollateralAmountInLovelace())
}

const onOrderCancelConfirm = (order: MappedOpenOrder) => {
const generateSwapCancellationCbor = async (bech32Address: string, utxo: string) => {
const collateralUtxo = await getCollateralUtxo()
const addressHex = await convertBech32ToHex(bech32Address)
const cbor = await swapApiOrder.cancel({
utxos: {collateral: collateralUtxo, order: utxo},
address: addressHex,
})

return cbor
}

const onOrderCancelConfirm = async (order: MappedOpenOrder) => {
if (!isString(order.utxo) || !isString(order.owner)) return

if (!hasCollateral()) {
handleCollateralError()
return
}

openModal(
strings.signTransaction,
<ConfirmRawTx
cancelOrder={swapApiOrder.cancel}
utxo={order.utxo}
bech32Address={order.owner}
onCancel={closeModal}
onConfirm={(rootKey) => onRawTxConfirm(rootKey, order)}
onHWConfirm={() => onRawTxHwConfirm(order)}
/>,
400,
)
const cbor = await generateSwapCancellationCbor(order.owner, order.utxo)

navigateToTxReview({
cbor,
onConfirm: () => {
if (!isString(order.utxo) || !isString(order.owner)) return

openModal(
strings.signTransaction,
<ConfirmRawTx
cancelOrder={swapApiOrder.cancel}
utxo={order.utxo}
bech32Address={order.owner}
onCancel={closeModal}
onConfirm={(rootKey) => onRawTxConfirm(rootKey, order, cbor)}
onHWConfirm={() => onRawTxHwConfirm(order)}
/>,
400,
)
},
})
}

const handleCollateralError = () => {
Expand All @@ -207,18 +228,12 @@ export const OpenOrders = () => {
const createCancellationTxAndSign = async (
orderId: string,
rootKey: string,
cbor: string,
): Promise<{txBase64: string} | undefined> => {
const order = normalizedOrders.find((o) => o.id === orderId)
if (!order || order.owner === undefined || order.utxo === undefined) return
const {utxo, owner: bech32Address} = order

try {
const collateralUtxo = await getCollateralUtxo()
const addressHex = await convertBech32ToHex(bech32Address)
const cbor = await swapApiOrder.cancel({
utxos: {collateral: collateralUtxo, order: utxo},
address: addressHex,
})
const signers = await getTransactionSigners(cbor, wallet, meta)
const keys = await Promise.all(signers.map(async (signer) => createRawTxSigningKey(rootKey, signer)))
const response = await wallet.signRawTx(cbor, keys)
Expand Down

0 comments on commit 8a77090

Please sign in to comment.