This repository was archived by the owner on Mar 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #55 from hyperoracle/dev
Add task timing
- Loading branch information
Showing
8 changed files
with
140 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters