-
Notifications
You must be signed in to change notification settings - Fork 32
use dns resolver to get account key #12
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
Conversation
pythclient/pythclient.py
Outdated
V2_FIRST_MAPPING_ACCOUNT_KEY = get_key("devnet", "mapping") | ||
V2_PROGRAM_KEY = get_key("devnet", "program") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we want to have the side effect like this at import time? Personally, I'd be ok with leaving first_mapping_account_key
in the PythClient
class blank and requiring it. Then we can remove these as constants, but make sure to put examples of them elsewhere.
wdyt?
""" | ||
Get the program or mapping keys from dns TXT records. | ||
|
||
Example dns records: | ||
|
||
devnet-program-v2.pyth.network | ||
mainnet-program-v2.pyth.network | ||
testnet-mapping-v2.pyth.network | ||
""" | ||
url = f"{network}-{type}-{version}.pyth.network" | ||
try: | ||
answer = dns.resolver.resolve(url, "TXT") | ||
except dns.resolver.NXDOMAIN: | ||
logger.error("TXT record for {} not found", url) | ||
return "" | ||
if len(answer) != 1: | ||
logger.error("Invalid number of records returned for {}!", url) | ||
return "" | ||
# Example of the raw_key: | ||
# "program=FsJ3A3u2vn5cTVofAjvy6y5kwABJAqYWpe4975bi2epH" | ||
raw_key = ast.literal_eval(list(answer)[0].to_text()) | ||
# program=FsJ3A3u2vn5cTVofAjvy6y5kwABJAqYWpe4975bi2epH" | ||
_, key = raw_key.split("=", 1) | ||
return key |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wrote this code for pyth-observer as a quick and dirty hack and don't consider it very pretty python. Would you mind adding some tests for it? I'd be willing to help you do so if necessary.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
will write tests for it 🙂
Side note, you'll want to rebase this once #13 is merged. |
V2_FIRST_MAPPING_ACCOUNT_KEY = 'BmA9Z6FjioHJPpjT39QazZyhDRUdZy2ezwx4GiDdE2u2' | ||
V2_PROGRAM_KEY = 'gSbePebfvPy7tRqimPoVecS2UsBvYv46ynrzWocc92s' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
declared as constants for the same reason as the constants below -- doesn't seems like there's a need to mock this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great stuff! As a general rule, fixtures are closer to properties than callables, so you shouldn't prefix the name with get_
. Better would be something like get_answer_program()
--> answer_program()
and get_answer_mapping()
--> answer_mapping()
.
tests/test_utils.py
Outdated
def test_utils_get_program_key(mock_dns_resolver_resolve: Mock, get_answer_program: dns.resolver.Answer) -> None: | ||
mock_dns_resolver_resolve.return_value = get_answer_program | ||
program_key = get_key("devnet", "program") | ||
assert program_key == "gSbePebfvPy7tRqimPoVecS2UsBvYv46ynrzWocc92s" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Data only and very little to no logic. This is a perfect example of a simple and good unit test. Nice 👍
closes #2 |
right, thank you! |
No description provided.