|
| 1 | +import argparse |
| 2 | +import json |
| 3 | +import os |
| 4 | +from collections import defaultdict |
| 5 | + |
| 6 | +import lz4.frame |
| 7 | + |
| 8 | +from hyperliquid.utils.signing import TOKEN_DELEGATE_TYPES, recover_user_from_user_signed_action |
| 9 | + |
| 10 | +REPLICA_CMD_BATCH_SIZE = 10000 |
| 11 | + |
| 12 | + |
| 13 | +def decompress_lz4(input_file, output_file): |
| 14 | + with open(input_file, "rb") as f_in: |
| 15 | + compressed_data = f_in.read() |
| 16 | + |
| 17 | + decompressed_data = lz4.frame.decompress(compressed_data) |
| 18 | + |
| 19 | + with open(output_file, "wb") as f_out: |
| 20 | + f_out.write(decompressed_data) |
| 21 | + |
| 22 | + |
| 23 | +def main(): |
| 24 | + parser = argparse.ArgumentParser(description="parse token delegate actions from replica cmds") |
| 25 | + parser.add_argument("--data-dir", type=str, required=True) |
| 26 | + parser.add_argument("--start-height", type=int, required=True) |
| 27 | + parser.add_argument("--end-height", type=int, required=True) |
| 28 | + args = parser.parse_args() |
| 29 | + |
| 30 | + data_dir = args.data_dir |
| 31 | + start_height = args.start_height |
| 32 | + end_height = args.end_height |
| 33 | + |
| 34 | + if start_height % REPLICA_CMD_BATCH_SIZE == 0: |
| 35 | + raise Exception("start height is not aligned with replica cmd batch size") |
| 36 | + if end_height % REPLICA_CMD_BATCH_SIZE == 0: |
| 37 | + raise Exception("end height is not aligned with replica cmd batch size") |
| 38 | + |
| 39 | + flns = [] |
| 40 | + for height in range(start_height, end_height, REPLICA_CMD_BATCH_SIZE): |
| 41 | + lz4_fln = f"{data_dir}/{height}.lz4" |
| 42 | + if not os.path.exists(lz4_fln): |
| 43 | + raise Exception( |
| 44 | + f"replica cmds file at {height} not found - download missing block files(s) using 'aws s3 cp s3://hl-[testnet | mainnet]-replica-cmds/<block_object_path> --request-payer requester'" |
| 45 | + ) |
| 46 | + fln = f"{data_dir}/{height}" |
| 47 | + decompress_lz4(lz4_fln, fln) |
| 48 | + flns.append(fln) |
| 49 | + |
| 50 | + user_to_validator_to_amount: dict[str, dict[str, int]] = defaultdict(lambda: defaultdict(int)) |
| 51 | + |
| 52 | + for fln in flns: |
| 53 | + f = open(fln) |
| 54 | + lines = f.readlines() |
| 55 | + for line in lines: |
| 56 | + if "tokenDelegate" not in line: |
| 57 | + continue |
| 58 | + data = json.loads(line) |
| 59 | + bundles = data["abci_block"]["signed_action_bundles"] |
| 60 | + for bundle in bundles: |
| 61 | + for signed_action in bundle[1]["signed_actions"]: |
| 62 | + action = signed_action["action"] |
| 63 | + if action["type"] != "tokenDelegate": |
| 64 | + continue |
| 65 | + validator = action["validator"] |
| 66 | + wei = action["wei"] |
| 67 | + is_delegate = not action["isUndelegate"] |
| 68 | + user = recover_user_from_user_signed_action( |
| 69 | + action, |
| 70 | + signed_action["signature"], |
| 71 | + TOKEN_DELEGATE_TYPES, |
| 72 | + "HyperliquidTransaction:TokenDelegate", |
| 73 | + True, |
| 74 | + ) |
| 75 | + if not is_delegate: |
| 76 | + wei = -wei |
| 77 | + user_to_validator_to_amount[user][validator] += wei / 100_000_000 # native token wei decimals |
| 78 | + |
| 79 | + print("user to validator to wei amount delegated", user_to_validator_to_amount) |
| 80 | + |
| 81 | + |
| 82 | +if __name__ == "__main__": |
| 83 | + main() |
0 commit comments