Build a simple task
This guide provides step-by-step instructions on how to build and deploy a task to automate a blockchain operation using Mimic Protocol.
Before you begin, ensure you have installed Mimic Protocol’s CLI:
yarn global add @mimicprotocol/cliWorkflow overview
The process consists of the following steps:
Initialize your project: Start a working directory to develop your task from scratch.
Define the manifest: Specify task inputs, ABIs, and metadata in a manifest file.
Write the task logic: Implement your task logic.
Build: Validate the manifest, generate supporting artifacts, and compile the task logic.
Deploy: Upload your build output to a task registry, making it available for relayers to execute.
Initialize your project
Let's start a new working directory to develop your task. To do that you can run the following command:
mimic init -d my-mimic-taskLet's continue analyzing the structure of this project.
Manifest definition
The manifest file provides the configuration for your task, including:
Metadata: Name, description, and version of the task.
Inputs: Parameters required by the task logic.
ABIs: Smart contract ABIs to generate type-safe interfaces for the task.
Save this configuration in a manifest.yaml file:
version: 1.0.0
name: Balance Monitoring Task
description: Fulfill an account's balance based on threshold in USD
inputs:
- chainId: uint32
- token: address
- amount: uint256
- fee: uint256
- recipient: address
- thresholdUSD: uint32
abis:
- ERC20: './abis/ERC20.json'This manifest file defines different inputs that will be accessible from the task 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:
inputs:
- amount:
type: uint256
description: Amount to transfer in wei
- recipient:
type: address
description: Target account addressFor the purpose of this example you will the ERC20 ABI, you can copy it from here:
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:
mimic codegenThis 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:
mimic codegen --manifest manifest.yml --output src/typesIf you pass the --clean flag, you will be asked to confirm before deleting the existing contents of the output directory.
Writing the task logic
The task logic is implemented in AssemblyScript and must export:
Input type: The generated
inputstype from the previous step.Main function: The core task logic, which receives the inputs as an argument.
Create a file ./src/task.ts and implement the logic:
import { ERC20Token, TokenAmount, Transfer, USD } from '@mimicprotocol/lib-ts'
import { ERC20 } from './types/ERC20'
import { inputs } from './types'
export default function main(): void {
const tokenContract = new ERC20(inputs.token, inputs.chainId)
const balance = tokenContract.balanceOf(inputs.recipient)
const token = ERC20Token.fromAddress(inputs.token, inputs.chainId)
const balanceInUsd = TokenAmount.fromBigInt(token, balance).toUsd()
const thresholdUsd = USD.fromI32(inputs.thresholdUSD)
console.log('Balance in USD: ' + balanceInUsd.toString())
if (balanceInUsd.lt(thresholdUsd)) {
Transfer.create(token, inputs.amount, inputs.recipient, inputs.fee).send()
}
}As you can see, the generated contract artifacts can be accessed from your task code.
For now Mimic tasks allows creating three types of intents:
Transfers
Generic calls
Cross-chain swaps
Compile process
The compile process converts your task logic and manifest into deployable artifacts:
build/task.wasm- Compiled WebAssembly binarybuild/manifest.json- Processed manifest configuration
Run the compile command:
mimic compileBy default, outputs are saved in the build directory. You can customize the paths:
mimic compile --task src/task.ts --manifest manifest.yaml --output buildHere is an example of the output produced by this command:
build/
├── task.wasm # Compiled WASM binary
├── manifest.json # Validated manifestDeploy your task
This is where you upload your task artifacts to the network so others can discover it. To do this you can run the deploy command using the CLI:
mimic deploy --key [DEPLOYMENT_KEY]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:
mimic deploy --input build --output buildThis 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.
Config your task
After deploying your task, you can now configure it to tell which config relayers should use to run your task. This means defining the parameters declared in your manifest.yml file. This configuration is done in the explorer UI where you will be requested to sign your config with your wallet.
Open the explorer and locate the task you just deployed under the tasks section.
Add or edit your config parameters
Sign the new config
This signature ensures relayers know the config is authorized by the task owner.
You can update or deprecate this config at any point in time to reflect changes, without needing to redeploy the task code again. Just sign the new config in the explorer, and relayers will pick up the latest version.
Next steps
Learn about available APIs for creating intents and interacting with contracts.
Discover advanced CLI commands for testing, validating, and managing tasks.
Build more complex use cases Expand your automation examples.
Last updated