Skip to content

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

Merged
merged 8 commits into from
Dec 6, 2021
Merged

Conversation

cctdaniel
Copy link
Contributor

No description provided.

@cctdaniel cctdaniel requested a review from SEJeff December 1, 2021 13:58
@cctdaniel cctdaniel self-assigned this Dec 1, 2021
Comment on lines 18 to 19
V2_FIRST_MAPPING_ACCOUNT_KEY = get_key("devnet", "mapping")
V2_PROGRAM_KEY = get_key("devnet", "program")
Copy link
Contributor

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?

Comment on lines +12 to +35
"""
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
Copy link
Contributor

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.

Copy link
Contributor Author

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 🙂

@SEJeff
Copy link
Contributor

SEJeff commented Dec 1, 2021

Side note, you'll want to rebase this once #13 is merged.

Comment on lines +27 to +28
V2_FIRST_MAPPING_ACCOUNT_KEY = 'BmA9Z6FjioHJPpjT39QazZyhDRUdZy2ezwx4GiDdE2u2'
V2_PROGRAM_KEY = 'gSbePebfvPy7tRqimPoVecS2UsBvYv46ynrzWocc92s'
Copy link
Contributor Author

@cctdaniel cctdaniel Dec 1, 2021

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?

Copy link
Contributor

@SEJeff SEJeff left a 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().

Comment on lines 59 to 62
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"
Copy link
Contributor

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 👍

@cctdaniel cctdaniel merged commit bdd32b4 into pyth-network:main Dec 6, 2021
@cctdaniel cctdaniel deleted the dns branch December 6, 2021 16:39
@murmer-blip
Copy link
Contributor

closes #2

@ali-behjati
Copy link
Contributor

right, thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants