-
Notifications
You must be signed in to change notification settings - Fork 2
feat(python): add utils #133
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| from dune_client.client import DuneClient | ||
| from dune_client.query import QueryBase | ||
| from dune_client.types import QueryParameter, DuneRecord, ParameterType | ||
|
|
||
|
|
||
| def query_dune( | ||
| query_id: int, api_key: str, latest_block: int | None = None | ||
| ) -> list[DuneRecord]: | ||
| """ | ||
| Run a Dune query and return the results. | ||
|
|
||
| :param query_id: The ID of the Dune query. | ||
| :param api_key: The API key Dune. | ||
| :param latest_block: The latest block to query (optional). | ||
| :return: The results of the Dune query. | ||
| """ | ||
| dune = DuneClient(api_key=api_key) | ||
| if latest_block is not None: | ||
| params = [ | ||
| QueryParameter( | ||
| name="max_block", | ||
| value=latest_block, | ||
| parameter_type=ParameterType.NUMBER, | ||
| ) | ||
| ] | ||
| else: | ||
| params = [] | ||
| query = QueryBase( | ||
| query_id=query_id, | ||
| params=params, | ||
| ) | ||
| return dune.run_query(query).get_rows() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| import subprocess | ||
|
|
||
| # from pathlib import Path | ||
|
|
||
|
|
||
| def get_starkli_private_key(keystore_file: str, keystore_password: str): | ||
| """ | ||
| Get the private key from the starkli keystore. | ||
|
|
||
| :param keystore_file: The path to the keystore file. | ||
| :param keystore_password: The password for the keystore file. | ||
| :return: The private key. | ||
| """ | ||
| # Verify keystore file. | ||
| # path = Path(keystore_file) | ||
| # if not path.exists(): | ||
| # raise FileNotFoundError(f"Keystore file {keystore_file} not found.") | ||
| # if not path.is_file(): | ||
| # raise FileNotFoundError(f"Keystore file {keystore_file} is not a file.") | ||
|
|
||
| # Get private key. | ||
| password_format = "--password={}" | ||
| command = [ | ||
| "starkli", | ||
| "signer", | ||
| "keystore", | ||
| "inspect-private", | ||
| keystore_file, | ||
| password_format.format(keystore_password), | ||
| ] | ||
|
|
||
| try: | ||
| result = subprocess.run(command, capture_output=True, text=True) | ||
|
|
||
| if result.returncode == 0: | ||
| return result.stdout.split()[2] | ||
| else: | ||
| raise Exception( | ||
| f"Private key extraction failed with error: {result.stderr}" | ||
| ) | ||
| except Exception as e: | ||
| raise Exception(f"An error occurred while getting private key: {e}") | ||
|
|
||
|
|
||
| def ledger_sign_tx(tx_hash: str, ledger_path: str) -> str: | ||
| """ | ||
| Sign a transaction with the ledger account. | ||
|
|
||
| :param tx_hash: The hash of the transaction to sign. | ||
| :param ledger_path: The derivation_path of the ledger. | ||
| :return: The signature of the transaction. | ||
| """ | ||
| command = [ | ||
| "starkli", | ||
| "ledger", | ||
| "sign-hash", | ||
| "--path", | ||
| ledger_path, | ||
| tx_hash, | ||
| ] | ||
| try: | ||
| input( | ||
| "Open ledger and press enter to continue, then sign the tx in your legder." | ||
| ) | ||
| result = subprocess.run(command, capture_output=True, text=True) | ||
|
|
||
| if result.returncode == 0: | ||
| print(f"Signing successful: {result.stdout.strip()}") | ||
| return result.stdout.strip() | ||
| else: | ||
| print(f"Signing failed with error: {result.stderr}") | ||
| except Exception as e: | ||
| print(f"An error occurred while signing transaction: {e}") | ||
|
|
||
|
|
||
| def get_ledger_public_key(ledger_path: str) -> str: | ||
| """ | ||
| Get the public key from the ledger account. | ||
|
|
||
| :param ledger_path: The derivation_path of the ledger. | ||
| :return: The public key of the ledger account. | ||
| """ | ||
| command = [ | ||
| "starkli", | ||
| "ledger", | ||
| "get-public-key", | ||
| "--no-display", | ||
| ledger_path, | ||
| ] | ||
| try: | ||
| input("Getting public key. Open ledger and press enter to continue.") | ||
| result = subprocess.run(command, capture_output=True, text=True) | ||
|
|
||
| if result.returncode == 0: | ||
| print(f"Public key: {result.stdout.strip()}") | ||
| return result.stdout.strip() | ||
| else: | ||
| print(f"Getting public key failed with error: {result.stderr}") | ||
| except Exception as e: | ||
| print(f"An error occurred while getting public key: {e}") | ||
omri-ba-starkware marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.