cadence-codegen

command module
v0.0.7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 11, 2025 License: MIT Imports: 1 Imported by: 0

README

Cadence Codegen

A tool for analyzing Cadence smart contracts and generating code. It extracts transaction and script information, including parameters, imports, and return types.

Installation

Using Homebrew
brew tap outblock/tap
brew install cadence-codegen
Using Go
go install github.com/outblock/cadence-codegen@latest

Usage

Analyze Cadence Files

Analyze Cadence files and generate a JSON report:

# Analyze a directory of Cadence files
cadence-codegen analyze ./contracts

# Analyze with custom output path
cadence-codegen analyze ./contracts output.json

# Analyze without base64 encoding
cadence-codegen analyze ./contracts --base64=false
Generate Swift Code

Generate Swift code from Cadence files or JSON:

# Generate from Cadence files (outputs to CadenceGen.swift)
cadence-codegen swift ./contracts

# Generate from Cadence files with custom output path
cadence-codegen swift ./contracts output.swift

# Generate from previously analyzed JSON
cadence-codegen swift analysis.json output.swift
Generate TypeScript Code

Generate TypeScript code from Cadence files or JSON:

# Generate from Cadence files (outputs to cadence.generated.ts)
cadence-codegen typescript ./contracts

# Generate from Cadence files with custom output path
cadence-codegen typescript ./contracts output.ts

# Generate from previously analyzed JSON
cadence-codegen typescript analysis.json output.ts

Features

  • Analyzes Cadence files (.cdc)
  • Extracts:
    • Transaction parameters and types
    • Script parameters and return types
    • Import statements
    • Struct definitions
  • Generates:
    • Structured JSON output
    • Swift code with type-safe wrappers
    • TypeScript code with FCL integration
  • Supports folder-based tagging for better organization
  • Base64 encoding of Cadence files (optional)

JSON Output Format

{
  "transactions": {
    "filename.cdc": {
      "fileName": "filename.cdc",
      "type": "transaction",
      "parameters": [
        {
          "name": "amount",
          "typeStr": "UFix64",
          "optional": false
        }
      ],
      "imports": [
        {
          "contract": "FungibleToken",
          "address": "0xFungibleToken"
        }
      ],
      "tag": "TokenTransfer"
    }
  },
  "scripts": {
    // Similar structure for scripts
  },
  "structs": {
    // Struct definitions
  }
}

Generated Swift Code

The generated Swift code includes:

  • Type-safe enums for transactions and scripts
    • Separate enums for each folder (e.g., CadenceGen.EVM for files in the EVM folder)
    • Main CadenceGen enum for files in the root directory
  • Struct definitions with proper Swift types
  • Automatic Flow SDK integration
  • Support for async/await
  • Error handling

Example usage of generated Swift code:

// Execute a script from the EVM folder
let result: String? = try await flow.query(CadenceGen.EVM.getAddr(flowAddress: address))

// Execute a script from the root directory
let result: String? = try await flow.query(CadenceGen.getAddr(flowAddress: address))

// Send a transaction
let txId = try await flow.sendTx(
    CadenceGen.EVM.createCoa(amount: amount),
    singers: [signer]
) {
    // Transaction build options
}

Generated TypeScript Code

The generated TypeScript code includes:

  • Type-safe functions for transactions and scripts
  • FCL (Flow Client Library) integration
  • Support for request and response interceptors
  • Automatic type conversion from Cadence to TypeScript
  • Support for async/await
  • Struct definitions with proper TypeScript interfaces

Example usage of generated TypeScript code:

import { CadenceService } from "./cadence.generated";

const service = new CadenceService();

// Add interceptors for request/response processing
service.useRequestInterceptor((config) => {
  console.log("Request config:", config);
  return config;
});

service.useResponseInterceptor((config, response) => {
  console.log("Response:", response);
  return { config, response };
});

// Execute a script
const result = await service.getAddr(flowAddress);

// Send a transaction
const txId = await service.createCoa(amount);

License

MIT License

Documentation

The Go Gopher

There is no documentation for this package.

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL