Skip to content
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

Add ENS resolve support #150

Merged
merged 4 commits into from
Nov 21, 2022
Merged

Add ENS resolve support #150

merged 4 commits into from
Nov 21, 2022

Conversation

dansimpson
Copy link
Contributor

This does work to resolve common ENS names to a given address (I'm using it), with the caveat that the name normalization isn't to spec... so it will not work with some edge case names.

Opening this PR per request.

@codecov-commenter
Copy link

codecov-commenter commented Sep 27, 2022

Codecov Report

Merging #150 (e11c27e) into main (6e1a491) will increase coverage by 0.00%.
The diff coverage is 100.00%.

❗ Current head e11c27e differs from pull request most recent head 7baf4a5. Consider uploading reports for the commit 7baf4a5 to get more accurate results

@@           Coverage Diff            @@
##             main     #150    +/-   ##
========================================
  Coverage   99.74%   99.75%            
========================================
  Files          67       70     +3     
  Lines        3899     4028   +129     
========================================
+ Hits         3889     4018   +129     
  Misses         10       10            
Impacted Files Coverage Δ
lib/eth.rb 100.00% <100.00%> (ø)
lib/eth/ens/resolver.rb 100.00% <100.00%> (ø)
spec/eth/ens/resolver_spec.rb 100.00% <100.00%> (ø)
lib/eth/chain.rb 100.00% <0.00%> (ø)
lib/eth/client.rb 100.00% <0.00%> (ø)
spec/eth/tx_spec.rb 100.00% <0.00%> (ø)
lib/eth/signature.rb 100.00% <0.00%> (ø)
spec/eth/chain_spec.rb 100.00% <0.00%> (ø)
spec/eth/client_spec.rb 100.00% <0.00%> (ø)
spec/eth/signature_spec.rb 100.00% <0.00%> (ø)
... and 4 more

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

@q9f q9f added the enhancement New feature or request label Sep 27, 2022
Copy link
Contributor

@mculp mculp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Had a few general Ruby comments, but really glad you did this. I let myself get stuck on the encoding, and you made the wise decision to get this out there even though it may not work in all use cases. Good call and thank you!

spec/eth/ens/resolver_spec.rb Outdated Show resolved Hide resolved

describe Ens::Resolver do

let(:geth_dev_http_path) { "http://127.0.0.1:8545" }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We shouldn't be making any http calls from tests. Look into vcr or webmock or simply putting a file with the expected response from the http endpoint in the spec/ directory somewhere.

Suggested change
let(:geth_dev_http_path) { "http://127.0.0.1:8545" }
let(:geth_dev_http_path) { "http://127.0.0.1:8545" }

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've used both vcr and webmock, but given that we live in a time where we can easily run services in test environments, I tend to disagree. GHA can easily run a geth dev node, so... why not? If the container is updated to the latest version, the tests are now running against that (integration), which I think is a good thing. Also, I was just imitating behavior I saw in the existing test suite in an attempt to not stray too far off the beaten path.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I needed a way to make sure that the HTTP client works, so I spawned a Geth node within the GHA to prevent relying on third parties. It's fine to reuse this in other test cases IMHO.

# @param ens_name [String] The ENS name, eg: fancy.eth
def initialize(client, address = DEFAULT_ADDRESS)
@client = client
@contract = Eth::Contract.from_abi(
Copy link
Contributor

@mculp mculp Sep 29, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Few things here:

  1. I would set only the contract filename here, so your object instantiation will not ever fail. Also set the ENS address as an instance variable. You may want to add a valid? predicate method, but not necessary.
Suggested change
@contract = Eth::Contract.from_abi(
@address = address
@contract_filename = "ens.json"
  1. I would define a constant that points to the ABIs root directory.
ABI_DIRECTORY = File.join(File.dirname(__FILE__), '../../../abis'))

If you see this constant being reused by other class (I do), then probably add it to Eth::Constants instead.

  1. Add a def parsed_abi method that reads and parses the JSON file with error handling.
def parsed_abi
  JSON.parse(File.read(File.join(ABI_DIRECTORY, @contract_address)
rescue StandardError => e
  # log something here
  puts "oh no, stuff blew up"
  @valid = false
end
  1. Add a memoized contract method that instantiates the `Eth::Contract with the parsed ABI and address.
def contract
  @contract ||= Eth::Contract.from_abi(name: 'ENS', address: address, abi: parsed_abi)
end
  1. Use attr_reader :address, :client, :parsed_abi instead of accessing instance variables directly.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with the constant path for ABIs and error handling. #3 and #5 are conflicting, at least for the parsed_abi. Regarding attr accessors, these values are effectively private from the library user... but could be useful for debugging, I suppose.

Co-authored-by: Matt Culpepper <mculp@users.noreply.github.com>
@dansimpson
Copy link
Contributor Author

Had a few general Ruby comments, but really glad you did this. I let myself get stuck on the encoding, and you made the wise decision to get this out there even though it may not work in all use cases. Good call and thank you!

Yeah, I had to fight myself with the encoding piece. Perfect is the enemy of good.

@dansimpson
Copy link
Contributor Author

I'll adjust some things based on feedback @mculp tomorrow.

Copy link
Owner

@q9f q9f left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! Happy to accept this. If you don't have time to get the CI green, I can also take this over. Just let me know.

@dansimpson
Copy link
Contributor Author

@q9f I was able to make CI happy, two of the tests I had to mark as pending due to: 1) Lack of a library to handle the full extent of text processing required for ENS, and 2) No existing way to test live chains, or to do a full deploy of ENS contracts locally, write to them, etc. If there is an infura/alchemy key in CI secrets, then resolving a known ENS address on mainnet would work... with the small caveat that when that changes the test will need to be updated.

@q9f
Copy link
Owner

q9f commented Nov 21, 2022

Thank you. I can add an infura key to the pipelines in future.

@q9f q9f merged commit 85e2283 into q9f:main Nov 21, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants