Skip to content

Commit

Permalink
fix(swap): Fix swap cancellation fee error handling (#2784)
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeljscript authored Oct 16, 2023
1 parent 2eb35ce commit 3abb343
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ export const OpenOrders = () => {
const keys = await Promise.all(signers.map(async (signer) => createRawTxSigningKey(rootKey, signer)))
const response = await wallet.signRawTx(cbor, keys)
if (!response) return
const hexBase64 = new Buffer(response).toString('base64')
const hexBase64 = Buffer.from(response).toString('base64')
return {txBase64: hexBase64}
}

Expand All @@ -194,19 +194,12 @@ export const OpenOrders = () => {

const getFee = React.useCallback(
async (utxo: string, collateralUtxo: string, bech32Address: string) => {
let fee = '0'
setIsLoading(true)

try {
fee = await getCancellationOrderFee(wallet, cancelOrder, {orderUtxo: utxo, collateralUtxo, bech32Address})
} catch (error) {
Alert.alert(strings.generalErrorTitle, strings.generalErrorMessage(error))
}

const fee = await getCancellationOrderFee(wallet, cancelOrder, {orderUtxo: utxo, collateralUtxo, bech32Address})
setIsLoading(false)
return fee
},
[cancelOrder, strings, wallet],
[cancelOrder, wallet],
)

const openCancellationModal = async (order: MappedOpenOrder) => {
Expand All @@ -230,24 +223,31 @@ export const OpenOrders = () => {
const totalReturned = `${fromTokenAmount} ${fromTokenInfo?.ticker}`
const collateralUtxo = await getCollateralUtxo()

const fee = await getFee(utxo, collateralUtxo, bech32Address)

openModal(
strings.listOrdersSheetTitle,
<ModalContent
assetFromIcon={<TokenIcon wallet={wallet} tokenId={fromTokenInfo?.id ?? ''} variant="swap" />}
assetToIcon={<TokenIcon wallet={wallet} tokenId={toTokenInfo?.id ?? ''} variant="swap" />}
onConfirm={() => onOrderCancelConfirm(order)}
onBack={closeModal}
assetFromLabel={assetFromLabel}
assetToLabel={assetToLabel}
assetAmount={`${tokenAmount} ${assetToLabel}`}
assetPrice={`${tokenPrice} ${assetFromLabel}`}
totalReturned={totalReturned}
fee={fee}
/>,
460,
)
try {
const fee = await getFee(utxo, collateralUtxo, bech32Address)
openModal(
strings.listOrdersSheetTitle,
<ModalContent
assetFromIcon={<TokenIcon wallet={wallet} tokenId={fromTokenInfo?.id ?? ''} variant="swap" />}
assetToIcon={<TokenIcon wallet={wallet} tokenId={toTokenInfo?.id ?? ''} variant="swap" />}
onConfirm={() => onOrderCancelConfirm(order)}
onBack={closeModal}
assetFromLabel={assetFromLabel}
assetToLabel={assetToLabel}
assetAmount={`${tokenAmount} ${assetToLabel}`}
assetPrice={`${tokenPrice} ${assetFromLabel}`}
totalReturned={totalReturned}
fee={fee}
/>,
460,
)
} catch (error) {
if (error instanceof Error) {
Alert.alert(strings.generalErrorTitle, strings.generalErrorMessage(error.message))
} else {
Alert.alert(strings.generalErrorTitle, strings.generalErrorMessage(JSON.stringify(error)))
}
}
}

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@ export const getCancellationOrderFee = async (
) => {
const address = await CardanoMobile.Address.fromBech32(options.bech32Address)
const bytes = await address.toBytes()
const addressHex = new Buffer(bytes).toString('hex')
const addressHex = Buffer.from(bytes).toString('hex')
const cbor = await cancelOrder({
utxos: {collateral: options.collateralUtxo, order: options.orderUtxo},
address: addressHex,
})
if (!cbor) throw new Error(`Failed to get CBOR from REST API for address ${options.bech32Address}`)
const tx = await CardanoMobile.Transaction.fromBytes(Buffer.from(cbor, 'hex'))
const feeNumber = await tx.body().then((b) => b.fee())
return Quantities.denominated(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@
},
{
"id": "swap.swapScreen.swapMinAda",
"defaultMessage": "!!!Min-ADA is the minimum ADA amount required to be contained when holding or sending Cardano native tokens.",
"defaultMessage": "!!!Min-ADA is the minimum ADA amount required to be contained when holding or sending Cardano native assets.",
"file": "src/features/Swap/common/strings.ts",
"start": {
"line": 222,
Expand Down Expand Up @@ -406,7 +406,7 @@
},
{
"id": "swap.swapScreen.swapMinReceived",
"defaultMessage": "!!!Minimum amount of tokens you can get because of the slippage tolerance.",
"defaultMessage": "!!!Minimum amount of assets you can get because of the slippage tolerance.",
"file": "src/features/Swap/common/strings.ts",
"start": {
"line": 243,
Expand Down

0 comments on commit 3abb343

Please sign in to comment.