-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstrategy.js
54 lines (43 loc) · 1.87 KB
/
strategy.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
const snarkjs = require("snarkjs");
const fs = require("fs");
const { mapActions } = require("./action");
const { getPoolState } = require("./pool");
function alignNumber(value, interval) {
return Math.floor(value / interval) * interval;
}
async function run() {
const state = await getPoolState("0x6337b3caf9c5236c7f3d1694410776119edaf9fa", "0x54f3a4f7a85c70c0c0e99f42f92b0ac34a18c534");
if (state.positions.length > 1) {
throw new Error(`This strategy only handles up to 1 existing liquidity position but found ${state.positions.length}`);
}
const tick = alignNumber(state.tick, state.tickSpacing);
const myTick = alignNumber(state.positions?.[0]?.tickLower?.tickIdx ?? 0, state.tickSpacing);
const { proof, publicSignals } = await snarkjs.plonk.fullProve(
{
currentTick: tick,
tickSpacing: state.tickSpacing,
myTick: myTick,
myLiquidity: state.positions?.[0]?.liquidity ?? "0",
availableLiquidity: "1000000"
},
"../circuits/simplemm_js/simplemm.wasm", "../circuits/simplemm_final.zkey"
);
// Convert the data into Solidity calldata that can be sent as a transaction
const calldataBlob = await snarkjs.plonk.exportSolidityCallData(proof, publicSignals);
const calldata = calldataBlob.split(',');
// To be inserted into foundry tests
console.log("Calldata parsed (first proof, then public signals)", calldata);
const NUM_ACTIONS = 8;
const actions = mapActions(publicSignals.slice(0, NUM_ACTIONS * 4));
console.log(actions);
const vKey = JSON.parse(fs.readFileSync("../circuits/verification_key.json"));
const res = await snarkjs.plonk.verify(vKey, publicSignals, proof);
if (res === true) {
console.log("Verification OK");
} else {
console.log("Invalid proof");
}
}
run().then(() => {
process.exit(0);
});