Skip to content

gelatodigital/w3f-pyth-poc-v2

Repository files navigation

Gelato Web3 functions <<-->> Pyth PoC

Gelato Web3 Functions together with Pyth offer the ability to create fine-tuned customized oracles pushing prices on-chain following predefined logic within the Web3 Function and verifying prices on-chain through the Pyth network.

We have three distinct demos in this repository, each demonstrating a different aspect:

  1. W3F to Pyth: this demo directly interacts with the Pyth network.

  2. W3F to Pyth Dynamic: this is an enhanced version of W3F to Pyth that allows dynamic configuration via Github Gist.

  3. W3F to Consumer Contract: this demo uses a consumer contract as an intermediary.

    In both cases the logic is the same, the prices are going to be updated following:

    • Push a price on-chain every hour
    • If the price changes by 2% or more in either direction since the last push, a new price will be pushed

Funding

We will fund Gelato and Pyth following this process:

  1. Gelato: The gelato fees are payed with 1Balance. 1Balance allows to deposit USDC on polygon and run the transactions on every network.

    To fund 1Balance please visit the 1balance app and deposit USDC. (The 1Balance account has to be created with the same address as the Web3 Function task)

  • Switch network to Polygon

  • Deposit USDC

  1. Pyth: The method that updates the price is payable, the update transaction has to include in the msg.value the corresponding fee:
    /// @notice Update price feeds with given update messages.
    /// This method requires the caller to pay a fee in wei; the required fee can be computed by calling
    /// `getUpdateFee` with the length of the `updateData` array.
    /// Prices will be updated if they are more recent than the current stored prices.
    /// The call will succeed even if the update is not the most recent.
    /// @dev Reverts if the transferred fee is not sufficient or the updateData is invalid.
    /// @param updateData Array of price update data.
    function updatePriceFeeds(bytes[] calldata updateData) external payable;

In our two demos we will transfer the fee differently:

  • W3F update pyth network
    The on-chain transaction executed via a web3 function gets routed through a proxy smart contract which is solely owned by the web3 function task creator. This dedicatedMsgSender proxy contract will be deployed with the first task created and will ultimately be responsible for transferring the fee to the Pyth contract.

    The address can be seen in your task dashboard (i.e example task dashboard)

    Once we have the dedicatedMsgSender address we will have to fund it for paying the fees. In our example we have sent funds to the dedicated MsgSender with this transaction

    Once the dedicatedMsgSender is funded, the Web3 Function will execute passing the fee as value in the callData:

    return {
      canExec: true,
      callData: [
        {
          to: pythNetworkAddress,
          data: callData,
          value: fee,
        },
      ],
    };
  • W3F Consumer Contract: The contract has to be funded with native tokens and then simply query the fee and include it in the transaction value.

        uint256 fee = _pyth.getUpdateFee(updatePriceData);
        _pyth.updatePriceFeeds{value: fee}(updatePriceData);

Demo W3F update directly Pyth Contract

Live on Mumbai

Demo W3F update Pyth consumer contract

Live on Mumbai

Development

How to run

  1. Install project dependencies:
yarn install
  1. Create a .env file with your private config:
cp .env.example .env

You will need to input your PROVIDER_URL, your RPC.

  1. Test the web3 function

W3F Pyth

npx w3f test web3-functions/pyth-oracle-w3f/index.ts --logs

W3F Consumer Contract

npx w3f test web3-functions/pyth-oracle-consumer-contract/index.ts --logs
  1. Deploy the web3 function on IPFS

W3F Pyth

npx w3f deploy web3-functions/pyth-oracle-w3f/index.ts

W3F Consumer Contract

npx w3f deploy web3-functions/pyth-oracle-consumer-contract/index.ts
  1. Create the task following the link provided when deploying the web3 to IPFS in our case:

W3F Pyth

 ✓ Web3Function deployed to ipfs.
 ✓ CID: QmSpBYETcncrJKbXGPjtdxn5L6i12joJ6kvWrCsNMP3XFD

To create a task that runs your Web3 Function every minute, visit:
> https://beta.app.gelato.network/new-task?cid=QmSpBYETcncrJKbXGPjtdxn5L6i12joJ6kvWrCsNMP3XFD

W3F Consumer Contract

 ✓ Web3Function deployed to ipfs.
 ✓ CID: QmdrJyVznzHBrzb3zjwxPWU35rCRQmiPTk1LFejeqHHeMt

To create a task that runs your Web3 Function every minute, visit:
> https://beta.app.gelato.network/new-task?cid=QmdrJyVznzHBrzb3zjwxPWU35rCRQmiPTk1LFejeqHHeMt

You can find a template/instructions for W3F Pyth with dynamic configuration values loaded from a Github gist that allows you to update priceIds and various parameters without having to re-deploy the task in web3-functions/pyth-oracle-w3f-priceIds.

W3F command options

  • --logs Show internal Web3Function logs
  • --runtime=thread|docker Use thread if you don't have dockerset up locally (default: thread)
  • --debug Show Runtime debug messages
  • --chain-id=number Specify the chainId (default is Goerli: 5)

Example: npx w3f test web3-functions/pyth-oracle-w3f-priceIds/index.ts --logs --debug

About

Updated Pyth PoC to Beta 2.0 and hardhat plugin

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published