Gelato
  • Introduction
    • Gelato, The Web3 Cloud Platform
  • Smart Wallets
    • Introduction
      • Understanding EIP-7702
      • Understanding ERC-4337
      • ERC-4337 vs EIP-7702
    • Templates & Examples
    • How-To Guides
      • Create a Sponsor API Key
      • Sponsor gas for your users
      • Allow users to pay gas with erc20
      • Allow users to pay gas with native
      • Create Dynamic's Environment Id
      • Use Dynamic/Privy signers with React SDK
      • Estimate Gas for your transactions
    • React SDK
    • Demo
    • Supported Networks
  • Rollup As A Service
    • Introduction
    • Rollup Stacks
      • Arbitrum Orbit
        • Run a Full Orbit Node
      • OP Stack
        • Run OP Node
    • Deploy your Rollup
    • Customization
      • Data Availability
        • Celestia
        • Avail
        • Eigen DA
      • Custom Gas Token
      • Marketplace
        • Gelato Services
        • Data Indexers
        • Block Explorers
        • Oracles
        • Bridges
        • Account Abstraction
        • On & Off-ramp
        • Community
        • Identity & KYC
        • Others
      • Verifier Node Package
    • Public Testnet
  • RPC Nodes
    • Introduction
    • Compute Units
    • Using RPC Nodes
    • Supported Networks
    • Pricing and Plans
    • FAQ
  • Web3 Services
    • Web3 Functions
      • Understanding Web3 Functions
        • Trigger Types
        • Typescript Function
        • Solidity Function
        • Automated Transactions
      • Security Considerations
      • Template & Use Cases
      • Quick Start
        • Writing Typescript Functions
          • Event Trigger
          • Private Typescript Functions
          • Callbacks
        • Test, Deploy & Run Typescript functions
        • Writing Solidity Functions
        • Test, Deploy & Run Solidity Functions
        • Initiate an Automated Transaction
      • Create a Web3 Function Task
        • Using the UI
        • Using the Safe App
        • Using a Smart Contract
        • Using the Automate SDK
      • Analytics & Monitoring
      • Supported Networks
      • Subscription & Payments
      • Legacy Automate Migration Guide
    • Relay
      • What is Relaying?
      • Security Considerations
        • ERC-2771 Delegatecall Vulnerability
      • Templates
      • Quick Start
        • Sponsored Calls
        • Non-Sponsored Calls
      • ERC-2771 (recommended)
        • SponsoredCallERC2771
        • CallWithSyncFeeERC2771
          • Relay Context Contracts ERC2771
      • Non-ERC-2771
        • SponsoredCall
        • CallWithSyncFee
          • Relay Context Contracts
      • Relay API
      • Gelato's Fee Oracle
      • Tracking your Relay Request
      • Supported Networks
      • Subscriptions and Payments
        • 1Balance & Relay
        • SyncFee Payment Tokens
        • Relay Pricing
      • ERC2771 Migration Guide
    • VRF
      • Understanding VRF
      • How does Gelato VRF Work?
      • Security Considerations
      • Template
      • Quick Start
      • Create a VRF Task
        • Create a Fallback VRF
        • Migrating from Chainlink VRF
      • Supported Networks
      • Pricing & Rate Limits
    • Oracles
      • Understanding Gelato Oracles
      • Quick Start
      • Data Providers
        • Stork
        • Choas Labs
      • Migrating from Chainlink Oracles
      • Available Price Feeds
      • Supported Networks
      • Pricing & Rate Limits
    • Account Abstraction
      • Understanding ERC-4337
      • Introduction to Gelato Bundler
      • Templates & Examples
      • Quick Start
      • Supported Networks
      • Bundler API Endpoints
        • eth_sendUserOperation
        • eth_estimateUserOperationGas
        • eth_getUserOperationByHash
        • eth_getUserOperationReceipt
        • eth_supportedEntryPoints
        • eth_maxPriorityFeePerGas
        • eth_chainId
    • 1Balance
      • 1Balance Alerts
      • Subscription Plans
      • Subscription Notifications
      • USDC Addresses
    • AI Agents
    • Teams
  • GELATO DAO
    • DAO & Token (GEL)
    • GEL Token Contracts
    • Governance Process
  • Social Media
Powered by GitBook
On this page
  • Deployed Typescript Functions are public
  • Private Typescript Function example
  • Writing onRun.js
  • 1. onRun.js file structure
  • 2. Using dependencies
  • 3. Accessing Web3 Function Context
  • 4. Return Web3 Function result
  • Creating private Typescript Function task
  • Secrets (strict)
  • Arguments (not strict)
  1. Web3 Services
  2. Web3 Functions
  3. Quick Start
  4. Writing Typescript Functions

Private Typescript Functions

PreviousEvent TriggerNextCallbacks

Last updated 1 month ago

Deployed Typescript Functions are public

When you deploy a Typescript Function the code is stored and pinned on IPFS making it accessible to everyone. If you would prefer to conceal your code, one approach is to store your code in a private Github Gist. Subsequently, this code can be retrieved and executed through a Web3 Function.

Note: this approach introduces a dependency on Github's availability. We aim to directly support private Web3 Function deployments in the future.

Private Typescript Function example

This Typescript Function fetches onRun.js (Github gist containing concealed code) with its gist id and executes it during runtime. Check out the example on GitHub .

The code in onRun.jsmust be in JavaScript

private-w3f/index.ts
import {
  Web3Function,
  Web3FunctionContext,
  Web3FunctionResult,
} from "@gelatonetwork/web3-functions-sdk";
import { Octokit } from "octokit";

// import dependencies used in onRun.js
import { ethers } from "ethers";
import ky from "ky";

Web3Function.onRun(async (context: Web3FunctionContext) => {
  const { secrets } = context;

  const gistId = (await secrets.get("GIST_ID")) as string;

  const octokit = new Octokit();

  let onRunScript: string | undefined;

  // fetch onRun.js from private github gist
  try {
    const gistDetails = await octokit.rest.gists.get({
      gist_id: gistId,
    });

    const files = gistDetails.data.files;

    if (!files) throw new Error(`No files in gist`);

    for (const file of Object.values(files)) {
      if (file?.filename === "onRun.js" && file.content) {
        onRunScript = file.content;
        break;
      }
    }

    if (!onRunScript) throw new Error(`No onRun.js`);
  } catch (err) {
    return {
      canExec: false,
      message: `Error fetching gist: ${err.message}`,
    };
  }

  // run onRun.js
  try {
    /**
     * context are passed into onRun.js.
     * onRun.js will have access to all userArgs, secrets & storage
     */
    const onRunFunction = new Function("context", "ky", "ethers", onRunScript);
    const onRunResult: Web3FunctionResult = await onRunFunction(
      context,
      ky,
      ethers
    );

    if (onRunResult) {
      return onRunResult;
    } else {
      return { canExec: false, message: `No result returned` };
    }
  } catch (err) {
    console.log(err);
    return {
      canExec: false,
      message: `Error running gist: ${err.message}`,
    };
  }
});

Writing onRun.js

1. onRun.js file structure

onRun.js should return a promise.

onRun.js
return (async () => {
  // ... your code here
})();

2. Using dependencies

Dependencies that are used in onRun.js should be imported into the Web3 Function index.ts file, not in onRun.js.

private-w3f/index.ts
// import dependencies used in onRun.js
import { ethers } from "ethers";
import ky from "ky";

3. Accessing Web3 Function Context

Web3 Function context which includes, secrets, userArgs, multiChainProvider can be accessed normally in onRun.js .

onRun.js
return (async () => {
    const {secrets, userArgs, multiChainProvider} = context
})();

4. Return Web3 Function result

Results returned in onRun.js will be bubbled up and returned in the private Web3 Function.

onRun.js
return {
  canExec: true,
  callData: [
    {
      to: oracleAddress,
      data: oracle.interface.encodeFunctionData("updatePrice", [price]),
    },
  ],
}j

Creating private Typescript Function task

Secrets (strict)

  • GIST_ID (Github gist id to fetch onRun.js from)

Make sure to store your GitHub gist id as a secret.

Arguments (not strict)

Since GitHub gists are editable, you can have a userArgs to be a JSON string so that arguments can be editable without re-deploying a web3 function with a different schema.

private-w3f/schema.json
{
  "web3FunctionVersion": "2.0.0",
  "runtime": "js-1.0",
  "memory": 128,
  "timeout": 30,
  "userArgs": {
    "args": "string"
  }
}

Example args when creating your task:

{
  "args": "{\"currency\":\"ethereum\",\"oracle\":\"0x71B9B0F6C999CBbB0FeF9c92B80D54e4973214da\"}"

Check out an example of a GitHub gist with onRun.js .

here
here