Skip to content
This repository was archived by the owner on Mar 3, 2024. It is now read-only.

Commit

Permalink
Merge pull request #55 from hyperoracle/dev
Browse files Browse the repository at this point in the history
Add task timing
  • Loading branch information
fewwwww authored Jul 29, 2023
2 parents 75978e2 + 4dab0b8 commit 6b23c59
Show file tree
Hide file tree
Showing 8 changed files with 140 additions and 69 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ export function handleEvents(events: Event[]): Bytes {
if (events.length > 0) {
state = events[0].address;
}
require(state.length == 20 ? 1 : 0);
require(state.length == 20);
return state;
}
```
Expand Down
10 changes: 9 additions & 1 deletion api/prove.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ import { config } from "../config.js";
import { zkwasm_prove } from "./requests/zkwasm_prove.js";
import { readFileSync, writeFileSync } from "fs";
import { ZkWasmUtil } from "zkwasm-service-helper";
import { waitTaskStatus } from "./requests/zkwasm_taskdetails.js";
import {
waitTaskStatus,
taskPrettyPrint,
} from "./requests/zkwasm_taskdetails.js";
import { instantiateWasm, setupZKWasmMock } from "./common/bundle.js";

program.version("1.0.0");
Expand Down Expand Up @@ -229,6 +232,11 @@ switch (options.inputgen || options.test || options.prove) {
"\n",
);

taskPrettyPrint(taskDetails, "[*] ");

// Log extra new line before divider.
console.log();

logDivider();

process.exit(0);
Expand Down
25 changes: 24 additions & 1 deletion api/requests/zkwasm_taskdetails.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,32 @@ export async function waitTaskStatus(
});
}

function millToHumanReadable(mill) {
let min = Math.floor(mill / 60000);
let sec = (mill % 60000) / 1000;
return `${min} min ${sec} sec`;
}

export function taskPrettyPrint(resData, prefix = "") {
console.log(`${prefix}Task submit time: ${resData.submit_time}`);
console.log(`${prefix}Process started: ${resData.process_started}`);
console.log(`${prefix}Process finished: ${resData.process_finished}`);
console.log(
`${prefix}Pending time: ${millToHumanReadable(
new Date(resData.process_started) - new Date(resData.submit_time),
)}`,
);
console.log(
`${prefix}Running time: ${millToHumanReadable(
new Date(resData.process_finished) - new Date(resData.process_started),
)}`,
);
}

// try{
// let a = await waitTaskStatus('64c0c2bbf0e3eee93f75c260', ['Done', 'Fail'], 100);
// console.log(a)
// // console.log(a)
// taskPrettyPrint(a, '[*] ')
// }catch(error) {
// console.log(error)
// }
Expand Down
10 changes: 9 additions & 1 deletion api/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ import {
currentNpmScriptName,
} from "./common/utils.js";
import { zkwasm_setup } from "./requests/zkwasm_setup.js";
import { waitTaskStatus } from "./requests/zkwasm_taskdetails.js";
import {
waitTaskStatus,
taskPrettyPrint,
} from "./requests/zkwasm_taskdetails.js";
import path from "path";

let wasmPath;
Expand Down Expand Up @@ -78,6 +81,11 @@ if (isSetUpSuccess) {
"\n",
);

// Log extra new line before divider.
console.log();

taskPrettyPrint(taskDetails, "[*] ");

logDivider();

process.exit(0);
Expand Down
67 changes: 67 additions & 0 deletions example/uniswapprice/mapping.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
//@ts-ignore
import { require } from "../lib/common/zkwasm";
import { Bytes, Event, BigInt } from "../lib/common/type";

var esig_sync = Bytes.fromHexString(
"0x1c411e9a96e071241c2f21f7726b17ae89e3cab4c78be50e062b03a9fffbbad1",
);
var esig_swap = Bytes.fromHexString(
"0xd78ad95fa46c994b6551d0da85fc275fe613ce37657fb8d5e3d130840159d822",
);

var token0_decimals = 6;
var token1_decimals = 18;
var price_decimals = 3;

var threshold_eth_price = 1880;

var token0_factor = BigInt.from(10).pow(token1_decimals);
var token1_factor = BigInt.from(10).pow(token0_decimals);
var price_factor = BigInt.from(10).pow(price_decimals);

function calcPrice(syncEvent: Event): BigInt {
const source = changetype<Bytes>(syncEvent.data);
const reserve0 = source.slice(0, 32);
const reserve1 = source.slice(32, 64);

const r0 = BigInt.fromBytesBigEndian(reserve0);
const r1 = BigInt.fromBytesBigEndian(reserve1);
let price0 = r0
.times(token0_factor)
.times(price_factor)
.div(r1.times(token1_factor));

return price0;
}

export function handleEvents(events: Event[]): Bytes {
let lastSyncEvent: Event | null = null;

for (let i = events.length - 1; i >= 0; i--) {
if (events[i].esig == esig_sync) {
// console.log('SYNC event');
lastSyncEvent = events[i];
break;
}
}

if (lastSyncEvent == null) {
// Don't Trigger if there's no event in the block
require(false);
return Bytes.empty(); // Omit compile error, never goes here
} else {
let price0 = calcPrice(lastSyncEvent);

// console.log("Current price is: " + (price0.toI64() / 10**price_decimals).toString() + "." + (price0.toI64() % 10**price_decimals).toString())

// Only Trigger when price > pre-defined threshold
let triggerCondition = price0.ge(
BigInt.fromI32(threshold_eth_price * 10 ** price_decimals),
);
require(triggerCondition);

// Set payload to the current price0 when triggering destination contract.
let payload = Bytes.fromHexString(price0.toString(16)).padStart(32, 0);
return payload;
}
}
21 changes: 21 additions & 0 deletions example/uniswapprice/zkgraph.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
specVersion: 0.0.2
description: Demo graph for zkAutomation.
repository: https://github.com/hyperoracle/zkgraph
dataSources:
- kind: ethereum/contract
network: mainnet
source:
address: '0xb4e16d0168e52d35cacd2c6185b44281ec28c9dc'
mapping:
kind: ethereum/events
apiVersion: 0.0.1
language: wasm/assemblyscript
file: ./mapping.ts
eventHandlers:
- event: Sync(uint112,uint112)
handler: handleEvents
dataDestinations:
- kind: ethereum/contract
network: mainnet
destination:
address: '0x123abc'
65 changes: 5 additions & 60 deletions src/mapping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,66 +2,11 @@
import { require } from "../lib/common/zkwasm";
import { Bytes, Event, BigInt } from "../lib/common/type";

var esig_sync = Bytes.fromHexString(
"0x1c411e9a96e071241c2f21f7726b17ae89e3cab4c78be50e062b03a9fffbbad1",
);
var esig_swap = Bytes.fromHexString(
"0xd78ad95fa46c994b6551d0da85fc275fe613ce37657fb8d5e3d130840159d822",
);

var token0_decimals = 6;
var token1_decimals = 18;
var price_decimals = 3;

var threshold_eth_price = 1880;

var token0_factor = BigInt.from(10).pow(token1_decimals);
var token1_factor = BigInt.from(10).pow(token0_decimals);
var price_factor = BigInt.from(10).pow(price_decimals);

function calcPrice(syncEvent: Event): BigInt {
const source = changetype<Bytes>(syncEvent.data);
const reserve0 = source.slice(0, 32);
const reserve1 = source.slice(32, 64);

const r0 = BigInt.fromBytesBigEndian(reserve0);
const r1 = BigInt.fromBytesBigEndian(reserve1);
let price0 = r0
.times(token0_factor)
.times(price_factor)
.div(r1.times(token1_factor));

return price0;
}

export function handleEvents(events: Event[]): Bytes {
let lastSyncEvent: Event | null = null;

for (let i = events.length - 1; i >= 0; i--) {
if (events[i].esig == esig_sync) {
// console.log('SYNC event');
lastSyncEvent = events[i];
break;
}
}

if (lastSyncEvent == null) {
// Don't Trigger if there's no event in the block
require(false);
return Bytes.empty(); // Omit compile error, never goes here
} else {
let price0 = calcPrice(lastSyncEvent);

// console.log("Current price is: " + (price0.toI64() / 10**price_decimals).toString() + "." + (price0.toI64() % 10**price_decimals).toString())

// Only Trigger when price > pre-defined threshold
let triggerCondition = price0.ge(
BigInt.fromI32(threshold_eth_price * 10 ** price_decimals),
);
require(triggerCondition);

// Set payload to the current price0 when triggering destination contract.
let payload = Bytes.fromHexString(price0.toString(16)).padStart(32, 0);
return payload;
let state = new Bytes(0);
if (events.length > 0) {
state = events[0].address;
}
require(state.length == 20);
return state;
}
9 changes: 4 additions & 5 deletions test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@
# Update `config.js` with your own parameters first!
# Then run `sh test.sh`

npm install &&
npm run compile-local &&
npm run exec-local -- 17633573 &&
npm run prove-local -- --inputgen 0x10d1125 0x0000000000000000000000000000000000000000000000000466ba8760e97fb2 &&
npm run prove-local -- --test 0x10d1125 0x0000000000000000000000000000000000000000000000000466ba8760e97fb2
npm run prove-local -- --inputgen 0x10d1125 b4e16d0168e52d35cacd2c6185b44281ec28c9dc &&
npm run prove-local -- --test 0x10d1125 b4e16d0168e52d35cacd2c6185b44281ec28c9dc

npm run compile &&
npm run exec -- 17633573 &&
npm run prove -- --inputgen 0x10d1125 0000000000000000000000000000000000000000000000000466ba8760e97fb2 &&
npm run prove -- --test 0x10d1125 0000000000000000000000000000000000000000000000000466ba8760e97fb2
npm run prove -- --inputgen 0x10d1125 b4e16d0168e52d35cacd2c6185b44281ec28c9dc &&
npm run prove -- --test 0x10d1125 b4e16d0168e52d35cacd2c6185b44281ec28c9dc

0 comments on commit 6b23c59

Please sign in to comment.