Creating Web3 Function Tasks
Configuring what triggers your Web3 Function to run and the target smart contract to execute
A Web3 Function Task links your Web3 Function to your target smart contract and enables you to configure what should trigger your Web3 Function to execute, any arguments you want to pass to it and the target smart contract to execute.
In order to create a task that will automatically run your Web3 Function on your behalf, go to https://beta.app.gelato.network/ and click on the "Create Task" button.
You will be invited to select what should trigger your Web3 Function and which Web3 Function you want to execute:

Make sure to have the IPFS CID of your Web3 Function at hand and paste it into the respective input. We fetch your Web3 Function and enable you to review the source code and enter any arguments that you have created:

Select the network on which your task should execute:

If you select a network that your wallet is not currently connected to you will be prompted to switch to the target network.
If your task uses
secrets
to store any private credentials, you can then configure the secret variables that will be exposed in your web3 function:
Secrets are optional, leave this part empty if your web3 function is not using any private credentials.
Finally, you will be prompted to enter a name for your task. Click on "Create Task" and confirm your name and task creation using your wallet:

You will then be redirected to the task page where you can view logs, past executions and more task-related information.
You can create a task that uses Web3 Function from your smart contract as well. Learn more about creating tasks via smart contract.
Web3 Function secrets are not available for smart contract created tasks.
To create a Web3 Function task with your smart contract, you can inherit
AutomateTaskCreator
which has helper functions to easily create your task. - Pass the
Module.PROXY
&Module.WEB3_FUNCTION
as modules inModuleData
ModuleData memory moduleData = ModuleData({
modules: new Module[](2),
args: new bytes[](2)
});
moduleData.modules[0] = Module.PROXY;
moduleData.modules[1] = Module.WEB3_FUNCTION;
- Use
_web3FunctionModuleArg
to encode arguments forWEB3_FUNCTION
module.
function _web3FunctionModuleArg(
string memory _web3FunctionHash, // IPFS CID of deployed web3Function
bytes calldata _web3FunctionArgsHex // Abi encoded web3 function arguments
)
Here is how you can encode your Web3Function arguments to get
web3FunctionArgsHex.
In this example, the Web3Function has 2 arguments,
counterW3fAddress
& count
. schema.json
{
"web3FunctionVersion": "2.0.0",
"runtime": "js-1.0",
"memory": 128,
"timeout": 30,
"userArgs": {
"counterW3fAddress": "string",
"count", "number"
}
}
In your contract, you would encode the arguments according to the sequence defined in
schema.json
. function _getWeb3FunctionArgsHex(
address counterW3fAddress,
uint256 count
)
internal
pure
returns (bytes memory web3FunctionArgsHex) {
web3FunctionArgsHex = abi.encode(counterW3fAddress, count)
}
function createTask(
string memory _web3FunctionHash,
bytes calldata _web3FunctionArgsHex
) external {
require(taskId == bytes32(""), "Already started task");
bytes memory execData = abi.encodeCall(this.increaseCount, (1));
ModuleData memory moduleData = ModuleData({
modules: new Module[](2),
args: new bytes[](2)
});
moduleData.modules[0] = Module.PROXY;
moduleData.modules[1] = Module.WEB3_FUNCTION;
moduleData.args[0] = _proxyModuleArg();
moduleData.args[1] = _web3FunctionModuleArg(
_web3FunctionHash,
_web3FunctionArgsHex
);
bytes32 id = _createTask(
address(this),
execData,
moduleData,
address(0)
);
taskId = id;
emit CounterTaskCreated(id);
}
Use the
automate-sdk
to easily create a new task:yarn install @gelatonetwork/[email protected]
Make sure to use the
beta
version of the automate-sdk
to have access to web3 functions methodImport the sdk and create task, passing your web3 function CID & arguments:
import { AutomateSDK, Web3Function } from "@gelatonetwork/automate-sdk";
const automate = new AutomateSDK(chainId, deployer);
const { taskId, tx } = await automate.createBatchExecTask({
name: "Web3Function - Eth Oracle",
web3FunctionHash: cid,
web3FunctionArgs: {
oracle: oracle.address,
currency: "ethereum",
},
});
await tx.wait();
If your task utilizes secrets, you can set them after the task has been created.
const web3Function = new Web3Function(chainId, deployer);
const secrets = {
API_KEY: "..." // Set your secret environment variables
}
await web3Function.secrets.set(secrts, taskId);
Last modified 30d ago