Skip to content

Commit

Permalink
fix: formatted script files
Browse files Browse the repository at this point in the history
  • Loading branch information
mexes20 committed Sep 11, 2024
1 parent c0035a6 commit 544a3b0
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 21 deletions.
30 changes: 18 additions & 12 deletions scripts/data/format_args.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def serialize(obj):
return 1 if obj else 0
elif isinstance(obj, int):
# This covers u8, u16, u32, u64, u128, felt252
assert(obj >= 0 and obj < 2 ** 252)
assert obj >= 0 and obj < 2**252
return obj
elif isinstance(obj, str):
if obj == "0" * 64:
Expand All @@ -30,25 +30,27 @@ def serialize(obj):
# TODO: there might still be collisions with hashes
# Try to cast to int and then to low/high parts
num = int(obj)
assert(num >= 0 and num < 2 ** 256)
lo = num % 2 ** 128
hi = num // 2 ** 128
assert num >= 0 and num < 2**256
lo = num % 2**128
hi = num // 2**128
return (lo, hi)
elif obj.startswith('0x'):
elif obj.startswith("0x"):
# Split into 31-byte chunks and save the remainder
src = bytes.fromhex(obj[2:])
num_chunks = len(src) // 31
main_len = num_chunks * 31
rem_len = len(src) - main_len
main = [int.from_bytes(src[i:i+31], 'big') for i in range(0, main_len, 31)]
main = [
int.from_bytes(src[i : i + 31], "big") for i in range(0, main_len, 31)
]
# TODO: check if this is how byte31 is implemented
rem = int.from_bytes(src[main_len:].rjust(31, b'\x00'), 'big')
rem = int.from_bytes(src[main_len:].rjust(31, b"\x00"), "big")
return tuple([len(main)] + main + [rem, rem_len])
else:
# Reversed hex string into 4-byte words then into BE u32
assert(len(obj) == 64)
assert len(obj) == 64
rev = list(reversed(bytes.fromhex(obj)))
return tuple(int.from_bytes(rev[i:i+4], 'big') for i in range(0, 32, 4))
return tuple(int.from_bytes(rev[i : i + 4], "big") for i in range(0, 32, 4))
elif isinstance(obj, list):
arr = list(map(serialize, obj))
return tuple([len(arr)] + arr)
Expand All @@ -68,6 +70,7 @@ def flatten_tuples(src):
:return: an object that can only contain integers and lists, top-level tuple converts to a list.
"""
res = []

def append_obj(obj, to):
if isinstance(obj, int):
to.append(obj)
Expand All @@ -81,6 +84,7 @@ def append_obj(obj, to):
append_obj(item, to)
else:
raise NotImplementedError(obj)

append_obj(src, res)
return res

Expand All @@ -92,12 +96,14 @@ def format_cairo1_run(args: list) -> str:
:param args: Python object containing already processed arguments.
:return: Returns string with removed commas.
"""

def format_item(item):
if isinstance(item, list):
arr = " ".join(map(format_item, item))
return f'[{arr}]'
return f"[{arr}]"
else:
return str(item)

return format_item(args)


Expand All @@ -106,12 +112,12 @@ def format_args():
Expects a single CLI argument containing file path.
Output is compatible with the Scarb runner arguments format.
"""
if (len(sys.argv) != 2):
if len(sys.argv) != 2:
raise TypeError("Expected single argument")
args = json.loads(Path(sys.argv[1]).read_text())
res = flatten_tuples(serialize(args))
print([res])


if __name__ == '__main__':
if __name__ == "__main__":
format_args()
6 changes: 4 additions & 2 deletions scripts/data/generate_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,13 +190,15 @@ def format_coinbase_input(input: dict):
"block_time": 0,
"is_coinbase": False,
},
"witness": ["0x0000000000000000000000000000000000000000000000000000000000000000"],
"witness": [
"0x0000000000000000000000000000000000000000000000000000000000000000"
],
}


def format_output(output: dict):
"""Formats transaction output according to the Cairo type."""
value = (Decimal(str(output["value"])) * Decimal('100000000')).to_integral_value()
value = (Decimal(str(output["value"])) * Decimal("100000000")).to_integral_value()
return {
"value": int(value),
"pk_script": f'0x{output["scriptPubKey"]["hex"]}',
Expand Down
24 changes: 17 additions & 7 deletions scripts/misc/create_issues.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,20 @@
import sys
import argparse


def create_issue(title, body, labels):
cmd = [
"gh", "issue", "create",
"--title", title,
"--body", body,
"--label", ",".join(labels)
"gh",
"issue",
"create",
"--title",
title,
"--body",
body,
"--label",
",".join(labels),
]

try:
subprocess.run(cmd, check=True)
print(f"Successfully created issue: {title}")
Expand All @@ -19,8 +25,11 @@ def create_issue(title, body, labels):
print(f"Error message: {e}")
sys.exit(1)


def main():
parser = argparse.ArgumentParser(description="Create GitHub issues from a JSON file.")
parser = argparse.ArgumentParser(
description="Create GitHub issues from a JSON file."
)
parser.add_argument("json_file", help="Path to the JSON file containing issue data")
args = parser.parse_args()

Expand All @@ -41,5 +50,6 @@ def main():
for issue in data["issues"]:
create_issue(issue["title"], issue["body"], issue["labels"])


if __name__ == "__main__":
main()
main()

0 comments on commit 544a3b0

Please sign in to comment.