Creating Web3 Function Task
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 target smart contract is deployed:

If you select a network that your wallet is not currently connected to you will be prompted to switch to the target network.
Enter your target contract address and your contract's ABI will be fetched and you will be prompted to enter the function on it that you want to be executed.

If your contract is not verified you can manually enter an ABI by switching to a custom ABI
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
OpsTaskCreator
which has helper functions to easily create your task. - Pass
Module.WEB3_FUNCTION
as a module 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": "1.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);
}
Last modified 7d ago