Smart Contract Utilities

Learn how to use the Anvil API to interact with smart contract scripts. Endpoints for deriving addresses, applying parameters, and serializing/parsing Plutus data.

Smart Contract Utilities provides endpoints for interacting with Plutus scripts (also known as validators). You can use these endpoints to derive addresses, apply parameters, and handle data serialization.

Endpoint
Description

GET /validators/{hash}/address

Derives the script address from a given script hash.

POST /blueprints/apply-params

Applies parameters to a script to generate a new, parameterized script hash.

POST /validators/{hash}/parse

Parses a CBOR hex-encoded datum or redeemer into a human-readable JSON object.

POST /validators/{hash}/serialize

Serializes a JSON object into a CBOR hex-encoded datum or redeemer string.

Available Endpoints

Get Script Address

GET /validators/{hash}/address

Derives the script address from a given script hash.

  • Path Parameters:

    • hash (string): The hex-encoded hash of the Plutus script.

  • Query Parameters:

    • stake (string, optional): A stake key hash to associate with the address.

chevron-rightTypeScript Example: Get a script address from its hashhashtag
async function getScriptAddress(scriptHash: string) {
  const response = await fetch(`https://preprod.api.ada-anvil.app/v2/services/validators/${scriptHash}/address`, {
    method: 'GET',
    headers: {
      'Content-Type': 'application/json',
      'X-Api-Key': 'YOUR_API_KEY'
    }
  });

  const { hex, bech32 } = await response.json();

  return { hex, bech32 };
}
// Sample output:
// {
//   "hex": "61c9a6c4d47f5c8e...",
//   "bech32": "addr_test1wqsw5u8u7g5m6x0s0qqp9g3zgx9n9ahm0t4105keg3zt0swsgjmzg"
// }

Apply Parameters to Script

POST /blueprints/apply-params

Applies a set of parameters to a parameterized script, returning the new script hash and compiled code. See Hello World Example with Parametersarrow-up-right for an example of a smart contract that uses parameters.

  • Body:

    • params (object): A record mapping validator hashes to arrays of parameters. Each validator hash maps to an array of parameters to apply.

      • Example: { "validator_hash_here": [param1, param2, ...] }

    • blueprint (object, optional): Only required if the script is not yet known to Anvil. This is usually your plutus.json file from an Aiken build. See the Blueprint Managementarrow-up-right guide for more details.

chevron-rightTypeScript Example: Apply parameters to a scripthashtag

Parse Plutus Data

POST /validators/{hash}/parse

Parses a hex-encoded Plutus data string (e.g., datum or redeemer) into a structured JSON object based on the script's schema.

  • Path Parameters:

    • hash (string): The hash of the script. The API uses this hash to fetch the validator's schema, which is required to correctly convert the data between JSON and its hex-encoded format.

  • Body:

    • type (string): The type of data to parse ("datum" or "redeemer").

    • purpose (string): The purpose of the script, which determines the context in which it's validated. Possible values are:

      • "mint": For minting or burning assets.

      • "spend": For spending transaction outputs from the script address.

      • "withdraw": For withdrawing staking rewards.

      • "publish": For publishing delegation certificates.

      • "vote": For voting on governance proposals.

      • "propose": For executing constitution guardrails when submitting governance proposals.

    • data (object): An object containing the hex string: { "hex": "..." }.

    • addressFormat (string, optional): The desired address format ("bech32" or "raw").

    • blueprint (object, optional): The CIP-57 blueprint if the script is not yet known to Anvil. See the Blueprint Managementarrow-up-right guide for more details.

chevron-rightTypeScript Example: Parse a datum from a transaction outputhashtag

Serialize Plutus Data

POST /validators/{hash}/serialize

Serializes a JSON object into a hex-encoded Plutus data string based on the script's schema. The opposite of parse.

  • Path Parameters:

    • hash (string): The hash of the script. The API uses this hash to fetch the validator's schema, which is required to correctly convert the data between JSON and its hex-encoded format.

  • Body:

    • type (string): The type of data to serialize ("datum" or "redeemer").

    • purpose (string): The purpose of the script, which determines the context in which it's validated. Possible values are:

      • "mint": For minting or burning assets.

      • "spend": For spending transaction outputs from the script address.

      • "withdraw": For withdrawing staking rewards.

      • "publish": For publishing delegation certificates.

      • "vote": For voting on governance proposals.

      • "propose": For executing constitution guardrails when submitting governance proposals.

    • data (object): The JSON object to serialize.

    • blueprint (object, optional): The CIP-57 blueprint if the script is not yet known to Anvil. See the Blueprint Managementarrow-up-right guide for more details.

chevron-rightTypeScript Example: Serialize a datum object into hexhashtag

Last updated

Was this helpful?