Build a simple function

This guide provides step-by-step instructions on how to build and deploy a function to automate a blockchain operation using Mimic Protocol.

Workflow overview

The process consists of the following steps:

  1. Initialize your project: Start a working directory to develop your function from scratch.

  2. Define the manifest: Specify function inputs, ABIs, and metadata in a manifest file.

  3. Write the function logic: Implement your function logic.

  4. Build: Validate the manifest, generate supporting artifacts, and compile the function logic.

  5. Deploy: Upload your build output to a function registry, making it available for relayers to execute.


Initialize your project

Let's start a new working directory to develop your function. To do that you can run the following command:

npx @mimicprotocol/cli init ./my-mimic-function

Let's continue analyzing the structure of this project.


Manifest definition

The manifest file provides the configuration for your function, including:

  • Metadata: Name, description, and version of the function.

  • Inputs: Parameters required by the function logic.

  • ABIs: Smart contract ABIs to generate type-safe interfaces for the function.

Save this configuration in a manifest.yaml file:

https://github.com/mimic-protocol/examples/blob/main/examples/04-transfer-balance-threshold-with-oracles/manifest.yaml

This manifest file defines different inputs that will be accessible from the function logic code thanks to the types generation process that will be explained in the next section.

Note that inputs and abis can be written in YAML as lists of single-key objects (as shown above). They are merged into maps during validation, and duplicate keys are rejected. ABI paths are resolved relative to the directory of your manifest.yaml.

Additionally, each input may include an optional description using the object form:

For the purpose of this example you will need the ERC20 ABI, you can copy it from here:

file-download
4KB

Generate types

This steps allows you to validate the manifest definitions and generate the corresponding code to access both your declared inputs and the contract objects for your declared ABIs.

To do this you can run the codegen command using the CLI:

This command will output the generated types to ./src/types by default. It will also assume your manifest file is called manifest.yaml. However, you can specify a different manifest or output paths using the following parameters:

If you pass the --clean flag, you will be asked to confirm before deleting the existing contents of the output directory.


Writing the function logic

The function logic is implemented in AssemblyScript and must export:

  1. Input type: The generated inputs type from the previous step.

  2. Main function: The core function logic, which receives the inputs as an argument.

Create a file ./src/function.ts and implement the logic:

https://github.com/mimic-protocol/examples/blob/main/examples/04-transfer-balance-threshold-with-oracles/src/function.ts

As you can see, the generated contract artifacts can be accessed from your function code.

For now Mimic functions allows creating three types of intents:

  • Transfers

  • Generic calls

  • Cross-chain swaps


Compile process

The compile process converts your function logic and manifest into deployable artifacts:

  • build/function.wasm - Compiled WebAssembly binary

  • build/manifest.json - Processed manifest configuration

Run the compile command:

By default, outputs are saved in the build directory. You can customize the paths:

Here is an example of the output produced by this command:


Deploy your function

This is where you upload your function artifacts to the network so others can discover it. To do this you can run the deploy command using the CLI:

You can generate a deployment key from the explorer app, where you can login using your wallet.

By default, this command will run code generation and compilation, then deploy the generated artifacts from the build directory. You can skip the build steps by passing --skip-compile if you already have up-to-date artifacts.

You can specify a different input/output directory using the following parameters:

This command will upload your artifacts to the Mimic Registry (which stores them on IPFS) and pin the resultant CID so it can be discovered by others. The CID is also written to CID.json in the specified output directory.


Trigger your function

After deploying your function, you can now add a trigger, to tell which trigger relayers should use to run your function. This means defining the parameters declared in your manifest.yml file. This is done in the explorer UIarrow-up-right where you will be requested to sign your trigger with your wallet or with the SDK.

  1. Open the explorer and locate the function you just deployed under the functions section.

  2. Add or edit your trigger parameters

  3. Sign the new trigger

This signature ensures relayers know the trigger is authorized by the function owner.

You can update or deprecate this trigger at any point in time to reflect changes, without needing to redeploy the function code again. Just sign the new trigger in the explorer, and relayers will pick up the latest version.


Next steps

  1. Read more about the Lib

    Learn about available APIs for creating intents and interacting with contracts.

  2. Learn more about the CLI

    Discover advanced CLI commands for testing, validating, and managing functions.

  3. Build more complex use cases Expand your automation examples.

Last updated