User Update Example

This guide provides a complete Deno implementation for user-driven CIP-68 NFT metadata updates using spend validators, fee payments, and token ownership verification with the Anvil API.

circle-info

Want to understand the smart contract validation logic first? Check out the Smart Contract Logic Guide to learn how the contract validates user updates. Otherwise, you can dive right into the implementation below!

Full Code Example

circle-info

Want to see the complete code first? Check out the full working implementationarrow-up-right on GitHub, then come back here for the step-by-step breakdown.

Implementation Overview

This example demonstrates user-driven metadata updates where token holders can update their own NFT metadata by:

  • Proving ownership of the user token (222)

  • Paying a 1 ADA fee to the admin

  • Updating limited metadata fields (e.g., nickname)

  • Following the spend validator's authorization rules

Prerequisites

This guide assumes you have completed the Minting Example and have:

  1. Existing CIP-68 tokens - Reference and user tokens from a successful mint

  2. User token ownership - The user token (222) must be in the customer's wallet

  3. Reference token location - Current UTXO of the reference token at the script address

  4. Blockfrost API access - For dynamic UTXO discovery

  5. Understanding of user updates - Review update logic for validation details

Step-by-Step Implementation

Let's build a user-driven CIP-68 metadata update script step by step. Each section explains the concept and shows the corresponding code.

Step 1: Setup and Configuration

Create update file with the required imports and configuration:

Key differences from admin updates:

  • Customer wallet signs the transaction (not admin)

  • Admin address receives the 1 ADA fee payment

  • Blockfrost integration for dynamic UTXO discovery

  • Asset name must match the originally minted token

Step 2: Apply Parameters and Find UTXOs

Apply parameters to the blueprint and locate the required UTXOs:

What's happening here:

  • Reference token discovery: Finds the current reference token UTXO at the script address

  • User token discovery: Locates the user token in the customer's wallet (proves ownership)

  • UTXO string format: Gets the hex-encoded UTXO string needed for requiredInputs

  • Validation: Ensures both tokens exist before proceeding

Step 3: Build the User Update Transaction

Now we build the spend transaction with user authorization:

Key redeemer components:

  • nickname: The updated value in hex format (limited user permission)

  • fee_output_index: Points to the 1 ADA fee payment output

  • output_ref_user_token: References the user token for ownership verification

Step 4: Define Transaction Outputs

Create the outputs for the updated reference token and fee payment:

Critical output requirements:

  • Output 0: Updated reference token with new metadata at script address

  • Output 1: Exactly 1 ADA fee payment to admin address

  • Datum preservation: Original asset name must be preserved

  • User token input: Must be included via requiredInputs for ownership proof

Step 5: Build, Sign, and Submit Transaction

Build and submit the user update transaction:

What's happening here:

  • Customer signature: Only the customer signs (proves they own the user token)

  • No admin signature: Admin authorization comes from fee payment, not signature

  • Validation on-chain: Smart contract validates ownership and fee payment

Running the Example

Prerequisites Setup

  1. Complete the minting example first to have CIP-68 tokens available

  2. Set up environment variables:

  3. Update configuration:

    • Set assetName to match your minted token

    • Modify nickname to your desired update value

    • Ensure wallet files are properly configured

  4. Run the user update script:

Expected Output

User vs Admin Updates

This user update approach differs from admin updates in three key ways:

Aspect
User Updates
Admin Updates

Authorization

Token ownership + 1 ADA fee

Admin signature only

Permissions

Limited fields (e.g., nickname)

Full metadata access

Cost

1 ADA fee to admin

Free for admin

The economic barrier prevents spam while giving users control over their metadata.

Troubleshooting

Common Validation Failures

If your transaction fails validation, check these common issues:

  • user_token_quantity != 1 - User token not included in transaction inputs

  • lovelace_paid != 1000000 - Fee amount is not exactly 1 ADA

  • !is_paid_to_admin - Fee payment sent to wrong address

  • current_datum != expected_datum - Attempting to update restricted metadata fields

circle-info

For detailed validator logic, see the Update Logic Guide.

Key Requirements

  • Include user token in requiredInputs for ownership proof

  • Pay exactly 1 ADA (1,000,000 lovelace) to admin address

  • Update only permitted fields as defined by the smart contract

  • Track UTXO locations dynamically (tokens move with each update)

Next Steps

You now have a complete user-driven update implementation. For production applications, consider:

  • Batch Updates: Multiple metadata fields in one transaction

  • Update History: Track metadata changes for audit trails

  • Fee Estimation: Display costs before transaction submission

  • User Interface: Build a frontend for non-technical users

References

Last updated

Was this helpful?