Skip to content

Commit

Permalink
Add function for constructing knockout claim proofs
Browse files Browse the repository at this point in the history
  • Loading branch information
Mister-Meeseeks committed Aug 26, 2024
1 parent 667dd10 commit 7507b53
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@crocswap-libs/sdk",
"version": "1.0.3",
"version": "1.0.4",
"description": "🛠🐊🛠 An SDK for building applications on top of CrocSwap",
"author": "Ben Wolski <[email protected]>",
"repository": "https://github.com/CrocSwap/sdk.git",
Expand Down
42 changes: 41 additions & 1 deletion src/encoding/knockout.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ethers } from "ethers";
import { BigNumberish, ethers } from "ethers";

export class KnockoutEncoder {
constructor(base: string, quote: string, poolIdx: number) {
Expand Down Expand Up @@ -50,6 +50,46 @@ export class KnockoutEncoder {
}
}

/* The decoded state of the tick from a CrocKnockoutCross event log. */
export interface KnockoutCrossState {
pivotTime: number,
mileage: BigNumberish,
commitEntropy: bigint
}

/* Packs a list of knockout cross events into a 256-bit array that can be passed directly
* as a Merkle proof to the Croc knockout claim function.
*
*
*
* @remarks These values should be taken directly from the CrocKnockoutCross
* event log. For an example see
* https://etherscan.io/tx/0x022b1f3792b98a54c761c0a79268dbcb6e5f1a2a9f7494bab743f722957e7219#eventlog
*
* @param crosses The list of knockout cross events *only* at the given tick since the knockout
* order was minted. Input can be in any order.
* @returns The 256-bit array for the knockout proof.
*/
export function packKnockoutLinks (crosses: KnockoutCrossState[]): bigint[] {
// Sort in reverse order to conform to proof standard
crosses.sort((a, b) => b.pivotTime - a.pivotTime)
return crosses.map(cross => packKnockoutLink(cross.pivotTime, cross.mileage, cross.commitEntropy))
}

/* Creates a single entry for an entry in a knockout proof.
*
* @param pivotTime The time of the pivot (from the event log).
* @param mileage The mileage at the knockout cross (from the event log).
* @param commitEntropy The random commit entropy (from the event log).
* @returns The 256-bit array entry for the knockout proof.
*/
function packKnockoutLink (pivotTime: BigNumberish,
mileage: BigNumberish, commitEntropy: bigint): bigint {
// Converted BigInt code
const packed = (BigInt(pivotTime) << BigInt(64)) + BigInt(mileage);
return commitEntropy + BigInt(packed);
}

const KNOCKOUT_ARG_TYPES = [
"uint8", // Type call
"address", // Base
Expand Down

0 comments on commit 7507b53

Please sign in to comment.