Skip to content

Commit

Permalink
feat: upgrade zkGraph api
Browse files Browse the repository at this point in the history
  • Loading branch information
murongg committed Dec 6, 2023
1 parent 3a6d3c4 commit e91f396
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 18 deletions.
16 changes: 12 additions & 4 deletions packages/zkgraph-cli/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,14 +151,22 @@ Usage cases:
})

cli
.command('publish <deployed contract address> <ipfs_hash> <bounty_reward_per_trigger>', 'Publish and Register zkGraph Onchain')
.command('publish <ipfs_hash> <bounty_reward_per_trigger>', 'Publish and Register zkGraph Onchain')
.example('zkgraph publish 0x00000000000000000000000000000000 0x00000000000000000000000000000000 100')
.usage(`publish <deployed contract address> <ipfs_hash> <bounty_reward_per_trigger>
.usage(`publish <ipfs_hash> <bounty_reward_per_trigger>
ipfs_hash: by finishing upload get it
`)
.action((contractAddress, ipfsHash, bountyRewardPerTrigger) => {
publish({ contractAddress, ipfsHash, bountyRewardPerTrigger, yamlPath: config.YamlPath, jsonRpcProviderUrl: config.JsonRpcProviderUrl, userPrivateKey: config.UserPrivateKey })
.action((ipfsHash, bountyRewardPerTrigger) => {
publish({
ipfsHash,
bountyRewardPerTrigger,
yamlPath: config.YamlPath,
wasmPath: config.WasmBinPath,
jsonRpcProviderUrl: config.JsonRpcProviderUrl,
userPrivateKey: config.UserPrivateKey,
zkWasmProviderUrl: config.ZkwasmProviderUrl,
})
})

cli
Expand Down
4 changes: 3 additions & 1 deletion packages/zkgraph-cli/src/commands/exec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import fs from 'node:fs'
import to from 'await-to-js'
import * as zkgapi from '@hyperoracle/zkgraph-api'
import ethres from 'ethers'
import { generateDspHubParams, loadJsonRpcProviderUrl, toHexString } from '../utils'
import { logger } from '../logger'
import type { UserConfig } from '../config'
Expand Down Expand Up @@ -36,13 +37,14 @@ export async function exec(options: ExecOptions) {
logger.info(`[*] Run zkgraph on block ${realParams?.blockId}`)

const jsonRpcUrl = loadJsonRpcProviderUrl(zkgraphYaml, jsonRpcProviderUrl, true)
const provider = new ethres.providers.JsonRpcProvider(jsonRpcUrl)

const wasm = fs.readFileSync(wasmPath)
const wasmUint8Array = new Uint8Array(wasm)

const execParams = dsp.toExecParams(
{
jsonRpcUrl,
provider,
...realParams,
},
)
Expand Down
4 changes: 2 additions & 2 deletions packages/zkgraph-cli/src/commands/prove.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export async function prove(options: ProveOptions) {

const proveParams = dsp.toProveParams(
{
jsonRpcUrl,
provider,
...realParams,
},
)
Expand Down Expand Up @@ -195,7 +195,7 @@ async function proveMode(userPrivateKey: string, md5: string, privateInputStr: s
logger.info('[+] Transaction Confirmed. Creating Prove Task')

const data = await dispatcher.queryTask(txhash)
const taskId = data.task.id
const taskId = data.task?.id
if (!taskId) {
logger.error('[+] PROVE TASK FAILED. \n')
return
Expand Down
13 changes: 9 additions & 4 deletions packages/zkgraph-cli/src/commands/publish.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
import fs from 'node:fs'
import { ethers } from 'ethers'
import * as zkgapi from '@hyperoracle/zkgraph-api'
import { logger } from '../logger'
import { loadJsonRpcProviderUrl, logDivider } from '../utils'
import type { UserConfig } from '../../dist/index.cjs'

export interface PublishOptions {
contractAddress: string
ipfsHash: string
bountyRewardPerTrigger: number
yamlPath: string
wasmPath: string
jsonRpcProviderUrl: UserConfig['JsonRpcProviderUrl']
userPrivateKey: string
zkWasmProviderUrl: string
}

export async function publish(options: PublishOptions) {
const { contractAddress, ipfsHash, jsonRpcProviderUrl, userPrivateKey, bountyRewardPerTrigger, yamlPath } = options
const { ipfsHash, jsonRpcProviderUrl, userPrivateKey, bountyRewardPerTrigger, yamlPath, wasmPath, zkWasmProviderUrl } = options
logger.info('>> PUBLISH ZKGRAPH')
if (isNaN(bountyRewardPerTrigger)) {
logger.warn('[-] BOUNTY REWARD IS NOT A VALID NUMBER.')
Expand All @@ -32,10 +34,13 @@ export async function publish(options: PublishOptions) {
const provider = new ethers.providers.JsonRpcProvider(JsonRpcProviderUrl)
const signer = new ethers.Wallet(userPrivateKey, provider)

const wasm = fs.readFileSync(wasmPath)
const wasmUint8Array = new Uint8Array(wasm)

const publishTxHash = await zkgapi.publish(
{ wasmUint8Array: null, zkgraphYaml },
{ wasmUint8Array, zkgraphYaml },
zkWasmProviderUrl,
provider,
contractAddress,
ipfsHash,
newBountyRewardPerTrigger,
signer,
Expand Down
15 changes: 9 additions & 6 deletions packages/zkgraph-cli/src/commands/verify.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// import fs from 'node:fs'
import { Error, ZkGraphYaml, verify as verifyApi } from '@hyperoracle/zkgraph-api'
import { Error, ZkGraphYaml, getVerifyProofParamsByTaskID, verify as verifyApi } from '@hyperoracle/zkgraph-api'
import { logger } from '../logger'
import { loadJsonRpcProviderUrl, logDivider } from '../utils'
import type { UserConfig } from '../config'
import { AggregatorVerifierAddress } from '../constants'
// import { AggregatorVerifierAddress } from '../constants'

export interface VerifyOptions {
taskId: string
Expand All @@ -25,12 +25,15 @@ export async function verify(options: VerifyOptions) {

// Get deployed verification contract address.
// TODO: I reused this func to save code, but the naming is a bit misleading, fix it later.
const verifierAddress = loadJsonRpcProviderUrl(zkgraphYaml, AggregatorVerifierAddress, false)
// const verifierAddress = loadJsonRpcProviderUrl(zkgraphYaml, AggregatorVerifierAddress, false)
const proofParams = await getVerifyProofParamsByTaskID(taskId, zkWasmProviderUrl)

const verifyResult = await verifyApi(
taskId,
zkWasmProviderUrl,
verifierAddress || '',
{
zkgraphYaml,
wasmUint8Array: null,
},
proofParams,
jsonRpcUrl || '',
).catch((error: Error) => {
if (error instanceof Error.ProveTaskNotReady)
Expand Down
2 changes: 1 addition & 1 deletion packages/zkgraph-cli/src/utils/dsp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export function getDspHubParams() {
return generateDspHubParamsMap(hub)
}

export const DoNotIncludeParams = ['jsonRpcUrl']
export const DoNotIncludeParams = ['jsonRpcUrl', 'provider']

export function generateDspHubParamsMap(hub: Map<string, {
execParams: string[]
Expand Down

0 comments on commit e91f396

Please sign in to comment.