npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@tns-money/tns.js

v1.2.0

Published

[![npm version](https://img.shields.io/npm/v/@tns-money/tns.js.svg?label=version)](https://www.npmjs.com/package/@tns-money/tns.js) [![package license](https://img.shields.io/npm/l/@tns-money/tns.js.svg)](https://www.npmjs.com/package/@tns-money/tns.js) <

Downloads

786

Readme

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 v14+
  • NPM or Yarn
  • @terra-money/terra.js >= v3.0.2

Installation

TNS.js is available as a package on NPM and is intended to to be used alongside Terra.js.

Add both:

  • @terra-money/terra.js
  • @tns-money/tns.js

To your JavaScript project's package.json as dependencies using your preferred package manager:

npm install @terra-money/terra.js @tns-money/tns.js

or

yarn add @terra-money/terra.js @tns-money/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 "bucky.ust" points to:

import { TNS } from '@tns-money/tns.js'

const tns = new TNS()

await tns.name('bucky.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 '@tns-money/tns.js'

const tns = new TNS()

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

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

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

import { TNS } from '@tns-money/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('bucky.ust')

await name.getOwner() // "terra17err4n4...m35eRv1c3"
await name.getTextData('twitter') // "https://twitter.com/tns_money"
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('bucky.ust')

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

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

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('bucky.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

| Method | Return | Description | | ------------------------------------- | -------- | ------------------------------------------- | | name(name: string) | Name | Create a Name object | | await getName(terraAddress: string) | string | Get TNS name of the specified terra address |

Name

| Method | Return | Description | | -------------------------------- | --------- | -------------------------------------- | | await isAvailable() | boolean | Get availability status | | await getExpires() | number | Get expiration date in Unix timestamp | | await getImage() | string | Get this TNS image in base64 string | | await getOwner() | string | Get TNS owner address | | await getEditor() | string | Get this TNS editor address | | await getTerraAddress() | string | Get Terra Address this TNS resolves to | | await getTextData(key: string) | string | Get Text Data of the specified key | | await getRegistrar() | string | Get Registrar address (NFT) | | await getResolver() | string | Get Resolver address |

Examples

You can find example usages of TNS.js via: