Skip to content
This repository has been archived by the owner on Jun 19, 2024. It is now read-only.
/ zkOracle-OCW Public archive

The zkOracle/Off-chain worker for MINA protocol, Feed On-chain Data with Worker. Such as, ETH price, MINA or DOT from External API.

Notifications You must be signed in to change notification settings

ubinix-warun/zkOracle-OCW

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

37 Commits
 
 
 
 
 
 
 
 

Repository files navigation

zkOracle and Off-chain worker

the zkApps Hackathon -- zkIgnite, Cohort 0 Begins

Oracles on Mina -- authenticated data from any HTTPS website available on chain.

The zkOracle/Off-chain worker for MINA protocol, Feed On-chain Data with Worker.
Such as, ETH price, MINA or DOT from External API.

Setup contract and Keygen

  • Install nodejs package on contracts.
cd zkOracle-OCW/contracts

npm install
  • Generate key for backends/contracts and testing.
node scripts/keygen.js > scripts/key.json
  • Validate key on key.json file.
cat scripts/key.json
{
  "privateKey": "<>",
  "publicKey": "<>"
}

Run Offchain Worker.

cd zkOracle-OCW/contracts

npm run build
node build/src/worker.js

Sample console log: "run worker for MINA/USD price @16/12/22"

OCW: SnarkyJS loaded
Using fee payer account with nonce 16, balance 45600000000
Compiling smart contract...
warning: using a `utils.ts` written before `isProved` made available. Check https://docs.minaprotocol.com/zkapps/tutorials/deploying-to-a-live-network for updates
OCW: current value of roundId is 7
Creating an execution proof...
creating proof took 41.937 seconds
Sending the transaction...
See transaction at https://berkeley.minaexplorer.com/transaction/CkpZXjwhrjELZE4LV8MxitPTwmwy8TPp1L1aMeqNPmy8kFnqYKdVR
waiting for zkApp state to change... (current state:  7)
......................
OCW: current value of roundId is 8
request https://min-api.cryptocompare.com/data/pricemultifull?fsyms=MINA&tsyms=USD
      - offchain-value 'RAW.MINA.USD.PRICE' = 0.493
      - onchain-value 'RAW.MINA.USD.PRICE' = 0.493
Creating an execution proof...
creating proof took 41.357 seconds
Sending the transaction...
See transaction at https://berkeley.minaexplorer.com/transaction/CkpZ3h6QXArsYe1JGgcG1xmZqTm3tzDF2q8J5fdxHYipVfdv9UFk6
waiting for zkApp state to change... (current state:  490)
............................................................

Demo -- 13 min


Testing on LocalBlockchain

cd zkOracle-OCW/contracts
npm run build
cd zkOracle-OCW/backends/offchain-price-signer
npm install
npm run start
  • Run test-script, call contract and simulate operator.
cd zkOracle-OCW/contracts
npm run test

Test results: 6 passed, 6 total


 PASS  src/OffchainOracleLocal.test.ts (101.53 s)
  OffchainOracle
    ✓ generates and deploys the `OffchainOracle` smart contract (9209 ms)
    simulate single operator
      ✓ create nextRound event for demo ocw (Off-chain worker) (30867 ms)
      ✓ call feed Data for demo ocw (Off-chain worker) (12556 ms)
    actual API requests
      ✓ call feed ETH price for demo ocw (Off-chain signer) (12523 ms)
      ✓ call feed MINA price for demo ocw (Off-chain signer) (12524 ms)
      ✓ call feed DOT price for demo ocw (Off-chain signer) (12793 ms)

Test Suites: 1 passed, 1 total
Tests:       6 passed, 6 total
Snapshots:   0 total
Time:        101.618 s
Ran all test suites.
  ●  process.exit called with "0"

Sample test case: Call price-signer, feed MINA price to on chain.
    ...
    
    const response = await fetch('http://localhost:3000/MINA/0');
    const data = await response.json();

    const roundId = Field(data.data.roundId); // First round's Zero
    const priceData = Field(data.data.price.onchain);
    const signature = Signature.fromJSON(data.signature);

    // Operator call feed fn, fetch 'newFeeddata' event.
    const txnFeed = await Mina.transaction(deployerAccount, () => {
      zkAppInstance.feed(
        roundId,
        priceData,
        signature ?? fail('something is wrong with the signature')
      );
    });
    await txnFeed.prove();
    await txnFeed.send();
    
    const eventsFeed = await zkAppInstance.fetchEvents();
    expect(eventsFeed[0].type).toEqual('newFeedData');
    
    ...
    

Deploy to Berkeley Network

  • Setting berkeley network via zk-cli.
cd zkOracle-OCW/contracts
zk config

-- Name: berkeley
-- URL: https://proxy.berkeley.minaexplorer.com/graphql
-- Fee: 0.1

  • Deploy OffchainOracle to Berkeley network.
zk deploy

...

  ┌────────────────┬─────────────────────────────────────────────────┐
  │ Network        │ berkeley                                        │
  ├────────────────┼─────────────────────────────────────────────────┤
  │ Url            │ https://proxy.berkeley.minaexplorer.com/graphql │
  ├────────────────┼─────────────────────────────────────────────────┤
  │ Smart Contract │ OffchainOracle                                  │
  └────────────────┴─────────────────────────────────────────────────┘
  

Credit

  • MINA Protocol - Mina is building the privacy and security layer for web3.
  • mina-credit-score-signer - Koa API that signs a specified user’s fake credit score with a Mina compatible signature scheme.
  • chainlink-polkadot - This pallet allows your substrate built parachain/blockchain to interract with chainlink.

About

The zkOracle/Off-chain worker for MINA protocol, Feed On-chain Data with Worker. Such as, ETH price, MINA or DOT from External API.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published