Skip to content
Afri edited this page Dec 29, 2022 · 1 revision

The Ruby Ethereum gem supports resolving ENS name records.

Quick resolver

The Eth::Client.resolve_ens function will cater to most of your needs.

infura = Eth::Client.create("https://mainnet.infura.io/v3/#{ENV['INFURA_TOKEN']}")
# => #<Eth::Client::Http:0x00005614b684fd70
#  @gas_limit=21000,
#  @host="mainnet.infura.io",
#  @id=0,
#  @max_fee_per_gas=0.4269e11,
#  @max_priority_fee_per_gas=0.101e10,
#  @port=443,
#  @ssl=true,
#  @uri=#<URI::HTTPS https://mainnet.infura.io/v3/31b..30c>>
infura.resolve_ens "ncwc6edqldzy6mlo.eth"
# => "0xde270e46d63b1816d1b798cff473c4ba238aca73"

Advanced features

For more advanced features, create an Eth::Ens::Resolver.

infura = Eth::Client.create("https://mainnet.infura.io/v3/#{ENV['INFURA_TOKEN']}")
# => #<Eth::Client::Http:0x00005614b684fd70
#  @gas_limit=21000,
#  @host="mainnet.infura.io",
#  @id=0,
#  @max_fee_per_gas=0.4269e11,
#  @max_priority_fee_per_gas=0.101e10,
#  @port=443,
#  @ssl=true,
#  @uri=#<URI::HTTPS https://mainnet.infura.io/v3/31b..30c>>
ens = Eth::Ens::Resolver.new(infura)
# => #<Eth::Ens::Resolver:0x00005614b663b908
#  @client=
#   #<Eth::Client::Http:0x00005614b684fd70>,
#  @registry=#<Eth::Contract::ENSRegistryWithFallback:0x00005614b676b300>>

It is possible to get the owner of an ENS, the public resolver contract, or a custom text record.

ens.owner("ncwc6edqldzy6mlo.eth")
# => "0xe611a720778a5f6723d6b4866f84828504657181"
ens.resolver("ncwc6edqldzy6mlo.eth").address
# => "0x4976fb03C32e5B8cfe2b6cCB31c09Ba78EBaBa41"
ens.text("ncwc6edqldzy6mlo.eth", "url")
# => "https://github.com/q9f/eth.rb"

It is also possible to query records for custom coin types.

ens.resolve("ncwc6edqldzy6mlo.eth", Eth::Ens::CoinType::ETHEREUM_CLASSIC)
=> "0x37287f68aC899b769FAa57033c78B78c76C68dc0"
Clone this wiki locally