0.1.13 • Published 3 years ago

@terranameservice/tns.js v0.1.13

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

TNS.js

npm version package license

TNS.js is a JavaScript SDK for building applications that interacts with Terra Name Service from within JavaScript runtimes, such as web browsers, server backends, and on mobile through React Native. TNS.js provides simple abstractions over core functionality such as queries and transaction executions.

Table of Contents

Getting Started

A walk through of the steps to get started with the Terra Name Service SDK alongside with minimum requirements are provided below.

Requirements

  • Node.js 12+
  • NPM or Yarn

Installation

TNS.js is available as a package on NPM, you can grab the latest version by:

npm install @terranameservice/tns.js

or

yarn add @terranameservice/tns.js

Usage

TNS.js can be used in Node.js, as well as inside the browser.

The most simplest usecase of TNS.js is to resolve a Terra Address from a TNS name. For example, resolving which Terra Address "test.ust" points to:

import { TNS } from '@terranameservice/tns.js'

const tns = new TNS()

await tns.name('test.ust').getTerraAddress() // "terra17err4n4...m35eRv1c3"

TNS object

TNS is the main interface for querying data, creating Name objects and also for name-independent operations such as getting a Reverse Record from a Terra Address.

import { TNS } from '@terranameservice/tns.js'

const tns = new TNS()

// Create a Name object
tns.name('test.ust') // Name object

// Get TNS name from terra address (Reverse Record)
await tns.getName('terra17err4n4...m35eRv1c3') // "test.ust"

You can also specify TNS config through TNS object constructor (optional):

import { TNS } from '@terranameservice/tns.js'

const tns = new TNS({
  /**
   * Network name indicates which contract address to be used.
   * (Registry, Resolver, Controller, etc.)
   */
  network: 'mainnet' | 'testnet',

  /**
   * Wallet address of the transaction sender.
   * (Required when making execution transaction)
   */
  walletAddress: 'terra17err4n4...m35eRv1c3',

  /**
   * Custom Terra Mantle indexer URL.
   */
  mantleUrl: 'https://mantle.terra.dev'
})

Name object

Name is the main interface for querying and building execution transactions. It acts as an abstraction layer over multiple contracts (Registry, Resolver, Registrar and Controller) with 2 main use cases:

  • Query: Runs smart contract queries through Mantle indexer with GraphQL
  • Execute: Creates MsgExecuteContract objects to be used in transactions

Query

const name = tns.name('test.ust')

await name.getOwner() // "terra17err4n4...m35eRv1c3"
await name.getTextData('github') // "https://github.com/terranameservice"
await name.getImage() // "data:image/svg+xml;base64,..."

Batch Query

With the power of Mantle Indexer, you can do a batch query to save network requests using name.query(...) method:

const name = tns.name('test.ust')

const { data } = await name.query(builder =>
  builder
    .getExpires()
    .getTerraAddress()
    .isAvailable()
    .getOwner()
    .getEditor()
    .getImage()
    .getTextData('github', 'twitter')
)

console.log(data)
// {
//   expires: 1667315503,
//   terraAddress: 'terra17err4n4...m35eRv1c3',
//   isAvailable: false,
//   owner: 'terra17err4n4...m35eRv1c3',
//   editor: 'terra17err4n4...m35eRv1c3',
//   image: 'data:image/svg+xml;base64,...',
//   textData: {
//     github: 'https://github.com/terranameservice',
//     url: 'https://twitter.com/tns_money'
//   }
// }

Execution

TNS.js provides a simple way to create transaction messages (MsgExecuteContract), so that it is independent from libraries used to broadcast transactions.

First, you need to specify the walletAddress of the transaction sender when creating a TNS instance (required):

// walletAddress will be used as a transaction sender in MsgExecuteContract
const tns = new TNS({ walletAddress: 'terra17err4n4...m35eRv1c3' })
const name = tns.name('test.ust')

Then, create a transaction message using the provided API:

// Create a transaction message (MsgExecuteContract)
const setTerraAddressMsg = await name.setTerraAddress('terra1new...address')

With the returned MsgExecuteContract, you can sign and broadcast it with any libraries of your preference:

// Broadcast transaction using @terra-money/terra.js LCDClient
const lcdClient = new LCDClient({
  URL: 'https://lcd.terra.dev',
  chainID: 'columbus-5'
})

const wallet = lcdClient.wallet(new MnemonicKey({ mnemonic: '...' }))

const tx = await wallet.createAndSignTx({
  msgs: [setTerraAddressMsg],
  fee: new StdFee(200000, '50000uusd'),
})

await lcdClient.tx.broadcastAsync(tx)

or

// Broadcast transaction using @terra-money/wallet-provider
const connectedWallet = useConnectedWallet()

await connectedWallet.post({
  msgs: [setTerraAddressMsg],
  fee: new StdFee(200000, '50000uusd'),
})

APIs

TNS

MethodReturnDescription
name(name: string)NameCreate a Name object
await getName(terraAddress: string)stringGet TNS name of the specified terra address

Name

MethodReturnDescription
await isAvailable()booleanGet availability status
await getExpires()numberGet expiration date in Unix timestamp
await getImage()stringGet this TNS image in base64 string
await getOwner()stringGet TNS owner address
await getEditor()stringGet this TNS editor address
await getTerraAddress()stringGet Terra Address this TNS resolves to
await getTextData(key: string)stringGet Text Data of the specified key
await getRegistrar()stringGet Registrar address (NFT)
await getResolver()stringGet Resolver address

Examples

You can find example usages of TNS.js via: