The Fadroma Guide
Homepage Documentation Github Crates.io NPM Hack.bg

Fadroma Compile API


Status: Covers the basics. To do:


Fadroma can deploy smart contracts from source. Therefore, it must be able to compile them. Various compilation options are implemented in the form of the Compiler classes, which set up the appropriate build environments.

Get a compiler

import { getCompiler } from '@hackbg/fadroma'
const compiler = getCompiler()

RawLocalRustCompiler

Runs the build procedure in the current shell environment (using Rust from your rustup setup, etc.)

const compiler = getCompiler({ container: false })

ContainerizedLocalRustCompiler

Runs the build procedure in a Docker container, https://ghcr.io/hackbg/fadroma, using @fadroma/oci.

const compiler = getCompiler({ container: true })
// specify custom docker connection:
const compiler = getCompiler({ container: true, dockerSocket: 'localhost:5000' })

Compile a crate

const compiled = await compiler.build({
  cargoToml: "./path/to/crate/Cargo.toml"
})

Compile a crate from a workspace

const compiled = await compiler.build({
  cargoWorkspace: "./path/to/workspace/Cargo.toml",
  cargoCrate: "my-contract-1"
})

This returns an instance of CompiledCode with the following properties:

name description
codePath path to compiled code
codeHash sha256 of compiled code
codeData (empty before fetch) Uint8Array with the compiled code

You can then upload and instantiate the compiled code with:

const uploaded = await agent.upload(compiled)
const instance = await agent.instantiate(uploaded, { label, initMsg })

To prevent reuploads of the same code, upload with:

const uploadStore = getUploadStore('/path/to/uploads/dir') 
const uploaded = await agent.upload(compiled, { uploadStore })

To get the actual code as a Uint8Array, use:

const binary = await compiled.fetch()