Part 3: Building Transactions

This is Part 3 of our guide to building a Cardano transaction application with Next.js and Weld. In this section, you'll implement transaction building and submission functionality.

Introduction

Now for the best part: With our wallet integration in place, we're now ready to implement the transaction functionality. This involves creating API endpoints for transaction building and submission, as well as building the user interface components for transaction input and status feedback.

Transaction Flow Overview

For a comprehensive explanation of Cardano transaction concepts, please refer to our Transaction Guide. In this tutorial, we'll focus on implementing the following streamlined transaction flow:

  1. Get Wallet Data: Retrieve the user's wallet address and UTXOs

  2. Build Transaction: Call Anvil API to construct the transaction

  3. Sign Transaction: Use the connected wallet to sign the transaction

  4. Submit Transaction: Send the signed transaction to the Cardano network

  5. Display Result: Show the transaction ID and success/error feedback

Implementation Steps

1. Create Anvil API Utility Functions

Instead of calling the Anvil API directly, we'll create utility functions that offer several advantages including type safety, error handling, and secure API key management through Next.js server components.

Our implementation will include three key functions:

  • callAnvilApi: A generic request handler with proper error management

  • buildTransaction: Creates transactions with sender addresses and payment details

  • submitTransaction: Sends signed transactions to the Cardano network

Let's implement these utilities:

2. Create API Endpoints for Transaction Building and Submission

Next.js offers a built-in API routes system through its App Router architecture that allows us to create serverless functions. For this application, we'll create two API endpoints to handle transaction building and submission.

See the Next.js Route Handlers documentation for more details on how these API routes work.

Transaction Building Endpoint

First, let's create the endpoint that builds a transaction with our Anvil API utility:

Transaction Submission Endpoint

Next, let's create the endpoint that submits a signed transaction to the Cardano network:

3. Create a Custom Hook for Transaction Management

To cleanly manage our transaction logic, we'll create a reusable custom hook that:

  • Tracks the entire transaction lifecycle with multiple status states

  • Manages API calls to our transaction endpoints

  • Handles wallet integration for signing

  • Provides consistent error handling and user feedback

  • Exposes a simple interface for components to use

This pattern keeps our UI components focused on presentation rather than complex transaction logic.

4. Create the Transaction Form Component

Now, let's create the UI component for transactions. This component will:

  • Provide inputs for recipient address and ADA amount

  • Display transaction status and results

  • Handle form validation and submission

  • Show a link to a blockchain explorer after successful transactions

The form integrates with our custom hook from the previous step to manage transaction state and processing. It also demonstrates proper error handling and user feedback throughout the transaction flow.

5. Update the Home Page to Include the Transaction Form

Finally, let's update the home page to include both the wallet connector and transaction form:

Testing Your Transaction Flow

  1. Start your development server:

  1. Navigate to your application (usually at http://localhost:3000)

  2. Connect your wallet following the instructions from Part 2

  3. Test the transaction flow:

    • Enter a valid recipient address (use a testnet address for testing)

    • Enter a small amount of ADA (e.g., 1-2 ADA)

    • Click the "Send" button

    • Observe the transaction states: Building → Signing → Submitting → Success

    • Verify the transaction ID appears and links to a block explorer

Troubleshooting

Common Transaction Errors

  1. Insufficient Funds

    • Ensure your wallet has enough testnet ADA for the transaction and fees

    • Try sending a smaller amount

  2. Invalid Address

    • Verify you're using a valid Cardano address format

    • Check that you're using a testnet address when testing on testnet

  3. Signature Failure

    • Make sure your wallet is unlocked

    • Try reconnecting your wallet if signing fails

    • Check that your wallet is enabled for dApp interactions

  4. Network Issues

    • Check your internet connection

    • Verify the Anvil API is available and responding (Use /health endpoint)

Deployment Considerations

When deploying your application to production:

  1. Environment Variables: Ensure your production environment has the correct API URLs and keys

  2. Network Selection: Update the NEXT_PUBLIC_NETWORK to mainnet for production use

  3. Error Handling: Implement more robust error handling and user feedback

  4. Security: Ensure your API keys are properly secured and not exposed to clients

Conclusion

Congratulations! You've successfully built a complete Cardano transaction application using Next.js and Weld. Your application can now:

  • Connect to Cardano wallets

  • Build and sign transactions

  • Submit transactions to the Cardano network

  • Display transaction results

Completed Cardano Transaction App with Next.js and Weld
Your completed Cardano transaction application

Next Steps

To further enhance your application, consider exploring:

Last updated

Was this helpful?