Simple ERC20 Transfer

Introduction

We will make a simple ERC20 Transfer FCT, where we will send KIRO tokens.

First, we need to import all the necessary tools

import { BatchMultiSigCall } from "@kirobo/fct-core"
import { ERC20 } from "@kirobo/fct-plugins"

BatchMultiSigCall is the class for FCT building

Build an ERC20 Transfer Plugin

Learn more about plugins at FCT Plugins documentation.

const transfer = new ERC20.actions.Transfer({
    chainId: 1,
})

After checking the ERC20 Transfer Plugin documentation, we can determine that the transfer plugin requires 3 parameters - to (token address), methodParams.recipient (who receives the tokens), and methodParams.amount (how many tokens to send)

Another way to get the required parameters is by calling:

const params = transfer.input.get()

Set parameters to ERC20 Transfer plugin

There are 2 ways to set parameters for plugins.

The first way is by calling transfer.input.set function

transfer.input.set({
    to: "0xB1191F691A355b43542Bea9B8847bc73e7Abb137", // KIRO Address
    methodParams: {
        recipient: "0x...", // Address, which is going to receive the tokens
        amount: "10" + "0".repeat(18) // Sending 10 tokens. (10 + 18 decimals)
    }
})

The second way is by calling each parameter separately

transfer.input.to.set({ value: "0xB1191F691A355b43542Bea9B8847bc73e7Abb137" })

transfer.input.methodParams.recipient.set({ value: "0x..." })

transfer.input.methodParams.amount.set({ value: "10" + "0".repeat(18) })

Now we have a plugin that has all of the required parameters. If you want to make sure your input values are valid you can use, which returns boolean:

const isValid = transfer.input.isValid

Build the FCT from the ERC20 Transfer plugin

First, we need to construct BatchMultiSigCall class.

const batchMultiSigCall = new BatchMultiSigCall({
     provider: new ethers.providers.JsonRpcProvider("..."),
     chainId: 1,
})

Once we have BatchMultiSigCall constructed, now we can add the plugin to BatchMultiSigCall class by calling create() function

await batchMultiSigCall.create({
    plugin: transfer,
    from: "0x...", // Here we put the vault address, from whom the transfer will happen
    nodeId: "ERC20_Transfer"
})

Now we have an FCT that is ready to be exported!

const FCT = await batchMultiSigCall.exportFCT()

Congratulations, you built your first FCT!

Full code example

import { BatchMultiSigCall } from "@kirobo/fct-core"
import { ERC20 } from "@kirobo/fct-plugins"

const transfer = new ERC20.actions.Transfer({
    chainId: 1,
})

transfer.input.set({
    to: "0xB1191F691A355b43542Bea9B8847bc73e7Abb137", // KIRO Address
    methodParams: {
        recipient: "0x...", // Address, which is going to recieve the tokens
        amount: "10" + "0".repeat(18) // Sending 10 tokens. (10 + 18 decimals)
    }
})

const batchMultiSigCall = new BatchMultiSigCall({
     provider: new ethers.providers.JsonRpcProvider("..."),
     chainId: 1,
})

await batchMultiSigCall.create({
    plugin: transfer,
    from: "0x...", // Here we put the vault address, from whom the transfer will happen
    nodeId: "ERC20_Transfer"
})

const FCT = await batchMultiSigCall.exportFCT()

// Use FCT.typedData to sign on to the FCT. If you are using @metamask/eth-sig-util
// to sign on EIP712 transaction, this is an example of how to sign on to the FCT

import { signTypedData, SignTypedDataVersion } from "@metamask/eth-sig-util";

const signature = signTypedData({
    data: FCT.typedData,
    privateKey: Buffer.from(key, "hex"),
    version: SignTypedDataVersion.V4
})

Last updated