Within this section, we will look at how to query the Injective name service contracts.
Abstraction Service (deprecated)
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 { Network, getNetworkEndpoints, getInjNameReverseResolverContractForNetwork,} from'@injectivelabs/networks'import { ChainGrpcWasmApi, QueryInjectiveAddress, InjNameServiceQueryTransformer,} from'@injectivelabs/sdk-ts'import { nameToNode, normalizeName } from'@injectivelabs/sdk-ts'constendpoints=getNetworkEndpoints(Network.Testnet)constchainGrpcWasmApi=newChainGrpcWasmApi(endpoints.grpc)constreverseResolverContractAddress=getInjNameReverseResolverContractForNetwork(Network.Testnet)constname='allen.inj'constnormalizedName=normalizeName(name)constnode=nameToNode(normalizedName)constquery=newQueryInjectiveAddress({ node }).toPayload()constresponse=awaitchainGrpcWasmApi.fetchSmartContractState( reverseResolverContractAddress, 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 { QueryInjName, ChainGrpcWasmApi, InjNameServiceQueryTransformer} from'@injectivelabs/sdk-ts'import { Network, getNetworkEndpoints, getInjNameReverseResolverContractForNetwork} from'@injectivelabs/networks'constendpoints=getNetworkEndpoints(Network.Testnet)constchainGrpcWasmApi=newChainGrpcWasmApi(endpoints.grpc)constreverseResolverContractAddress=getInjNameReverseResolverContractForNetwork(Network.Testnet)constinjectiveAddress=''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)