Skip to content

Commit

Permalink
fix: withdrawal transaction now always nominate at least one input
Browse files Browse the repository at this point in the history
  • Loading branch information
AngelCastilloB committed Sep 25, 2024
1 parent 7f1cfe5 commit ef6be70
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
23 changes: 20 additions & 3 deletions src/api/extension/wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,15 @@ import { ERROR, TX } from '../../config/config';
import Loader from '../loader';
import { blockfrostRequest } from '../util';
import { decodeTx, encodeTx, transformTx } from 'cardano-hw-interop-lib';
import { TransactionOutput, AuxiliaryData, Address, Credential, PoolId, RewardAccount } from '@blaze-cardano/core';
import {
TransactionOutput,
AuxiliaryData,
Address,
Credential,
PoolId,
RewardAccount,
TransactionUnspentOutput,
} from '@blaze-cardano/core';

const RETRIES = 5;

Expand Down Expand Up @@ -173,7 +181,7 @@ export const delegationTx = async (
}
};

export const withdrawalTx = async (account, delegation, protocolParameters) => {
export const withdrawalTx = async (account, delegation, protocolParameters, utxos) => {
try {
await Loader.load();

Expand All @@ -188,7 +196,16 @@ export const withdrawalTx = async (account, delegation, protocolParameters) => {
(protocolParameters.slot + TX.invalid_hereafter).toString()
));

tx.setChangeAddress(Address.fromBech32(account.paymentAddr));
const changeAddress = Address.fromBech32(account.paymentAddr);

if (!utxos || utxos.length === 0) {
throw new Error('No inputs found on wallet. Withdrawal transaction needs to have at least one input.');
}

// Transactions need to have at least one input to be valid. If the rewards amount is enough to cover the fees
// blaze tx builder won't select any inputs.
tx.addInput(TransactionUnspentOutput.fromCbor(utxos[0].to_cbor_hex()));
tx.setChangeAddress(changeAddress);

const result = await tx.complete();

Expand Down
3 changes: 2 additions & 1 deletion src/ui/app/components/transactionBuilder.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,8 @@ const TransactionBuilder = React.forwardRef(({ onConfirm }, ref) => {
withdrawRef.current.openModal(account.index);
const protocolParameters = await initTx();
try {
const tx = await withdrawalTx(account, delegation, protocolParameters);
const utxos = await getUtxos();
const tx = await withdrawalTx(account, delegation, protocolParameters, utxos);
setData({
pool: { ...poolDefaultValue },
tx,
Expand Down

0 comments on commit ef6be70

Please sign in to comment.