Within this section, we will look at how to query the Injective name service contracts.
Abstraction Service
You can use our InjNameServiceabstraction to query the smart contracts with a single method call. Below this example, you can also find the raw implementation on how to query the smart contracts in case you need more flexibility.
import { getNetworkEndpoints, Network } from'@injectivelabs/network'import { InjNameService } from'@injectivelabs/sdk-ui-ts'constinjNameService=newInjNameService(Network.Testnet)constname='ninja.inj'// Fetch the address for the particular nameconstaddressForName=awaitinjNameService.fetchInjAddress(name) // Fetch the name for the particular addressconstnameFromAddress=awaitinjNameService.fetchInjName(addressForName)
Raw Smart Contract Querying
Example code snippets to resolve .inj domain name.
import { getNetworkEndpoints, Network } from'@injectivelabs/networks'import { ChainId } from'@injectivelabs/ts-types'import { nameToNode, normalizeName, ChainGrpcWasmApi, QueryInjectiveAddress, InjNameServiceQueryTransformer INJ_NAME_REGISTRY_CONTRACT_BY_NETWORK,} from'@injectivelabs/sdk-ts'constendpoints=getNetworkEndpoints(Network.Testnet)constchainGrpcWasmApi=newChainGrpcWasmApi(endpoints.grpc)constregistryContractAddress=INJ_NAME_REGISTRY_CONTRACT_BY_NETWORK[Network.Testnet]constname='allen.inj'constresolverAddress=''/** from above query */constnormalizedName=normalizeName(name)constnode=nameToNode(normalized)constquery=newQueryInjectiveAddress({ node }).toPayload()constresponse=awaitchainGrpcWasmApi.fetchSmartContractState( resolverAddress, query,)constinjectiveAddress=InjNameServiceQueryTransformer.injectiveAddressResponseToInjectiveAddress(response)if (!injectiveAddress) {thrownewError(`address not found for ${name}`)}console.log(injectiveAddress)
Reverse Resolution
Get the primary name for injective address.
import { getNetworkEndpoints, Network } from'@injectivelabs/networks'import { ChainId } from'@injectivelabs/ts-types'import { nameToNode, normalizeName, ChainGrpcWasmApi, QueryInjectiveAddress, InjNameServiceQueryTransformer INJ_NAME_REVERSE_RESOLVER_CONTRACT_BY_NETWORK,} from'@injectivelabs/sdk-ts'constendpoints=getNetworkEndpoints(Network.Testnet)constchainGrpcWasmApi=newChainGrpcWasmApi(endpoints.grpc)constreverseResolverContractAddress=INJ_NAME_REVERSE_RESOLVER_CONTRACT_BY_NETWORK[Network.Testnet]constinjectiveAddress=''constresolverAddress=''/** from above query */constquery=newQueryInjName({ address: injectiveAddress }).toPayload()constresponse=awaitchainGrpcWasmApi.fetchSmartContractState( reverseResolverContractAddress, query,)constname=InjNameServiceQueryTransformer.injectiveNameResponseToInjectiveName(response)if (!name) {thrownewError(`.inj not found for ${injectiveAddress}`)}constaddressForName=/** fetch as above example */if (addressForName.toLowerCase() !==address.toLowerCase()) {thrownewError(`.inj not found for ${injectiveAddress}`)}console.log(name)