I’ve written the following code:
async function buildAndSignTransaction(api, recipientAddressStr, amount, fixedFee) {
try {
// Convert recipient address from Bech32
const recipientAddress = wasm.Address.from_bech32(recipientAddressStr);
// Step 1: Get used address
const usedAddresses = await api.getUsedAddresses();
const senderAddressHex = usedAddresses.length > 0
? usedAddresses[0]
: null;
if (!senderAddressHex) {
throw new Error("No sender address found.");
}
console.log("Sender Address Hex:", senderAddressHex);
// Step 2: Create transaction builder
const linearFee = wasm.LinearFee.new(
wasm.BigNum.from_str("1000"), // constant fee
wasm.BigNum.from_str("100"), // fee per byte
);
// Initialize required parameters for TransactionBuilder
const minUtxoValue = wasm.BigNum.from_str("1000000"); // minimum UTXO value
const poolDeposit = wasm.BigNum.from_str("500000"); // pool deposit
const keyDeposit = wasm.BigNum.from_str("2000000"); // key deposit
const maxValueSize = 1000000; // maximum value size
const maxTxSize = 1000000; // maximum transaction size
const txBuilder = wasm.TransactionBuilder.new(
linearFee,
minUtxoValue,
poolDeposit,
keyDeposit,
maxValueSize,
maxTxSize
);
// Step 3: Get UTXOs
const utxos = await api.getUtxos();
if (utxos.length === 0) {
throw new Error("No UTXOs found for the sender.");
}
// Use the first UTXO for simplicity
const utxoHex = utxos[0];
const utxo = wasm.TransactionUnspentOutput.from_bytes(hexToBytes(utxoHex));
const txInput = utxo.input();
// Extract the value from the UTXO
const outputValue = utxo.output().amount();
console.log('Before Add Input');
console.log('UTXO:', utxo);
console.log('txInput:', txInput);
// Add input to the transaction builder
txBuilder.add_input(
wasm.Address.from_bytes(hexToBytes(senderAddressHex)),
txInput,
outputValue // Pass the UTXO's value directly
);
console.log('After Add Input');
// Step 4: Add output
try {
const lovelace = wasm.BigNum.from_str(amount.toString());
if (lovelace.to_str() === "0") {
throw new Error("The output amount must be greater than zero.");
}
// Calculate adjusted value manually
const adjustedValueStr = (parseInt(amount) - fixedFee).toString();
if (parseInt(adjustedValueStr) < 0) {
throw new Error("Insufficient funds to cover transaction fee.");
}
const adjustedValue = wasm.BigNum.from_str(adjustedValueStr);
const outputAddress = wasm.Address.from_bytes(recipientAddress.to_bytes());
console.log("Output Address (Bech32):", recipientAddress.to_bech32());
console.log("Output Value (Lovelace):", adjustedValue.to_str());
txBuilder.add_output(wasm.TransactionOutput.new(outputAddress, wasm.Value.new(adjustedValue)));
} catch (error) {
console.error("Error adding output:", error);
throw error; // Rethrow to be caught in the outer try-catch
}
console.log('After Add Output');
console.log(txBuilder);
// Step 5: Build the transaction
const transaction = txBuilder.build();
// Step 6: Sign the transaction
const signedTx = await api.signTx(transaction.to_bytes());
// Step 7: Submit the signed transaction
const txHash = await api.submitTx(signedTx);
console.log("Transaction submitted. TX Hash:", txHash);
} catch (error) {
console.error("Error building and signing transaction:", error);
}
}
I’m getting the error: “Error building and signing transaction: Fee not specified”
Please help me how to fix this error. All I want is to build a trasnsaction and submit so that the user can sign the transaction using dApp wallet.