Watch Now: Learn more by watching our video Supercharge your Web3 Function, available on YouTube.
Installation
Configuration
Create a.env
file in your project root with the following variables:
TypeScript Function Example
This TypeScript function updates an oracle smart contract with data returned by Coingecko’s price API at an interval. Check out more examples here.schema.json
to specify your runtime configuration:
TypeScript Function Context
When writing the Web3 Function, it is very helpful to understand the context Gelato injects into the execution, providing additional features to widen the Web3 Functions applicability.User Arguments
Declare your expected userArgs in your schema.json, accepted types are string, string[], number, number[], boolean, boolean[]:userArgs.json
and fill in your userArgs to test your web3 function:
State / Storage
Web3Functions are stateless scripts, that will run in a new & empty memory context on every execution. If you need to manage some state variable, we provide a simple key/value store that you can access from your web3 function context. See the above example to read & update values from your storage:storage.json
and fill in the storage values:
Secrets
In the same directory as your web3 function, create a.env
file and fill up your secrets:
Multichain Provider
ThemultichainProvider
allows us to instantiate RPC providers for every network Gelato is deployed on.
.env
at the root folder:
Interoperability with Other Libraries
AlthoughmultiChainProvider
is designed to work seamlessly within the Gelato Web3 Functions SDK, it is possible to extract the underlying RPC URL and use it with other client libraries. This flexibility is valuable for developers who prefer or require features from other libraries, such as viem
.
Here’s an example of how to utilize the RPC URL from multiChainProvider
with the viem
library:
Gelato Arguments
Gelato injects thechainId
, the gasPrice
, and the taskId
into the context.
- chainId: The unique number identifying the blockchain network where the function is running.
- gasPrice: The cost of executing transactions on the blockchain.
- taskId: A string that uniquely identifies the task.