Fund Locking
Implement the fund locking functionality to allow users to securely lock their ADA in an escrow contract.
Introduction
In this part, we'll implement the fund locking functionality, which allows users to securely lock their ADA in an escrow contract. This involves creating a transaction that transfers funds to a script address with specific conditions for unlocking.
Understanding the Fund Locking Process
The fund locking process involves these steps:
User Input: The user selects an amount of ADA to lock.
Transaction Building: The application calls the Anvil API to build a transaction that sends funds to the escrow script address with the user's key hash as a datum.
Transaction Signing: The connected wallet signs the transaction, authorizing the fund transfer.
Transaction Submission: The signed transaction is submitted to the Cardano blockchain.
Status Tracking: The transaction hash is displayed to the user. In Part 4, we'll implement proper status tracking.
Smart Contract Datum Structure
When locking funds in the escrow address, we attach a datum containing the owner's verification key hash. This datum is essential for two reasons:
The Hello World smart contract uses it to validate unlock requests
It associates locked funds with their rightful owner
When building transaction outputs, we include this datum with the escrow script address, ensuring only the original owner can later unlock these funds with the correct signature.
This datum associates the locked funds with the original owner, ensuring only they can unlock these funds later when providing the correct redeemer message ("Hello, World!") and their signature.
Setup React Query
Before we integrate with the Anvil API, let's first set up React Query which we'll use for managing transaction state src/components/ReactQueryProvider.tsx:
1. Create React Query Provider
2. Update Root Layout
Update your src/app/layout.tsx component to include the React Query provider:
You need to add the import and wrap the application with the ReactQueryProvider provider.
Adding Anvil API Integration
We'll first set up the Anvil API integration, which we'll use for transaction building and submission.
2. Create the Anvil API Module
Let's create a src/lib/anvil-api.ts module to handle interactions with the Anvil API for building and submitting transactions:
Notice how the lockFunds function that calls the Anvil API. This is building a transaction that sends ADA to the escrow script address with the specified datum.
Once submitted, the transaction will be included in a block and the ADA will be locked in the escrow address.
4. Update Transaction Status Types
Let's add a new types.ts file to include the states for the locking process:
5. Create API Endpoint for Locking Funds
Now, let's create an API endpoint src/app/api/escrow/lock/route.ts in our Next.js application for locking funds:
6. Create API Endpoint for Transaction Submission
After we have called the API to build the transaction, we will prompt the user to sign the transaction via Weld.
Upon signing, we will submit the signed transaction to the blockchain via the following API route:
This src/app/api/escrow/submit/route.ts endpoint will be used for submitting both lock and unlock transactions. We will update it later to handle unlock transactions.
7. Create the Transaction Operations Hook
Let's create a custom hook src/hooks/useTransactionOperations.ts to handle transaction operations:
See the lockFunds function to see how we call our API endpoints and use Weld to sign the transaction between calls. We will also update this later to handle unlock transactions.
This hook encapsulates the logic for building, signing, and submitting transactions. It handles all the API calls and error states, making our component code cleaner.
8a. Create the Lock Funds Form Component
Now, let's create the form component src/components/LockFundsForm.tsx for locking funds:
8b. Create the Amount Slider Hook
Let's create a custom hook src/hooks/useAmountSlider.ts for the amount slider to make our form component cleaner:
9. Update the Home Page
Finally, update the home page src/app/page.tsx to include the LockFundsForm component:
Testing Your Implementation
To test the fund locking functionality:
Start your development server:
Navigate to http://localhost:3000 in your browser
Connect your wallet using the wallet connector
Use the slider to select an amount of ADA to lock
Click the "Lock Funds" button
Approve the transaction in your wallet when prompted
Verify that you see a success message with the transaction hash
Troubleshooting
Transaction Building Errors
If you encounter errors when building transactions:
Check that your Anvil API key is correct in your
.env.localfileVerify that the validator hash is set correctly in your environment variables
Ensure your wallet has sufficient funds (including for transaction fees)
Check the browser console and server logs for more detailed error messages
Wallet Signing Errors
If transaction signing fails:
Make sure your wallet is unlocked
Check that you've approved the dApp connection
Try refreshing the page and reconnecting your wallet
Congratulations! You've completed Part 3 of the guide. Your application can now lock funds in a Cardano smart contract.
Last updated
Was this helpful?

