Skip to content

Commit

Permalink
feat: add custom order (#967)
Browse files Browse the repository at this point in the history
  • Loading branch information
ericnordelo authored Apr 11, 2024
1 parent 17e6bcc commit 9e0e248
Showing 1 changed file with 36 additions and 2 deletions.
38 changes: 36 additions & 2 deletions scripts/get_hashes_page.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
import sys
import json

KNOWN_ORDER = [
"ERC20",
"ERC721",
"ERC1155",
"Account",
"EthAccountUpgradeable",
"UniversalDeployer"
]

def main():
# Required compiler version argument
Expand All @@ -18,17 +26,43 @@ def generate_doc_file(cmp_version, contracts):
https://crates.io/crates/cairo-lang-compiler/{cmp_version}[cairo {cmp_version}]
"""
hashes = "// Class Hashes\n"
contracts['contracts'] = remove_prefix_from_names(contracts['contracts'])
contracts['contracts'].sort(key=lambda x: x['name'])

hashes += get_known_order_hashes(contracts['contracts'])
for contract in contracts['contracts']:
# The [13:] is to remove the "openzeppelin_" prefix from the contract name
hashes += f":{contract['name'][13:]}-class-hash: {normalize_len(contract['sierra'])}\n"
# Avoid the already added contracts in the known order
if contract['name'] in KNOWN_ORDER:
continue
hashes += f":{contract['name']}-class-hash: {normalize_len(contract['sierra'])}\n"

footer = """// Presets page
:presets-page: xref:presets.adoc[Sierra class hash]"""

return f"{header}\n{hashes}\n{footer}\n"


def remove_prefix_from_names(contracts):
for contract in contracts:
contract.update([("name", remove_prefix(contract['name'], 'openzeppelin_'))])
return contracts


def remove_prefix(text, prefix):
if text.startswith(prefix):
return text[len(prefix):]
return text


def get_known_order_hashes(contracts):
known_order_hashes = [""] * len(KNOWN_ORDER)
for contract in contracts:
if contract['name'] in KNOWN_ORDER:
index = KNOWN_ORDER.index(contract['name'])
known_order_hashes[index] = f":{contract['name']}-class-hash: {normalize_len(contract['sierra'])}\n"
return ''.join(known_order_hashes)


def normalize_len(sierra_hash):
return "0x" + "0" * (66 - len(sierra_hash)) + sierra_hash[2:]

Expand Down

0 comments on commit 9e0e248

Please sign in to comment.