Skip to content

Cadence to Go Code Converter — Numio

Generates Go structs from the structs, resources and events in a Flow Cadence contract, using easi-gen compiled to WebAssembly. Fields get `godence:"..."` tags for the godence encoder; optional types like String? become their plain Go type.

Decoding script results or contract events from the Flow blockchain in a Go backend that uses flow-go-sdk and godence.

Cadence types

Go types

When to use it

  • Decoding script results or contract events from the Flow blockchain in a Go backend that uses flow-go-sdk and godence.
  • Generating a typed Go client with query functions for a contract's public functions instead of writing scripts by hand.

How to use

  1. Paste a Cadence contract into the Cadence types editor; keep the top-level pub contract block, since structs outside a contract are rejected.
  2. Optionally open Output Settings and turn on "Generate Interaction Code With Functions" to also get a package header, imports, a ContractX type and one Go method per pub fun.
  3. Copy the Go types panel into your project and add the godence import; import "math/big" is added automatically when a field uses *big.Int.

Cadence contract

pub contract Marketplace {
  pub struct Listing {
    pub let id: UInt64
    pub let seller: Address
    pub var price: UFix64
    pub var tags: [String]
    pub var meta: {String: String}
    pub var note: String?

    init() { self.id = 0; self.seller = 0x01; self.price = 0.0; self.tags = []; self.meta = {}; self.note = nil }
  }

  pub event Sold(id: UInt64, buyer: Address)
}

Go structs

type StructListing struct {
	Id     uint64            `godence:"id"`
	Seller string            `godence:"seller"`
	Price  uint64            `godence:"price"`
	Tags   []string          `godence:"tags"`
	Meta   map[string]string `godence:"meta"`
	Note   string            `godence:"note"`
}

type EventSold struct {
	Id    uint64 `godence:"id"`
	Buyer string `godence:"buyer"`
}

Details and limitations

Generation runs entirely in your browser: the page loads the easi-gen WebAssembly module and nothing is sent to a server. The generator is a large (about 29 MB) WebAssembly bundle embedded in the page script, so the first load can be slow.

Names are prefixed by kind: StructListing, ResourceNFT, EventSold. Field names are capitalised and the original Cadence name is kept in the godence tag.

Type mapping in practice: UInt64 to uint64, Int8 to int8, Int to *big.Int, UFix64 to uint64, Fix64 to int64, Address to string, [T;2] to [2]T, {K:V} to map[K]V.

With the default setting only type declarations are produced, without a package line, and pub fun bodies are ignored. The only import added is "math/big", when an Int field needs *big.Int.

FAQ

Why do I get "no top level contract found"?

The generator only reads declarations inside a pub contract, so standalone structs are shown as this error. Wrap them in a contract block, for example pub contract Example { ... }, as in the editor's default sample.

How are optional types handled?

The optional marker is dropped: String? becomes string, and a function returning Listing? is mapped to StructListing. Check for missing values yourself, since a nil result decodes to a zero value.

What does "Generate Interaction Code With Functions" add?

It adds package and import lines, a ContractName type with a constructor that checks the contract address, and a method per public function that builds a Cadence script, runs ExecuteScriptAtLatestBlock and decodes the result with godence.

Are nested structs and multi-dimensional arrays supported?

Yes. A struct declared inside another becomes its own Go type, [[String]] becomes [][]string, and nested dictionaries become nested maps such as map[string]map[string]string.

Specifications and references